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: xfactory.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_package.hxx"
33 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
34 #include <com/sun/star/embed/ElementModes.hpp>
35 #include <com/sun/star/beans/PropertyValue.hpp>
36 #include <com/sun/star/io/XSeekable.hpp>
39 #include "xfactory.hxx"
40 #include "xstorage.hxx"
43 using namespace ::com::sun::star
;
45 //-------------------------------------------------------------------------
46 sal_Bool
CheckPackageSignature_Impl( const uno::Reference
< io::XInputStream
>& xInputStream
,
47 const uno::Reference
< io::XSeekable
>& xSeekable
)
49 if ( !xInputStream
.is() || !xSeekable
.is() )
50 throw uno::RuntimeException();
52 if ( xSeekable
->getLength() )
54 uno::Sequence
< sal_Int8
> aData( 4 );
56 sal_Int32 nRead
= xInputStream
->readBytes( aData
, 4 );
59 // TODO/LATER: should the disk spanned files be supported?
60 // 0x50, 0x4b, 0x07, 0x08
61 return ( nRead
== 4 && aData
[0] == 0x50 && aData
[1] == 0x4b && aData
[2] == 0x03 && aData
[3] == 0x04 );
64 return sal_True
; // allow to create a storage based on empty stream
67 //-------------------------------------------------------------------------
68 uno::Sequence
< ::rtl::OUString
> SAL_CALL
OStorageFactory::impl_staticGetSupportedServiceNames()
70 uno::Sequence
< ::rtl::OUString
> aRet(2);
71 aRet
[0] = ::rtl::OUString::createFromAscii("com.sun.star.embed.StorageFactory");
72 aRet
[1] = ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.StorageFactory");
76 //-------------------------------------------------------------------------
77 ::rtl::OUString SAL_CALL
OStorageFactory::impl_staticGetImplementationName()
79 return ::rtl::OUString::createFromAscii("com.sun.star.comp.embed.StorageFactory");
82 //-------------------------------------------------------------------------
83 uno::Reference
< uno::XInterface
> SAL_CALL
OStorageFactory::impl_staticCreateSelfInstance(
84 const uno::Reference
< lang::XMultiServiceFactory
>& xServiceManager
)
86 return uno::Reference
< uno::XInterface
>( *new OStorageFactory( xServiceManager
) );
89 //-------------------------------------------------------------------------
90 uno::Reference
< uno::XInterface
> SAL_CALL
OStorageFactory::createInstance()
91 throw ( uno::Exception
,
92 uno::RuntimeException
)
94 // TODO: reimplement TempStream service to support XStream interface
95 uno::Reference
< io::XStream
> xTempStream(
96 m_xFactory
->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.io.TempFile" ) ),
99 if ( !xTempStream
.is() )
100 throw uno::RuntimeException(); // TODO:
102 return uno::Reference
< uno::XInterface
>(
103 static_cast< OWeakObject
* >( new OStorage( xTempStream
,
104 embed::ElementModes::READWRITE
,
105 uno::Sequence
< beans::PropertyValue
>(),
111 //-------------------------------------------------------------------------
112 uno::Reference
< uno::XInterface
> SAL_CALL
OStorageFactory::createInstanceWithArguments(
113 const uno::Sequence
< uno::Any
>& aArguments
)
114 throw ( uno::Exception
,
115 uno::RuntimeException
)
117 // The request for storage can be done with up to three arguments
119 // The first argument specifies a source for the storage
120 // it can be URL, XStream, XInputStream.
121 // The second value is a mode the storage should be open in.
122 // And the third value is a media descriptor.
124 sal_Int32 nArgNum
= aArguments
.getLength();
125 OSL_ENSURE( nArgNum
< 4, "Wrong parameter number" );
128 return createInstance();
130 // first try to retrieve storage open mode if any
131 // by default the storage will be open in readonly mode
132 sal_Int32 nStorageMode
= embed::ElementModes::READ
;
135 if( !( aArguments
[1] >>= nStorageMode
) )
137 OSL_ENSURE( sal_False
, "Wrong second argument!\n" );
138 throw lang::IllegalArgumentException(); // TODO:
140 // it's allways possible to read written storage in this implementation
141 nStorageMode
|= embed::ElementModes::READ
;
144 if ( ( nStorageMode
& embed::ElementModes::TRUNCATE
) == embed::ElementModes::TRUNCATE
145 && ( nStorageMode
& embed::ElementModes::WRITE
) != embed::ElementModes::WRITE
)
146 throw lang::IllegalArgumentException(); // TODO:
148 // retrieve storage source stream
149 ::rtl::OUString aURL
;
150 uno::Reference
< io::XStream
> xStream
;
151 uno::Reference
< io::XInputStream
> xInputStream
;
153 if ( aArguments
[0] >>= aURL
)
155 if ( !aURL
.getLength() )
157 OSL_ENSURE( sal_False
, "Empty URL is provided!\n" );
158 throw lang::IllegalArgumentException(); // TODO:
161 if ( aURL
.equalsIgnoreAsciiCaseAsciiL( "vnd.sun.star.pkg", 16 ) )
163 OSL_ENSURE( sal_False
, "Packages URL's are not valid for storages!\n" ); // ???
164 throw lang::IllegalArgumentException(); // TODO:
167 uno::Reference
< ::com::sun::star::ucb::XSimpleFileAccess
> xTempAccess(
168 m_xFactory
->createInstance (
169 ::rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ),
172 if ( !xTempAccess
.is() )
173 throw uno::RuntimeException(); // TODO:
175 if ( nStorageMode
& embed::ElementModes::WRITE
)
176 xStream
= xTempAccess
->openFileReadWrite( aURL
);
178 xInputStream
= xTempAccess
->openFileRead( aURL
);
180 else if ( !( aArguments
[0] >>= xStream
) && !( aArguments
[0] >>= xInputStream
) )
182 OSL_ENSURE( sal_False
, "Wrong first argument!\n" );
183 throw uno::Exception(); // TODO: Illegal argument
186 // retrieve mediadescriptor and set storage properties
187 uno::Sequence
< beans::PropertyValue
> aDescr
;
188 uno::Sequence
< beans::PropertyValue
> aPropsToSet
;
190 sal_Int16 nStorageType
= PACKAGE_STORAGE
;
194 if( aArguments
[2] >>= aDescr
)
196 if ( aURL
.getLength() )
198 aPropsToSet
.realloc(1);
199 aPropsToSet
[0].Name
= ::rtl::OUString::createFromAscii( "URL" );
200 aPropsToSet
[0].Value
<<= aURL
;
203 for ( sal_Int32 nInd
= 0, nNumArgs
= 1; nInd
< aDescr
.getLength(); nInd
++ )
205 if ( aDescr
[nInd
].Name
.equalsAscii( "InteractionHandler" )
206 || aDescr
[nInd
].Name
.equalsAscii( "Password" )
207 || aDescr
[nInd
].Name
.equalsAscii( "RepairPackage" )
208 || aDescr
[nInd
].Name
.equalsAscii( "StatusIndicator" ) )
209 // || aDescr[nInd].Name.equalsAscii( "Unpacked" ) // TODO:
211 aPropsToSet
.realloc( ++nNumArgs
);
212 aPropsToSet
[nNumArgs
-1].Name
= aDescr
[nInd
].Name
;
213 aPropsToSet
[nNumArgs
-1].Value
= aDescr
[nInd
].Value
;
215 else if ( aDescr
[nInd
].Name
.equalsAscii( "StorageFormat" ) )
217 ::rtl::OUString aFormatName
;
218 aDescr
[nInd
].Value
>>= aFormatName
;
219 if ( aFormatName
.equalsAscii( "PackageFormat" ) )
220 nStorageType
= PACKAGE_STORAGE
;
221 else if ( aFormatName
.equalsAscii( "ZipFormat" ) )
222 nStorageType
= ZIP_STORAGE
;
223 else if ( aFormatName
.equalsAscii( "OFOPXMLFormat" ) )
224 nStorageType
= OFOPXML_STORAGE
;
226 throw lang::IllegalArgumentException(); // TODO:
229 OSL_ENSURE( sal_False
, "Unacceptable property, will be ignored!\n" );
234 OSL_ENSURE( sal_False
, "Wrong third argument!\n" );
235 throw uno::Exception(); // TODO: Illegal argument
240 // create storage based on source
241 if ( xInputStream
.is() )
243 // if xInputStream is set the storage should be open from it
244 if ( ( nStorageMode
& embed::ElementModes::WRITE
) )
245 throw uno::Exception(); // TODO: access denied
247 uno::Reference
< io::XSeekable
> xSeekable( xInputStream
, uno::UNO_QUERY
);
248 if ( !xSeekable
.is() )
250 // TODO: wrap stream to let it be seekable
251 OSL_ENSURE( sal_False
, "Nonseekable streams are not supported for now!\n" );
254 if ( !CheckPackageSignature_Impl( xInputStream
, xSeekable
) )
255 throw io::IOException(); // TODO: this is not a package file
257 return uno::Reference
< uno::XInterface
>(
258 static_cast< OWeakObject
* >( new OStorage( xInputStream
, nStorageMode
, aPropsToSet
, m_xFactory
, nStorageType
) ),
261 else if ( xStream
.is() )
263 if ( ( nStorageMode
& embed::ElementModes::WRITE
) && !xStream
->getOutputStream().is()
264 || !xStream
->getInputStream().is() )
265 throw uno::Exception(); // TODO: access denied
267 uno::Reference
< io::XSeekable
> xSeekable( xStream
, uno::UNO_QUERY
);
268 if ( !xSeekable
.is() )
270 // TODO: wrap stream to let it be seekable
271 OSL_ENSURE( sal_False
, "Nonseekable streams are not supported for now!\n" );
274 if ( !CheckPackageSignature_Impl( xStream
->getInputStream(), xSeekable
) )
275 throw io::IOException(); // TODO: this is not a package file
277 return uno::Reference
< uno::XInterface
>(
278 static_cast< OWeakObject
* >( new OStorage( xStream
, nStorageMode
, aPropsToSet
, m_xFactory
, nStorageType
) ),
282 throw uno::Exception(); // general error during creation
285 //-------------------------------------------------------------------------
286 ::rtl::OUString SAL_CALL
OStorageFactory::getImplementationName()
287 throw ( uno::RuntimeException
)
289 return impl_staticGetImplementationName();
292 //-------------------------------------------------------------------------
293 sal_Bool SAL_CALL
OStorageFactory::supportsService( const ::rtl::OUString
& ServiceName
)
294 throw ( uno::RuntimeException
)
296 uno::Sequence
< ::rtl::OUString
> aSeq
= impl_staticGetSupportedServiceNames();
298 for ( sal_Int32 nInd
= 0; nInd
< aSeq
.getLength(); nInd
++ )
299 if ( ServiceName
.compareTo( aSeq
[nInd
] ) == 0 )
305 //-------------------------------------------------------------------------
306 uno::Sequence
< ::rtl::OUString
> SAL_CALL
OStorageFactory::getSupportedServiceNames()
307 throw ( uno::RuntimeException
)
309 return impl_staticGetSupportedServiceNames();