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