1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/style/XStyle.hpp>
21 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
22 #include <com/sun/star/style/PageStyleLayout.hpp>
23 #include <com/sun/star/beans/XMultiPropertyStates.hpp>
24 #include <osl/diagnose.h>
25 #include <xmloff/nmspmap.hxx>
26 #include <xmloff/xmlnmspe.hxx>
27 #include <xmloff/xmltoken.hxx>
28 #include <xmloff/XMLTextMasterPageContext.hxx>
29 #include "XMLTextHeaderFooterContext.hxx"
30 #include <xmloff/xmlimp.hxx>
31 #include "PageMasterImportContext.hxx"
35 using namespace ::com::sun::star
;
36 using namespace ::com::sun::star::uno
;
37 using namespace ::com::sun::star::xml::sax
;
38 using namespace ::com::sun::star::style
;
39 using namespace ::com::sun::star::text
;
40 using namespace ::com::sun::star::beans
;
41 using namespace ::com::sun::star::container
;
42 using namespace ::com::sun::star::lang
;
43 using namespace ::xmloff::token
;
45 Reference
< XStyle
> XMLTextMasterPageContext::Create()
47 Reference
< XStyle
> xNewStyle
;
49 Reference
< XMultiServiceFactory
> xFactory( GetImport().GetModel(),
53 Reference
< XInterface
> xIfc
=
54 xFactory
->createInstance("com.sun.star.style.PageStyle");
56 xNewStyle
= Reference
< XStyle
>( xIfc
, UNO_QUERY
);
61 TYPEINIT1( XMLTextMasterPageContext
, SvXMLStyleContext
);
63 XMLTextMasterPageContext::XMLTextMasterPageContext( SvXMLImport
& rImport
,
64 sal_uInt16 nPrfx
, const OUString
& rLName
,
65 const Reference
< XAttributeList
> & xAttrList
,
67 : SvXMLStyleContext( rImport
, nPrfx
, rLName
, xAttrList
, XML_STYLE_FAMILY_MASTER_PAGE
)
68 , sIsPhysical( "IsPhysical" )
69 , sFollowStyle( "FollowStyle" )
70 , bInsertHeader( false )
71 , bInsertFooter( false )
72 , bInsertHeaderLeft( false )
73 , bInsertFooterLeft( false )
74 , bInsertHeaderFirst( false )
75 , bInsertFooterFirst( false )
76 , bHeaderInserted( false )
77 , bFooterInserted( false )
78 , bHeaderLeftInserted( false )
79 , bFooterLeftInserted( false )
80 , bHeaderFirstInserted( false )
81 , bFooterFirstInserted( false )
83 OUString sName
, sDisplayName
;
84 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
85 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
87 const OUString
& rAttrName
= xAttrList
->getNameByIndex( i
);
89 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName
, &aLocalName
);
90 if( XML_NAMESPACE_STYLE
== nPrefix
)
92 if( IsXMLToken( aLocalName
, XML_NAME
) )
94 sName
= xAttrList
->getValueByIndex( i
);
96 else if( IsXMLToken( aLocalName
, XML_DISPLAY_NAME
) )
98 sDisplayName
= xAttrList
->getValueByIndex( i
);
100 else if( IsXMLToken( aLocalName
, XML_NEXT_STYLE_NAME
) )
102 sFollow
= xAttrList
->getValueByIndex( i
);
104 else if( IsXMLToken( aLocalName
, XML_PAGE_LAYOUT_NAME
) )
106 sPageMasterName
= xAttrList
->getValueByIndex( i
);
111 if( !sDisplayName
.isEmpty() )
113 rImport
.AddStyleDisplayName( XML_STYLE_FAMILY_MASTER_PAGE
, sName
,
118 sDisplayName
= sName
;
121 if( sDisplayName
.isEmpty() )
124 Reference
< XNameContainer
> xPageStyles
=
125 GetImport().GetTextImport()->GetPageStyles();
126 if( !xPageStyles
.is() )
131 if( xPageStyles
->hasByName( sDisplayName
) )
133 aAny
= xPageStyles
->getByName( sDisplayName
);
143 xPageStyles
->insertByName( sDisplayName
, aAny
);
147 Reference
< XPropertySet
> xPropSet( xStyle
, UNO_QUERY
);
148 Reference
< XPropertySetInfo
> xPropSetInfo
=
149 xPropSet
->getPropertySetInfo();
150 if( !bNew
&& xPropSetInfo
->hasPropertyByName( sIsPhysical
) )
152 aAny
= xPropSet
->getPropertyValue( sIsPhysical
);
153 bNew
= !*static_cast<sal_Bool
const *>(aAny
.getValue());
157 if( bOverwrite
|| bNew
)
159 Reference
< XMultiPropertyStates
> xMultiStates( xPropSet
,
161 OSL_ENSURE( xMultiStates
.is(),
162 "text page style does not support multi property set" );
163 if( xMultiStates
.is() )
164 xMultiStates
->setAllPropertiesToDefault();
166 bInsertHeader
= bInsertFooter
= true;
167 bInsertHeaderLeft
= bInsertFooterLeft
= true;
168 bInsertHeaderFirst
= bInsertFooterFirst
= true;
172 XMLTextMasterPageContext::~XMLTextMasterPageContext()
176 SvXMLImportContext
*XMLTextMasterPageContext::CreateChildContext(
178 const OUString
& rLocalName
,
179 const Reference
< XAttributeList
> & xAttrList
)
181 SvXMLImportContext
*pContext
= 0;
183 const SvXMLTokenMap
& rTokenMap
=
184 GetImport().GetTextImport()->GetTextMasterPageElemTokenMap();
186 bool bInsert
= false, bFooter
= false, bLeft
= false, bFirst
= false;
187 switch( rTokenMap
.Get( nPrefix
, rLocalName
) )
189 case XML_TOK_TEXT_MP_HEADER
:
190 if( bInsertHeader
&& !bHeaderInserted
)
193 bHeaderInserted
= true;
196 case XML_TOK_TEXT_MP_FOOTER
:
197 if( bInsertFooter
&& !bFooterInserted
)
199 bInsert
= bFooter
= true;
200 bFooterInserted
= true;
203 case XML_TOK_TEXT_MP_HEADER_LEFT
:
204 if( bInsertHeaderLeft
&& bHeaderInserted
&& !bHeaderLeftInserted
)
205 bInsert
= bLeft
= true;
207 case XML_TOK_TEXT_MP_FOOTER_LEFT
:
208 if( bInsertFooterLeft
&& bFooterInserted
&& !bFooterLeftInserted
)
209 bInsert
= bFooter
= bLeft
= true;
211 case XML_TOK_TEXT_MP_HEADER_FIRST
:
212 if( bInsertHeaderFirst
&& bHeaderInserted
&& !bHeaderFirstInserted
)
213 bInsert
= bFirst
= true;
215 case XML_TOK_TEXT_MP_FOOTER_FIRST
:
216 if( bInsertFooterFirst
&& bFooterInserted
&& !bFooterFirstInserted
)
217 bInsert
= bFooter
= bFirst
= true;
221 if( bInsert
&& xStyle
.is() )
223 pContext
= CreateHeaderFooterContext( nPrefix
, rLocalName
,
225 bFooter
, bLeft
, bFirst
);
229 pContext
= SvXMLStyleContext::CreateChildContext( nPrefix
, rLocalName
,
236 SvXMLImportContext
*XMLTextMasterPageContext::CreateHeaderFooterContext(
238 const OUString
& rLocalName
,
239 const ::com::sun::star::uno::Reference
< ::com::sun::star::xml::sax::XAttributeList
> & xAttrList
,
244 Reference
< XPropertySet
> xPropSet( xStyle
, UNO_QUERY
);
245 return new XMLTextHeaderFooterContext( GetImport(),
249 bFooter
, bLeft
, bFirst
);
252 void XMLTextMasterPageContext::Finish( bool bOverwrite
)
254 if( xStyle
.is() && (IsNew() || bOverwrite
) )
256 Reference
< XPropertySet
> xPropSet( xStyle
, UNO_QUERY
);
257 if( !sPageMasterName
.isEmpty() )
259 XMLPropStyleContext
* pStyle
=
260 GetImport().GetTextImport()->FindPageMaster( sPageMasterName
);
263 pStyle
->FillPropertySet(xPropSet
);
267 Reference
< XNameContainer
> xPageStyles
=
268 GetImport().GetTextImport()->GetPageStyles();
269 if( !xPageStyles
.is() )
272 Reference
< XPropertySetInfo
> xPropSetInfo
=
273 xPropSet
->getPropertySetInfo();
274 if( xPropSetInfo
->hasPropertyByName( sFollowStyle
) )
276 OUString
sDisplayFollow(
277 GetImport().GetStyleDisplayName(
278 XML_STYLE_FAMILY_MASTER_PAGE
, sFollow
) );
279 if( sDisplayFollow
.isEmpty() ||
280 !xPageStyles
->hasByName( sDisplayFollow
) )
281 sDisplayFollow
= xStyle
->getName();
283 Any aAny
= xPropSet
->getPropertyValue( sFollowStyle
);
284 OUString sCurrFollow
;
285 aAny
>>= sCurrFollow
;
286 if( sCurrFollow
!= sDisplayFollow
)
288 aAny
<<= sDisplayFollow
;
289 xPropSet
->setPropertyValue( sFollowStyle
, aAny
);
293 if ( xPropSetInfo
->hasPropertyByName( "Hidden" ) )
295 xPropSet
->setPropertyValue( "Hidden", uno::makeAny( IsHidden( ) ) );
300 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */