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