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