1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  **************************************************************/
21 
22  // MARKER(update_precomp.py): autogen include statement, do not remove
23 //This file is about the conversion of the UOF v2.0 and ODF document format
24 #ifndef FILTER_SOURCE_XSLTFILTER_UOF2STORAGE_HXX
25 #define FILTER_SOURCE_XSLTFILTER_UOF2STORAGE_HXX
26 
27 #include "containerhelper.hxx"
28 
29 #include <vector>
30 
31 #include <com/sun/star/uno/Reference.h>
32 #include <rtl/ustring.hxx>
33 
34 namespace com { namespace sun { namespace star {
35 	namespace embed { class XStorage; }
36 	namespace io {
37 		class XInputStream;
38 		class XOutputStream;
39 		class XStream;
40 	}
41 	namespace lang{
42 		class XMultiServiceFactory;
43 	}
44 }}}
45 
46 namespace XSLT{
47 
48 const ::rtl::OUString METAELEMNAME = ::rtl::OUString::createFromAscii("_meta/meta.xml");
49 const ::rtl::OUString UOFELEMNAME = ::rtl::OUString::createFromAscii("uof.xml");
50 const ::rtl::OUString CONTENTELEMNAME = ::rtl::OUString::createFromAscii("content.xml");
51 
52 class StorageBase;
53 typedef ::boost::shared_ptr< StorageBase > StorageRef;
54 
55 class StorageBase
56 {
57 public:
58 	explicit StorageBase(
59 		const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& rxInStream,
60 		bool bBaseStreamAccess);
61 
62 	explicit StorageBase(
63 		const ::com::sun::star::uno::Reference< ::com::sun::star::io::XStream >& rxOutStream,
64 		bool bBaseStreamAccess);
65 
66 	virtual ~StorageBase();
67 
68 	/************************************************************************
69 	* Returns true, if the object represents a valid storage
70 	************************************************************************/
71 	bool isStorage() const;
72 
73 	/************************************************************************
74 	* Returns true, if the object represent the root storage
75 	************************************************************************/
76 	bool isRootStorage()const;
77 
78 	/************************************************************************
79 	* Returns true, if the storage operates in read-only mode(based on an
80 	* input stream
81 	************************************************************************/
82 	bool isReadOnly()const;
83 
84 	/************************************************************************
85 	* Returns the com.sun.star.embed.XStorage interface of the current storage
86 	************************************************************************/
87 	::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >
88 		getXStorage() const;
89 
90 	/** Returns the element name of this storage */
91 	const ::rtl::OUString& getName() const;
92 
93 	::rtl::OUString getPath() const;
94 
95 	void getElementNames( ::std::vector< ::rtl::OUString >& orElementNames ) const;
96 
97 	StorageRef openSubStorage( const ::rtl::OUString& rStorageName, bool bCreatedMissing );
98 
99 	::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >
100 		openInputStream(const ::rtl::OUString& rStreamName);
101 
102 	::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >
103 		openOutputStream(const ::rtl::OUString& rStreamName);
104 
105 	void copyToStorage(StorageBase& rDestStrg, const ::rtl::OUString& rElementName );
106 
107 	void copyStorageToStorage(StorageBase& rDestStrg);
108 
109 	void commit();
110 protected:
111 	explicit StorageBase( const StorageBase& rParentStorage, const ::rtl::OUString& rStorageName, bool bReadOnly );
112 private:
113 	StorageBase( const StorageBase& );
114 	StorageBase& operator = (const StorageBase& );
115 
116 	virtual bool implIsStorage() const = 0;
117 
118 	virtual ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >
119 		implGetXStorage() const = 0;
120 
121 	virtual void implGetElementNames( ::std::vector< ::rtl::OUString >& orElementNames ) const = 0;
122 
123 	virtual StorageRef implOpenSubStorage( const ::rtl::OUString& rElementName, bool bCreate ) = 0;
124 
125 	virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >
126 		implOpenInputStream( const ::rtl::OUString& rElementName ) = 0;
127 
128 	virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >
129 		implOpenOutputStream( const ::rtl::OUString& rElementName ) = 0;
130 
131 	virtual void implCommit() const = 0;
132 
133 	StorageRef getSubStorage( const ::rtl::OUString& rElementName, bool bCreateMissing );
134 private:
135 	typedef RefMap< ::rtl::OUString, StorageBase > SubStorageMap;
136 
137 	SubStorageMap m_aSubStorages;///Map of direct sub storages.
138 	::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > m_xInStream;///Cached base input stream(to keep it alive)
139 	::com::sun::star::uno::Reference< ::com::sun::star::io::XStream > m_xOutStream;/// Cached base output stream(to keep it alive)
140 	::rtl::OUString m_aParentPath;///Full path of parent storage
141 	::rtl::OUString m_aStorageName;///Name of this storage, if it is a substorage
142 	bool m_bBaseStreamAccess;///True = access base streams with empty stream name.
143 	bool m_bReadOnly; ///True = storage opened read-only (based on input stream)
144 };
145 
146 class ZipStorage : public StorageBase
147 {
148 public:
149 	explicit ZipStorage(
150 		const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxFactory,
151 		const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& rxInStream);
152 
153 	explicit ZipStorage(
154 		const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxFactory,
155 		const ::com::sun::star::uno::Reference< ::com::sun::star::io::XStream >& rxStream);
156 
157 	virtual ~ZipStorage();
158 
159 private:
160 	explicit ZipStorage(
161 		const ZipStorage& rParentStorage,
162 		const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& rxStorage,
163 		const ::rtl::OUString& rElementName);
164 
165 	virtual bool implIsStorage() const;
166 
167 	virtual ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >
168 		implGetXStorage() const;
169 
170 	virtual void implGetElementNames( ::std::vector< ::rtl::OUString >& orElementNames ) const;
171 
172 	virtual StorageRef implOpenSubStorage( const ::rtl::OUString& rElementName, bool bCreatedMissing );
173 
174 	virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >
175 		implOpenInputStream( const ::rtl::OUString& rElementName );
176 
177 	virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >
178 		implOpenOutputStream( const ::rtl::OUString& rElementName );
179 
180 	virtual void implCommit() const;
181 
182 private:
183 	typedef ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage > XStorageRef;
184 	XStorageRef m_xStorage;
185 };
186 
187 class UOF2Storage
188 {
189 public:
190 	explicit UOF2Storage( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxFactory,
191 		const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& rxInStream );
192 
193 	explicit UOF2Storage( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxFactory,
194 		const ::com::sun::star::uno::Reference< ::com::sun::star::io::XStream >& rxOutStream );
195 
196 	~UOF2Storage();
197 
198 	::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > getMetaInputStream() const;
199 	::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > getMetaOutputStream(bool bMissingCreate);
200 
201 	bool isValidUOF2Doc() const;// just for UOF2 document import
202 
getMainStorageRef()203 	StorageRef getMainStorageRef(){ return m_pMainStorage; }
204 private:
205 	StorageRef m_pMainStorage;
206 };
207 
208 }
209 #endif
210