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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
/* -*- 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/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <com/sun/star/io/XActiveDataControl.hpp>
#include <com/sun/star/io/XInputStream.hpp>
#include <com/sun/star/io/XActiveDataSink.hpp>
#include <com/sun/star/beans/NamedValue.hpp>
#include <com/sun/star/container/XNameContainer.hpp>
#include <com/sun/star/container/XNamed.hpp>
#include <com/sun/star/container/XChild.hpp>
#include <com/sun/star/lang/IllegalArgumentException.hpp>
#include <com/sun/star/lang/XUnoTunnel.hpp>
#include <com/sun/star/util/XChangesBatch.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <comphelper/oslfile2streamwrap.hxx>
#include <comphelper/storagehelper.hxx>
#include <osl/file.hxx>
#include <unotools/pathoptions.hxx>
#include <unotools/streamwrap.hxx>
#include <unotools/tempfile.hxx>
#include <svl/urihelper.hxx>
#include <tools/diagnose_ex.h>
#include <tools/stream.hxx>
#include <tools/urlobj.hxx>
#include <rtl/uri.hxx>
#include "xmlfiltercommon.hxx"
#include "xmlfilterjar.hxx"
#include "typedetectionexport.hxx"
#include "typedetectionimport.hxx"
using namespace osl;
using namespace comphelper;
using namespace com::sun::star;
using namespace com::sun::star::lang;
using namespace com::sun::star::uno;
using namespace com::sun::star::util;
using namespace com::sun::star::container;
using namespace com::sun::star::beans;
using namespace com::sun::star::io;
using ::rtl::Uri;
constexpr OUStringLiteral sVndSunStarPackage(u"vnd.sun.star.Package:");
XMLFilterJarHelper::XMLFilterJarHelper( const Reference< XComponentContext >& rxContext )
: mxContext( rxContext ),
sXSLTPath( "$(user)/xslt/" ),
sTemplatePath( "$(user)/template/" ),
sProgPath( "$(prog)/" )
{
SvtPathOptions aOptions;
sProgPath = aOptions.SubstituteVariable( sProgPath );
sXSLTPath = aOptions.SubstituteVariable( sXSLTPath );
sTemplatePath = aOptions.SubstituteVariable( sTemplatePath );
}
static OUString encodeZipUri( const OUString& rURI )
{
return Uri::encode( rURI, rtl_UriCharClassUric, rtl_UriEncodeCheckEscapes, RTL_TEXTENCODING_UTF8 );
}
/// @throws Exception
static Reference< XInterface > addFolder( Reference< XInterface > const & xRootFolder, Reference< XSingleServiceFactory > const & xFactory, const OUString& rName )
{
if ( rName == ".." || rName == "." )
throw lang::IllegalArgumentException();
Sequence< Any > aArgs{ Any(true) };
Reference< XInterface > xFolder( xFactory->createInstanceWithArguments(aArgs) );
Reference< XNamed > xNamed( xFolder, UNO_QUERY );
Reference< XChild > xChild( xFolder, UNO_QUERY );
if( xNamed.is() && xChild.is() )
{
OUString aName( encodeZipUri( rName ) );
xNamed->setName( aName );
xChild->setParent( xRootFolder );
}
return xFolder;
}
/// @throws Exception
static void addFile_( Reference< XInterface > const & xRootFolder, Reference< XSingleServiceFactory > const & xFactory, Reference< XInputStream > const & xInput, const OUString& aName )
{
Reference< XActiveDataSink > xSink( xFactory->createInstance(), UNO_QUERY );
Reference< XUnoTunnel > xTunnel( xSink, UNO_QUERY );
if( xSink.is() && xTunnel.is())
{
Reference< XNameContainer > xNameContainer(xRootFolder, UNO_QUERY );
xNameContainer->insertByName(encodeZipUri( aName ), Any(xTunnel));
xSink->setInputStream( xInput );
}
}
void XMLFilterJarHelper::addFile( Reference< XInterface > const & xRootFolder, Reference< XSingleServiceFactory > const & xFactory, const OUString& rSourceFile )
{
if( rSourceFile.isEmpty() ||
rSourceFile.startsWith("http:") ||
rSourceFile.startsWith("https:") ||
rSourceFile.startsWith("jar:") ||
rSourceFile.startsWith("ftp:") )
return;
OUString aFileURL( rSourceFile );
if( !aFileURL.matchIgnoreAsciiCase("file://") )
{
aFileURL = URIHelper::SmartRel2Abs( INetURLObject(sProgPath), aFileURL, Link<OUString *, bool>(), false );
}
INetURLObject aURL( aFileURL );
OUString aName( aURL.getName() );
SvFileStream* pStream = new SvFileStream(aFileURL, StreamMode::READ );
Reference< XInputStream > xInput( new utl::OSeekableInputStreamWrapper( pStream, true ) );
addFile_( xRootFolder, xFactory, xInput, aName );
}
bool XMLFilterJarHelper::savePackage( const OUString& rPackageURL, const std::vector<filter_info_impl*>& rFilters )
{
try
{
osl::File::remove( rPackageURL );
// create the package jar file
Sequence< Any > aArguments{ Any(rPackageURL),
// let ZipPackage be used ( no manifest.xml is required )
Any(beans::NamedValue(
"StorageFormat", Any(OUString(ZIP_STORAGE_FORMAT_STRING)))) };
Reference< XHierarchicalNameAccess > xIfc(
mxContext->getServiceManager()->createInstanceWithArgumentsAndContext(
"com.sun.star.packages.comp.ZipPackage",
aArguments, mxContext ), UNO_QUERY );
if( xIfc.is() )
{
Reference< XSingleServiceFactory > xFactory( xIfc, UNO_QUERY );
// get root zip folder
Reference< XInterface > xRootFolder;
xIfc->getByHierarchicalName( "/" ) >>= xRootFolder;
// export filters files
for (auto const& filter : rFilters)
{
Reference< XInterface > xFilterRoot( addFolder( xRootFolder, xFactory, filter->maFilterName ) );
if( xFilterRoot.is() )
{
if( !filter->maExportXSLT.isEmpty() )
addFile( xFilterRoot, xFactory, filter->maExportXSLT );
try
{
if( !filter->maImportXSLT.isEmpty() )
addFile( xFilterRoot, xFactory, filter->maImportXSLT );
}
catch(const css::container::ElementExistException&)
{
// in case of same named import / export XSLT the latter
// is ignored
TOOLS_WARN_EXCEPTION("filter.xslt", "same named xslt filter exception!");
}
if( !filter->maImportTemplate.isEmpty() )
addFile( xFilterRoot, xFactory, filter->maImportTemplate );
}
}
// create TypeDetection.xcu
utl::TempFile aTempFile;
aTempFile.EnableKillingFile();
OUString aTempFileURL( aTempFile.GetURL() );
{
osl::File aOutputFile( aTempFileURL );
(void)aOutputFile.open(osl_File_OpenFlag_Write);
Reference< XOutputStream > xOS( new OSLOutputStreamWrapper( aOutputFile ) );
TypeDetectionExporter aExporter( mxContext );
aExporter.doExport(xOS,rFilters);
}
Reference< XInputStream > XIS( new utl::OSeekableInputStreamWrapper( new SvFileStream(aTempFileURL, StreamMode::READ ), true ) );
addFile_( xRootFolder, xFactory, XIS, "TypeDetection.xcu" );
Reference< XChangesBatch > xBatch( xIfc, UNO_QUERY );
if( xBatch.is() )
xBatch->commitChanges();
return true;
}
}
catch( const Exception& )
{
TOOLS_WARN_EXCEPTION("filter.xslt", "");
}
osl::File::remove( rPackageURL );
return false;
}
void XMLFilterJarHelper::openPackage( const OUString& rPackageURL,
std::vector< std::unique_ptr<filter_info_impl> >& rFilters )
{
try
{
// create the package jar file
// let ZipPackage be used ( no manifest.xml is required )
beans::NamedValue aArg;
aArg.Name = "StorageFormat";
aArg.Value <<= OUString(ZIP_STORAGE_FORMAT_STRING);
Sequence< Any > aArguments{ Any(rPackageURL), Any(aArg) };
Reference< XHierarchicalNameAccess > xIfc(
mxContext->getServiceManager()->createInstanceWithArgumentsAndContext(
"com.sun.star.packages.comp.ZipPackage",
aArguments, mxContext ), UNO_QUERY );
if( xIfc.is() )
{
// get root zip folder
Reference< XInterface > xRootFolder;
xIfc->getByHierarchicalName( "/" ) >>= xRootFolder;
OUString szTypeDetection("TypeDetection.xcu");
if( xIfc->hasByHierarchicalName( szTypeDetection ) )
{
Reference< XActiveDataSink > xTypeDetection;
xIfc->getByHierarchicalName( szTypeDetection ) >>= xTypeDetection;
if( xTypeDetection.is() )
{
Reference< XInputStream > xIS( xTypeDetection->getInputStream() );
std::vector< std::unique_ptr<filter_info_impl> > aFilters;
TypeDetectionImporter::doImport( mxContext, xIS, aFilters );
// copy all files used by the filters imported from the
// typedetection to office/user/xslt
for (auto& filter : aFilters)
{
if( copyFiles( xIfc, filter.get() ) )
{
rFilters.push_back(std::move(filter));
}
else
{
// failed to copy all files
filter.reset();
}
}
}
}
}
}
catch( const Exception& )
{
TOOLS_WARN_EXCEPTION("filter.xslt", "");
}
}
bool XMLFilterJarHelper::copyFiles( const Reference< XHierarchicalNameAccess >& xIfc, filter_info_impl* pFilter )
{
bool bOk = copyFile( xIfc, pFilter->maExportXSLT, sXSLTPath );
if( bOk )
bOk = copyFile( xIfc, pFilter->maImportXSLT, sXSLTPath );
if( bOk )
bOk = copyFile( xIfc, pFilter->maImportTemplate, sTemplatePath );
return bOk;
}
bool XMLFilterJarHelper::copyFile( const Reference< XHierarchicalNameAccess >& xIfc, OUString& rURL, std::u16string_view rTargetURL )
{
if( !rURL.matchIgnoreAsciiCase( sVndSunStarPackage ) )
return true;
try
{
OUString szPackagePath( encodeZipUri( rURL.copy( sVndSunStarPackage.getLength() ) ) );
if ( ::comphelper::OStorageHelper::PathHasSegment( szPackagePath, u".." )
|| ::comphelper::OStorageHelper::PathHasSegment( szPackagePath, u"." ) )
throw lang::IllegalArgumentException();
if( xIfc->hasByHierarchicalName( szPackagePath ) )
{
Reference< XActiveDataSink > xFileEntry;
xIfc->getByHierarchicalName( szPackagePath ) >>= xFileEntry;
if( xFileEntry.is() )
{
Reference< XInputStream > xIS( xFileEntry->getInputStream() );
INetURLObject aBaseURL( rTargetURL );
rURL = URIHelper::SmartRel2Abs( aBaseURL, szPackagePath, Link<OUString *, bool>(), false );
if( !rURL.isEmpty() )
{
// create output directory if needed
if( !createDirectory( rURL ) )
return false;
::osl::File file(rURL);
::osl::FileBase::RC rc =
file.open(osl_File_OpenFlag_Write|osl_File_OpenFlag_Create);
if (::osl::FileBase::E_EXIST == rc) {
rc = file.open(osl_File_OpenFlag_Write);
if (::osl::FileBase::E_None == rc) {
file.setSize(0); // #i97170# truncate
}
}
if (::osl::FileBase::E_None != rc) {
throw RuntimeException();
}
Reference< XOutputStream > const xOS(
new comphelper::OSLOutputStreamWrapper(file));
return copyStreams( xIS, xOS );
}
}
}
}
catch( const Exception& )
{
TOOLS_WARN_EXCEPTION("filter.xslt", "");
}
return false;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|