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/text/XText.hpp>
27cdf0e10cSrcweir #include <com/sun/star/text/XRelativeTextContentRemove.hpp>
28cdf0e10cSrcweir #include <xmloff/nmspmap.hxx>
29cdf0e10cSrcweir #include "xmloff/xmlnmspe.hxx"
30cdf0e10cSrcweir #include "XMLTextHeaderFooterContext.hxx"
31cdf0e10cSrcweir #ifndef _XMLOFF_TEXTTABLECONTEXT_HXX_
32cdf0e10cSrcweir #include <xmloff/XMLTextTableContext.hxx>
33cdf0e10cSrcweir #endif
34cdf0e10cSrcweir #include <xmloff/xmlimp.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir 
37cdf0e10cSrcweir using ::rtl::OUString;
38cdf0e10cSrcweir using ::rtl::OUStringBuffer;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir using namespace ::com::sun::star;
41cdf0e10cSrcweir using namespace ::com::sun::star::uno;
42cdf0e10cSrcweir using namespace ::com::sun::star::xml::sax;
43cdf0e10cSrcweir //using namespace ::com::sun::star::style;
44cdf0e10cSrcweir using namespace ::com::sun::star::text;
45cdf0e10cSrcweir using namespace ::com::sun::star::beans;
46cdf0e10cSrcweir //using namespace ::com::sun::star::container;
47cdf0e10cSrcweir //using namespace ::com::sun::star::lang;
48cdf0e10cSrcweir //using namespace ::com::sun::star::text;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir 
51cdf0e10cSrcweir TYPEINIT1( XMLTextHeaderFooterContext, SvXMLImportContext );
52cdf0e10cSrcweir 
XMLTextHeaderFooterContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const OUString & rLName,const uno::Reference<xml::sax::XAttributeList> &,const Reference<XPropertySet> & rPageStylePropSet,sal_Bool bFooter,sal_Bool bLft)53cdf0e10cSrcweir XMLTextHeaderFooterContext::XMLTextHeaderFooterContext( SvXMLImport& rImport, sal_uInt16 nPrfx,
54cdf0e10cSrcweir 					   const OUString& rLName,
55cdf0e10cSrcweir 					   const uno::Reference<
56cdf0e10cSrcweir 							xml::sax::XAttributeList > &,
57cdf0e10cSrcweir 						const Reference < XPropertySet > & rPageStylePropSet,
58cdf0e10cSrcweir 					   sal_Bool bFooter, sal_Bool bLft ) :
59cdf0e10cSrcweir 	SvXMLImportContext( rImport, nPrfx, rLName ),
60cdf0e10cSrcweir 	xPropSet( rPageStylePropSet ),
61cdf0e10cSrcweir 	sOn( OUString::createFromAscii( bFooter ? "FooterIsOn" : "HeaderIsOn" ) ),
62cdf0e10cSrcweir 	sShareContent( OUString::createFromAscii( bFooter ? "FooterIsShared"
63cdf0e10cSrcweir 													  : "HeaderIsShared" ) ),
64cdf0e10cSrcweir 	sText( OUString::createFromAscii( bFooter ? "FooterText" : "HeaderText" ) ),
65cdf0e10cSrcweir 	sTextLeft( OUString::createFromAscii( bFooter ? "FooterTextLeft"
66cdf0e10cSrcweir 													 : "HeaderTextLeft" ) ),
67cdf0e10cSrcweir 	bInsertContent( sal_True ),
68cdf0e10cSrcweir 	bLeft( bLft )
69cdf0e10cSrcweir {
70cdf0e10cSrcweir 	if( bLeft )
71cdf0e10cSrcweir 	{
72cdf0e10cSrcweir 		Any aAny;
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 		aAny = xPropSet->getPropertyValue( sOn );
75cdf0e10cSrcweir 		sal_Bool bOn = *(sal_Bool *)aAny.getValue();
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 		if( bOn )
78cdf0e10cSrcweir 		{
79cdf0e10cSrcweir 			aAny = xPropSet->getPropertyValue( sShareContent );
80cdf0e10cSrcweir 			sal_Bool bShared = *(sal_Bool *)aAny.getValue();
81cdf0e10cSrcweir 			if( bShared )
82cdf0e10cSrcweir 			{
83cdf0e10cSrcweir 				// Don't share headers any longer
84cdf0e10cSrcweir 				bShared = sal_False;
85cdf0e10cSrcweir 				aAny.setValue( &bShared, ::getBooleanCppuType() );
86cdf0e10cSrcweir 				xPropSet->setPropertyValue( sShareContent, aAny );
87cdf0e10cSrcweir 			}
88cdf0e10cSrcweir 		}
89cdf0e10cSrcweir 		else
90cdf0e10cSrcweir 		{
91cdf0e10cSrcweir 			// If headers or footers are switched off, no content must be
92cdf0e10cSrcweir 			// inserted.
93cdf0e10cSrcweir 			bInsertContent = sal_False;
94cdf0e10cSrcweir 		}
95cdf0e10cSrcweir 	}
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
~XMLTextHeaderFooterContext()98cdf0e10cSrcweir XMLTextHeaderFooterContext::~XMLTextHeaderFooterContext()
99cdf0e10cSrcweir {
100cdf0e10cSrcweir }
101cdf0e10cSrcweir 
CreateChildContext(sal_uInt16 nPrefix,const OUString & rLocalName,const uno::Reference<xml::sax::XAttributeList> & xAttrList)102cdf0e10cSrcweir SvXMLImportContext *XMLTextHeaderFooterContext::CreateChildContext(
103cdf0e10cSrcweir 	sal_uInt16 nPrefix,
104cdf0e10cSrcweir 	const OUString& rLocalName,
105cdf0e10cSrcweir 	const uno::Reference< xml::sax::XAttributeList > & xAttrList )
106cdf0e10cSrcweir {
107cdf0e10cSrcweir 	SvXMLImportContext *pContext = 0;
108cdf0e10cSrcweir 	if( bInsertContent )
109cdf0e10cSrcweir 	{
110cdf0e10cSrcweir 		if( !xOldTextCursor.is() )
111cdf0e10cSrcweir 		{
112cdf0e10cSrcweir 			sal_Bool bRemoveContent = sal_True;
113cdf0e10cSrcweir 			Any aAny;
114cdf0e10cSrcweir 			if( bLeft )
115cdf0e10cSrcweir 			{
116cdf0e10cSrcweir 				// Headers and footers are switched on already,
117cdf0e10cSrcweir 				// and they aren't shared.
118cdf0e10cSrcweir 				aAny = xPropSet->getPropertyValue( sTextLeft );
119cdf0e10cSrcweir 			}
120cdf0e10cSrcweir 			else
121cdf0e10cSrcweir 			{
122cdf0e10cSrcweir 				aAny = xPropSet->getPropertyValue( sOn );
123cdf0e10cSrcweir 				sal_Bool bOn = *(sal_Bool *)aAny.getValue();
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 				if( !bOn )
126cdf0e10cSrcweir 				{
127cdf0e10cSrcweir 					// Switch header on
128cdf0e10cSrcweir 					bOn = sal_True;
129cdf0e10cSrcweir 					aAny.setValue( &bOn, ::getBooleanCppuType() );
130cdf0e10cSrcweir 					xPropSet->setPropertyValue( sOn, aAny );
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 					// The content has not to be removed, because the header
133cdf0e10cSrcweir 					// or footer is empty already.
134cdf0e10cSrcweir 					bRemoveContent = sal_False;
135cdf0e10cSrcweir 				}
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 				// If a header or footer is not shared, share it now.
138cdf0e10cSrcweir 				aAny = xPropSet->getPropertyValue( sShareContent );
139cdf0e10cSrcweir 				sal_Bool bShared = *(sal_Bool *)aAny.getValue();
140cdf0e10cSrcweir 				if( !bShared )
141cdf0e10cSrcweir 				{
142cdf0e10cSrcweir 					bShared = sal_True;
143cdf0e10cSrcweir 					aAny.setValue( &bShared, ::getBooleanCppuType() );
144cdf0e10cSrcweir 					xPropSet->setPropertyValue( sShareContent, aAny );
145cdf0e10cSrcweir 				}
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 				aAny = xPropSet->getPropertyValue( sText );
148cdf0e10cSrcweir 			}
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 			Reference < XText > xText;
151cdf0e10cSrcweir 			aAny >>= xText;
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 			if( bRemoveContent )
154cdf0e10cSrcweir 			{
155cdf0e10cSrcweir 				OUString aText;
156cdf0e10cSrcweir 				xText->setString( aText );
157cdf0e10cSrcweir 			}
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 			UniReference < XMLTextImportHelper > xTxtImport =
160cdf0e10cSrcweir 				GetImport().GetTextImport();
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 			xOldTextCursor = xTxtImport->GetCursor();
163cdf0e10cSrcweir 			xTxtImport->SetCursor( xText->createTextCursor() );
164cdf0e10cSrcweir 		}
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 		pContext =
167cdf0e10cSrcweir 			GetImport().GetTextImport()->CreateTextChildContext(
168cdf0e10cSrcweir 				GetImport(), nPrefix, rLocalName, xAttrList,
169cdf0e10cSrcweir 				XML_TEXT_TYPE_HEADER_FOOTER );
170cdf0e10cSrcweir 	}
171cdf0e10cSrcweir 	if( !pContext )
172cdf0e10cSrcweir 		pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 	return pContext;
175cdf0e10cSrcweir }
176cdf0e10cSrcweir 
EndElement()177cdf0e10cSrcweir void XMLTextHeaderFooterContext::EndElement()
178cdf0e10cSrcweir {
179cdf0e10cSrcweir 	if( xOldTextCursor.is() )
180cdf0e10cSrcweir 	{
181cdf0e10cSrcweir 		GetImport().GetTextImport()->DeleteParagraph();
182cdf0e10cSrcweir 		GetImport().GetTextImport()->SetCursor( xOldTextCursor );
183cdf0e10cSrcweir 	}
184cdf0e10cSrcweir 	else if( !bLeft )
185cdf0e10cSrcweir 	{
186cdf0e10cSrcweir 		// If no content has been inserted inro the header or footer,
187cdf0e10cSrcweir 		// switch it off.
188cdf0e10cSrcweir 		sal_Bool bOn = sal_False;
189cdf0e10cSrcweir 		Any aAny;
190cdf0e10cSrcweir 		aAny.setValue( &bOn, ::getBooleanCppuType() );
191cdf0e10cSrcweir 		xPropSet->setPropertyValue( sOn, aAny );
192cdf0e10cSrcweir 	}
193cdf0e10cSrcweir }
194cdf0e10cSrcweir 
195