Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <com/sun/star/style/XStyle.hpp>
21 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
22 : #include <com/sun/star/style/PageStyleLayout.hpp>
23 : #include <com/sun/star/beans/XMultiPropertyStates.hpp>
24 : #include <xmloff/nmspmap.hxx>
25 : #include "xmloff/xmlnmspe.hxx"
26 : #include <xmloff/xmltoken.hxx>
27 : #include <xmloff/XMLTextMasterPageContext.hxx>
28 : #include "XMLTextHeaderFooterContext.hxx"
29 : #include <xmloff/xmlimp.hxx>
30 : #include "PageMasterImportContext.hxx"
31 :
32 :
33 : using ::rtl::OUString;
34 : using ::rtl::OUStringBuffer;
35 :
36 : using namespace ::com::sun::star;
37 : using namespace ::com::sun::star::uno;
38 : using namespace ::com::sun::star::xml::sax;
39 : using namespace ::com::sun::star::style;
40 : using namespace ::com::sun::star::text;
41 : using namespace ::com::sun::star::beans;
42 : using namespace ::com::sun::star::container;
43 : using namespace ::com::sun::star::lang;
44 : using namespace ::xmloff::token;
45 :
46 9 : Reference < XStyle > XMLTextMasterPageContext::Create()
47 : {
48 9 : Reference < XStyle > xNewStyle;
49 :
50 9 : Reference< XMultiServiceFactory > xFactory( GetImport().GetModel(),
51 9 : UNO_QUERY );
52 9 : if( xFactory.is() )
53 : {
54 : Reference < XInterface > xIfc =
55 9 : xFactory->createInstance(OUString(RTL_CONSTASCII_USTRINGPARAM(
56 9 : "com.sun.star.style.PageStyle")) );
57 9 : if( xIfc.is() )
58 9 : xNewStyle = Reference < XStyle >( xIfc, UNO_QUERY );
59 : }
60 :
61 9 : return xNewStyle;
62 : }
63 0 : TYPEINIT1( XMLTextMasterPageContext, SvXMLStyleContext );
64 :
65 72 : XMLTextMasterPageContext::XMLTextMasterPageContext( SvXMLImport& rImport,
66 : sal_uInt16 nPrfx, const OUString& rLName,
67 : const Reference< XAttributeList > & xAttrList,
68 : sal_Bool bOverwrite )
69 : : SvXMLStyleContext( rImport, nPrfx, rLName, xAttrList, XML_STYLE_FAMILY_MASTER_PAGE )
70 : , sIsPhysical( RTL_CONSTASCII_USTRINGPARAM( "IsPhysical" ) )
71 : , sPageStyleLayout( RTL_CONSTASCII_USTRINGPARAM( "PageStyleLayout" ) )
72 : , sFollowStyle( RTL_CONSTASCII_USTRINGPARAM( "FollowStyle" ) )
73 : , bInsertHeader( sal_False )
74 : , bInsertFooter( sal_False )
75 : , bInsertHeaderLeft( sal_False )
76 : , bInsertFooterLeft( sal_False )
77 : , bInsertHeaderFirst( sal_False )
78 : , bInsertFooterFirst( sal_False )
79 : , bHeaderInserted( sal_False )
80 : , bFooterInserted( sal_False )
81 : , bHeaderLeftInserted( sal_False )
82 : , bFooterLeftInserted( sal_False )
83 : , bHeaderFirstInserted( sal_False )
84 72 : , bFooterFirstInserted( sal_False )
85 : {
86 72 : OUString sName, sDisplayName;
87 72 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
88 224 : for( sal_Int16 i=0; i < nAttrCount; i++ )
89 : {
90 152 : const OUString& rAttrName = xAttrList->getNameByIndex( i );
91 152 : OUString aLocalName;
92 152 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName, &aLocalName );
93 152 : if( XML_NAMESPACE_STYLE == nPrefix )
94 : {
95 152 : if( IsXMLToken( aLocalName, XML_NAME ) )
96 : {
97 72 : sName = xAttrList->getValueByIndex( i );
98 : }
99 80 : else if( IsXMLToken( aLocalName, XML_DISPLAY_NAME ) )
100 : {
101 4 : sDisplayName = xAttrList->getValueByIndex( i );
102 : }
103 76 : else if( IsXMLToken( aLocalName, XML_NEXT_STYLE_NAME ) )
104 : {
105 4 : sFollow = xAttrList->getValueByIndex( i );
106 : }
107 72 : else if( IsXMLToken( aLocalName, XML_PAGE_LAYOUT_NAME ) )
108 : {
109 72 : sPageMasterName = xAttrList->getValueByIndex( i );
110 : }
111 : }
112 152 : }
113 :
114 72 : if( !sDisplayName.isEmpty() )
115 : {
116 : rImport.AddStyleDisplayName( XML_STYLE_FAMILY_MASTER_PAGE, sName,
117 4 : sDisplayName );
118 : }
119 : else
120 : {
121 68 : sDisplayName = sName;
122 : }
123 :
124 72 : if( sDisplayName.isEmpty() )
125 : return;
126 :
127 : Reference < XNameContainer > xPageStyles =
128 72 : GetImport().GetTextImport()->GetPageStyles();
129 72 : if( !xPageStyles.is() )
130 : return;
131 :
132 72 : Any aAny;
133 72 : sal_Bool bNew = sal_False;
134 72 : if( xPageStyles->hasByName( sDisplayName ) )
135 : {
136 63 : aAny = xPageStyles->getByName( sDisplayName );
137 63 : aAny >>= xStyle;
138 : }
139 : else
140 : {
141 9 : xStyle = Create();
142 9 : if( !xStyle.is() )
143 : return;
144 :
145 9 : aAny <<= xStyle;
146 9 : xPageStyles->insertByName( sDisplayName, aAny );
147 9 : bNew = sal_True;
148 : }
149 :
150 72 : Reference < XPropertySet > xPropSet( xStyle, UNO_QUERY );
151 : Reference< XPropertySetInfo > xPropSetInfo =
152 72 : xPropSet->getPropertySetInfo();
153 72 : if( !bNew && xPropSetInfo->hasPropertyByName( sIsPhysical ) )
154 : {
155 19 : aAny = xPropSet->getPropertyValue( sIsPhysical );
156 19 : bNew = !*(sal_Bool *)aAny.getValue();
157 : }
158 72 : SetNew( bNew );
159 :
160 72 : if( bOverwrite || bNew )
161 : {
162 : Reference < XMultiPropertyStates > xMultiStates( xPropSet,
163 72 : UNO_QUERY );
164 : OSL_ENSURE( xMultiStates.is(),
165 : "text page style does not support multi property set" );
166 72 : if( xMultiStates.is() )
167 72 : xMultiStates->setAllPropertiesToDefault();
168 :
169 72 : bInsertHeader = bInsertFooter = sal_True;
170 72 : bInsertHeaderLeft = bInsertFooterLeft = sal_True;
171 72 : bInsertHeaderFirst = bInsertFooterFirst = sal_True;
172 72 : }
173 : }
174 :
175 100 : XMLTextMasterPageContext::~XMLTextMasterPageContext()
176 : {
177 100 : }
178 :
179 206 : SvXMLImportContext *XMLTextMasterPageContext::CreateChildContext(
180 : sal_uInt16 nPrefix,
181 : const OUString& rLocalName,
182 : const Reference< XAttributeList > & xAttrList )
183 : {
184 206 : SvXMLImportContext *pContext = 0;
185 :
186 : const SvXMLTokenMap& rTokenMap =
187 206 : GetImport().GetTextImport()->GetTextMasterPageElemTokenMap();
188 :
189 206 : sal_Bool bInsert = sal_False, bFooter = sal_False, bLeft = sal_False, bFirst = sal_False;
190 206 : switch( rTokenMap.Get( nPrefix, rLocalName ) )
191 : {
192 : case XML_TOK_TEXT_MP_HEADER:
193 51 : if( bInsertHeader && !bHeaderInserted )
194 : {
195 51 : bInsert = sal_True;
196 51 : bHeaderInserted = sal_True;
197 : }
198 51 : break;
199 : case XML_TOK_TEXT_MP_FOOTER:
200 51 : if( bInsertFooter && !bFooterInserted )
201 : {
202 51 : bInsert = bFooter = sal_True;
203 51 : bFooterInserted = sal_True;
204 : }
205 51 : break;
206 : case XML_TOK_TEXT_MP_HEADER_LEFT:
207 48 : if( bInsertHeaderLeft && bHeaderInserted && !bHeaderLeftInserted )
208 48 : bInsert = bLeft = sal_True;
209 48 : break;
210 : case XML_TOK_TEXT_MP_FOOTER_LEFT:
211 48 : if( bInsertFooterLeft && bFooterInserted && !bFooterLeftInserted )
212 48 : bInsert = bFooter = bLeft = sal_True;
213 48 : break;
214 : case XML_TOK_TEXT_MP_HEADER_FIRST:
215 4 : if( bInsertHeaderFirst && bHeaderInserted && !bHeaderFirstInserted )
216 4 : bInsert = bFirst = sal_True;
217 4 : break;
218 : case XML_TOK_TEXT_MP_FOOTER_FIRST:
219 4 : if( bInsertFooterFirst && bFooterInserted && !bFooterFirstInserted )
220 4 : bInsert = bFooter = bFirst = sal_True;
221 4 : break;
222 : }
223 :
224 206 : if( bInsert && xStyle.is() )
225 : {
226 : pContext = CreateHeaderFooterContext( nPrefix, rLocalName,
227 : xAttrList,
228 206 : bFooter, bLeft, bFirst );
229 : }
230 : else
231 : {
232 : pContext = SvXMLStyleContext::CreateChildContext( nPrefix, rLocalName,
233 0 : xAttrList );
234 : }
235 :
236 206 : return pContext;
237 : }
238 :
239 30 : SvXMLImportContext *XMLTextMasterPageContext::CreateHeaderFooterContext(
240 : sal_uInt16 nPrefix,
241 : const ::rtl::OUString& rLocalName,
242 : const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList > & xAttrList,
243 : const sal_Bool bFooter,
244 : const sal_Bool bLeft,
245 : const sal_Bool bFirst )
246 : {
247 30 : Reference < XPropertySet > xPropSet( xStyle, UNO_QUERY );
248 30 : return new XMLTextHeaderFooterContext( GetImport(),
249 : nPrefix, rLocalName,
250 : xAttrList,
251 : xPropSet,
252 30 : bFooter, bLeft, bFirst );
253 : }
254 :
255 72 : void XMLTextMasterPageContext::Finish( sal_Bool bOverwrite )
256 : {
257 72 : if( xStyle.is() && (IsNew() || bOverwrite) )
258 : {
259 72 : Reference < XPropertySet > xPropSet( xStyle, UNO_QUERY );
260 72 : if( !sPageMasterName.isEmpty() )
261 : {
262 : XMLPropStyleContext* pStyle =
263 72 : GetImport().GetTextImport()->FindPageMaster( sPageMasterName );
264 72 : if (pStyle)
265 : {
266 72 : pStyle->FillPropertySet(xPropSet);
267 : }
268 : }
269 :
270 : Reference < XNameContainer > xPageStyles =
271 72 : GetImport().GetTextImport()->GetPageStyles();
272 72 : if( !xPageStyles.is() )
273 72 : return;
274 :
275 : Reference< XPropertySetInfo > xPropSetInfo =
276 72 : xPropSet->getPropertySetInfo();
277 72 : if( xPropSetInfo->hasPropertyByName( sFollowStyle ) )
278 : {
279 : OUString sDisplayFollow(
280 28 : GetImport().GetStyleDisplayName(
281 56 : XML_STYLE_FAMILY_MASTER_PAGE, sFollow ) );
282 32 : if( sDisplayFollow.isEmpty() ||
283 4 : !xPageStyles->hasByName( sDisplayFollow ) )
284 24 : sDisplayFollow = xStyle->getName();
285 :
286 28 : Any aAny = xPropSet->getPropertyValue( sFollowStyle );
287 28 : OUString sCurrFollow;
288 28 : aAny >>= sCurrFollow;
289 28 : if( sCurrFollow != sDisplayFollow )
290 : {
291 2 : aAny <<= sDisplayFollow;
292 2 : xPropSet->setPropertyValue( sFollowStyle, aAny );
293 28 : }
294 : }
295 :
296 72 : if ( xPropSetInfo->hasPropertyByName( "Hidden" ) )
297 : {
298 72 : xPropSet->setPropertyValue( "Hidden", uno::makeAny( IsHidden( ) ) );
299 72 : }
300 : }
301 : }
302 :
303 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|