bump product version to 4.1.6.2
[LibreOffice.git] / filter / source / odfflatxml / OdfFlatXml.cxx
blob57b5489e9866114940c075e4091d8161e1e94a94
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
11 #include <comphelper/processfactory.hxx>
12 #include <cppuhelper/factory.hxx>
13 #include <cppuhelper/implbase3.hxx>
14 #include <cppuhelper/implbase.hxx>
16 #include <sax/tools/documenthandleradapter.hxx>
18 #include <com/sun/star/lang/XComponent.hpp>
20 #include <com/sun/star/uno/Any.hxx>
21 #include <com/sun/star/uno/Type.hxx>
23 #include <com/sun/star/beans/PropertyValue.hpp>
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>
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>
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;
51 namespace filter {
52 namespace odfflatxml {
54 * OdfFlatXml export and imports ODF flat XML documents by plugging a pass-through
55 * filter implementation into XmlFilterAdaptor.
57 class OdfFlatXml : public WeakImplHelper3<XImportFilter,
58 XExportFilter, DocumentHandlerAdapter>
60 private:
61 Reference< XMultiServiceFactory > m_rServiceFactory;
63 public:
65 OdfFlatXml(const Reference<XMultiServiceFactory> &r) :
66 m_rServiceFactory(r)
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);
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);
85 // UNO component helper methods
87 static OUString impl_getImplementationName();
89 static Sequence< OUString > impl_getSupportedServiceNames();
91 static Reference< XInterface > impl_createInstance(const Reference< XMultiServiceFactory >& fact);
96 using namespace ::filter::odfflatxml;
98 sal_Bool
99 OdfFlatXml::importer(
100 const Sequence< PropertyValue >& sourceData,
101 const Reference< XDocumentHandler >& docHandler,
102 const Sequence< OUString >& /* userData */)
103 throw (IllegalArgumentException, RuntimeException)
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 Reference<XInputStream> inputStream;
108 OUString paramName;
109 OUString url;
111 sal_Int32 paramCount = sourceData.getLength();
112 for (sal_Int32 paramIdx = 0; paramIdx < paramCount; paramIdx++)
114 paramName = sourceData[paramIdx].Name;
115 if ( paramName == "InputStream" )
116 sourceData[paramIdx].Value >>= inputStream;
117 else if ( paramName == "URL" )
118 sourceData[paramIdx].Value >>= url;
121 OSL_ASSERT(inputStream.is());
122 if (!inputStream.is())
123 return sal_False;
125 Reference<XParser> saxParser = Parser::create(comphelper::getComponentContext(m_rServiceFactory));
127 InputSource inputSource;
128 inputSource.sSystemId = url;
129 inputSource.sPublicId = url;
130 inputSource.aInputStream = inputStream;
131 saxParser->setDocumentHandler(docHandler);
134 saxParser->parseStream(inputSource);
136 catch (const Exception &exc)
138 OString msg = OUStringToOString(exc.Message,
139 RTL_TEXTENCODING_ASCII_US);
140 OSL_FAIL(msg.getStr());
141 return sal_False;
143 return sal_True;
146 sal_Bool
147 OdfFlatXml::exporter(const Sequence< PropertyValue >& sourceData,
148 const Sequence< OUString >& /*msUserData*/)
149 throw (IllegalArgumentException, RuntimeException)
151 OUString paramName;
152 OUString targetURL;
153 Reference<XOutputStream> outputStream;
155 // Read output stream and target URL from the parameters given in sourceData.
156 sal_Int32 paramCount = sourceData.getLength();
157 for (sal_Int32 paramIdx = 0; paramIdx < paramCount; paramIdx++)
159 paramName = sourceData[paramIdx].Name;
160 if ( paramName == "OutputStream" )
161 sourceData[paramIdx].Value >>= outputStream;
162 else if ( paramName == "URL" )
163 sourceData[paramIdx].Value >>= targetURL;
166 if (!getDelegate().is())
168 Reference< XDocumentHandler > saxWriter( Writer::create(comphelper::getComponentContext(m_rServiceFactory)), UNO_QUERY_THROW );
169 setDelegate(saxWriter);
171 // get data source interface ...
172 Reference<XActiveDataSource> dataSource(getDelegate(), UNO_QUERY);
173 OSL_ASSERT(dataSource.is());
174 if (!dataSource.is())
175 return sal_False;
176 OSL_ASSERT(outputStream.is());
177 if (!outputStream.is())
178 return sal_False;
179 dataSource->setOutputStream(outputStream);
181 return sal_True;
185 OUString OdfFlatXml::impl_getImplementationName()
187 return OUString("com.sun.star.comp.filter.OdfFlatXml");
190 Sequence< OUString > OdfFlatXml::impl_getSupportedServiceNames()
192 Sequence< OUString > lServiceNames(2);
193 lServiceNames[0] = OUString( "com.sun.star.document.ImportFilter" );
194 lServiceNames[1] = OUString( "com.sun.star.document.ExportFilter" );
195 return lServiceNames;
198 Reference< XInterface > SAL_CALL OdfFlatXml::impl_createInstance(const Reference< XMultiServiceFactory >& fact)
200 return Reference<XInterface> ((OWeakObject *) new OdfFlatXml(fact));
204 extern "C" SAL_DLLPUBLIC_EXPORT void* SAL_CALL
205 odfflatxml_component_getFactory( const sal_Char* pImplementationName,
206 void* pServiceManager,
207 void* /* pRegistryKey */ )
209 if ((!pImplementationName) || (!pServiceManager))
210 return NULL;
212 com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory >
213 xSMGR = reinterpret_cast< com::sun::star::lang::XMultiServiceFactory* >(pServiceManager);
214 com::sun::star::uno::Reference< com::sun::star::lang::XSingleServiceFactory > xFactory;
215 OUString sImplName = OUString::createFromAscii(pImplementationName);
217 if (OdfFlatXml::impl_getImplementationName() == sImplName)
218 xFactory = cppu::createOneInstanceFactory( xSMGR,
219 OdfFlatXml::impl_getImplementationName(),
220 OdfFlatXml::impl_createInstance,
221 OdfFlatXml::impl_getSupportedServiceNames() );
223 if (xFactory.is())
225 xFactory->acquire();
226 return xFactory.get();
229 return NULL;
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */