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