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_xmloff.hxx"
26 
27 
28 #include <com/sun/star/util/XStringSubstitution.hpp>
29 #include <xmloff/DocumentSettingsContext.hxx>
30 #include <xmloff/xmlimp.hxx>
31 #include <xmloff/xmltoken.hxx>
32 #include "xmloff/xmlnmspe.hxx"
33 #include <xmloff/nmspmap.hxx>
34 #include <xmloff/xmluconv.hxx>
35 #include <tools/debug.hxx>
36 
37 #ifndef __SGI_STL_LIST
38 #include <list>
39 #endif
40 #include <com/sun/star/i18n/XForbiddenCharacters.hpp>
41 #include <com/sun/star/container/XIndexContainer.hpp>
42 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
43 #include <com/sun/star/formula/SymbolDescriptor.hpp>
44 #include <comphelper/processfactory.hxx>
45 #include <com/sun/star/util/DateTime.hpp>
46 #include <com/sun/star/document/XViewDataSupplier.hpp>
47 #include <com/sun/star/document/PrinterIndependentLayout.hpp>
48 #include <comphelper/configurationhelper.hxx>
49 #include <rtl/ustrbuf.hxx>
50 #include <xmlenums.hxx>
51 
52 using namespace com::sun::star;
53 using namespace ::xmloff::token;
54 
55 #define C2U(cChar) ::rtl::OUString::createFromAscii(cChar)
56 
57 //------------------------------------------------------------------
58 
59 class XMLMyList
60 {
61 	std::list<beans::PropertyValue>	aProps;
62 	sal_uInt32						nCount;
63 
64 	// #110680#
65 	::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > mxServiceFactory;
66 
67 public:
68 	// #110680#
69 	XMLMyList(const uno::Reference<lang::XMultiServiceFactory>& xServiceFactory);
70 	~XMLMyList();
71 
push_back(beans::PropertyValue & aProp)72 	void push_back(beans::PropertyValue& aProp) { aProps.push_back(aProp); nCount++; }
73 	uno::Sequence<beans::PropertyValue> GetSequence();
74 	uno::Reference<container::XNameContainer> GetNameContainer();
75 	uno::Reference<container::XIndexContainer> GetIndexContainer();
76 };
77 
78 // #110680#
XMLMyList(const uno::Reference<lang::XMultiServiceFactory> & xServiceFactory)79 XMLMyList::XMLMyList(const uno::Reference<lang::XMultiServiceFactory>& xServiceFactory)
80 :	nCount(0),
81 	mxServiceFactory(xServiceFactory)
82 {
83 	DBG_ASSERT( mxServiceFactory.is(), "got no service manager" );
84 }
85 
86 // #110680#
~XMLMyList()87 XMLMyList::~XMLMyList()
88 {
89 }
90 
GetSequence()91 uno::Sequence<beans::PropertyValue> XMLMyList::GetSequence()
92 {
93 	uno::Sequence<beans::PropertyValue> aSeq;
94 	if(nCount)
95 	{
96 		DBG_ASSERT(nCount == aProps.size(), "wrong count of PropertyValue");
97 		aSeq.realloc(nCount);
98 		beans::PropertyValue* pProps = aSeq.getArray();
99 		std::list<beans::PropertyValue>::iterator aItr = aProps.begin();
100 		while (aItr != aProps.end())
101 		{
102 			*pProps = *aItr;
103 			pProps++;
104 			aItr++;
105 		}
106 	}
107 	return aSeq;
108 }
109 
GetNameContainer()110 uno::Reference<container::XNameContainer> XMLMyList::GetNameContainer()
111 {
112 	uno::Reference<container::XNameContainer> xNameContainer;
113 
114 	// #110680#
115 	// uno::Reference<lang::XMultiServiceFactory> xServiceFactory = comphelper::getProcessServiceFactory();
116 	// DBG_ASSERT( xServiceFactory.is(), "got no service manager" );
117 
118 	if( mxServiceFactory.is() )
119 	{
120 		rtl::OUString sName(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.document.NamedPropertyValues"));
121 		xNameContainer = uno::Reference<container::XNameContainer>(mxServiceFactory->createInstance(sName), uno::UNO_QUERY);
122 		if (xNameContainer.is())
123 		{
124 			std::list<beans::PropertyValue>::iterator aItr = aProps.begin();
125 			while (aItr != aProps.end())
126 			{
127 				xNameContainer->insertByName(aItr->Name, aItr->Value);
128 				aItr++;
129 			}
130 		}
131 	}
132 	return xNameContainer;
133 }
134 
GetIndexContainer()135 uno::Reference<container::XIndexContainer> XMLMyList::GetIndexContainer()
136 {
137 	uno::Reference<container::XIndexContainer> xIndexContainer;
138 	// #110680#
139 	// uno::Reference<lang::XMultiServiceFactory> xServiceFactory = comphelper::getProcessServiceFactory();
140 	// DBG_ASSERT( xServiceFactory.is(), "got no service manager" );
141 
142 	if( mxServiceFactory.is() )
143 	{
144 		rtl::OUString sName(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.document.IndexedPropertyValues"));
145 		xIndexContainer = uno::Reference<container::XIndexContainer>(mxServiceFactory->createInstance(sName), uno::UNO_QUERY);
146 		if (xIndexContainer.is())
147 		{
148 			std::list<beans::PropertyValue>::iterator aItr = aProps.begin();
149 			sal_uInt32 i(0);
150 			while (aItr != aProps.end())
151 			{
152 				xIndexContainer->insertByIndex(i, aItr->Value);
153 				aItr++;
154 				i++;
155 			}
156 		}
157 	}
158 	return xIndexContainer;
159 }
160 
161 //=============================================================================
162 
163 class XMLConfigBaseContext : public SvXMLImportContext
164 {
165 protected:
166 	XMLMyList					maProps;
167 	beans::PropertyValue		maProp;
168 	com::sun::star::uno::Any&	mrAny;
169 	XMLConfigBaseContext*		mpBaseContext;
170 public:
171 	XMLConfigBaseContext(SvXMLImport& rImport, sal_uInt16 nPrfx, const rtl::OUString& rLName,
172 									com::sun::star::uno::Any& rAny,
173 									XMLConfigBaseContext* pBaseContext);
174 	virtual ~XMLConfigBaseContext();
175 
AddPropertyValue()176 	void AddPropertyValue() { maProps.push_back(maProp); }
177 };
178 
179 //=============================================================================
180 
181 class XMLConfigItemContext : public SvXMLImportContext
182 {
183 	rtl::OUString				msType;
184 	rtl::OUString				msValue;
185 	uno::Sequence<sal_Int8>		maDecoded;
186 	com::sun::star::uno::Any&	mrAny;
187     const rtl::OUString         mrItemName;
188 	XMLConfigBaseContext*		mpBaseContext;
189 
190 public:
191 	XMLConfigItemContext(SvXMLImport& rImport, sal_uInt16 nPrfx, const rtl::OUString& rLName,
192 									const ::com::sun::star::uno::Reference<
193 									::com::sun::star::xml::sax::XAttributeList>& xAttrList,
194 									com::sun::star::uno::Any& rAny,
195                                     const rtl::OUString& rItemName,
196 									XMLConfigBaseContext* pBaseContext);
197 	virtual ~XMLConfigItemContext();
198 
199 	virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix,
200 													const rtl::OUString& rLocalName,
201 													const ::com::sun::star::uno::Reference<
202 									  	::com::sun::star::xml::sax::XAttributeList>& xAttrList );
203 	virtual void Characters( const ::rtl::OUString& rChars );
204 
205 	virtual void EndElement();
206 
207     virtual void ManipulateConfigItem();
208 };
209 
210 //=============================================================================
211 
212 class XMLConfigItemSetContext : public XMLConfigBaseContext
213 {
214 public:
215 	XMLConfigItemSetContext(SvXMLImport& rImport, sal_uInt16 nPrfx, const rtl::OUString& rLName,
216 									const ::com::sun::star::uno::Reference<
217 									::com::sun::star::xml::sax::XAttributeList>& xAttrList,
218 									com::sun::star::uno::Any& rAny,
219 									XMLConfigBaseContext* pBaseContext);
220 	virtual ~XMLConfigItemSetContext();
221 
222 	virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix,
223 													const rtl::OUString& rLocalName,
224 													const ::com::sun::star::uno::Reference<
225 									  	::com::sun::star::xml::sax::XAttributeList>& xAttrList );
226 
227 	virtual void EndElement();
228 };
229 
230 //=============================================================================
231 
232 class XMLConfigItemMapNamedContext : public XMLConfigBaseContext
233 {
234 public:
235 	XMLConfigItemMapNamedContext(SvXMLImport& rImport, sal_uInt16 nPrfx, const rtl::OUString& rLName,
236 									const ::com::sun::star::uno::Reference<
237 									::com::sun::star::xml::sax::XAttributeList>& xAttrList,
238 									com::sun::star::uno::Any& rAny,
239 									XMLConfigBaseContext* pBaseContext);
240 	virtual ~XMLConfigItemMapNamedContext();
241 
242 	virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix,
243 													const rtl::OUString& rLocalName,
244 													const ::com::sun::star::uno::Reference<
245 									  	::com::sun::star::xml::sax::XAttributeList>& xAttrList );
246 
247 	virtual void EndElement();
248 };
249 
250 //=============================================================================
251 
252 class XMLConfigItemMapIndexedContext : public XMLConfigBaseContext
253 {
254 private:
255 	rtl::OUString maConfigItemName;
256 
257 public:
258 	XMLConfigItemMapIndexedContext(SvXMLImport& rImport, sal_uInt16 nPrfx,
259 									const rtl::OUString& rLName,
260 									const ::com::sun::star::uno::Reference<
261 									::com::sun::star::xml::sax::XAttributeList>& xAttrList,
262 									com::sun::star::uno::Any& rAny,
263 									const rtl::OUString& rConfigItemName,
264 									XMLConfigBaseContext* pBaseContext);
265 	virtual ~XMLConfigItemMapIndexedContext();
266 
267 	virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix,
268 													const rtl::OUString& rLocalName,
269 													const ::com::sun::star::uno::Reference<
270 										::com::sun::star::xml::sax::XAttributeList>& xAttrList );
271 
272 	virtual void EndElement();
273 };
274 
275 //=============================================================================
276 
CreateSettingsContext(SvXMLImport & rImport,sal_uInt16 p_nPrefix,const rtl::OUString & rLocalName,const uno::Reference<xml::sax::XAttributeList> & xAttrList,beans::PropertyValue & rProp,XMLConfigBaseContext * pBaseContext)277 SvXMLImportContext *CreateSettingsContext(SvXMLImport& rImport, sal_uInt16 p_nPrefix,
278 						const rtl::OUString& rLocalName,
279 						const uno::Reference<xml::sax::XAttributeList>& xAttrList,
280 						beans::PropertyValue& rProp, XMLConfigBaseContext* pBaseContext)
281 {
282 	SvXMLImportContext *pContext = 0;
283 
284 	rProp.Name = rtl::OUString();
285 	sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
286 	for( sal_Int16 i=0; i < nAttrCount; i++ )
287 	{
288 		rtl::OUString sAttrName = xAttrList->getNameByIndex( i );
289 		rtl::OUString aLocalName;
290 		sal_uInt16 nPrefix = rImport.GetNamespaceMap().GetKeyByAttrName(
291 											sAttrName, &aLocalName );
292 		rtl::OUString sValue = xAttrList->getValueByIndex( i );
293 
294 		if (nPrefix == XML_NAMESPACE_CONFIG)
295 		{
296 			if (IsXMLToken(aLocalName, XML_NAME))
297 				rProp.Name = sValue;
298 		}
299 	}
300 
301 	if (p_nPrefix == XML_NAMESPACE_CONFIG)
302 	{
303 		if (IsXMLToken(rLocalName, XML_CONFIG_ITEM))
304 			pContext = new XMLConfigItemContext(rImport, p_nPrefix, rLocalName, xAttrList, rProp.Value, rProp.Name, pBaseContext);
305 		else if((IsXMLToken(rLocalName, XML_CONFIG_ITEM_SET)) ||
306 				(IsXMLToken(rLocalName, XML_CONFIG_ITEM_MAP_ENTRY)) )
307 			pContext = new XMLConfigItemSetContext(rImport, p_nPrefix, rLocalName, xAttrList, rProp.Value, pBaseContext);
308 		else if(IsXMLToken(rLocalName, XML_CONFIG_ITEM_MAP_NAMED))
309 			pContext = new XMLConfigItemMapNamedContext(rImport, p_nPrefix, rLocalName, xAttrList, rProp.Value, pBaseContext);
310 		else if(IsXMLToken(rLocalName, XML_CONFIG_ITEM_MAP_INDEXED))
311 			pContext = new XMLConfigItemMapIndexedContext(rImport, p_nPrefix, rLocalName, xAttrList, rProp.Value, rProp.Name, pBaseContext);
312 	}
313 
314 	if( !pContext )
315 		pContext = new SvXMLImportContext( rImport, p_nPrefix, rLocalName );
316 
317 	return pContext;
318 }
319 
320 //=============================================================================
321 namespace
322 {
323     struct SettingsGroup
324     {
325         ::rtl::OUString sGroupName;
326         uno::Any        aSettings;
327 
SettingsGroup__anona11987400111::SettingsGroup328         SettingsGroup()
329             :sGroupName()
330             ,aSettings()
331         {
332         }
333 
SettingsGroup__anona11987400111::SettingsGroup334         SettingsGroup( const ::rtl::OUString& _rGroupName, const uno::Any& _rSettings )
335             :sGroupName( _rGroupName )
336             ,aSettings( _rSettings )
337         {
338         }
339     };
340 }
341 
342 struct XMLDocumentSettingsContext_Data
343 {
344 	com::sun::star::uno::Any	    aViewProps;
345 	com::sun::star::uno::Any	    aConfigProps;
346     ::std::list< SettingsGroup >    aDocSpecificSettings;
347 };
348 
349 //=============================================================================
350 
XMLDocumentSettingsContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const rtl::OUString & rLName,const uno::Reference<xml::sax::XAttributeList> &)351 XMLDocumentSettingsContext::XMLDocumentSettingsContext(SvXMLImport& rImport, sal_uInt16 nPrfx, const rtl::OUString& rLName,
352 					const uno::Reference<xml::sax::XAttributeList>& )
353 	: SvXMLImportContext( rImport, nPrfx, rLName )
354     , m_pData( new XMLDocumentSettingsContext_Data )
355 {
356 	// here are no attributes
357 }
358 
~XMLDocumentSettingsContext()359 XMLDocumentSettingsContext::~XMLDocumentSettingsContext()
360 {
361 }
362 
CreateChildContext(sal_uInt16 p_nPrefix,const rtl::OUString & rLocalName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> & xAttrList)363 SvXMLImportContext *XMLDocumentSettingsContext::CreateChildContext( sal_uInt16 p_nPrefix,
364 									 const rtl::OUString& rLocalName,
365 									 const ::com::sun::star::uno::Reference<
366 									  	::com::sun::star::xml::sax::XAttributeList>& xAttrList )
367 {
368 	SvXMLImportContext *pContext = 0;
369 	rtl::OUString sName;
370 
371 	sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
372 	for( sal_Int16 i=0; i < nAttrCount; i++ )
373 	{
374 		rtl::OUString sAttrName = xAttrList->getNameByIndex( i );
375 		rtl::OUString aLocalName;
376 		sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName(
377 											sAttrName, &aLocalName );
378 		rtl::OUString sValue = xAttrList->getValueByIndex( i );
379 
380 		if (nPrefix == XML_NAMESPACE_CONFIG)
381 		{
382 			if (IsXMLToken(aLocalName, XML_NAME))
383 				sName = sValue;
384 		}
385 	}
386 
387 	if (p_nPrefix == XML_NAMESPACE_CONFIG)
388 	{
389 		if (IsXMLToken(rLocalName, XML_CONFIG_ITEM_SET))
390 		{
391 			::rtl::OUString aLocalConfigName;
392 			sal_uInt16 nConfigPrefix =
393 				GetImport().GetNamespaceMap().GetKeyByAttrName(
394 											sName, &aLocalConfigName );
395 
396 			if( XML_NAMESPACE_OOO == nConfigPrefix )
397 			{
398 				if (IsXMLToken(aLocalConfigName, XML_VIEW_SETTINGS))
399 					pContext = new XMLConfigItemSetContext(GetImport(),
400 										p_nPrefix, rLocalName, xAttrList,
401 										m_pData->aViewProps, NULL);
402 				else if (IsXMLToken(aLocalConfigName,
403 												XML_CONFIGURATION_SETTINGS))
404 					pContext = new XMLConfigItemSetContext(GetImport(),
405 										p_nPrefix, rLocalName, xAttrList,
406 										m_pData->aConfigProps, NULL);
407                 else
408                 {
409                     m_pData->aDocSpecificSettings.push_back( SettingsGroup( aLocalConfigName, uno::Any() ) );
410 
411                     ::std::list< SettingsGroup >::reverse_iterator settingsPos =
412                         m_pData->aDocSpecificSettings.rbegin();
413 
414                     pContext = new XMLConfigItemSetContext(GetImport(),
415 										p_nPrefix, rLocalName, xAttrList,
416 										settingsPos->aSettings, NULL);
417                 }
418 			}
419 		}
420 	}
421 
422 	if( !pContext )
423 		pContext = new SvXMLImportContext( GetImport(), p_nPrefix, rLocalName );
424 
425 	return pContext;
426 }
427 
EndElement()428 void XMLDocumentSettingsContext::EndElement()
429 {
430 	uno::Sequence<beans::PropertyValue> aSeqViewProps;
431 	if (m_pData->aViewProps >>= aSeqViewProps)
432 	{
433 		GetImport().SetViewSettings(aSeqViewProps);
434 		sal_Int32 i(aSeqViewProps.getLength() - 1);
435 		sal_Bool bFound(sal_False);
436 		while((i >= 0) && !bFound)
437 		{
438 			if (aSeqViewProps[i].Name.compareToAscii("Views") == 0)
439 			{
440 				bFound = sal_True;
441 				uno::Reference<container::XIndexAccess> xIndexAccess;
442 				if (aSeqViewProps[i].Value >>= xIndexAccess)
443 				{
444 					uno::Reference<document::XViewDataSupplier> xViewDataSupplier(GetImport().GetModel(), uno::UNO_QUERY);
445 					if (xViewDataSupplier.is())
446 						xViewDataSupplier->setViewData(xIndexAccess);
447 				}
448 			}
449 			else
450 				i--;
451 		}
452 	}
453 
454     sal_Bool bLoadDocPrinter( sal_True );
455     ::comphelper::ConfigurationHelper::readDirectKey(
456         ::comphelper::getProcessServiceFactory(),
457         C2U("org.openoffice.Office.Common/"), C2U("Save/Document"), C2U("LoadPrinter"),
458         ::comphelper::ConfigurationHelper::E_READONLY ) >>= bLoadDocPrinter;
459     uno::Sequence<beans::PropertyValue> aSeqConfigProps;
460     if ( m_pData->aConfigProps >>= aSeqConfigProps )
461     {
462         if ( !bLoadDocPrinter )
463         {
464             sal_Int32 i = aSeqConfigProps.getLength() - 1;
465             int nFound = 0;
466 
467             while ( ( i >= 0 ) && nFound < 2 )
468             {
469                 rtl::OUString sProp( aSeqConfigProps[i].Name );
470 
471                 if ( sProp.compareToAscii("PrinterName") == 0 )
472                 {
473                     rtl::OUString sEmpty;
474                     aSeqConfigProps[i].Value = uno::makeAny( sEmpty );
475                     nFound++;
476                 }
477                 else if ( sProp.compareToAscii("PrinterSetup") == 0 )
478                 {
479                     uno::Sequence< sal_Int8 > aEmpty;
480                     aSeqConfigProps[i].Value = uno::makeAny( aEmpty );
481                     nFound++;
482                 }
483 
484                 i--;
485             }
486         }
487 
488         GetImport().SetConfigurationSettings( aSeqConfigProps );
489     }
490 
491     for (   ::std::list< SettingsGroup >::const_iterator settings = m_pData->aDocSpecificSettings.begin();
492             settings != m_pData->aDocSpecificSettings.end();
493             ++settings
494         )
495     {
496         uno::Sequence< beans::PropertyValue > aDocSettings;
497         OSL_VERIFY( settings->aSettings >>= aDocSettings );
498         GetImport().SetDocumentSpecificSettings( settings->sGroupName, aDocSettings );
499     }
500 }
501 
502 //=============================================================================
503 
XMLConfigBaseContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const rtl::OUString & rLName,com::sun::star::uno::Any & rTempAny,XMLConfigBaseContext * pTempBaseContext)504 XMLConfigBaseContext::XMLConfigBaseContext(SvXMLImport& rImport, sal_uInt16 nPrfx,
505 		const rtl::OUString& rLName, com::sun::star::uno::Any& rTempAny,
506 		XMLConfigBaseContext* pTempBaseContext)
507 	: SvXMLImportContext( rImport, nPrfx, rLName ),
508 	// #110680#
509 	maProps(rImport.getServiceFactory()),
510 	maProp(),
511 	mrAny(rTempAny),
512 	mpBaseContext(pTempBaseContext)
513 {
514 }
515 
~XMLConfigBaseContext()516 XMLConfigBaseContext::~XMLConfigBaseContext()
517 {
518 }
519 
520 //=============================================================================
521 
XMLConfigItemSetContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const rtl::OUString & rLName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> &,com::sun::star::uno::Any & rAny,XMLConfigBaseContext * pBaseContext)522 XMLConfigItemSetContext::XMLConfigItemSetContext(SvXMLImport& rImport, sal_uInt16 nPrfx,
523 									const rtl::OUString& rLName,
524 									const ::com::sun::star::uno::Reference<
525 									::com::sun::star::xml::sax::XAttributeList>&,
526 									com::sun::star::uno::Any& rAny,
527 									XMLConfigBaseContext* pBaseContext)
528 	: XMLConfigBaseContext( rImport, nPrfx, rLName, rAny, pBaseContext )
529 {
530 	// here are no attributes
531 }
532 
~XMLConfigItemSetContext()533 XMLConfigItemSetContext::~XMLConfigItemSetContext()
534 {
535 }
536 
CreateChildContext(sal_uInt16 nPrefix,const rtl::OUString & rLocalName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> & xAttrList)537 SvXMLImportContext *XMLConfigItemSetContext::CreateChildContext( sal_uInt16 nPrefix,
538 									 const rtl::OUString& rLocalName,
539 									 const ::com::sun::star::uno::Reference<
540 									  	::com::sun::star::xml::sax::XAttributeList>& xAttrList )
541 {
542 	return CreateSettingsContext(GetImport(), nPrefix, rLocalName, xAttrList, maProp, this);
543 }
544 
EndElement()545 void XMLConfigItemSetContext::EndElement()
546 {
547 	mrAny <<= maProps.GetSequence();
548 	if (mpBaseContext)
549 		mpBaseContext->AddPropertyValue();
550 }
551 
552 //=============================================================================
553 
XMLConfigItemContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const rtl::OUString & rLName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> & xAttrList,com::sun::star::uno::Any & rTempAny,const rtl::OUString & rTempItemName,XMLConfigBaseContext * pTempBaseContext)554 XMLConfigItemContext::XMLConfigItemContext(SvXMLImport& rImport, sal_uInt16 nPrfx, const rtl::OUString& rLName,
555 									const ::com::sun::star::uno::Reference<
556 									::com::sun::star::xml::sax::XAttributeList>& xAttrList,
557 									com::sun::star::uno::Any& rTempAny,
558                                     const rtl::OUString& rTempItemName,
559 									XMLConfigBaseContext* pTempBaseContext)
560 	: SvXMLImportContext(rImport, nPrfx, rLName),
561 	mrAny(rTempAny),
562     mrItemName(rTempItemName),
563 	mpBaseContext(pTempBaseContext)
564 {
565 	sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
566 	for( sal_Int16 i=0; i < nAttrCount; i++ )
567 	{
568 		rtl::OUString sAttrName = xAttrList->getNameByIndex( i );
569 		rtl::OUString aLocalName;
570 		sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName(
571 											sAttrName, &aLocalName );
572 		rtl::OUString sValue = xAttrList->getValueByIndex( i );
573 
574 		if (nPrefix == XML_NAMESPACE_CONFIG)
575 		{
576 			if (IsXMLToken(aLocalName, XML_TYPE))
577 				msType = sValue;
578 		}
579 	}
580 }
581 
~XMLConfigItemContext()582 XMLConfigItemContext::~XMLConfigItemContext()
583 {
584 }
585 
CreateChildContext(sal_uInt16 nPrefix,const rtl::OUString & rLocalName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> &)586 SvXMLImportContext *XMLConfigItemContext::CreateChildContext( sal_uInt16 nPrefix,
587 													const rtl::OUString& rLocalName,
588 													const ::com::sun::star::uno::Reference<
589 									  	::com::sun::star::xml::sax::XAttributeList>& )
590 {
591 	SvXMLImportContext* pContext = new SvXMLImportContext(GetImport(), nPrefix, rLocalName);
592 	return pContext;
593 }
594 
Characters(const::rtl::OUString & rChars)595 void XMLConfigItemContext::Characters( const ::rtl::OUString& rChars )
596 {
597 	if (IsXMLToken(msType, XML_BASE64BINARY))
598 	{
599 		rtl::OUString sTrimmedChars( rChars.trim() );
600 		if( sTrimmedChars.getLength() )
601 		{
602 			rtl::OUString sChars;
603 			if( !msValue.isEmpty() )
604 			{
605 				sChars = msValue;
606 				sChars += sTrimmedChars;
607 				msValue = rtl::OUString();
608 			}
609 			else
610 			{
611 				sChars = sTrimmedChars;
612 			}
613 			uno::Sequence<sal_Int8> aBuffer((sChars.getLength() / 4) * 3 );
614 			sal_Int32 nCharsDecoded =
615 				GetImport().GetMM100UnitConverter().
616 					decodeBase64SomeChars( aBuffer, sChars );
617 			sal_uInt32 nStartPos(maDecoded.getLength());
618 			sal_uInt32 nCount(aBuffer.getLength());
619 			maDecoded.realloc(nStartPos + nCount);
620 			sal_Int8* pDecoded = maDecoded.getArray();
621 			sal_Int8* pBuffer = aBuffer.getArray();
622 			for (sal_uInt32 i = 0; i < nCount; i++, pBuffer++)
623 				pDecoded[nStartPos + i] = *pBuffer;
624 			if( nCharsDecoded != sChars.getLength() )
625 				msValue = sChars.copy( nCharsDecoded );
626 		}
627 	}
628 	else
629 		msValue += rChars;
630 }
631 
632 
EndElement()633 void XMLConfigItemContext::EndElement()
634 {
635 	if (mpBaseContext)
636 	{
637 		if (IsXMLToken(msType, XML_BOOLEAN))
638 		{
639 			sal_Bool bValue(sal_False);
640 			if (IsXMLToken(msValue, XML_TRUE))
641 				bValue = sal_True;
642 			mrAny <<= bValue;
643 		}
644 		else if (IsXMLToken(msType, XML_BYTE))
645 		{
646 			sal_Int32 nValue(0);
647 			SvXMLUnitConverter::convertNumber(nValue, msValue);
648 			mrAny <<= static_cast<sal_Int8>(nValue);
649 		}
650 		else if (IsXMLToken(msType, XML_SHORT))
651 		{
652 			sal_Int32 nValue(0);
653 			SvXMLUnitConverter::convertNumber(nValue, msValue);
654 			mrAny <<= static_cast<sal_Int16>(nValue);
655 		}
656 		else if (IsXMLToken(msType, XML_INT))
657 		{
658 			sal_Int32 nValue(0);
659 			SvXMLUnitConverter::convertNumber(nValue, msValue);
660 			mrAny <<= nValue;
661 		}
662 		else if (IsXMLToken(msType, XML_LONG))
663 		{
664 			sal_Int64 nValue(msValue.toInt64());
665 			mrAny <<= nValue;
666 		}
667 		else if (IsXMLToken(msType, XML_DOUBLE))
668 		{
669 			double fValue(0.0);
670 			SvXMLUnitConverter::convertDouble(fValue, msValue);
671 			mrAny <<= fValue;
672 		}
673 		else if (IsXMLToken(msType, XML_STRING))
674 		{
675 			mrAny <<= msValue;
676 		}
677 		else if (IsXMLToken(msType, XML_DATETIME))
678 		{
679 			util::DateTime aDateTime;
680 			SvXMLUnitConverter::convertDateTime(aDateTime, msValue);
681 			mrAny <<= aDateTime;
682 		}
683 		else if (IsXMLToken(msType, XML_BASE64BINARY))
684 		{
685 			mrAny <<= maDecoded;
686 		}
687 		else {
688 			DBG_ERROR("wrong type");
689         }
690 
691         ManipulateConfigItem();
692 
693 		mpBaseContext->AddPropertyValue();
694 	}
695 	else {
696 		DBG_ERROR("no BaseContext");
697     }
698 }
699 
700 /** There are some instances where there is a mismatch between API and
701  * XML mapping of a setting. In this case, this method allows us to
702  * manipulate the values accordingly. */
ManipulateConfigItem()703 void XMLConfigItemContext::ManipulateConfigItem()
704 {
705     if( mrItemName.equalsAsciiL(
706             RTL_CONSTASCII_STRINGPARAM( "PrinterIndependentLayout" ) ) )
707     {
708         rtl::OUString sValue;
709         mrAny >>= sValue;
710 
711         sal_Int16 nTmp = document::PrinterIndependentLayout::HIGH_RESOLUTION;
712 
713         if( sValue.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("enabled")) ||
714             sValue.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("low-resolution")) )
715         {
716             nTmp = document::PrinterIndependentLayout::LOW_RESOLUTION;
717         }
718         else if( sValue.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("disabled")) )
719         {
720             nTmp = document::PrinterIndependentLayout::DISABLED;
721         }
722         // else: default to high_resolution
723 
724         mrAny <<= nTmp;
725     }
726 	else if( (mrItemName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "ColorTableURL" ) ) ) ||
727 			 (mrItemName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "LineEndTableURL" ) ) ) ||
728 			 (mrItemName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "HatchTableURL" ) ) ) ||
729 			 (mrItemName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "DashTableURL" ) ) ) ||
730 			 (mrItemName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "GradientTableURL") ) ) ||
731 			 (mrItemName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "BitmapTableURL" ) ) ) )
732 	{
733 		if( GetImport().getServiceFactory().is() ) try
734 		{
735 			uno::Reference< util::XStringSubstitution > xStringSubsitution(
736 				GetImport().getServiceFactory()->
737 					createInstance(::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.PathSubstitution" ) ) ), uno::UNO_QUERY );
738 
739 			if( xStringSubsitution.is() )
740 			{
741 				rtl::OUString aURL;
742 				mrAny >>= aURL;
743 				aURL = xStringSubsitution->substituteVariables( aURL, sal_False );
744 				mrAny <<= aURL;
745 			}
746 		}
747 		catch( uno::Exception& )
748 		{
749 		}
750 	}
751 }
752 
753 
754 //=============================================================================
755 
XMLConfigItemMapNamedContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const rtl::OUString & rLName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> &,com::sun::star::uno::Any & rAny,XMLConfigBaseContext * pBaseContext)756 XMLConfigItemMapNamedContext::XMLConfigItemMapNamedContext(SvXMLImport& rImport, sal_uInt16 nPrfx, const rtl::OUString& rLName,
757 									const ::com::sun::star::uno::Reference<
758 									::com::sun::star::xml::sax::XAttributeList>&,
759 									com::sun::star::uno::Any& rAny,
760 									XMLConfigBaseContext* pBaseContext)
761 	: XMLConfigBaseContext(rImport, nPrfx, rLName, rAny, pBaseContext)
762 {
763 }
764 
~XMLConfigItemMapNamedContext()765 XMLConfigItemMapNamedContext::~XMLConfigItemMapNamedContext()
766 {
767 }
768 
CreateChildContext(sal_uInt16 nPrefix,const rtl::OUString & rLocalName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> & xAttrList)769 SvXMLImportContext *XMLConfigItemMapNamedContext::CreateChildContext( sal_uInt16 nPrefix,
770 													const rtl::OUString& rLocalName,
771 													const ::com::sun::star::uno::Reference<
772 									  	::com::sun::star::xml::sax::XAttributeList>& xAttrList )
773 {
774 	return CreateSettingsContext(GetImport(), nPrefix, rLocalName, xAttrList, maProp, this);
775 }
776 
EndElement()777 void XMLConfigItemMapNamedContext::EndElement()
778 {
779 	if (mpBaseContext)
780 	{
781 		mrAny <<= maProps.GetNameContainer();
782 		mpBaseContext->AddPropertyValue();
783 	}
784 	else {
785 		DBG_ERROR("no BaseContext");
786     }
787 }
788 
789 //=============================================================================
790 
XMLConfigItemMapIndexedContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const rtl::OUString & rLName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> &,com::sun::star::uno::Any & rAny,const::rtl::OUString & rConfigItemName,XMLConfigBaseContext * pBaseContext)791 XMLConfigItemMapIndexedContext::XMLConfigItemMapIndexedContext(SvXMLImport& rImport, sal_uInt16 nPrfx,
792 									const rtl::OUString& rLName,
793 									const ::com::sun::star::uno::Reference<
794 									::com::sun::star::xml::sax::XAttributeList>&,
795 									com::sun::star::uno::Any& rAny,
796 									const ::rtl::OUString& rConfigItemName,
797 									XMLConfigBaseContext* pBaseContext)
798 	: XMLConfigBaseContext(rImport, nPrfx, rLName, rAny, pBaseContext),
799 	  maConfigItemName( rConfigItemName )
800 {
801 }
802 
~XMLConfigItemMapIndexedContext()803 XMLConfigItemMapIndexedContext::~XMLConfigItemMapIndexedContext()
804 {
805 }
806 
CreateChildContext(sal_uInt16 nPrefix,const rtl::OUString & rLocalName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> & xAttrList)807 SvXMLImportContext *XMLConfigItemMapIndexedContext::CreateChildContext( sal_uInt16 nPrefix,
808 													const rtl::OUString& rLocalName,
809 													const ::com::sun::star::uno::Reference<
810 										::com::sun::star::xml::sax::XAttributeList>& xAttrList )
811 {
812 	return CreateSettingsContext(GetImport(), nPrefix, rLocalName, xAttrList, maProp, this);
813 }
814 
EndElement()815 void XMLConfigItemMapIndexedContext::EndElement()
816 {
817 	if (mpBaseContext)
818 	{
819 		if( maConfigItemName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "ForbiddenCharacters" ) ) )
820 		{
821 			uno::Reference< i18n::XForbiddenCharacters > xForbChars;
822 
823 			// get the forbidden characters from the document
824 			uno::Reference< lang::XMultiServiceFactory > xFac( GetImport().GetModel(), uno::UNO_QUERY );
825 			if( xFac.is() )
826 			{
827 				uno::Reference< beans::XPropertySet > xProps( xFac->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.Settings" ) ) ), uno::UNO_QUERY );
828 				if( xProps.is() && xProps->getPropertySetInfo()->hasPropertyByName( maConfigItemName ) )
829 				{
830 					xProps->getPropertyValue( maConfigItemName ) >>= xForbChars;
831 				}
832 			}
833 
834 			if( xForbChars.is() )
835 			{
836 
837 				uno::Reference< container::XIndexAccess > xIndex( maProps.GetIndexContainer(), uno::UNO_QUERY );
838 
839 				const sal_Int32 nCount = xIndex->getCount();
840 				uno::Sequence < beans::PropertyValue > aProps;
841 				for (sal_Int32 i = 0; i < nCount; i++)
842 				{
843 					if ((xIndex->getByIndex( i ) >>= aProps) && (aProps.getLength() == XML_FORBIDDEN_CHARACTER_MAX ) )
844 					{
845 						beans::PropertyValue *pForChar = aProps.getArray();
846 						i18n::ForbiddenCharacters aForbid;
847 						lang::Locale aLocale;
848 						const rtl::OUString sLanguage  ( RTL_CONSTASCII_USTRINGPARAM ( "Language" ) );
849 						const rtl::OUString sCountry   ( RTL_CONSTASCII_USTRINGPARAM ( "Country" ) );
850 						const rtl::OUString sVariant   ( RTL_CONSTASCII_USTRINGPARAM ( "Variant" ) );
851 						const rtl::OUString sBeginLine ( RTL_CONSTASCII_USTRINGPARAM ( "BeginLine" ) );
852 						const rtl::OUString sEndLine   ( RTL_CONSTASCII_USTRINGPARAM ( "EndLine" ) );
853 						sal_Bool bHaveLanguage = sal_False, bHaveCountry = sal_False, bHaveVariant = sal_False,
854 								 bHaveBegin = sal_False, bHaveEnd = sal_False;
855 
856 						for ( sal_Int32 j = 0 ; j < XML_FORBIDDEN_CHARACTER_MAX ; j++ )
857 						{
858 							if (pForChar->Name.equals (sLanguage ) )
859 							{
860 								pForChar->Value >>= aLocale.Language;
861 								bHaveLanguage = sal_True;
862 							}
863 							else if (pForChar->Name.equals (sCountry ) )
864 							{
865 								pForChar->Value >>= aLocale.Country;
866 								bHaveCountry = sal_True;
867 							}
868 							else if (pForChar->Name.equals (sVariant ) )
869 							{
870 								pForChar->Value >>= aLocale.Variant;
871 								bHaveVariant = sal_True;
872 							}
873 							else if (pForChar->Name.equals (sBeginLine ) )
874 							{
875 								pForChar->Value >>= aForbid.beginLine;
876 								bHaveBegin = sal_True;
877 							}
878 							else if (pForChar->Name.equals (sEndLine ) )
879 							{
880 								pForChar->Value >>= aForbid.endLine;
881 								bHaveEnd = sal_True;
882 							}
883 							pForChar++;
884 						}
885 
886 						if ( bHaveLanguage && bHaveCountry && bHaveVariant && bHaveBegin && bHaveEnd )
887 						{
888 							try
889 							{
890 								xForbChars->setForbiddenCharacters( aLocale, aForbid );
891 							}
892 							catch( uno::Exception& )
893 							{
894 								DBG_ERROR( "Exception while importing forbidden characters" );
895 							}
896 						}
897 					}
898 				}
899 			}
900 			else
901 			{
902 				DBG_ERROR( "could not get the XForbiddenCharacters from document!" );
903 				mrAny <<= maProps.GetIndexContainer();
904 			}
905 		}
906 		else if( maConfigItemName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Symbols" ) ) )
907 		{
908 			uno::Reference< container::XIndexAccess > xIndex( maProps.GetIndexContainer(), uno::UNO_QUERY );
909 
910 			const sal_Int32 nCount = xIndex->getCount();
911 			uno::Sequence < beans::PropertyValue > aProps;
912 			uno::Sequence < formula::SymbolDescriptor > aSymbolList ( nCount );
913 
914 			formula::SymbolDescriptor *pDescriptor = aSymbolList.getArray();
915 
916 			const rtl::OUString sName     ( RTL_CONSTASCII_USTRINGPARAM ( "Name" ) );
917 			const rtl::OUString sExportName ( RTL_CONSTASCII_USTRINGPARAM ( "ExportName" ) );
918 			const rtl::OUString sFontName ( RTL_CONSTASCII_USTRINGPARAM ( "FontName" ) );
919 			const rtl::OUString sSymbolSet ( RTL_CONSTASCII_USTRINGPARAM ( "SymbolSet" ) );
920 			const rtl::OUString sCharacter ( RTL_CONSTASCII_USTRINGPARAM ( "Character" ) );
921 			const rtl::OUString sCharSet  ( RTL_CONSTASCII_USTRINGPARAM ( "CharSet" ) );
922 			const rtl::OUString sFamily   ( RTL_CONSTASCII_USTRINGPARAM ( "Family" ) );
923 			const rtl::OUString sPitch    ( RTL_CONSTASCII_USTRINGPARAM ( "Pitch" ) );
924 			const rtl::OUString sWeight   ( RTL_CONSTASCII_USTRINGPARAM ( "Weight" ) );
925 			const rtl::OUString sItalic   ( RTL_CONSTASCII_USTRINGPARAM ( "Italic" ) );
926 			sal_Int16 nNumFullEntries = 0;
927 
928 			for ( sal_Int32 i = 0; i < nCount; i++ )
929 			{
930 				if ((xIndex->getByIndex( i ) >>= aProps) && (aProps.getLength() == XML_SYMBOL_DESCRIPTOR_MAX ) )
931 				{
932 					sal_Bool bHaveName = sal_False, bHaveExportName = sal_False, bHaveCharSet = sal_False,
933 					 		 bHaveFontName = sal_False, bHaveFamily = sal_False, bHavePitch = sal_False,
934 					 		 bHaveWeight = sal_False, bHaveItalic = sal_False, bHaveSymbolSet = sal_False,
935 							 bHaveCharacter = sal_False;
936 					beans::PropertyValue *pSymbol = aProps.getArray();
937 
938 					for ( sal_Int32 j = 0 ; j < XML_SYMBOL_DESCRIPTOR_MAX ; j++ )
939 					{
940 						if (pSymbol->Name.equals ( sName ) )
941 						{
942 							pSymbol->Value >>= pDescriptor[nNumFullEntries].sName;
943 							bHaveName = sal_True;
944 						}
945 						else if (pSymbol->Name.equals (sExportName ) )
946 						{
947 							pSymbol->Value >>= pDescriptor[nNumFullEntries].sExportName;
948 							bHaveExportName = sal_True;
949 						}
950 						else if (pSymbol->Name.equals (sFontName ) )
951 						{
952 							pSymbol->Value >>= pDescriptor[nNumFullEntries].sFontName;
953 							bHaveFontName = sal_True;
954 						}
955 						else if (pSymbol->Name.equals (sCharSet ) )
956 						{
957 							pSymbol->Value >>= pDescriptor[nNumFullEntries].nCharSet;
958 							bHaveCharSet = sal_True;
959 						}
960 						else if (pSymbol->Name.equals (sFamily ) )
961 						{
962 							pSymbol->Value >>= pDescriptor[nNumFullEntries].nFamily;
963 							bHaveFamily = sal_True;
964 						}
965 						else if (pSymbol->Name.equals (sPitch ) )
966 						{
967 							pSymbol->Value >>= pDescriptor[nNumFullEntries].nPitch;
968 							bHavePitch = sal_True;
969 						}
970 						else if (pSymbol->Name.equals (sWeight ) )
971 						{
972 							pSymbol->Value >>= pDescriptor[nNumFullEntries].nWeight;
973 							bHaveWeight = sal_True;
974 						}
975 						else if (pSymbol->Name.equals (sItalic ) )
976 						{
977 							pSymbol->Value >>= pDescriptor[nNumFullEntries].nItalic;
978 							bHaveItalic = sal_True;
979 						}
980 						else if (pSymbol->Name.equals (sSymbolSet ) )
981 						{
982 							pSymbol->Value >>= pDescriptor[nNumFullEntries].sSymbolSet;
983 							bHaveSymbolSet = sal_True;
984 						}
985 						else if (pSymbol->Name.equals (sCharacter ) )
986 						{
987 							pSymbol->Value >>= pDescriptor[nNumFullEntries].nCharacter;
988 							bHaveCharacter = sal_True;
989 						}
990 						pSymbol++;
991 					}
992 					if ( bHaveName && bHaveExportName && bHaveCharSet && bHaveFontName && bHaveCharacter
993 						 && bHaveFamily && bHavePitch && bHaveWeight && bHaveItalic && bHaveSymbolSet)
994 						nNumFullEntries++;
995 				}
996 			}
997 			aSymbolList.realloc (nNumFullEntries);
998 			mrAny <<= aSymbolList;
999 		}
1000 		else
1001 		{
1002 			mrAny <<= maProps.GetIndexContainer();
1003 		}
1004 		mpBaseContext->AddPropertyValue();
1005 	}
1006 	else {
1007 		DBG_ERROR("no BaseContext");
1008     }
1009 }
1010 
1011