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