1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: storagehelper.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
33 #include <com/sun/star/embed/ElementModes.hpp>
34 #include <com/sun/star/embed/XEncryptionProtectedSource.hpp>
35 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
36 #include <com/sun/star/beans/XPropertySet.hpp>
37 #include <com/sun/star/beans/PropertyValue.hpp>
38 #include <com/sun/star/beans/IllegalTypeException.hpp>
40 #include <ucbhelper/content.hxx>
42 #include <comphelper/fileformat.h>
43 #include <comphelper/processfactory.hxx>
44 #include <comphelper/documentconstants.hxx>
46 #include <comphelper/storagehelper.hxx>
49 using namespace ::com::sun::star
;
51 namespace comphelper
{
53 // ----------------------------------------------------------------------
54 uno::Reference
< lang::XSingleServiceFactory
> OStorageHelper::GetStorageFactory(
55 const uno::Reference
< lang::XMultiServiceFactory
>& xSF
)
56 throw ( uno::Exception
)
58 uno::Reference
< lang::XMultiServiceFactory
> xFactory
= xSF
.is() ? xSF
: ::comphelper::getProcessServiceFactory();
60 throw uno::RuntimeException();
62 uno::Reference
< lang::XSingleServiceFactory
> xStorageFactory(
63 xFactory
->createInstance ( ::rtl::OUString::createFromAscii( "com.sun.star.embed.StorageFactory" ) ),
66 if ( !xStorageFactory
.is() )
67 throw uno::RuntimeException();
69 return xStorageFactory
;
72 // ----------------------------------------------------------------------
73 uno::Reference
< lang::XSingleServiceFactory
> OStorageHelper::GetFileSystemStorageFactory(
74 const uno::Reference
< lang::XMultiServiceFactory
>& xSF
)
75 throw ( uno::Exception
)
77 uno::Reference
< lang::XMultiServiceFactory
> xFactory
= xSF
.is() ? xSF
: ::comphelper::getProcessServiceFactory();
79 throw uno::RuntimeException();
81 uno::Reference
< lang::XSingleServiceFactory
> xStorageFactory(
82 xFactory
->createInstance ( ::rtl::OUString::createFromAscii( "com.sun.star.embed.FileSystemStorageFactory" ) ),
85 if ( !xStorageFactory
.is() )
86 throw uno::RuntimeException();
88 return xStorageFactory
;
91 // ----------------------------------------------------------------------
92 uno::Reference
< embed::XStorage
> OStorageHelper::GetTemporaryStorage(
93 const uno::Reference
< lang::XMultiServiceFactory
>& xFactory
)
94 throw ( uno::Exception
)
96 uno::Reference
< embed::XStorage
> xTempStorage( GetStorageFactory( xFactory
)->createInstance(),
98 if ( !xTempStorage
.is() )
99 throw uno::RuntimeException();
104 // ----------------------------------------------------------------------
105 uno::Reference
< embed::XStorage
> OStorageHelper::GetStorageFromURL(
106 const ::rtl::OUString
& aURL
,
107 sal_Int32 nStorageMode
,
108 const uno::Reference
< lang::XMultiServiceFactory
>& xFactory
)
109 throw ( uno::Exception
)
111 uno::Sequence
< uno::Any
> aArgs( 2 );
113 aArgs
[1] <<= nStorageMode
;
115 uno::Reference
< embed::XStorage
> xTempStorage( GetStorageFactory( xFactory
)->createInstanceWithArguments( aArgs
),
117 if ( !xTempStorage
.is() )
118 throw uno::RuntimeException();
123 // ----------------------------------------------------------------------
124 uno::Reference
< embed::XStorage
> OStorageHelper::GetStorageFromURL2(
125 const ::rtl::OUString
& aURL
,
126 sal_Int32 nStorageMode
,
127 const uno::Reference
< lang::XMultiServiceFactory
>& xFactory
)
128 throw ( uno::Exception
)
130 uno::Sequence
< uno::Any
> aArgs( 2 );
132 aArgs
[1] <<= nStorageMode
;
134 uno::Reference
< lang::XSingleServiceFactory
> xFact
;
136 ::ucbhelper::Content
aCntnt( aURL
,
137 uno::Reference
< ::com::sun::star::ucb::XCommandEnvironment
> () );
138 if (aCntnt
.isDocument()) {
139 xFact
= GetStorageFactory( xFactory
);
141 xFact
= GetFileSystemStorageFactory( xFactory
);
143 } catch (uno::Exception
&) { }
145 if (!xFact
.is()) throw uno::RuntimeException();
147 uno::Reference
< embed::XStorage
> xTempStorage(
148 xFact
->createInstanceWithArguments( aArgs
), uno::UNO_QUERY
);
149 if ( !xTempStorage
.is() )
150 throw uno::RuntimeException();
155 // ----------------------------------------------------------------------
156 uno::Reference
< embed::XStorage
> OStorageHelper::GetStorageFromInputStream(
157 const uno::Reference
< io::XInputStream
>& xStream
,
158 const uno::Reference
< lang::XMultiServiceFactory
>& xFactory
)
159 throw ( uno::Exception
)
161 uno::Sequence
< uno::Any
> aArgs( 2 );
162 aArgs
[0] <<= xStream
;
163 aArgs
[1] <<= embed::ElementModes::READ
;
165 uno::Reference
< embed::XStorage
> xTempStorage( GetStorageFactory( xFactory
)->createInstanceWithArguments( aArgs
),
167 if ( !xTempStorage
.is() )
168 throw uno::RuntimeException();
173 // ----------------------------------------------------------------------
174 uno::Reference
< embed::XStorage
> OStorageHelper::GetStorageFromStream(
175 const uno::Reference
< io::XStream
>& xStream
,
176 sal_Int32 nStorageMode
,
177 const uno::Reference
< lang::XMultiServiceFactory
>& xFactory
)
178 throw ( uno::Exception
)
180 uno::Sequence
< uno::Any
> aArgs( 2 );
181 aArgs
[0] <<= xStream
;
182 aArgs
[1] <<= nStorageMode
;
184 uno::Reference
< embed::XStorage
> xTempStorage( GetStorageFactory( xFactory
)->createInstanceWithArguments( aArgs
),
186 if ( !xTempStorage
.is() )
187 throw uno::RuntimeException();
192 // ----------------------------------------------------------------------
193 void OStorageHelper::CopyInputToOutput(
194 const uno::Reference
< io::XInputStream
>& xInput
,
195 const uno::Reference
< io::XOutputStream
>& xOutput
)
196 throw ( uno::Exception
)
198 static const sal_Int32 nConstBufferSize
= 32000;
201 uno::Sequence
< sal_Int8
> aSequence ( nConstBufferSize
);
205 nRead
= xInput
->readBytes ( aSequence
, nConstBufferSize
);
206 if ( nRead
< nConstBufferSize
)
208 uno::Sequence
< sal_Int8
> aTempBuf ( aSequence
.getConstArray(), nRead
);
209 xOutput
->writeBytes ( aTempBuf
);
212 xOutput
->writeBytes ( aSequence
);
214 while ( nRead
== nConstBufferSize
);
217 // ----------------------------------------------------------------------
218 uno::Reference
< io::XInputStream
> OStorageHelper::GetInputStreamFromURL(
219 const ::rtl::OUString
& aURL
,
220 const uno::Reference
< lang::XMultiServiceFactory
>& xSF
)
221 throw ( uno::Exception
)
223 uno::Reference
< lang::XMultiServiceFactory
> xFactory
= xSF
.is() ? xSF
: ::comphelper::getProcessServiceFactory();
224 if ( !xFactory
.is() )
225 throw uno::RuntimeException();
227 uno::Reference
< ::com::sun::star::ucb::XSimpleFileAccess
> xTempAccess(
228 xFactory
->createInstance ( ::rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ),
231 if ( !xTempAccess
.is() )
232 throw uno::RuntimeException();
234 uno::Reference
< io::XInputStream
> xInputStream
= xTempAccess
->openFileRead( aURL
);
235 if ( !xInputStream
.is() )
236 throw uno::RuntimeException();
241 // ----------------------------------------------------------------------
242 void OStorageHelper::SetCommonStoragePassword(
243 const uno::Reference
< embed::XStorage
>& xStorage
,
244 const ::rtl::OUString
& aPass
)
245 throw ( uno::Exception
)
247 uno::Reference
< embed::XEncryptionProtectedSource
> xEncrSet( xStorage
, uno::UNO_QUERY
);
248 if ( !xEncrSet
.is() )
249 throw io::IOException(); // TODO
251 xEncrSet
->setEncryptionPassword( aPass
);
254 // ----------------------------------------------------------------------
255 sal_Int32
OStorageHelper::GetXStorageFormat(
256 const uno::Reference
< embed::XStorage
>& xStorage
)
257 throw ( uno::Exception
)
259 uno::Reference
< beans::XPropertySet
> xStorProps( xStorage
, uno::UNO_QUERY_THROW
);
261 ::rtl::OUString aMediaType
;
262 xStorProps
->getPropertyValue( ::rtl::OUString::createFromAscii( "MediaType" ) ) >>= aMediaType
;
264 sal_Int32 nResult
= 0;
266 // TODO/LATER: the filter configuration could be used to detect it later, or batter a special service
268 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_VND_SUN_XML_WRITER_ASCII
) ||
269 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_VND_SUN_XML_WRITER_WEB_ASCII
) ||
270 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_VND_SUN_XML_WRITER_GLOBAL_ASCII
) ||
271 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_VND_SUN_XML_DRAW_ASCII
) ||
272 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_VND_SUN_XML_IMPRESS_ASCII
) ||
273 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_VND_SUN_XML_CALC_ASCII
) ||
274 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_VND_SUN_XML_CHART_ASCII
) ||
275 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_VND_SUN_XML_MATH_ASCII
)
278 nResult
= SOFFICE_FILEFORMAT_60
;
282 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_TEXT_ASCII
) ||
283 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_TEXT_WEB_ASCII
) ||
284 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_TEXT_GLOBAL_ASCII
) ||
285 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_ASCII
) ||
286 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_ASCII
) ||
287 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_ASCII
) ||
288 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_CHART_ASCII
) ||
289 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_FORMULA_ASCII
) ||
290 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_DATABASE_ASCII
) ||
291 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_REPORT_ASCII
) ||
292 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_REPORT_CHART_ASCII
) ||
293 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_TEXT_TEMPLATE_ASCII
) ||
294 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_TEMPLATE_ASCII
) ||
295 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_TEMPLATE_ASCII
) ||
296 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_TEMPLATE_ASCII
) ||
297 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_CHART_TEMPLATE_ASCII
) ||
298 aMediaType
.equalsIgnoreAsciiCaseAscii(MIMETYPE_OASIS_OPENDOCUMENT_FORMULA_TEMPLATE_ASCII
)
301 nResult
= SOFFICE_FILEFORMAT_8
;
305 // the mediatype is not known
306 throw beans::IllegalTypeException();
312 // ----------------------------------------------------------------------
313 uno::Reference
< embed::XStorage
> OStorageHelper::GetTemporaryStorageOfFormat(
314 const ::rtl::OUString
& aFormat
,
315 const uno::Reference
< lang::XMultiServiceFactory
>& xFactory
)
316 throw ( uno::Exception
)
318 uno::Reference
< lang::XMultiServiceFactory
> xFactoryToUse
= xFactory
.is() ? xFactory
: ::comphelper::getProcessServiceFactory();
319 if ( !xFactoryToUse
.is() )
320 throw uno::RuntimeException();
322 uno::Reference
< io::XStream
> xTmpStream(
323 xFactoryToUse
->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.io.TempFile" ) ) ),
324 uno::UNO_QUERY_THROW
);
326 return GetStorageOfFormatFromStream( aFormat
, xTmpStream
, embed::ElementModes::READWRITE
, xFactoryToUse
);
329 // ----------------------------------------------------------------------
330 uno::Reference
< embed::XStorage
> OStorageHelper::GetStorageOfFormatFromURL(
331 const ::rtl::OUString
& aFormat
,
332 const ::rtl::OUString
& aURL
,
333 sal_Int32 nStorageMode
,
334 const uno::Reference
< lang::XMultiServiceFactory
>& xFactory
)
335 throw ( uno::Exception
)
337 uno::Sequence
< beans::PropertyValue
> aProps( 1 );
338 aProps
[0].Name
= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "StorageFormat" ) );
339 aProps
[0].Value
<<= aFormat
;
341 uno::Sequence
< uno::Any
> aArgs( 3 );
343 aArgs
[1] <<= nStorageMode
;
346 uno::Reference
< embed::XStorage
> xTempStorage( GetStorageFactory( xFactory
)->createInstanceWithArguments( aArgs
),
348 if ( !xTempStorage
.is() )
349 throw uno::RuntimeException();
354 // ----------------------------------------------------------------------
355 uno::Reference
< embed::XStorage
> OStorageHelper::GetStorageOfFormatFromInputStream(
356 const ::rtl::OUString
& aFormat
,
357 const uno::Reference
< io::XInputStream
>& xStream
,
358 const uno::Reference
< lang::XMultiServiceFactory
>& xFactory
)
359 throw ( uno::Exception
)
361 uno::Sequence
< beans::PropertyValue
> aProps( 1 );
362 aProps
[0].Name
= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "StorageFormat" ) );
363 aProps
[0].Value
<<= aFormat
;
365 uno::Sequence
< uno::Any
> aArgs( 3 );
366 aArgs
[0] <<= xStream
;
367 aArgs
[1] <<= embed::ElementModes::READ
;
370 uno::Reference
< embed::XStorage
> xTempStorage( GetStorageFactory( xFactory
)->createInstanceWithArguments( aArgs
),
372 if ( !xTempStorage
.is() )
373 throw uno::RuntimeException();
378 // ----------------------------------------------------------------------
379 uno::Reference
< embed::XStorage
> OStorageHelper::GetStorageOfFormatFromStream(
380 const ::rtl::OUString
& aFormat
,
381 const uno::Reference
< io::XStream
>& xStream
,
382 sal_Int32 nStorageMode
,
383 const uno::Reference
< lang::XMultiServiceFactory
>& xFactory
)
384 throw ( uno::Exception
)
386 uno::Sequence
< beans::PropertyValue
> aProps( 1 );
387 aProps
[0].Name
= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "StorageFormat" ) );
388 aProps
[0].Value
<<= aFormat
;
390 uno::Sequence
< uno::Any
> aArgs( 3 );
391 aArgs
[0] <<= xStream
;
392 aArgs
[1] <<= nStorageMode
;
395 uno::Reference
< embed::XStorage
> xTempStorage( GetStorageFactory( xFactory
)->createInstanceWithArguments( aArgs
),
397 if ( !xTempStorage
.is() )
398 throw uno::RuntimeException();
403 // ----------------------------------------------------------------------
404 sal_Bool
OStorageHelper::IsValidZipEntryFileName( const ::rtl::OUString
& aName
, sal_Bool bSlashAllowed
)
406 return IsValidZipEntryFileName( aName
.getStr(), aName
.getLength(), bSlashAllowed
);
409 // ----------------------------------------------------------------------
410 sal_Bool
OStorageHelper::IsValidZipEntryFileName(
411 const sal_Unicode
*pChar
, sal_Int32 nLength
, sal_Bool bSlashAllowed
)
413 for ( sal_Int32 i
= 0; i
< nLength
; i
++ )
426 if ( !bSlashAllowed
)
430 if ( pChar
[i
] < 32 || (pChar
[i
] >= 0xD800 && pChar
[i
] <= 0xDFFF) )