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