1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 #include <com/sun/star/text/XText.hpp>
27 #include <xmloff/nmspmap.hxx>
28 #include <xmloff/xmlnmspe.hxx>
29 #include "XMLTableHeaderFooterContext.hxx"
30 #include <xmloff/xmltoken.hxx>
31 #include <comphelper/extract.hxx>
32 
33 #include "unonames.hxx"
34 
35 using ::rtl::OUString;
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::xml::sax;
39 using namespace ::com::sun::star::text;
40 using namespace ::com::sun::star::beans;
41 using namespace xmloff::token;
42 
43 
44 TYPEINIT1( XMLTableHeaderFooterContext, SvXMLImportContext );
45 
XMLTableHeaderFooterContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const OUString & rLName,const uno::Reference<xml::sax::XAttributeList> & xAttrList,const Reference<XPropertySet> & rPageStylePropSet,sal_Bool bFooter,sal_Bool bLft)46 XMLTableHeaderFooterContext::XMLTableHeaderFooterContext( SvXMLImport& rImport, sal_uInt16 nPrfx,
47 					   const OUString& rLName,
48 					   const uno::Reference<
49 							xml::sax::XAttributeList > & xAttrList,
50 						const Reference < XPropertySet > & rPageStylePropSet,
51 					   sal_Bool bFooter, sal_Bool bLft ) :
52 	SvXMLImportContext( rImport, nPrfx, rLName ),
53 	xPropSet( rPageStylePropSet ),
54 	sOn( OUString::createFromAscii( bFooter ? SC_UNO_PAGE_FTRON : SC_UNO_PAGE_HDRON ) ),
55 	sShareContent( OUString::createFromAscii( bFooter ? SC_UNO_PAGE_FTRSHARED : SC_UNO_PAGE_HDRSHARED ) ),
56 	sContent( OUString::createFromAscii( bFooter ? SC_UNO_PAGE_RIGHTFTRCON : SC_UNO_PAGE_RIGHTHDRCON ) ),
57 	sContentLeft( OUString::createFromAscii( bFooter ? SC_UNO_PAGE_LEFTFTRCONT : SC_UNO_PAGE_LEFTHDRCONT ) ),
58 	bDisplay( sal_True ),
59 	bInsertContent( sal_True ),
60 	bLeft( bLft ),
61 	bContainsLeft(sal_False),
62 	bContainsRight(sal_False),
63 	bContainsCenter(sal_False)
64 {
65 	sal_Int16 nAttrCount(xAttrList.is() ? xAttrList->getLength() : 0);
66 	for( sal_Int16 i=0; i < nAttrCount; ++i )
67 	{
68 		const OUString& rAttrName(xAttrList->getNameByIndex( i ));
69 		OUString aLName;
70 		sal_uInt16 nPrefix(GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName, &aLName ));
71 		const OUString& rValue(xAttrList->getValueByIndex( i ));
72 
73 		// TODO: use a map here
74         if( XML_NAMESPACE_STYLE == nPrefix )
75 		{
76 			if( IsXMLToken(aLName, XML_DISPLAY ) )
77 				bDisplay = IsXMLToken(rValue, XML_TRUE);
78 		}
79 	}
80 	if( bLeft )
81 	{
82         sal_Bool bOn(::cppu::any2bool(xPropSet->getPropertyValue( sOn )));
83 
84 		if( bOn && bDisplay )
85 		{
86 			if( ::cppu::any2bool(xPropSet->getPropertyValue( sShareContent )) )
87 				// Don't share headers any longer
88                 xPropSet->setPropertyValue( sShareContent, uno::makeAny(sal_False) );
89 		}
90 		else
91 		{
92 			if( !::cppu::any2bool(xPropSet->getPropertyValue( sShareContent )) )
93 				// share headers
94                 xPropSet->setPropertyValue( sShareContent, uno::makeAny(sal_True) );
95 		}
96 	}
97 	else
98 	{
99         sal_Bool bOn(::cppu::any2bool(xPropSet->getPropertyValue( sOn )));
100 		if ( bOn != bDisplay )
101             xPropSet->setPropertyValue( sOn, uno::makeAny(bDisplay) );
102 	}
103 	if (bLeft)
104 		sCont = sContentLeft;
105 	else
106 		sCont = sContent;
107 	xPropSet->getPropertyValue( sCont ) >>= xHeaderFooterContent;
108 }
109 
~XMLTableHeaderFooterContext()110 XMLTableHeaderFooterContext::~XMLTableHeaderFooterContext()
111 {
112 }
113 
CreateChildContext(sal_uInt16 nPrefix,const OUString & rLocalName,const uno::Reference<xml::sax::XAttributeList> & xAttrList)114 SvXMLImportContext *XMLTableHeaderFooterContext::CreateChildContext(
115 	sal_uInt16 nPrefix,
116 	const OUString& rLocalName,
117 	const uno::Reference< xml::sax::XAttributeList > & xAttrList )
118 {
119 	SvXMLImportContext *pContext(0);
120 
121 	if ((nPrefix == XML_NAMESPACE_TEXT) &&
122 		IsXMLToken(rLocalName, XML_P))
123 	{
124 		if (!xTextCursor.is())
125 		{
126 			if( xHeaderFooterContent.is() )
127 			{
128 				uno::Reference < text::XText > xText(xHeaderFooterContent->getCenterText());
129 				xText->setString(sEmpty);
130 				xTextCursor.set(xText->createTextCursor());
131 				xOldTextCursor.set(GetImport().GetTextImport()->GetCursor());
132 				GetImport().GetTextImport()->SetCursor( xTextCursor );
133 				bContainsCenter = sal_True;
134 			}
135 		}
136 		pContext =
137 			GetImport().GetTextImport()->CreateTextChildContext(GetImport(),
138 																	nPrefix,
139 																	rLocalName,
140 																	xAttrList);
141 	}
142 	else
143 	{
144 		if (nPrefix == XML_NAMESPACE_STYLE)
145 		{
146 			if (xHeaderFooterContent.is())
147 			{
148 				uno::Reference < text::XText > xText;
149 				if (IsXMLToken(rLocalName, XML_REGION_LEFT ))
150 				{
151 					xText.set(xHeaderFooterContent->getLeftText());
152 					bContainsLeft = sal_True;
153 				}
154 				else if (IsXMLToken(rLocalName, XML_REGION_CENTER ))
155 				{
156 					xText.set(xHeaderFooterContent->getCenterText());
157 					bContainsCenter = sal_True;
158 				}
159 				else if (IsXMLToken(rLocalName, XML_REGION_RIGHT ))
160 				{
161 					xText.set(xHeaderFooterContent->getRightText());
162 					bContainsRight = sal_True;
163 				}
164 				if (xText.is())
165 				{
166 					xText->setString(sEmpty);
167                     //SvXMLImport aSvXMLImport( GetImport() );
168                     uno::Reference < text::XTextCursor > xTempTextCursor(xText->createTextCursor());
169 					pContext = new XMLHeaderFooterRegionContext( GetImport(), nPrefix, rLocalName, xAttrList, xTempTextCursor);
170 				}
171 			}
172 		}
173 	}
174 	if( !pContext )
175 		pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
176 
177 	return pContext;
178 }
179 
EndElement()180 void XMLTableHeaderFooterContext::EndElement()
181 {
182 	if( GetImport().GetTextImport()->GetCursor().is() )
183 	{
184 		//GetImport().GetTextImport()->GetCursor()->gotoEnd(sal_False);
185 		if( GetImport().GetTextImport()->GetCursor()->goLeft( 1, sal_True ) )
186 		{
187 			GetImport().GetTextImport()->GetText()->insertString(
188 				GetImport().GetTextImport()->GetCursorAsRange(), sEmpty,
189 				sal_True );
190 		}
191 		GetImport().GetTextImport()->ResetCursor();
192 	}
193 	if (xOldTextCursor.is())
194 		GetImport().GetTextImport()->SetCursor(xOldTextCursor);
195 	if (xHeaderFooterContent.is())
196 	{
197 		if (!bContainsLeft)
198 			xHeaderFooterContent->getLeftText()->setString(sEmpty);
199 		if (!bContainsCenter)
200 			xHeaderFooterContent->getCenterText()->setString(sEmpty);
201 		if (!bContainsRight)
202 			xHeaderFooterContent->getRightText()->setString(sEmpty);
203 
204         xPropSet->setPropertyValue( sCont, uno::makeAny(xHeaderFooterContent) );
205 	}
206 }
207 
208 TYPEINIT1( XMLHeaderFooterRegionContext, SvXMLImportContext );
209 
XMLHeaderFooterRegionContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const OUString & rLName,const uno::Reference<xml::sax::XAttributeList> &,uno::Reference<text::XTextCursor> & xCursor)210 XMLHeaderFooterRegionContext::XMLHeaderFooterRegionContext( SvXMLImport& rImport, sal_uInt16 nPrfx,
211 					   const OUString& rLName,
212 					   const uno::Reference<
213                             xml::sax::XAttributeList > & /* xAttrList */,
214 					   uno::Reference< text::XTextCursor >& xCursor ) :
215 	SvXMLImportContext( rImport, nPrfx, rLName ),
216 	xTextCursor ( xCursor )
217 {
218 	xOldTextCursor.set(GetImport().GetTextImport()->GetCursor());
219 	GetImport().GetTextImport()->SetCursor( xTextCursor );
220 }
221 
~XMLHeaderFooterRegionContext()222 XMLHeaderFooterRegionContext::~XMLHeaderFooterRegionContext()
223 {
224 }
225 
CreateChildContext(sal_uInt16 nPrefix,const OUString & rLocalName,const uno::Reference<xml::sax::XAttributeList> & xAttrList)226 SvXMLImportContext *XMLHeaderFooterRegionContext::CreateChildContext(
227 	sal_uInt16 nPrefix,
228 	const OUString& rLocalName,
229 	const uno::Reference< xml::sax::XAttributeList > & xAttrList )
230 {
231 	SvXMLImportContext *pContext(0);
232 
233 	if ((nPrefix == XML_NAMESPACE_TEXT) &&
234 		IsXMLToken(rLocalName, XML_P))
235 	{
236 		pContext =
237 			GetImport().GetTextImport()->CreateTextChildContext(GetImport(),
238 																	nPrefix,
239 																	rLocalName,
240 																	xAttrList);
241 	}
242 	if( !pContext )
243 		pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
244 
245 	return pContext;
246 }
247 
EndElement()248 void XMLHeaderFooterRegionContext::EndElement()
249 {
250 	if( GetImport().GetTextImport()->GetCursor().is() )
251 	{
252 		//GetImport().GetTextImport()->GetCursor()->gotoEnd(sal_False);
253 		if( GetImport().GetTextImport()->GetCursor()->goLeft( 1, sal_True ) )
254 		{
255 			OUString sEmpty;
256 			GetImport().GetTextImport()->GetText()->insertString(
257 				GetImport().GetTextImport()->GetCursorAsRange(), sEmpty,
258 				sal_True );
259 		}
260 		GetImport().GetTextImport()->ResetCursor();
261 	}
262 	if (xOldTextCursor.is())
263 		GetImport().GetTextImport()->SetCursor(xOldTextCursor);
264 }
265