Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : */
9 :
10 :
11 : #include <comphelper/processfactory.hxx>
12 : #include <cppuhelper/factory.hxx>
13 : #include <cppuhelper/implbase3.hxx>
14 : #include <cppuhelper/implbase.hxx>
15 :
16 : #include <sax/tools/documenthandleradapter.hxx>
17 :
18 : #include <com/sun/star/lang/XComponent.hpp>
19 :
20 : #include <com/sun/star/uno/Any.hxx>
21 : #include <com/sun/star/uno/Type.hxx>
22 :
23 : #include <com/sun/star/beans/PropertyValue.hpp>
24 :
25 : #include <com/sun/star/xml/XImportFilter.hpp>
26 : #include <com/sun/star/xml/XExportFilter.hpp>
27 : #include <com/sun/star/xml/sax/Parser.hpp>
28 : #include <com/sun/star/xml/sax/InputSource.hpp>
29 : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
30 : #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
31 : #include <com/sun/star/xml/sax/SAXException.hpp>
32 : #include <com/sun/star/xml/sax/Writer.hpp>
33 :
34 : #include <com/sun/star/io/XInputStream.hpp>
35 : #include <com/sun/star/io/XOutputStream.hpp>
36 : #include <com/sun/star/io/XActiveDataSource.hpp>
37 : #include <com/sun/star/io/XSeekable.hpp>
38 :
39 : using namespace ::rtl;
40 : using namespace ::cppu;
41 : using namespace ::osl;
42 : using namespace ::sax;
43 : using namespace ::com::sun::star::beans;
44 : using namespace ::com::sun::star::io;
45 : using namespace ::com::sun::star::uno;
46 : using namespace ::com::sun::star::lang;
47 : using namespace ::com::sun::star::registry;
48 : using namespace ::com::sun::star::xml;
49 : using namespace ::com::sun::star::xml::sax;
50 :
51 : namespace filter {
52 : namespace odfflatxml {
53 : /*
54 : * OdfFlatXml export and imports ODF flat XML documents by plugging a pass-through
55 : * filter implementation into XmlFilterAdaptor.
56 : */
57 0 : class OdfFlatXml : public WeakImplHelper3<XImportFilter,
58 : XExportFilter, DocumentHandlerAdapter>
59 : {
60 : private:
61 : Reference< XComponentContext > m_xContext;
62 :
63 : public:
64 :
65 0 : OdfFlatXml(const Reference<XComponentContext> &r) :
66 0 : m_xContext(r)
67 : {
68 0 : }
69 :
70 : // XImportFilter
71 : virtual sal_Bool SAL_CALL
72 : importer(const Sequence< PropertyValue >& sourceData,
73 : const Reference< XDocumentHandler >& docHandler,
74 : const Sequence< OUString >& userData)
75 : throw (IllegalArgumentException, RuntimeException, std::exception) SAL_OVERRIDE;
76 :
77 : // XExportFilter
78 : virtual sal_Bool SAL_CALL
79 : exporter(
80 : const Sequence< PropertyValue >& sourceData,
81 : const Sequence< OUString >& userData)
82 : throw (IllegalArgumentException,
83 : RuntimeException, std::exception) SAL_OVERRIDE;
84 :
85 : // UNO component helper methods
86 :
87 : static OUString impl_getImplementationName();
88 :
89 : static Sequence< OUString > impl_getSupportedServiceNames();
90 :
91 : static Reference< XInterface > impl_createInstance(const Reference< XMultiServiceFactory >& fact);
92 : };
93 : }
94 : }
95 :
96 : using namespace ::filter::odfflatxml;
97 :
98 : sal_Bool
99 0 : OdfFlatXml::importer(
100 : const Sequence< PropertyValue >& sourceData,
101 : const Reference< XDocumentHandler >& docHandler,
102 : const Sequence< OUString >& /* userData */)
103 : throw (IllegalArgumentException, RuntimeException, std::exception)
104 : {
105 : // Read InputStream to read from and an URL used for the system id
106 : // of the InputSource we create from the given sourceData sequence
107 0 : Reference<XInputStream> inputStream;
108 0 : OUString paramName;
109 0 : OUString url;
110 :
111 0 : sal_Int32 paramCount = sourceData.getLength();
112 0 : for (sal_Int32 paramIdx = 0; paramIdx < paramCount; paramIdx++)
113 : {
114 0 : paramName = sourceData[paramIdx].Name;
115 0 : if ( paramName == "InputStream" )
116 0 : sourceData[paramIdx].Value >>= inputStream;
117 0 : else if ( paramName == "URL" )
118 0 : sourceData[paramIdx].Value >>= url;
119 : }
120 :
121 : OSL_ASSERT(inputStream.is());
122 0 : if (!inputStream.is())
123 0 : return sal_False;
124 :
125 0 : Reference<XParser> saxParser = Parser::create(m_xContext);
126 :
127 0 : InputSource inputSource;
128 0 : inputSource.sSystemId = url;
129 0 : inputSource.sPublicId = url;
130 0 : inputSource.aInputStream = inputStream;
131 0 : saxParser->setDocumentHandler(docHandler);
132 : try
133 : {
134 0 : saxParser->parseStream(inputSource);
135 : }
136 0 : catch (const Exception &exc)
137 : {
138 : SAL_WARN(
139 : "filter.odfflatxml",
140 : "caught exception \"" << exc.Message << "\"");
141 0 : return sal_False;
142 : }
143 0 : return sal_True;
144 : }
145 :
146 : sal_Bool
147 0 : OdfFlatXml::exporter(const Sequence< PropertyValue >& sourceData,
148 : const Sequence< OUString >& /*msUserData*/)
149 : throw (IllegalArgumentException, RuntimeException, std::exception)
150 : {
151 0 : OUString paramName;
152 0 : OUString targetURL;
153 0 : Reference<XOutputStream> outputStream;
154 :
155 : // Read output stream and target URL from the parameters given in sourceData.
156 0 : sal_Int32 paramCount = sourceData.getLength();
157 0 : for (sal_Int32 paramIdx = 0; paramIdx < paramCount; paramIdx++)
158 : {
159 0 : paramName = sourceData[paramIdx].Name;
160 0 : if ( paramName == "OutputStream" )
161 0 : sourceData[paramIdx].Value >>= outputStream;
162 0 : else if ( paramName == "URL" )
163 0 : sourceData[paramIdx].Value >>= targetURL;
164 : }
165 :
166 0 : if (!getDelegate().is())
167 : {
168 0 : Reference< XDocumentHandler > saxWriter = Writer::create(m_xContext);
169 0 : setDelegate(saxWriter);
170 : }
171 : // get data source interface ...
172 0 : Reference<XActiveDataSource> dataSource(getDelegate(), UNO_QUERY);
173 : OSL_ASSERT(dataSource.is());
174 0 : if (!dataSource.is())
175 0 : return sal_False;
176 : OSL_ASSERT(outputStream.is());
177 0 : if (!outputStream.is())
178 0 : return sal_False;
179 0 : dataSource->setOutputStream(outputStream);
180 :
181 0 : return sal_True;
182 : }
183 :
184 :
185 0 : OUString OdfFlatXml::impl_getImplementationName()
186 : {
187 0 : return OUString("com.sun.star.comp.filter.OdfFlatXml");
188 : }
189 :
190 0 : Sequence< OUString > OdfFlatXml::impl_getSupportedServiceNames()
191 : {
192 0 : Sequence< OUString > lServiceNames(2);
193 0 : lServiceNames[0] = "com.sun.star.document.ImportFilter";
194 0 : lServiceNames[1] = "com.sun.star.document.ExportFilter";
195 0 : return lServiceNames;
196 : }
197 :
198 0 : Reference< XInterface > SAL_CALL OdfFlatXml::impl_createInstance(const Reference< XMultiServiceFactory >& fact)
199 : {
200 0 : return Reference<XInterface> ((OWeakObject *) new OdfFlatXml( comphelper::getComponentContext(fact) ));
201 :
202 : }
203 :
204 : extern "C" SAL_DLLPUBLIC_EXPORT void* SAL_CALL
205 0 : odfflatxml_component_getFactory( const sal_Char* pImplementationName,
206 : void* pServiceManager,
207 : void* /* pRegistryKey */ )
208 : {
209 0 : if ((!pImplementationName) || (!pServiceManager))
210 0 : return NULL;
211 :
212 : com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory >
213 0 : xSMGR = reinterpret_cast< com::sun::star::lang::XMultiServiceFactory* >(pServiceManager);
214 0 : com::sun::star::uno::Reference< com::sun::star::lang::XSingleServiceFactory > xFactory;
215 0 : OUString sImplName = OUString::createFromAscii(pImplementationName);
216 :
217 0 : if (OdfFlatXml::impl_getImplementationName() == sImplName)
218 0 : xFactory = cppu::createOneInstanceFactory( xSMGR,
219 : OdfFlatXml::impl_getImplementationName(),
220 : OdfFlatXml::impl_createInstance,
221 0 : OdfFlatXml::impl_getSupportedServiceNames() );
222 :
223 0 : if (xFactory.is())
224 : {
225 0 : xFactory->acquire();
226 0 : return xFactory.get();
227 : }
228 :
229 0 : return NULL;
230 : }
231 :
232 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|