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  // MARKER(update_precomp.py): autogen include statement, do not remove
23 //This file is about the conversion of the UOF v2.0 and ODF document format
24 #include "precompiled_filter.hxx"
25 
26 
27 #include "uof2merge.hxx"
28 
29 #include <cppuhelper/implbase1.hxx>
30 #include <rtl/ustrbuf.hxx>
31 
32 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
33 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
34 #include <com/sun/star/io/XInputStream.hpp>
35 #include <com/sun/star/io/XOutputStream.hpp>
36 #include <com/sun/star/xml/sax/XParser.hpp>
37 #include <com/sun/star/io/XActiveDataSource.hpp>
38 #include <com/sun/star/xml/sax/InputSource.hpp>
39 #include <com/sun/star/xml/sax/XAttributeList.hpp>
40 
41 #include "XMLBase64Codec.hxx"
42 
43 namespace XSLT{
44 
45 const ::rtl::OUString UOF2ROOTELEM = ::rtl::OUString::createFromAscii("uof:UOF_0000");
46 const ::rtl::OUString UOF2OBJDATAXML = ::rtl::OUString::createFromAscii("objectdata.xml");
47 const ::rtl::OUString UOF2DATADIR = ::rtl::OUString::createFromAscii("data");
48 
49 /************************************************************************/
50 /*  class UOF2AttributeList                                             */
51 /************************************************************************/
52 
53 class UOF2AttributeList : public ::cppu::WeakImplHelper1< ::com::sun::star::xml::sax::XAttributeList >
54 {
55 public:
56 	struct UOF2Attribute
57 	{
58 		::rtl::OUString m_sName;
59 		::rtl::OUString m_sValue;
60 		::rtl::OUString m_sType;
UOF2AttributeXSLT::UOF2AttributeList::UOF2Attribute61 		UOF2Attribute( const ::rtl::OUString& rName, const ::rtl::OUString& rValue, const ::rtl::OUString& rType)
62 		: m_sName(rName)
63 		, m_sValue(rValue)
64 		, m_sType(rType)
65 		{
66 		}
67 	};
68 
69 	explicit UOF2AttributeList();
70 	virtual ~UOF2AttributeList();
71 
72 	void addAttribute( const UOF2Attribute& rAttribute );
73 
74 	virtual sal_Int16 SAL_CALL getLength() throw ( ::com::sun::star::uno::RuntimeException );
75 	virtual ::rtl::OUString SAL_CALL getNameByIndex( sal_Int16 i) throw ( ::com::sun::star::uno::RuntimeException );
76 	virtual ::rtl::OUString SAL_CALL getTypeByIndex( sal_Int16 i) throw ( ::com::sun::star::uno::RuntimeException );
77 	virtual ::rtl::OUString SAL_CALL getTypeByName( const ::rtl::OUString& rName ) throw ( ::com::sun::star::uno::RuntimeException );
78 	virtual ::rtl::OUString SAL_CALL getValueByIndex( sal_Int16 i ) throw ( ::com::sun::star::uno::RuntimeException );
79 	virtual ::rtl::OUString SAL_CALL getValueByName( const ::rtl::OUString& rName ) throw ( ::com::sun::star::uno::RuntimeException );
80 private:
81 	::std::vector< UOF2Attribute > m_aAttributes;
82 };
83 
UOF2AttributeList()84 UOF2AttributeList::UOF2AttributeList()
85 {
86 }
87 
~UOF2AttributeList()88 UOF2AttributeList::~UOF2AttributeList()
89 {
90 }
91 
addAttribute(const UOF2Attribute & rAttribute)92 void UOF2AttributeList::addAttribute( const UOF2Attribute& rAttribute )
93 {
94 	if(rAttribute.m_sName.getLength() && rAttribute.m_sValue.getLength())
95 		m_aAttributes.push_back(rAttribute);
96 }
97 
getLength()98 sal_Int16 SAL_CALL UOF2AttributeList::getLength() throw ( ::com::sun::star::uno::RuntimeException )
99 {
100 	return static_cast< sal_Int16 >(m_aAttributes.size());
101 }
102 
getNameByIndex(sal_Int16 i)103 ::rtl::OUString SAL_CALL UOF2AttributeList::getNameByIndex( sal_Int16 i ) throw ( ::com::sun::star::uno::RuntimeException )
104 {
105 	return m_aAttributes[i].m_sName;
106 }
107 
getTypeByIndex(sal_Int16 i)108 ::rtl::OUString SAL_CALL UOF2AttributeList::getTypeByIndex( sal_Int16 i ) throw ( ::com::sun::star::uno::RuntimeException )
109 {
110 	return m_aAttributes[i].m_sType;
111 }
112 
getTypeByName(const::rtl::OUString & rName)113 ::rtl::OUString SAL_CALL UOF2AttributeList::getTypeByName( const ::rtl::OUString& rName ) throw ( ::com::sun::star::uno::RuntimeException )
114 {
115 	::std::vector< UOF2AttributeList::UOF2Attribute >::const_iterator aIter = m_aAttributes.begin();
116 	::std::vector< UOF2AttributeList::UOF2Attribute >::const_iterator aEnd = m_aAttributes.end();
117 	while(aIter != aEnd)
118 	{
119 		if((*aIter).m_sName.equals(rName))
120 			return (*aIter).m_sType;
121 		++aIter;
122 	}
123 
124 	return ::rtl::OUString();
125 }
126 
getValueByIndex(sal_Int16 i)127 ::rtl::OUString SAL_CALL UOF2AttributeList::getValueByIndex( sal_Int16 i ) throw ( ::com::sun::star::uno::RuntimeException )
128 {
129 	return m_aAttributes[i].m_sValue;
130 }
131 
getValueByName(const::rtl::OUString & rName)132 ::rtl::OUString SAL_CALL UOF2AttributeList::getValueByName( const ::rtl::OUString& rName ) throw ( ::com::sun::star::uno::RuntimeException )
133 {
134 	::std::vector< UOF2AttributeList::UOF2Attribute >::const_iterator aIter = m_aAttributes.begin();
135 	::std::vector< UOF2AttributeList::UOF2Attribute >::const_iterator aEnd = m_aAttributes.end();
136 	while(aIter != aEnd)
137 	{
138 		if((*aIter).m_sName.equals(rName))
139 			return (*aIter).m_sValue;
140 		++aIter;
141 	}
142 
143 	return ::rtl::OUString();
144 }
145 
146 /************************************************************************/
147 /* class UOF2FlatDocMergeHandler                                        */
148 /************************************************************************/
149 
150 class UOF2FlatDocMergeHandler : public ::cppu::WeakImplHelper1< ::com::sun::star::xml::sax::XDocumentHandler >
151 {
152 public:
153 	explicit UOF2FlatDocMergeHandler(UOF2Merge& rUOF2Merge);
154 	virtual ~UOF2FlatDocMergeHandler();
155 
156 	virtual void SAL_CALL startDocument()
157 		throw ( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException );
158 
159 	virtual void SAL_CALL endDocument()
160 		throw ( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException );
161 
162 	virtual void SAL_CALL startElement( const ::rtl::OUString& rElemName,
163 		const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& rAttribs )
164 		throw ( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException );
165 
166 	virtual void SAL_CALL endElement( const ::rtl::OUString& rElemName )
167 		throw ( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException );
168 
169 	virtual void SAL_CALL characters( const ::rtl::OUString& rElemName )
170 		throw ( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException );
171 
172 	virtual void SAL_CALL ignorableWhitespace( const ::rtl::OUString& rWhiteSpaces )
173 		throw ( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException );
174 
175 	virtual void SAL_CALL processingInstruction( const ::rtl::OUString& rTarget, const ::rtl::OUString& rData )
176 		throw ( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException );
177 
178 	virtual void SAL_CALL setDocumentLocator( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XLocator >& xLocator )
179 		throw ( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException );
180 protected:
getUOF2Merge()181 	UOF2Merge& getUOF2Merge(){ return m_rUOF2Merge; }
182 private:
183 	UOF2Merge& m_rUOF2Merge;
184 	sal_Int32 m_nLevel;
185 private:
186 	UOF2FlatDocMergeHandler(const UOF2FlatDocMergeHandler& rDocHdl);
187 	UOF2FlatDocMergeHandler& operator=(const UOF2FlatDocMergeHandler& rDocHdl);
188 };
189 
UOF2FlatDocMergeHandler(UOF2Merge & rUOF2Merge)190 UOF2FlatDocMergeHandler::UOF2FlatDocMergeHandler( UOF2Merge& rUOF2Merge )
191 : m_rUOF2Merge(rUOF2Merge)
192 , m_nLevel(0)
193 {
194 }
195 
~UOF2FlatDocMergeHandler()196 UOF2FlatDocMergeHandler::~UOF2FlatDocMergeHandler()
197 {
198 }
199 
startDocument()200 void SAL_CALL UOF2FlatDocMergeHandler::startDocument()
201 	throw ( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException )
202 {
203 }
204 
endDocument()205 void SAL_CALL UOF2FlatDocMergeHandler::endDocument()
206 	throw ( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException )
207 {
208 }
209 
startElement(const::rtl::OUString & rElemName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> & rAttribs)210 void SAL_CALL UOF2FlatDocMergeHandler::startElement( const ::rtl::OUString& rElemName,
211 								const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& rAttribs )
212 							throw ( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException )
213 {
214 	++m_nLevel;
215 	if( m_nLevel == 1)
216 	{
217 		UOF2AttributeList *pUOF2AttrList = new UOF2AttributeList;
218 		sal_Int16 nLen = rAttribs->getLength();
219 		if(nLen > 0)
220 		{
221 			for( sal_Int16 i = 0; i < nLen; ++i)
222 			{
223 				bool bIsExistNMS = false;
224 				if((rAttribs->getNameByIndex(i).indexOf( ::rtl::OUString::createFromAscii("xmlns:"))) == 0)
225 				{
226 					bIsExistNMS = m_rUOF2Merge.isInsertedNamespace(rAttribs->getNameByIndex(i));
227 					if(!bIsExistNMS)
228 						m_rUOF2Merge.addNamespace(rAttribs->getNameByIndex(i), rAttribs->getValueByIndex(i));
229 				}
230 
231 				if(!bIsExistNMS)
232 				{
233 					pUOF2AttrList->addAttribute(
234 						UOF2AttributeList::UOF2Attribute( rAttribs->getNameByIndex(i), rAttribs->getValueByIndex(i), rAttribs->getTypeByIndex(i)) );
235 				}
236 			}
237 		}
238 		::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList > xAttrList(pUOF2AttrList);
239 		m_rUOF2Merge.getSaxWriter()->startElement(rElemName, xAttrList);
240 	}
241 	else
242 		m_rUOF2Merge.getSaxWriter()->startElement(rElemName, rAttribs);
243 }
244 
endElement(const::rtl::OUString & rElemName)245 void SAL_CALL UOF2FlatDocMergeHandler::endElement( const ::rtl::OUString& rElemName )
246 	throw ( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException )
247 {
248 	--m_nLevel;
249 	m_rUOF2Merge.getSaxWriter()->endElement(rElemName);
250 }
251 
characters(const::rtl::OUString & rElemName)252 void SAL_CALL UOF2FlatDocMergeHandler::characters( const ::rtl::OUString& rElemName )
253 	throw ( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException )
254 {
255 	m_rUOF2Merge.getSaxWriter()->characters(rElemName);
256 }
257 
ignorableWhitespace(const::rtl::OUString &)258 void SAL_CALL UOF2FlatDocMergeHandler::ignorableWhitespace( const ::rtl::OUString& /*rWhiteSpaces*/ )
259 	throw ( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException )
260 {
261 }
262 
processingInstruction(const::rtl::OUString &,const::rtl::OUString &)263 void SAL_CALL UOF2FlatDocMergeHandler::processingInstruction( const ::rtl::OUString& /*rTarget*/, const ::rtl::OUString&/* rData */)
264 	throw ( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException )
265 {
266 }
267 
setDocumentLocator(const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XLocator> &)268 void SAL_CALL UOF2FlatDocMergeHandler::setDocumentLocator(
269 		const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XLocator >& /*xLocator*/ )
270 	throw ( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException )
271 {
272 }
273 
274 /************************************************************************/
275 /* class UOF2UOFXMLDocMergeHandler                                      */
276 /************************************************************************/
277 
278 class UOF2UOFXMLDocMergeHandler : public UOF2FlatDocMergeHandler
279 {
280 public:
281 	explicit UOF2UOFXMLDocMergeHandler( UOF2Merge& rUOF2Merge);
282 	virtual ~UOF2UOFXMLDocMergeHandler();
283 
284 	virtual void SAL_CALL endElement( const ::rtl::OUString& rElemName )
285 		throw ( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException );
286 };
287 
UOF2UOFXMLDocMergeHandler(UOF2Merge & rUOF2Merge)288 UOF2UOFXMLDocMergeHandler::UOF2UOFXMLDocMergeHandler( UOF2Merge& rUOF2Merge )
289 : UOF2FlatDocMergeHandler(rUOF2Merge)
290 {
291 }
292 
~UOF2UOFXMLDocMergeHandler()293 UOF2UOFXMLDocMergeHandler::~UOF2UOFXMLDocMergeHandler()
294 {
295 }
296 
endElement(const::rtl::OUString &)297 void SAL_CALL UOF2UOFXMLDocMergeHandler::endElement( const ::rtl::OUString& /*rElemName*/ )
298 	throw ( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException )
299 {
300 }
301 
302 /************************************************************************/
303 /* class UOF2ObjdataXMLDocMergeHandler                                  */
304 /************************************************************************/
305 
306 class UOF2ObjdataXMLDocMergeHandler : public UOF2FlatDocMergeHandler
307 {
308 public:
309 	UOF2ObjdataXMLDocMergeHandler( UOF2Merge& rMerge );
310 	virtual ~UOF2ObjdataXMLDocMergeHandler();
311 
312 	virtual void SAL_CALL startElement( const ::rtl::OUString& rElemName,
313 		const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& rAttribs )
314 		throw ( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException );
315 
316 	virtual void SAL_CALL endElement( const ::rtl::OUString& rElemName )
317 		throw ( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException );
318 
319 	virtual void SAL_CALL characters( const ::rtl::OUString& rChars )
320 		throw ( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException );
321 private:
322 	static const ::rtl::OUString OBJPATH;
323 	static const ::rtl::OUString OBJDATA;
324 
325 	bool m_bIsObjPathElem;
326 };
327 
328 const ::rtl::OUString UOF2ObjdataXMLDocMergeHandler::OBJPATH( ::rtl::OStringToOUString( ::rtl::OString("对象:路径_D703"),  RTL_TEXTENCODING_UTF8 ) );
329 const ::rtl::OUString UOF2ObjdataXMLDocMergeHandler::OBJDATA( ::rtl::OStringToOUString( ::rtl::OString("对象:数据_D702"), RTL_TEXTENCODING_UTF8 ) );
330 
UOF2ObjdataXMLDocMergeHandler(UOF2Merge & rMerge)331 UOF2ObjdataXMLDocMergeHandler::UOF2ObjdataXMLDocMergeHandler( UOF2Merge& rMerge )
332 : UOF2FlatDocMergeHandler(rMerge)
333 , m_bIsObjPathElem(false)
334 {
335 }
336 
~UOF2ObjdataXMLDocMergeHandler()337 UOF2ObjdataXMLDocMergeHandler::~UOF2ObjdataXMLDocMergeHandler()
338 {
339 }
340 
startElement(const::rtl::OUString & rElemName,const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XAttributeList> & rAttribs)341 void SAL_CALL UOF2ObjdataXMLDocMergeHandler::startElement( const ::rtl::OUString& rElemName,
342 						const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& rAttribs )
343 					throw ( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException )
344 {
345 	if(rElemName.equals(OBJPATH))
346 	{
347 		m_bIsObjPathElem = true;
348 		UOF2FlatDocMergeHandler::startElement(OBJDATA, rAttribs);
349 	}
350 	else
351 	{
352 		UOF2FlatDocMergeHandler::startElement(rElemName, rAttribs);
353 		m_bIsObjPathElem = false;
354 	}
355 }
356 
endElement(const::rtl::OUString & rElemName)357 void SAL_CALL UOF2ObjdataXMLDocMergeHandler::endElement( const ::rtl::OUString& rElemName )
358 	throw ( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException )
359 {
360 	if(m_bIsObjPathElem)
361 		UOF2FlatDocMergeHandler::endElement(OBJDATA);
362 	else
363 		UOF2FlatDocMergeHandler::endElement(rElemName);
364 
365 	m_bIsObjPathElem = false;
366 }
367 
characters(const::rtl::OUString & rChars)368 void SAL_CALL UOF2ObjdataXMLDocMergeHandler::characters( const ::rtl::OUString& rChars )
369 	throw ( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException )
370 {
371 	if(m_bIsObjPathElem)
372 	{
373 		::rtl::OUStringBuffer sBuffer;
374 		bool bHasBase64 = getUOF2Merge().getBase64Codec(sBuffer, rChars);
375 		if(bHasBase64)
376 			UOF2FlatDocMergeHandler::characters(sBuffer.makeStringAndClear());
377 	}
378 	else
379 		UOF2FlatDocMergeHandler::characters(rChars);
380 }
381 
382 /************************************************************************/
383 /* class UOF2Merge                                                      */
384 /************************************************************************/
385 
UOF2Merge(UOF2Storage & rStorage,const::com::sun::star::uno::Reference<::com::sun::star::lang::XMultiServiceFactory> & rxFactory)386 UOF2Merge::UOF2Merge( UOF2Storage& rStorage,
387 					 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxFactory )
388 : m_rUOF2Storage(rStorage)
389 , m_xServiceFactory(rxFactory)
390 {
391 	OSL_ENSURE(rxFactory.is(), "UOF2Merge::UOF2Merge need XMultiServiceFactory");
392 	OSL_ENSURE(rStorage.isValidUOF2Doc(), "UOF2Merge::UOF2Merge - You must import valid UOF2 document");
393 	init();
394 }
395 
~UOF2Merge()396 UOF2Merge::~UOF2Merge()
397 {
398 }
399 
init()400 void UOF2Merge::init()
401 {
402 	try
403 	{
404 		m_xPipeInStream.set(m_xServiceFactory->createInstance( ::rtl::OUString::createFromAscii("com.sun.star.io.Pipe") ),
405 			::com::sun::star::uno::UNO_QUERY);
406 		m_xPipeOutStream.set(m_xPipeInStream, ::com::sun::star::uno::UNO_QUERY);
407 
408 		m_xSaxParser.set(m_xServiceFactory->createInstance( ::rtl::OUString::createFromAscii("com.sun.star.xml.sax.Parser") ),
409 			::com::sun::star::uno::UNO_QUERY);
410 
411 		m_xExtDocHdl.set(m_xServiceFactory->createInstance( ::rtl::OUString::createFromAscii("com.sun.star.xml.sax.Writer") ),
412 			::com::sun::star::uno::UNO_QUERY);
413 
414 		::com::sun::star::uno::Reference< ::com::sun::star::io::XActiveDataSource > xmlSource(
415 			m_xExtDocHdl, ::com::sun::star::uno::UNO_QUERY);
416 		xmlSource->setOutputStream(m_xPipeOutStream);
417 	}
418 	catch( ::com::sun::star::uno::Exception& exc)
419 	{
420 		OSL_ENSURE(0, ::rtl::OUStringToOString(exc.Message, RTL_TEXTENCODING_ASCII_US).getStr());
421 	}
422 }
423 
merge()424 bool UOF2Merge::merge()
425 {
426 	bool bRet = true;
427 
428 	::std::vector< ::rtl::OUString > aElemNames;
429 	StorageRef storageRef = m_rUOF2Storage.getMainStorageRef();
430 	storageRef->getElementNames(aElemNames);
431 
432 	m_xExtDocHdl->startDocument();
433 
434 	::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > xUOFXMLInputStream =
435 		storageRef->openInputStream(UOFELEMNAME);
436 	startUOFRootXML(xUOFXMLInputStream);
437 
438 	::com::sun::star::uno::Reference<
439 		::com::sun::star::xml::sax::XDocumentHandler > xUOF2SubXMLDocHdl( new UOF2FlatDocMergeHandler(*this) );
440 	::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XDocumentHandler > xObjdataXMLDocHdl;
441 
442 	::std::vector< ::rtl::OUString >::const_iterator aIter = aElemNames.begin();
443 	::std::vector< ::rtl::OUString >::const_iterator aEndIt = aElemNames.end();
444 	while(aIter != aEndIt)
445 	{
446 		m_xSaxParser->setDocumentHandler(xUOF2SubXMLDocHdl);
447 
448 		if((*aIter) != UOFELEMNAME)
449 		{
450 			::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > xInputStream = storageRef->openInputStream(*aIter);
451 			if(xInputStream.is())
452 			{
453 				if((*aIter) == UOF2OBJDATAXML)
454 				{
455 					xObjdataXMLDocHdl.set( new UOF2ObjdataXMLDocMergeHandler(*this) );
456 					m_xSaxParser->setDocumentHandler(xObjdataXMLDocHdl);
457 				}
458 
459 				::com::sun::star::xml::sax::InputSource inputSource;
460 				inputSource.sSystemId = *aIter;
461 				inputSource.aInputStream = xInputStream;
462 				m_xSaxParser->parseStream(inputSource);
463 			}
464 			else
465 			{
466 				StorageRef subStorage =
467 					storageRef->openSubStorage(*aIter, false);
468 				if(subStorage.get())
469 				{
470 					if((*aIter) != UOF2DATADIR)
471 					{
472 						::std::vector< ::rtl::OUString > aSubElemNames;
473 						subStorage->getElementNames(aSubElemNames);
474 						if(!aSubElemNames.empty())
475 						{
476 							::std::vector< ::rtl::OUString >::const_iterator aSubIter = aSubElemNames.begin();
477 							::std::vector< ::rtl::OUString >::const_iterator aSubEnd = aSubElemNames.end();
478 							while(aSubIter != aSubEnd)
479 							{
480 								::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > xSubInputStream =
481 									subStorage->openInputStream(*aSubIter);
482 								if(xSubInputStream.is())
483 								{
484 									::com::sun::star::xml::sax::InputSource inputSource;
485 									inputSource.sSystemId = *aSubIter;
486 									inputSource.aInputStream = xSubInputStream;
487 									m_xSaxParser->parseStream(inputSource);
488 								}
489 								++aSubIter;
490 							}
491 						}
492 					}
493 				}
494 			}
495 		}
496 		++aIter;
497 	}
498 	endUOFRootXML();
499 	m_xExtDocHdl->endDocument();
500 
501 	return bRet;
502 }
503 
getMergedInStream() const504 ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > UOF2Merge::getMergedInStream() const
505 {
506 	return m_xPipeInStream;
507 }
508 
getSaxWriter()509 ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XExtendedDocumentHandler > UOF2Merge::getSaxWriter()
510 {
511 	return m_xExtDocHdl;
512 }
513 
startUOFRootXML(const::com::sun::star::uno::Reference<::com::sun::star::io::XInputStream> & xUOFXMLInStream)514 void UOF2Merge::startUOFRootXML( const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& xUOFXMLInStream )
515 {
516 	::com::sun::star::uno::Reference<
517 		::com::sun::star::xml::sax::XDocumentHandler > xUOFXMLDocHdl(new UOF2UOFXMLDocMergeHandler(*this));
518 	m_xSaxParser->setDocumentHandler(xUOFXMLDocHdl);
519 
520 	::com::sun::star::xml::sax::InputSource inputSource;
521 	inputSource.sSystemId = UOFELEMNAME;
522 	inputSource.aInputStream = xUOFXMLInStream;
523 	m_xSaxParser->parseStream(inputSource);
524 }
525 
endUOFRootXML()526 void UOF2Merge::endUOFRootXML()
527 {
528 	m_xExtDocHdl->endElement( ::rtl::OUString::createFromAscii("uof:UOF_0000") );
529 }
530 
addNamespace(const::rtl::OUString & rName,const::rtl::OUString & rURL)531 void UOF2Merge::addNamespace( const ::rtl::OUString& rName, const ::rtl::OUString& rURL )
532 {
533 	if(rName.getLength()> 0 && rURL.getLength() > 0)
534 	{
535 		m_aNamespaceMap.insert( ::std::map< ::rtl::OUString, ::rtl::OUString >::value_type( rName, rURL ));
536 	}
537 }
538 
isInsertedNamespace(const::rtl::OUString & rName) const539 bool UOF2Merge::isInsertedNamespace( const ::rtl::OUString& rName ) const
540 {
541 	bool bRet = false;
542 	typedef ::std::map< ::rtl::OUString, ::rtl::OUString >::const_iterator NMSIter;
543 	NMSIter aFoundIter = m_aNamespaceMap.find( rName );
544 	if(aFoundIter != m_aNamespaceMap.end())
545 		bRet = true;
546 
547 	return bRet;
548 }
549 
getBase64Codec(::rtl::OUStringBuffer & rBuffer,const::rtl::OUString & rObjPath)550 bool UOF2Merge::getBase64Codec( ::rtl::OUStringBuffer& rBuffer, const ::rtl::OUString& rObjPath )
551 {
552 	bool bRet = false;
553 	if(rObjPath.getLength())
554 	{
555 		::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > xInputStream =
556 			m_rUOF2Storage.getMainStorageRef()->openInputStream(rObjPath);
557 		if(xInputStream.is())
558 		{
559 			sal_Int32 nMax = 512;
560 			::com::sun::star::uno::Sequence< sal_Int8 > aOutSeq;
561 			sal_Int32 nRead = 0;
562 			while(true)
563 			{
564 				::com::sun::star::uno::Sequence< sal_Int8 > aInSeq;
565 				nRead = xInputStream->readBytes(aInSeq, nMax);
566 				if(nRead)
567 				{
568 					sal_Int32 nLen = aInSeq.getLength();
569 					if(nLen)
570 					{
571 						sal_Int32 nOrigLen = aOutSeq.getLength();
572 						aOutSeq.realloc(nOrigLen + nLen);
573 						sal_Int8 * pArray = aOutSeq.getArray() + nOrigLen;
574 						for(sal_Int32 i = 0; i < nLen; ++i)
575 						{
576 							*pArray++ = aInSeq[i];
577 						}
578 					}
579 				}
580 				else
581 					break;
582 			}
583 
584 			if(aOutSeq.getLength() > 0)
585 			{
586 				XMLBase64Codec::encodeBase64(rBuffer, aOutSeq);
587 				bRet = true;
588 			}
589 		}
590 	}
591 
592 	return bRet;
593 }
594 
595 }