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