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