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 <comphelper/processfactory.hxx>
32 : #include <cppuhelper/factory.hxx>
33 : #include <cppuhelper/servicefactory.hxx>
34 : #include <cppuhelper/implbase3.hxx>
35 : #include <cppuhelper/implbase.hxx>
36 :
37 : #include <sax/tools/documenthandleradapter.hxx>
38 :
39 : #include <com/sun/star/lang/XComponent.hpp>
40 :
41 : #include <com/sun/star/uno/Any.hxx>
42 : #include <com/sun/star/uno/Type.hxx>
43 :
44 : #include <com/sun/star/beans/PropertyValue.hpp>
45 :
46 : #include <com/sun/star/xml/XImportFilter.hpp>
47 : #include <com/sun/star/xml/XExportFilter.hpp>
48 : #include <com/sun/star/xml/sax/Parser.hpp>
49 : #include <com/sun/star/xml/sax/InputSource.hpp>
50 : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
51 : #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
52 : #include <com/sun/star/xml/sax/SAXException.hpp>
53 : #include <com/sun/star/xml/sax/Writer.hpp>
54 :
55 : #include <com/sun/star/io/XInputStream.hpp>
56 : #include <com/sun/star/io/XOutputStream.hpp>
57 : #include <com/sun/star/io/XActiveDataSource.hpp>
58 : #include <com/sun/star/io/XSeekable.hpp>
59 :
60 : using namespace ::rtl;
61 : using namespace ::cppu;
62 : using namespace ::osl;
63 : using namespace ::sax;
64 : using namespace ::com::sun::star::beans;
65 : using namespace ::com::sun::star::io;
66 : using namespace ::com::sun::star::uno;
67 : using namespace ::com::sun::star::lang;
68 : using namespace ::com::sun::star::registry;
69 : using namespace ::com::sun::star::xml;
70 : using namespace ::com::sun::star::xml::sax;
71 :
72 : namespace filter {
73 : namespace odfflatxml {
74 : /*
75 : * OdfFlatXml export and imports ODF flat XML documents by plugging a pass-through
76 : * filter implementation into XmlFilterAdaptor.
77 : */
78 0 : class OdfFlatXml : public WeakImplHelper3<XImportFilter,
79 : XExportFilter, DocumentHandlerAdapter>
80 : {
81 : private:
82 : Reference< XMultiServiceFactory > m_rServiceFactory;
83 :
84 : public:
85 :
86 0 : OdfFlatXml(const Reference<XMultiServiceFactory> &r) :
87 0 : m_rServiceFactory(r)
88 : {
89 0 : }
90 :
91 : // XImportFilter
92 : virtual sal_Bool SAL_CALL
93 : importer(const Sequence< PropertyValue >& sourceData,
94 : const Reference< XDocumentHandler >& docHandler,
95 : const Sequence< OUString >& userData)
96 : throw (IllegalArgumentException, RuntimeException);
97 :
98 : // XExportFilter
99 : virtual sal_Bool SAL_CALL
100 : exporter(
101 : const Sequence< PropertyValue >& sourceData,
102 : const Sequence< OUString >& userData)
103 : throw (IllegalArgumentException,
104 : RuntimeException);
105 :
106 : // UNO component helper methods
107 :
108 : static OUString impl_getImplementationName();
109 :
110 : static Sequence< OUString > impl_getSupportedServiceNames();
111 :
112 : static Reference< XInterface > impl_createInstance(const Reference< XMultiServiceFactory >& fact);
113 : };
114 : }
115 : }
116 :
117 : using namespace ::filter::odfflatxml;
118 :
119 : sal_Bool
120 0 : OdfFlatXml::importer(
121 : const Sequence< PropertyValue >& sourceData,
122 : const Reference< XDocumentHandler >& docHandler,
123 : const Sequence< OUString >& /* userData */)
124 : throw (IllegalArgumentException, RuntimeException)
125 : {
126 : // Read InputStream to read from and an URL used for the system id
127 : // of the InputSource we create from the given sourceData sequence
128 0 : Reference<XInputStream> inputStream;
129 0 : OUString paramName;
130 0 : OUString url;
131 :
132 0 : sal_Int32 paramCount = sourceData.getLength();
133 0 : for (sal_Int32 paramIdx = 0; paramIdx < paramCount; paramIdx++)
134 : {
135 0 : paramName = sourceData[paramIdx].Name;
136 0 : if ( paramName == "InputStream" )
137 0 : sourceData[paramIdx].Value >>= inputStream;
138 0 : else if ( paramName == "URL" )
139 0 : sourceData[paramIdx].Value >>= url;
140 : }
141 :
142 : OSL_ASSERT(inputStream.is());
143 0 : if (!inputStream.is())
144 0 : return sal_False;
145 :
146 0 : Reference<XParser> saxParser = Parser::create(comphelper::getComponentContext(m_rServiceFactory));
147 :
148 0 : InputSource inputSource;
149 0 : inputSource.sSystemId = url;
150 0 : inputSource.sPublicId = url;
151 0 : inputSource.aInputStream = inputStream;
152 0 : saxParser->setDocumentHandler(docHandler);
153 : try
154 : {
155 0 : saxParser->parseStream(inputSource);
156 : }
157 0 : catch (const Exception &exc)
158 : {
159 : OString msg = OUStringToOString(exc.Message,
160 0 : RTL_TEXTENCODING_ASCII_US);
161 : OSL_FAIL(msg.getStr());
162 0 : return sal_False;
163 : }
164 0 : return sal_True;
165 : }
166 :
167 : sal_Bool
168 0 : OdfFlatXml::exporter(const Sequence< PropertyValue >& sourceData,
169 : const Sequence< OUString >& /*msUserData*/)
170 : throw (IllegalArgumentException, RuntimeException)
171 : {
172 0 : OUString paramName;
173 0 : OUString targetURL;
174 0 : Reference<XOutputStream> outputStream;
175 :
176 : // Read output stream and target URL from the parameters given in sourceData.
177 0 : sal_Int32 paramCount = sourceData.getLength();
178 0 : for (sal_Int32 paramIdx = 0; paramIdx < paramCount; paramIdx++)
179 : {
180 0 : paramName = sourceData[paramIdx].Name;
181 0 : if ( paramName == "OutputStream" )
182 0 : sourceData[paramIdx].Value >>= outputStream;
183 0 : else if ( paramName == "URL" )
184 0 : sourceData[paramIdx].Value >>= targetURL;
185 : }
186 :
187 0 : if (!getDelegate().is())
188 : {
189 0 : Reference< XDocumentHandler > saxWriter( Writer::create(comphelper::getComponentContext(m_rServiceFactory)), UNO_QUERY_THROW );
190 0 : setDelegate(saxWriter);
191 : }
192 : // get data source interface ...
193 0 : Reference<XActiveDataSource> dataSource(getDelegate(), UNO_QUERY);
194 : OSL_ASSERT(dataSource.is());
195 0 : if (!dataSource.is())
196 0 : return sal_False;
197 : OSL_ASSERT(outputStream.is());
198 0 : if (!outputStream.is())
199 0 : return sal_False;
200 0 : dataSource->setOutputStream(outputStream);
201 :
202 0 : return sal_True;
203 : }
204 :
205 :
206 0 : OUString OdfFlatXml::impl_getImplementationName()
207 : {
208 0 : return OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.filter.OdfFlatXml"));
209 : }
210 :
211 0 : Sequence< OUString > OdfFlatXml::impl_getSupportedServiceNames()
212 : {
213 0 : Sequence< OUString > lServiceNames(2);
214 0 : lServiceNames[0] = OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.ImportFilter" ));
215 0 : lServiceNames[1] = OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.ExportFilter" ));
216 0 : return lServiceNames;
217 : }
218 :
219 0 : Reference< XInterface > SAL_CALL OdfFlatXml::impl_createInstance(const Reference< XMultiServiceFactory >& fact)
220 : {
221 0 : return Reference<XInterface> ((OWeakObject *) new OdfFlatXml(fact));
222 :
223 : }
224 :
225 : extern "C" SAL_DLLPUBLIC_EXPORT void* SAL_CALL
226 0 : component_getFactory( const sal_Char* pImplementationName,
227 : void* pServiceManager,
228 : void* /* pRegistryKey */ )
229 : {
230 0 : if ((!pImplementationName) || (!pServiceManager))
231 0 : return NULL;
232 :
233 : com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory >
234 0 : xSMGR = reinterpret_cast< com::sun::star::lang::XMultiServiceFactory* >(pServiceManager);
235 0 : com::sun::star::uno::Reference< com::sun::star::lang::XSingleServiceFactory > xFactory;
236 0 : rtl::OUString sImplName = rtl::OUString::createFromAscii(pImplementationName);
237 :
238 0 : if (OdfFlatXml::impl_getImplementationName() == sImplName)
239 : xFactory = cppu::createOneInstanceFactory( xSMGR,
240 : OdfFlatXml::impl_getImplementationName(),
241 : OdfFlatXml::impl_createInstance,
242 0 : OdfFlatXml::impl_getSupportedServiceNames() );
243 :
244 0 : if (xFactory.is())
245 : {
246 0 : xFactory->acquire();
247 0 : return xFactory.get();
248 : }
249 :
250 0 : return NULL;
251 : }
252 :
253 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|