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