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