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_framework.hxx"
30 
31 #include <xml/imagesconfiguration.hxx>
32 #include <services.h>
33 
34 #include <xml/imagesdocumenthandler.hxx>
35 #include <xml/saxnamespacefilter.hxx>
36 
37 //_________________________________________________________________________________________________________________
38 //	interface includes
39 //_________________________________________________________________________________________________________________
40 #include <com/sun/star/xml/sax/XParser.hpp>
41 #include <com/sun/star/io/XActiveDataSource.hpp>
42 #include <com/sun/star/io/XInputStream.hpp>
43 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
44 
45 //_________________________________________________________________________________________________________________
46 //	other includes
47 //_________________________________________________________________________________________________________________
48 
49 #ifndef _UNOTOOLS_PROCESSFACTORY_HXX
50 #include <comphelper/processfactory.hxx>
51 #endif
52 #include <unotools/streamwrap.hxx>
53 #include <tools/debug.hxx>
54 
55 //_________________________________________________________________________________________________________________
56 //	namespace
57 //_________________________________________________________________________________________________________________
58 
59 using namespace ::com::sun::star::uno;
60 using namespace ::com::sun::star::xml::sax;
61 using namespace ::com::sun::star::lang;
62 using namespace ::com::sun::star::io;
63 
64 
65 namespace framework
66 {
67 
68 SV_IMPL_PTRARR( ImageItemListDescriptor, ImageItemDescriptorPtr );
69 SV_IMPL_PTRARR( ExternalImageItemListDescriptor, ExternalImageItemDescriptorPtr );
70 SV_IMPL_PTRARR( ImageListDescriptor, ImageListItemDescriptorPtr );
71 
72 static Reference< XParser > GetSaxParser(
73 	// #110897#
74 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory
75 	)
76 {
77 	//Reference< XMultiServiceFactory > xServiceManager = ::comphelper::getProcessServiceFactory();
78 	//return Reference< XParser >( xServiceManager->createInstance( SERVICENAME_SAXPARSER), UNO_QUERY);
79 	return Reference< XParser >( xServiceFactory->createInstance( SERVICENAME_SAXPARSER), UNO_QUERY);
80 }
81 
82 static Reference< XDocumentHandler > GetSaxWriter(
83 	// #110897#
84 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory
85 	)
86 {
87 	//Reference< XMultiServiceFactory > xServiceManager = ::comphelper::getProcessServiceFactory();
88 	//return Reference< XDocumentHandler >( xServiceManager->createInstance( SERVICENAME_SAXWRITER), UNO_QUERY) ;
89 	return Reference< XDocumentHandler >( xServiceFactory->createInstance( SERVICENAME_SAXWRITER), UNO_QUERY) ;
90 }
91 
92 // #110897#
93 sal_Bool ImagesConfiguration::LoadImages(
94 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
95 	SvStream& rInStream, ImageListsDescriptor& aItems )
96 {
97 	Reference< XParser > xParser( GetSaxParser( xServiceFactory ) );
98 	Reference< XInputStream > xInputStream(
99 								(::cppu::OWeakObject *)new utl::OInputStreamWrapper( rInStream ),
100 								UNO_QUERY );
101 
102 	// connect stream to input stream to the parser
103 	InputSource aInputSource;
104 
105 	aInputSource.aInputStream = xInputStream;
106 
107 	// create namespace filter and set document handler inside to support xml namespaces
108 	Reference< XDocumentHandler > xDocHandler( new OReadImagesDocumentHandler( aItems ));
109 	Reference< XDocumentHandler > xFilter( new SaxNamespaceFilter( xDocHandler ));
110 
111 	// connect parser and filter
112 	xParser->setDocumentHandler( xFilter );
113 
114 	try
115 	{
116 		xParser->parseStream( aInputSource );
117 		return sal_True;
118 	}
119 	catch ( RuntimeException& )
120 	{
121 		return sal_False;
122 	}
123 	catch( SAXException& )
124 	{
125 		return sal_False;
126 	}
127 	catch( ::com::sun::star::io::IOException& )
128 	{
129 		return sal_False;
130 	}
131 }
132 
133 
134 // #110897#
135 sal_Bool ImagesConfiguration::StoreImages(
136 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
137 	SvStream& rOutStream, const ImageListsDescriptor& aItems )
138 {
139 	Reference< XDocumentHandler > xWriter( GetSaxWriter( xServiceFactory ) );
140 
141 	Reference< XOutputStream > xOutputStream(
142 								(::cppu::OWeakObject *)new utl::OOutputStreamWrapper( rOutStream ),
143 								UNO_QUERY );
144 
145 	Reference< ::com::sun::star::io::XActiveDataSource> xDataSource( xWriter , UNO_QUERY );
146 	xDataSource->setOutputStream( xOutputStream );
147 
148 	try
149 	{
150 		OWriteImagesDocumentHandler aWriteImagesDocumentHandler( aItems, xWriter );
151 		aWriteImagesDocumentHandler.WriteImagesDocument();
152 		return sal_True;
153 	}
154 	catch ( RuntimeException& )
155 	{
156 		return sal_False;
157 	}
158 	catch ( SAXException& )
159 	{
160 		return sal_False;
161 	}
162 	catch ( ::com::sun::star::io::IOException& )
163 	{
164 		return sal_False;
165 	}
166 }
167 
168 sal_Bool ImagesConfiguration::LoadImages(
169 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
170     const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& rInputStream,
171     ImageListsDescriptor& rItems )
172 {
173 	Reference< XParser > xParser( GetSaxParser( xServiceFactory ) );
174 
175     // connect stream to input stream to the parser
176 	InputSource aInputSource;
177 
178 	aInputSource.aInputStream = rInputStream;
179 
180 	// create namespace filter and set document handler inside to support xml namespaces
181 	Reference< XDocumentHandler > xDocHandler( new OReadImagesDocumentHandler( rItems ));
182 	Reference< XDocumentHandler > xFilter( new SaxNamespaceFilter( xDocHandler ));
183 
184 	// connect parser and filter
185 	xParser->setDocumentHandler( xFilter );
186 
187 	try
188 	{
189 		xParser->parseStream( aInputSource );
190 		return sal_True;
191 	}
192 	catch ( RuntimeException& )
193 	{
194 		return sal_False;
195 	}
196 	catch( SAXException& )
197 	{
198 		return sal_False;
199 	}
200 	catch( ::com::sun::star::io::IOException& )
201 	{
202 		return sal_False;
203 	}
204 }
205 
206 sal_Bool ImagesConfiguration::StoreImages(
207 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
208     const ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >& rOutputStream,
209     const ImageListsDescriptor& rItems )
210 {
211 	Reference< XDocumentHandler > xWriter( GetSaxWriter( xServiceFactory ) );
212 
213 	Reference< ::com::sun::star::io::XActiveDataSource> xDataSource( xWriter , UNO_QUERY );
214 	xDataSource->setOutputStream( rOutputStream );
215 
216 	try
217 	{
218 		OWriteImagesDocumentHandler aWriteImagesDocumentHandler( rItems, xWriter );
219 		aWriteImagesDocumentHandler.WriteImagesDocument();
220 		return sal_True;
221 	}
222 	catch ( RuntimeException& )
223 	{
224 		return sal_False;
225 	}
226 	catch ( SAXException& )
227 	{
228 		return sal_False;
229 	}
230 	catch ( ::com::sun::star::io::IOException& )
231 	{
232 		return sal_False;
233 	}
234 }
235 
236 }
237 
238