Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : : *
5 : : * The contents of this file are subject to the Mozilla Public License Version
6 : : * 1.1 (the "License"); you may not use this file except in compliance with
7 : : * the License or as specified alternatively below. You may obtain a copy of
8 : : * the License at http://www.mozilla.org/MPL/
9 : : *
10 : : * Software distributed under the License is distributed on an "AS IS" basis,
11 : : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : : * for the specific language governing rights and limitations under the
13 : : * License.
14 : : *
15 : : * The Initial Developer of the Original Code is
16 : : * Peter Jentsch <pjotr@guineapics.de>
17 : : *
18 : : * Portions created by the Initial Developer are Copyright (C) 2011 the
19 : : * Initial Developer. All Rights Reserved.
20 : : *
21 : : * For minor contributions see the git repository.
22 : : *
23 : : * Alternatively, the contents of this file may be used under the terms of
24 : : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
25 : : * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
26 : : * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
27 : : * instead of those above.
28 : : */
29 : :
30 : :
31 : : #include <cppuhelper/factory.hxx>
32 : : #include <cppuhelper/servicefactory.hxx>
33 : : #include <cppuhelper/implbase3.hxx>
34 : : #include <cppuhelper/implbase.hxx>
35 : :
36 : : #include <sax/tools/documenthandleradapter.hxx>
37 : :
38 : : #include <com/sun/star/lang/XComponent.hpp>
39 : :
40 : : #include <com/sun/star/uno/Any.hxx>
41 : : #include <com/sun/star/uno/Type.hxx>
42 : :
43 : : #include <com/sun/star/beans/PropertyValue.hpp>
44 : :
45 : : #include <com/sun/star/xml/XImportFilter.hpp>
46 : : #include <com/sun/star/xml/XExportFilter.hpp>
47 : : #include <com/sun/star/xml/sax/XParser.hpp>
48 : : #include <com/sun/star/xml/sax/InputSource.hpp>
49 : : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
50 : : #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
51 : : #include <com/sun/star/xml/sax/SAXException.hpp>
52 : :
53 : : #include <com/sun/star/io/XInputStream.hpp>
54 : : #include <com/sun/star/io/XOutputStream.hpp>
55 : : #include <com/sun/star/io/XActiveDataSource.hpp>
56 : : #include <com/sun/star/io/XSeekable.hpp>
57 : :
58 : : using namespace ::rtl;
59 : : using namespace ::cppu;
60 : : using namespace ::osl;
61 : : using namespace ::sax;
62 : : using namespace ::com::sun::star::beans;
63 : : using namespace ::com::sun::star::io;
64 : : using namespace ::com::sun::star::uno;
65 : : using namespace ::com::sun::star::lang;
66 : : using namespace ::com::sun::star::registry;
67 : : using namespace ::com::sun::star::xml;
68 : : using namespace ::com::sun::star::xml::sax;
69 : :
70 : : namespace filter {
71 : : namespace odfflatxml {
72 : : /*
73 : : * OdfFlatXml export and imports ODF flat XML documents by plugging a pass-through
74 : : * filter implementation into XmlFilterAdaptor.
75 : : */
76 : 0 : class OdfFlatXml : public WeakImplHelper3<XImportFilter,
77 : : XExportFilter, DocumentHandlerAdapter>
78 : : {
79 : : private:
80 : : Reference< XMultiServiceFactory > m_rServiceFactory;
81 : :
82 : : public:
83 : :
84 : 0 : OdfFlatXml(const Reference<XMultiServiceFactory> &r) :
85 : 0 : m_rServiceFactory(r)
86 : : {
87 : 0 : }
88 : :
89 : : // XImportFilter
90 : : virtual sal_Bool SAL_CALL
91 : : importer(const Sequence< PropertyValue >& sourceData,
92 : : const Reference< XDocumentHandler >& docHandler,
93 : : const Sequence< OUString >& userData)
94 : : throw (IllegalArgumentException, RuntimeException);
95 : :
96 : : // XExportFilter
97 : : virtual sal_Bool SAL_CALL
98 : : exporter(
99 : : const Sequence< PropertyValue >& sourceData,
100 : : const Sequence< OUString >& userData)
101 : : throw (IllegalArgumentException,
102 : : RuntimeException);
103 : :
104 : : // UNO component helper methods
105 : :
106 : : static OUString impl_getImplementationName();
107 : :
108 : : static Sequence< OUString > impl_getSupportedServiceNames();
109 : :
110 : : static Reference< XInterface > impl_createInstance(const Reference< XMultiServiceFactory >& fact);
111 : : };
112 : : }
113 : : }
114 : :
115 : : using namespace ::filter::odfflatxml;
116 : :
117 : : sal_Bool
118 : 0 : OdfFlatXml::importer(
119 : : const Sequence< PropertyValue >& sourceData,
120 : : const Reference< XDocumentHandler >& docHandler,
121 : : const Sequence< OUString >& /* userData */)
122 : : throw (IllegalArgumentException, RuntimeException)
123 : : {
124 : : // Read InputStream to read from and an URL used for the system id
125 : : // of the InputSource we create from the given sourceData sequence
126 : 0 : Reference<XInputStream> inputStream;
127 : 0 : OUString paramName;
128 : 0 : OUString url;
129 : :
130 : 0 : sal_Int32 paramCount = sourceData.getLength();
131 : 0 : for (sal_Int32 paramIdx = 0; paramIdx < paramCount; paramIdx++)
132 : : {
133 : 0 : paramName = sourceData[paramIdx].Name;
134 : 0 : if ( paramName == "InputStream" )
135 : 0 : sourceData[paramIdx].Value >>= inputStream;
136 : 0 : else if ( paramName == "URL" )
137 : 0 : sourceData[paramIdx].Value >>= url;
138 : : }
139 : :
140 : : OSL_ASSERT(inputStream.is());
141 : 0 : if (!inputStream.is())
142 : 0 : return sal_False;
143 : :
144 : : OUString SAX_PARSER_SERVICE(
145 : 0 : RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Parser"));
146 : 0 : Reference<XParser> saxParser(m_rServiceFactory->createInstance(
147 : 0 : SAX_PARSER_SERVICE), UNO_QUERY);
148 : :
149 : : OSL_ASSERT(saxParser.is());
150 : 0 : if (!saxParser.is())
151 : 0 : return sal_False;
152 : :
153 : 0 : InputSource inputSource;
154 : 0 : inputSource.sSystemId = url;
155 : 0 : inputSource.sPublicId = url;
156 : 0 : inputSource.aInputStream = inputStream;
157 : 0 : saxParser->setDocumentHandler(docHandler);
158 : : try
159 : : {
160 : 0 : saxParser->parseStream(inputSource);
161 : : }
162 : 0 : catch (const Exception &exc)
163 : : {
164 : : OString msg = OUStringToOString(exc.Message,
165 : 0 : RTL_TEXTENCODING_ASCII_US);
166 : : OSL_FAIL(msg.getStr());
167 : 0 : return sal_False;
168 : : }
169 : 0 : return sal_True;
170 : : }
171 : :
172 : : sal_Bool
173 : 0 : OdfFlatXml::exporter(const Sequence< PropertyValue >& sourceData,
174 : : const Sequence< OUString >& /*msUserData*/)
175 : : throw (IllegalArgumentException, RuntimeException)
176 : : {
177 : 0 : OUString paramName;
178 : 0 : OUString targetURL;
179 : 0 : Reference<XOutputStream> outputStream;
180 : :
181 : : // Read output stream and target URL from the parameters given in sourceData.
182 : 0 : sal_Int32 paramCount = sourceData.getLength();
183 : 0 : for (sal_Int32 paramIdx = 0; paramIdx < paramCount; paramIdx++)
184 : : {
185 : 0 : paramName = sourceData[paramIdx].Name;
186 : 0 : if ( paramName == "OutputStream" )
187 : 0 : sourceData[paramIdx].Value >>= outputStream;
188 : 0 : else if ( paramName == "URL" )
189 : 0 : sourceData[paramIdx].Value >>= targetURL;
190 : : }
191 : :
192 : 0 : if (!getDelegate().is())
193 : : {
194 : 0 : OUString SAX_WRITER_SERVICE(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Writer"));
195 : 0 : Reference< XDocumentHandler > saxWriter(m_rServiceFactory->createInstance(SAX_WRITER_SERVICE),
196 : 0 : UNO_QUERY);
197 : 0 : setDelegate(saxWriter);
198 : 0 : if (!getDelegate().is())
199 : 0 : return sal_False;
200 : : }
201 : : // get data source interface ...
202 : 0 : Reference<XActiveDataSource> dataSource(getDelegate(), UNO_QUERY);
203 : : OSL_ASSERT(dataSource.is());
204 : 0 : if (!dataSource.is())
205 : 0 : return sal_False;
206 : : OSL_ASSERT(outputStream.is());
207 : 0 : if (!outputStream.is())
208 : 0 : return sal_False;
209 : 0 : dataSource->setOutputStream(outputStream);
210 : :
211 : 0 : return sal_True;
212 : : }
213 : :
214 : :
215 : 0 : OUString OdfFlatXml::impl_getImplementationName()
216 : : {
217 : 0 : return OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.filter.OdfFlatXml"));
218 : : }
219 : :
220 : 0 : Sequence< OUString > OdfFlatXml::impl_getSupportedServiceNames()
221 : : {
222 : 0 : Sequence< OUString > lServiceNames(2);
223 : 0 : lServiceNames[0] = OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.ImportFilter" ));
224 : 0 : lServiceNames[1] = OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.ExportFilter" ));
225 : 0 : return lServiceNames;
226 : : }
227 : :
228 : 0 : Reference< XInterface > SAL_CALL OdfFlatXml::impl_createInstance(const Reference< XMultiServiceFactory >& fact)
229 : : {
230 : 0 : return Reference<XInterface> ((OWeakObject *) new OdfFlatXml(fact));
231 : :
232 : : }
233 : :
234 : : extern "C" SAL_DLLPUBLIC_EXPORT void* SAL_CALL
235 : 0 : component_getFactory( const sal_Char* pImplementationName,
236 : : void* pServiceManager,
237 : : void* /* pRegistryKey */ )
238 : : {
239 : 0 : if ((!pImplementationName) || (!pServiceManager))
240 : 0 : return NULL;
241 : :
242 : : com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory >
243 : 0 : xSMGR = reinterpret_cast< com::sun::star::lang::XMultiServiceFactory* >(pServiceManager);
244 : 0 : com::sun::star::uno::Reference< com::sun::star::lang::XSingleServiceFactory > xFactory;
245 : 0 : rtl::OUString sImplName = rtl::OUString::createFromAscii(pImplementationName);
246 : :
247 : 0 : if (OdfFlatXml::impl_getImplementationName() == sImplName)
248 : : xFactory = cppu::createOneInstanceFactory( xSMGR,
249 : : OdfFlatXml::impl_getImplementationName(),
250 : : OdfFlatXml::impl_createInstance,
251 : 0 : OdfFlatXml::impl_getSupportedServiceNames() );
252 : :
253 : 0 : if (xFactory.is())
254 : : {
255 : 0 : xFactory->acquire();
256 : 0 : return xFactory.get();
257 : : }
258 : :
259 : 0 : return NULL;
260 : : }
261 : :
262 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|