xref: /AOO41X/main/package/qa/storages/TestHelper.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir package complex.storages;
2*cdf0e10cSrcweir 
3*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
4*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
5*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
6*cdf0e10cSrcweir 
7*cdf0e10cSrcweir import com.sun.star.lang.*;
8*cdf0e10cSrcweir import com.sun.star.embed.*;
9*cdf0e10cSrcweir import com.sun.star.packages.*;
10*cdf0e10cSrcweir import com.sun.star.io.*;
11*cdf0e10cSrcweir import com.sun.star.beans.*;
12*cdf0e10cSrcweir 
13*cdf0e10cSrcweir import share.LogWriter;
14*cdf0e10cSrcweir 
15*cdf0e10cSrcweir public class TestHelper  {
16*cdf0e10cSrcweir 
17*cdf0e10cSrcweir 	LogWriter m_aLogWriter;
18*cdf0e10cSrcweir 	String m_sTestPrefix;
19*cdf0e10cSrcweir 
20*cdf0e10cSrcweir 	public TestHelper( LogWriter aLogWriter, String sTestPrefix )
21*cdf0e10cSrcweir 	{
22*cdf0e10cSrcweir 		m_aLogWriter = aLogWriter;
23*cdf0e10cSrcweir 		m_sTestPrefix = sTestPrefix;
24*cdf0e10cSrcweir 	}
25*cdf0e10cSrcweir 
26*cdf0e10cSrcweir 	public boolean WriteBytesToStream( XStream xStream,
27*cdf0e10cSrcweir 										String sStreamName,
28*cdf0e10cSrcweir 										String sMediaType,
29*cdf0e10cSrcweir 										boolean bCompressed,
30*cdf0e10cSrcweir 										byte[] pBytes )
31*cdf0e10cSrcweir 	{
32*cdf0e10cSrcweir 		// get output stream of substream
33*cdf0e10cSrcweir 		XOutputStream xOutput = xStream.getOutputStream();
34*cdf0e10cSrcweir 		if ( xOutput == null )
35*cdf0e10cSrcweir 		{
36*cdf0e10cSrcweir 			Error( "Can't get XOutputStream implementation from substream '" + sStreamName + "'!" );
37*cdf0e10cSrcweir 			return false;
38*cdf0e10cSrcweir 		}
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir 		// get XTrucate implementation from output stream
41*cdf0e10cSrcweir 		XTruncate xTruncate = (XTruncate) UnoRuntime.queryInterface( XTruncate.class, xOutput );
42*cdf0e10cSrcweir 		if ( xTruncate == null )
43*cdf0e10cSrcweir 		{
44*cdf0e10cSrcweir 			Error( "Can't get XTruncate implementation from substream '" + sStreamName + "'!" );
45*cdf0e10cSrcweir 			return false;
46*cdf0e10cSrcweir 		}
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir 		// write requested byte sequence
49*cdf0e10cSrcweir 		try
50*cdf0e10cSrcweir 		{
51*cdf0e10cSrcweir 			xTruncate.truncate();
52*cdf0e10cSrcweir 			xOutput.writeBytes( pBytes );
53*cdf0e10cSrcweir 		}
54*cdf0e10cSrcweir 		catch( Exception e )
55*cdf0e10cSrcweir 		{
56*cdf0e10cSrcweir 			Error( "Can't write to stream '" + sStreamName + "', exception: " + e );
57*cdf0e10cSrcweir 			return false;
58*cdf0e10cSrcweir 		}
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir 		// get access to the XPropertySet interface
61*cdf0e10cSrcweir 		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStream );
62*cdf0e10cSrcweir 		if ( xPropSet == null )
63*cdf0e10cSrcweir 		{
64*cdf0e10cSrcweir 			Error( "Can't get XPropertySet implementation from substream '" + sStreamName + "'!" );
65*cdf0e10cSrcweir 			return false;
66*cdf0e10cSrcweir 		}
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir 		// set properties to the stream
69*cdf0e10cSrcweir 		try
70*cdf0e10cSrcweir 		{
71*cdf0e10cSrcweir 			xPropSet.setPropertyValue( "MediaType", sMediaType );
72*cdf0e10cSrcweir 			xPropSet.setPropertyValue( "Compressed", new Boolean( bCompressed ) );
73*cdf0e10cSrcweir 		}
74*cdf0e10cSrcweir 		catch( Exception e )
75*cdf0e10cSrcweir 		{
76*cdf0e10cSrcweir 			Error( "Can't set properties to substream '" + sStreamName + "', exception: " + e );
77*cdf0e10cSrcweir 			return false;
78*cdf0e10cSrcweir 		}
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir 		// check size property of the stream
81*cdf0e10cSrcweir 		try
82*cdf0e10cSrcweir 		{
83*cdf0e10cSrcweir 			int nSize = AnyConverter.toInt( xPropSet.getPropertyValue( "Size" ) );
84*cdf0e10cSrcweir 			if ( nSize != pBytes.length )
85*cdf0e10cSrcweir 			{
86*cdf0e10cSrcweir 				Error( "The 'Size' property of substream '" + sStreamName + "' contains wrong value!" );
87*cdf0e10cSrcweir 				return false;
88*cdf0e10cSrcweir 			}
89*cdf0e10cSrcweir 		}
90*cdf0e10cSrcweir 		catch( Exception e )
91*cdf0e10cSrcweir 		{
92*cdf0e10cSrcweir 			Error( "Can't get 'Size' property from substream '" + sStreamName + "', exception: " + e );
93*cdf0e10cSrcweir 			return false;
94*cdf0e10cSrcweir 		}
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir 		return true;
97*cdf0e10cSrcweir 	}
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir 	public boolean WriteBytesToSubstreamDefaultCompressed( XStorage xStorage,
100*cdf0e10cSrcweir 										                    String sStreamName,
101*cdf0e10cSrcweir 										                    String sMediaType,
102*cdf0e10cSrcweir 										                    byte[] pBytes )
103*cdf0e10cSrcweir 	{
104*cdf0e10cSrcweir 		// open substream element
105*cdf0e10cSrcweir 		XStream xSubStream = null;
106*cdf0e10cSrcweir 		try
107*cdf0e10cSrcweir 		{
108*cdf0e10cSrcweir 			Object oSubStream = xStorage.openStreamElement( sStreamName, ElementModes.WRITE );
109*cdf0e10cSrcweir 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
110*cdf0e10cSrcweir 			if ( xSubStream == null )
111*cdf0e10cSrcweir 			{
112*cdf0e10cSrcweir 				Error( "Can't create substream '" + sStreamName + "'!" );
113*cdf0e10cSrcweir 				return false;
114*cdf0e10cSrcweir 			}
115*cdf0e10cSrcweir 		}
116*cdf0e10cSrcweir 		catch( Exception e )
117*cdf0e10cSrcweir 		{
118*cdf0e10cSrcweir 			Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
119*cdf0e10cSrcweir 			return false;
120*cdf0e10cSrcweir 		}
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir 		// get output stream of substream
123*cdf0e10cSrcweir 		XOutputStream xOutput = xSubStream.getOutputStream();
124*cdf0e10cSrcweir 		if ( xOutput == null )
125*cdf0e10cSrcweir 		{
126*cdf0e10cSrcweir 			Error( "Can't get XOutputStream implementation from substream '" + sStreamName + "'!" );
127*cdf0e10cSrcweir 			return false;
128*cdf0e10cSrcweir 		}
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir 		// get XTrucate implementation from output stream
131*cdf0e10cSrcweir 		XTruncate xTruncate = (XTruncate) UnoRuntime.queryInterface( XTruncate.class, xOutput );
132*cdf0e10cSrcweir 		if ( xTruncate == null )
133*cdf0e10cSrcweir 		{
134*cdf0e10cSrcweir 			Error( "Can't get XTruncate implementation from substream '" + sStreamName + "'!" );
135*cdf0e10cSrcweir 			return false;
136*cdf0e10cSrcweir 		}
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir 		// write requested byte sequence
139*cdf0e10cSrcweir 		try
140*cdf0e10cSrcweir 		{
141*cdf0e10cSrcweir 			xTruncate.truncate();
142*cdf0e10cSrcweir 			xOutput.writeBytes( pBytes );
143*cdf0e10cSrcweir 		}
144*cdf0e10cSrcweir 		catch( Exception e )
145*cdf0e10cSrcweir 		{
146*cdf0e10cSrcweir 			Error( "Can't write to stream '" + sStreamName + "', exception: " + e );
147*cdf0e10cSrcweir 			return false;
148*cdf0e10cSrcweir 		}
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir 		// get access to the XPropertySet interface
151*cdf0e10cSrcweir 		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xSubStream );
152*cdf0e10cSrcweir 		if ( xPropSet == null )
153*cdf0e10cSrcweir 		{
154*cdf0e10cSrcweir 			Error( "Can't get XPropertySet implementation from substream '" + sStreamName + "'!" );
155*cdf0e10cSrcweir 			return false;
156*cdf0e10cSrcweir 		}
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 		// set properties to the stream
159*cdf0e10cSrcweir         // do not set the compressed property
160*cdf0e10cSrcweir 		try
161*cdf0e10cSrcweir 		{
162*cdf0e10cSrcweir 			xPropSet.setPropertyValue( "MediaType", sMediaType );
163*cdf0e10cSrcweir 		}
164*cdf0e10cSrcweir 		catch( Exception e )
165*cdf0e10cSrcweir 		{
166*cdf0e10cSrcweir 			Error( "Can't set properties to substream '" + sStreamName + "', exception: " + e );
167*cdf0e10cSrcweir 			return false;
168*cdf0e10cSrcweir 		}
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 		// check size property of the stream
171*cdf0e10cSrcweir 		try
172*cdf0e10cSrcweir 		{
173*cdf0e10cSrcweir 			int nSize = AnyConverter.toInt( xPropSet.getPropertyValue( "Size" ) );
174*cdf0e10cSrcweir 			if ( nSize != pBytes.length )
175*cdf0e10cSrcweir 			{
176*cdf0e10cSrcweir 				Error( "The 'Size' property of substream '" + sStreamName + "' contains wrong value!" );
177*cdf0e10cSrcweir 				return false;
178*cdf0e10cSrcweir 			}
179*cdf0e10cSrcweir 		}
180*cdf0e10cSrcweir 		catch( Exception e )
181*cdf0e10cSrcweir 		{
182*cdf0e10cSrcweir 			Error( "Can't get 'Size' property from substream '" + sStreamName + "', exception: " + e );
183*cdf0e10cSrcweir 			return false;
184*cdf0e10cSrcweir 		}
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir 		// free the stream resources, garbage collector may remove the object too late
187*cdf0e10cSrcweir 		if ( !disposeStream( xSubStream, sStreamName ) )
188*cdf0e10cSrcweir 			return false;
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 		return true;
191*cdf0e10cSrcweir 	}
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 	public boolean WriteBytesToSubstream( XStorage xStorage,
194*cdf0e10cSrcweir 										  String sStreamName,
195*cdf0e10cSrcweir 										  String sMediaType,
196*cdf0e10cSrcweir 										  boolean bCompressed,
197*cdf0e10cSrcweir 										  byte[] pBytes )
198*cdf0e10cSrcweir 	{
199*cdf0e10cSrcweir 		// open substream element
200*cdf0e10cSrcweir 		XStream xSubStream = null;
201*cdf0e10cSrcweir 		try
202*cdf0e10cSrcweir 		{
203*cdf0e10cSrcweir 			Object oSubStream = xStorage.openStreamElement( sStreamName, ElementModes.WRITE );
204*cdf0e10cSrcweir 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
205*cdf0e10cSrcweir 			if ( xSubStream == null )
206*cdf0e10cSrcweir 			{
207*cdf0e10cSrcweir 				Error( "Can't create substream '" + sStreamName + "'!" );
208*cdf0e10cSrcweir 				return false;
209*cdf0e10cSrcweir 			}
210*cdf0e10cSrcweir 		}
211*cdf0e10cSrcweir 		catch( Exception e )
212*cdf0e10cSrcweir 		{
213*cdf0e10cSrcweir 			Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
214*cdf0e10cSrcweir 			return false;
215*cdf0e10cSrcweir 		}
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir 		if ( !WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes ) )
218*cdf0e10cSrcweir 			return false;
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir 		// free the stream resources, garbage collector may remove the object too late
221*cdf0e10cSrcweir 		if ( !disposeStream( xSubStream, sStreamName ) )
222*cdf0e10cSrcweir 			return false;
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir 		return true;
225*cdf0e10cSrcweir 	}
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir 	public boolean WriteBytesToEncrSubstream( XStorage xStorage,
228*cdf0e10cSrcweir 											  String sStreamName,
229*cdf0e10cSrcweir 											  String sMediaType,
230*cdf0e10cSrcweir 											  boolean bCompressed,
231*cdf0e10cSrcweir 											  byte[] pBytes,
232*cdf0e10cSrcweir 											  String sPass )
233*cdf0e10cSrcweir 	{
234*cdf0e10cSrcweir 		// open substream element
235*cdf0e10cSrcweir 		XStream xSubStream = null;
236*cdf0e10cSrcweir 		try
237*cdf0e10cSrcweir 		{
238*cdf0e10cSrcweir 			Object oSubStream = xStorage.openEncryptedStreamElement( sStreamName, ElementModes.WRITE, sPass );
239*cdf0e10cSrcweir 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
240*cdf0e10cSrcweir 			if ( xSubStream == null )
241*cdf0e10cSrcweir 			{
242*cdf0e10cSrcweir 				Error( "Can't create substream '" + sStreamName + "'!" );
243*cdf0e10cSrcweir 				return false;
244*cdf0e10cSrcweir 			}
245*cdf0e10cSrcweir 		}
246*cdf0e10cSrcweir 		catch( Exception e )
247*cdf0e10cSrcweir 		{
248*cdf0e10cSrcweir 			Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
249*cdf0e10cSrcweir 			return false;
250*cdf0e10cSrcweir 		}
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir 		if ( !WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes ) )
253*cdf0e10cSrcweir 			return false;
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir 		// free the stream resources, garbage collector may remove the object too late
256*cdf0e10cSrcweir 		if ( !disposeStream( xSubStream, sStreamName ) )
257*cdf0e10cSrcweir 			return false;
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir 		return true;
260*cdf0e10cSrcweir 	}
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir 	public boolean WBToSubstrOfEncr( XStorage xStorage,
263*cdf0e10cSrcweir 											  String sStreamName,
264*cdf0e10cSrcweir 											  String sMediaType,
265*cdf0e10cSrcweir 											  boolean bCompressed,
266*cdf0e10cSrcweir 											  byte[] pBytes,
267*cdf0e10cSrcweir 											  boolean bEncrypted )
268*cdf0e10cSrcweir 	{
269*cdf0e10cSrcweir 		// open substream element
270*cdf0e10cSrcweir 		XStream xSubStream = null;
271*cdf0e10cSrcweir 		try
272*cdf0e10cSrcweir 		{
273*cdf0e10cSrcweir 			Object oSubStream = xStorage.openStreamElement( sStreamName, ElementModes.WRITE );
274*cdf0e10cSrcweir 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
275*cdf0e10cSrcweir 			if ( xSubStream == null )
276*cdf0e10cSrcweir 			{
277*cdf0e10cSrcweir 				Error( "Can't create substream '" + sStreamName + "'!" );
278*cdf0e10cSrcweir 				return false;
279*cdf0e10cSrcweir 			}
280*cdf0e10cSrcweir 		}
281*cdf0e10cSrcweir 		catch( Exception e )
282*cdf0e10cSrcweir 		{
283*cdf0e10cSrcweir 			Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
284*cdf0e10cSrcweir 			return false;
285*cdf0e10cSrcweir 		}
286*cdf0e10cSrcweir 
287*cdf0e10cSrcweir 		// get access to the XPropertySet interface
288*cdf0e10cSrcweir 		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xSubStream );
289*cdf0e10cSrcweir 		if ( xPropSet == null )
290*cdf0e10cSrcweir 		{
291*cdf0e10cSrcweir 			Error( "Can't get XPropertySet implementation from substream '" + sStreamName + "'!" );
292*cdf0e10cSrcweir 			return false;
293*cdf0e10cSrcweir 		}
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir 		// set properties to the stream
296*cdf0e10cSrcweir 		try
297*cdf0e10cSrcweir 		{
298*cdf0e10cSrcweir 			xPropSet.setPropertyValue( "UseCommonStoragePasswordEncryption", new Boolean( bEncrypted ) );
299*cdf0e10cSrcweir 		}
300*cdf0e10cSrcweir 		catch( Exception e )
301*cdf0e10cSrcweir 		{
302*cdf0e10cSrcweir 			Error( "Can't set 'UseCommonStoragePasswordEncryption' property to substream '" + sStreamName + "', exception: " + e );
303*cdf0e10cSrcweir 			return false;
304*cdf0e10cSrcweir 		}
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir 		if ( !WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes ) )
307*cdf0e10cSrcweir 			return false;
308*cdf0e10cSrcweir 
309*cdf0e10cSrcweir 		// free the stream resources, garbage collector may remove the object too late
310*cdf0e10cSrcweir 		if ( !disposeStream( xSubStream, sStreamName ) )
311*cdf0e10cSrcweir 			return false;
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir 		return true;
314*cdf0e10cSrcweir 	}
315*cdf0e10cSrcweir 
316*cdf0e10cSrcweir 	public boolean WriteBytesToStreamH( XStorage xStorage,
317*cdf0e10cSrcweir 										  String sStreamPath,
318*cdf0e10cSrcweir 										  String sMediaType,
319*cdf0e10cSrcweir 										  boolean bCompressed,
320*cdf0e10cSrcweir 										  byte[] pBytes,
321*cdf0e10cSrcweir 										  boolean bCommit )
322*cdf0e10cSrcweir 	{
323*cdf0e10cSrcweir 		// open substream element
324*cdf0e10cSrcweir 		XStream xSubStream = null;
325*cdf0e10cSrcweir 		try
326*cdf0e10cSrcweir 		{
327*cdf0e10cSrcweir 			XHierarchicalStorageAccess xHStorage =
328*cdf0e10cSrcweir 				(XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage );
329*cdf0e10cSrcweir 			if ( xHStorage == null )
330*cdf0e10cSrcweir 			{
331*cdf0e10cSrcweir 				Error( "The storage does not support hierarchical access!" );
332*cdf0e10cSrcweir 				return false;
333*cdf0e10cSrcweir 			}
334*cdf0e10cSrcweir 
335*cdf0e10cSrcweir 			Object oSubStream = xHStorage.openStreamElementByHierarchicalName( sStreamPath, ElementModes.WRITE );
336*cdf0e10cSrcweir 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
337*cdf0e10cSrcweir 			if ( xSubStream == null )
338*cdf0e10cSrcweir 			{
339*cdf0e10cSrcweir 				Error( "Can't create substream '" + sStreamPath + "'!" );
340*cdf0e10cSrcweir 				return false;
341*cdf0e10cSrcweir 			}
342*cdf0e10cSrcweir 		}
343*cdf0e10cSrcweir 		catch( Exception e )
344*cdf0e10cSrcweir 		{
345*cdf0e10cSrcweir 			Error( "Can't create substream '" + sStreamPath + "', exception : " + e + "!" );
346*cdf0e10cSrcweir 			return false;
347*cdf0e10cSrcweir 		}
348*cdf0e10cSrcweir 
349*cdf0e10cSrcweir 		if ( !WriteBytesToStream( xSubStream, sStreamPath, sMediaType, bCompressed, pBytes ) )
350*cdf0e10cSrcweir 			return false;
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir 		XTransactedObject xTransact =
353*cdf0e10cSrcweir 			(XTransactedObject) UnoRuntime.queryInterface( XTransactedObject.class, xSubStream );
354*cdf0e10cSrcweir 		if ( xTransact == null )
355*cdf0e10cSrcweir 		{
356*cdf0e10cSrcweir 			Error( "Substream '" + sStreamPath + "', stream opened for writing must be transacted!" );
357*cdf0e10cSrcweir 			return false;
358*cdf0e10cSrcweir 		}
359*cdf0e10cSrcweir 
360*cdf0e10cSrcweir 		if ( bCommit )
361*cdf0e10cSrcweir 		{
362*cdf0e10cSrcweir 			try {
363*cdf0e10cSrcweir 				xTransact.commit();
364*cdf0e10cSrcweir 			} catch( Exception e )
365*cdf0e10cSrcweir 			{
366*cdf0e10cSrcweir 				Error( "Can't commit storage after substream '" + sStreamPath + "' change, exception : " + e + "!" );
367*cdf0e10cSrcweir 				return false;
368*cdf0e10cSrcweir 			}
369*cdf0e10cSrcweir 		}
370*cdf0e10cSrcweir 
371*cdf0e10cSrcweir 		// free the stream resources, garbage collector may remove the object too late
372*cdf0e10cSrcweir 		if ( !disposeStream( xSubStream, sStreamPath ) )
373*cdf0e10cSrcweir 			return false;
374*cdf0e10cSrcweir 
375*cdf0e10cSrcweir 		return true;
376*cdf0e10cSrcweir 	}
377*cdf0e10cSrcweir 
378*cdf0e10cSrcweir 	public boolean WriteBytesToEncrStreamH( XStorage xStorage,
379*cdf0e10cSrcweir 										  String sStreamPath,
380*cdf0e10cSrcweir 										  String sMediaType,
381*cdf0e10cSrcweir 										  boolean bCompressed,
382*cdf0e10cSrcweir 										  byte[] pBytes,
383*cdf0e10cSrcweir 										  String sPass,
384*cdf0e10cSrcweir 										  boolean bCommit )
385*cdf0e10cSrcweir 	{
386*cdf0e10cSrcweir 		// open substream element
387*cdf0e10cSrcweir 		XStream xSubStream = null;
388*cdf0e10cSrcweir 		try
389*cdf0e10cSrcweir 		{
390*cdf0e10cSrcweir 			XHierarchicalStorageAccess xHStorage =
391*cdf0e10cSrcweir 				(XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage );
392*cdf0e10cSrcweir 			if ( xHStorage == null )
393*cdf0e10cSrcweir 			{
394*cdf0e10cSrcweir 				Error( "The storage does not support hierarchical access!" );
395*cdf0e10cSrcweir 				return false;
396*cdf0e10cSrcweir 			}
397*cdf0e10cSrcweir 
398*cdf0e10cSrcweir 			Object oSubStream = xHStorage.openEncryptedStreamElementByHierarchicalName( sStreamPath,
399*cdf0e10cSrcweir 																						ElementModes.WRITE,
400*cdf0e10cSrcweir 																						sPass );
401*cdf0e10cSrcweir 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
402*cdf0e10cSrcweir 			if ( xSubStream == null )
403*cdf0e10cSrcweir 			{
404*cdf0e10cSrcweir 				Error( "Can't create substream '" + sStreamPath + "'!" );
405*cdf0e10cSrcweir 				return false;
406*cdf0e10cSrcweir 			}
407*cdf0e10cSrcweir 		}
408*cdf0e10cSrcweir 		catch( Exception e )
409*cdf0e10cSrcweir 		{
410*cdf0e10cSrcweir 			Error( "Can't create substream '" + sStreamPath + "', exception : " + e + "!" );
411*cdf0e10cSrcweir 			return false;
412*cdf0e10cSrcweir 		}
413*cdf0e10cSrcweir 
414*cdf0e10cSrcweir 		if ( !WriteBytesToStream( xSubStream, sStreamPath, sMediaType, bCompressed, pBytes ) )
415*cdf0e10cSrcweir 			return false;
416*cdf0e10cSrcweir 
417*cdf0e10cSrcweir 		XTransactedObject xTransact =
418*cdf0e10cSrcweir 			(XTransactedObject) UnoRuntime.queryInterface( XTransactedObject.class, xSubStream );
419*cdf0e10cSrcweir 		if ( xTransact == null )
420*cdf0e10cSrcweir 		{
421*cdf0e10cSrcweir 			Error( "Substream '" + sStreamPath + "', stream opened for writing must be transacted!" );
422*cdf0e10cSrcweir 			return false;
423*cdf0e10cSrcweir 		}
424*cdf0e10cSrcweir 
425*cdf0e10cSrcweir 		if ( bCommit )
426*cdf0e10cSrcweir 		{
427*cdf0e10cSrcweir 			try {
428*cdf0e10cSrcweir 				xTransact.commit();
429*cdf0e10cSrcweir 			} catch( Exception e )
430*cdf0e10cSrcweir 			{
431*cdf0e10cSrcweir 				Error( "Can't commit storage after substream '" + sStreamPath + "' change, exception : " + e + "!" );
432*cdf0e10cSrcweir 				return false;
433*cdf0e10cSrcweir 			}
434*cdf0e10cSrcweir 		}
435*cdf0e10cSrcweir 
436*cdf0e10cSrcweir 		// free the stream resources, garbage collector may remove the object too late
437*cdf0e10cSrcweir 		if ( !disposeStream( xSubStream, sStreamPath ) )
438*cdf0e10cSrcweir 			return false;
439*cdf0e10cSrcweir 
440*cdf0e10cSrcweir 		return true;
441*cdf0e10cSrcweir 	}
442*cdf0e10cSrcweir 
443*cdf0e10cSrcweir 	public boolean WBToSubstrOfEncrH( XStorage xStorage,
444*cdf0e10cSrcweir 									  String sStreamPath,
445*cdf0e10cSrcweir 									  String sMediaType,
446*cdf0e10cSrcweir 									  boolean bCompressed,
447*cdf0e10cSrcweir 									  byte[] pBytes,
448*cdf0e10cSrcweir 									  boolean bEncrypted,
449*cdf0e10cSrcweir 									  boolean bCommit )
450*cdf0e10cSrcweir 	{
451*cdf0e10cSrcweir 		// open substream element
452*cdf0e10cSrcweir 		XStream xSubStream = null;
453*cdf0e10cSrcweir 		try
454*cdf0e10cSrcweir 		{
455*cdf0e10cSrcweir 			XHierarchicalStorageAccess xHStorage =
456*cdf0e10cSrcweir 				(XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage );
457*cdf0e10cSrcweir 			if ( xHStorage == null )
458*cdf0e10cSrcweir 			{
459*cdf0e10cSrcweir 				Error( "The storage does not support hierarchical access!" );
460*cdf0e10cSrcweir 				return false;
461*cdf0e10cSrcweir 			}
462*cdf0e10cSrcweir 
463*cdf0e10cSrcweir 			Object oSubStream = xHStorage.openStreamElementByHierarchicalName( sStreamPath, ElementModes.WRITE );
464*cdf0e10cSrcweir 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
465*cdf0e10cSrcweir 			if ( xSubStream == null )
466*cdf0e10cSrcweir 			{
467*cdf0e10cSrcweir 				Error( "Can't create substream '" + sStreamPath + "'!" );
468*cdf0e10cSrcweir 				return false;
469*cdf0e10cSrcweir 			}
470*cdf0e10cSrcweir 		}
471*cdf0e10cSrcweir 		catch( Exception e )
472*cdf0e10cSrcweir 		{
473*cdf0e10cSrcweir 			Error( "Can't create substream '" + sStreamPath + "', exception : " + e + "!" );
474*cdf0e10cSrcweir 			return false;
475*cdf0e10cSrcweir 		}
476*cdf0e10cSrcweir 
477*cdf0e10cSrcweir 		// get access to the XPropertySet interface
478*cdf0e10cSrcweir 		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xSubStream );
479*cdf0e10cSrcweir 		if ( xPropSet == null )
480*cdf0e10cSrcweir 		{
481*cdf0e10cSrcweir 			Error( "Can't get XPropertySet implementation from substream '" + sStreamPath + "'!" );
482*cdf0e10cSrcweir 			return false;
483*cdf0e10cSrcweir 		}
484*cdf0e10cSrcweir 
485*cdf0e10cSrcweir 		// set properties to the stream
486*cdf0e10cSrcweir 		try
487*cdf0e10cSrcweir 		{
488*cdf0e10cSrcweir 			xPropSet.setPropertyValue( "UseCommonStoragePasswordEncryption", new Boolean( bEncrypted ) );
489*cdf0e10cSrcweir 		}
490*cdf0e10cSrcweir 		catch( Exception e )
491*cdf0e10cSrcweir 		{
492*cdf0e10cSrcweir 			Error( "Can't set 'UseCommonStoragePasswordEncryption' property to substream '" + sStreamPath + "', exception: " + e );
493*cdf0e10cSrcweir 			return false;
494*cdf0e10cSrcweir 		}
495*cdf0e10cSrcweir 
496*cdf0e10cSrcweir 		if ( !WriteBytesToStream( xSubStream, sStreamPath, sMediaType, bCompressed, pBytes ) )
497*cdf0e10cSrcweir 			return false;
498*cdf0e10cSrcweir 
499*cdf0e10cSrcweir 		XTransactedObject xTransact =
500*cdf0e10cSrcweir 			(XTransactedObject) UnoRuntime.queryInterface( XTransactedObject.class, xSubStream );
501*cdf0e10cSrcweir 		if ( xTransact == null )
502*cdf0e10cSrcweir 		{
503*cdf0e10cSrcweir 			Error( "Substream '" + sStreamPath + "', stream opened for writing must be transacted!" );
504*cdf0e10cSrcweir 			return false;
505*cdf0e10cSrcweir 		}
506*cdf0e10cSrcweir 
507*cdf0e10cSrcweir 		if ( bCommit )
508*cdf0e10cSrcweir 		{
509*cdf0e10cSrcweir 			try {
510*cdf0e10cSrcweir 				xTransact.commit();
511*cdf0e10cSrcweir 			} catch( Exception e )
512*cdf0e10cSrcweir 			{
513*cdf0e10cSrcweir 				Error( "Can't commit storage after substream '" + sStreamPath + "' change, exception : " + e + "!" );
514*cdf0e10cSrcweir 				return false;
515*cdf0e10cSrcweir 			}
516*cdf0e10cSrcweir 		}
517*cdf0e10cSrcweir 
518*cdf0e10cSrcweir 		// free the stream resources, garbage collector may remove the object too late
519*cdf0e10cSrcweir 		if ( !disposeStream( xSubStream, sStreamPath ) )
520*cdf0e10cSrcweir 			return false;
521*cdf0e10cSrcweir 
522*cdf0e10cSrcweir 		return true;
523*cdf0e10cSrcweir 	}
524*cdf0e10cSrcweir 
525*cdf0e10cSrcweir 	public int ChangeStreamPass( XStorage xStorage,
526*cdf0e10cSrcweir 								 String sStreamName,
527*cdf0e10cSrcweir 								 String sOldPass,
528*cdf0e10cSrcweir 								 String sNewPass )
529*cdf0e10cSrcweir 	{
530*cdf0e10cSrcweir 		// open substream element
531*cdf0e10cSrcweir 		XStream xSubStream = null;
532*cdf0e10cSrcweir 		try
533*cdf0e10cSrcweir 		{
534*cdf0e10cSrcweir 			Object oSubStream = xStorage.openEncryptedStreamElement( sStreamName, ElementModes.WRITE, sOldPass );
535*cdf0e10cSrcweir 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
536*cdf0e10cSrcweir 			if ( xSubStream == null )
537*cdf0e10cSrcweir 			{
538*cdf0e10cSrcweir 				Error( "Can't open substream '" + sStreamName + "'!" );
539*cdf0e10cSrcweir 				return 0;
540*cdf0e10cSrcweir 			}
541*cdf0e10cSrcweir 		}
542*cdf0e10cSrcweir 		catch( Exception e )
543*cdf0e10cSrcweir 		{
544*cdf0e10cSrcweir 			Error( "Can't open substream '" + sStreamName + "', exception : " + e + "!" );
545*cdf0e10cSrcweir 			return 0;
546*cdf0e10cSrcweir 		}
547*cdf0e10cSrcweir 
548*cdf0e10cSrcweir 
549*cdf0e10cSrcweir 		// change the password for the stream
550*cdf0e10cSrcweir 		XEncryptionProtectedSource xStreamEncryption =
551*cdf0e10cSrcweir 				(XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xSubStream );
552*cdf0e10cSrcweir 
553*cdf0e10cSrcweir 		if ( xStreamEncryption == null )
554*cdf0e10cSrcweir 		{
555*cdf0e10cSrcweir 			Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
556*cdf0e10cSrcweir 			return -1;
557*cdf0e10cSrcweir 		}
558*cdf0e10cSrcweir 
559*cdf0e10cSrcweir 		try {
560*cdf0e10cSrcweir 			xStreamEncryption.setEncryptionPassword( sNewPass );
561*cdf0e10cSrcweir 		}
562*cdf0e10cSrcweir 		catch( Exception e )
563*cdf0e10cSrcweir 		{
564*cdf0e10cSrcweir 			Error( "Can't change encryption key of the substream '" + sStreamName + "', exception:" + e );
565*cdf0e10cSrcweir 			return 0;
566*cdf0e10cSrcweir 		}
567*cdf0e10cSrcweir 
568*cdf0e10cSrcweir 		// free the stream resources, garbage collector may remove the object too late
569*cdf0e10cSrcweir 		if ( !disposeStream( xSubStream, sStreamName ) )
570*cdf0e10cSrcweir 			return 0;
571*cdf0e10cSrcweir 
572*cdf0e10cSrcweir 		return 1;
573*cdf0e10cSrcweir 	}
574*cdf0e10cSrcweir 
575*cdf0e10cSrcweir 	public int ChangeStreamPassH( XStorage xStorage,
576*cdf0e10cSrcweir 								 String sPath,
577*cdf0e10cSrcweir 								 String sOldPass,
578*cdf0e10cSrcweir 								 String sNewPass,
579*cdf0e10cSrcweir 								 boolean bCommit )
580*cdf0e10cSrcweir 	{
581*cdf0e10cSrcweir 		// open substream element
582*cdf0e10cSrcweir 		XHierarchicalStorageAccess xHStorage =
583*cdf0e10cSrcweir 			(XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage );
584*cdf0e10cSrcweir 		if ( xHStorage == null )
585*cdf0e10cSrcweir 		{
586*cdf0e10cSrcweir 			Error( "The storage does not support hierarchical access!" );
587*cdf0e10cSrcweir 			return 0;
588*cdf0e10cSrcweir 		}
589*cdf0e10cSrcweir 
590*cdf0e10cSrcweir 		XStream xSubStream = null;
591*cdf0e10cSrcweir 		try
592*cdf0e10cSrcweir 		{
593*cdf0e10cSrcweir 			Object oSubStream = xHStorage.openEncryptedStreamElementByHierarchicalName( sPath, ElementModes.WRITE, sOldPass );
594*cdf0e10cSrcweir 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
595*cdf0e10cSrcweir 			if ( xSubStream == null )
596*cdf0e10cSrcweir 			{
597*cdf0e10cSrcweir 				Error( "Can't open encrypted substream '" + sPath + "'!" );
598*cdf0e10cSrcweir 				return 0;
599*cdf0e10cSrcweir 			}
600*cdf0e10cSrcweir 		}
601*cdf0e10cSrcweir 		catch( Exception e )
602*cdf0e10cSrcweir 		{
603*cdf0e10cSrcweir 			Error( "Can't open encrypted substream '" + sPath + "', exception : " + e + "!" );
604*cdf0e10cSrcweir 			return 0;
605*cdf0e10cSrcweir 		}
606*cdf0e10cSrcweir 
607*cdf0e10cSrcweir 		// change the password for the stream
608*cdf0e10cSrcweir 		XEncryptionProtectedSource xStreamEncryption =
609*cdf0e10cSrcweir 				(XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xSubStream );
610*cdf0e10cSrcweir 
611*cdf0e10cSrcweir 		if ( xStreamEncryption == null )
612*cdf0e10cSrcweir 		{
613*cdf0e10cSrcweir 			Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" );
614*cdf0e10cSrcweir 			return -1;
615*cdf0e10cSrcweir 		}
616*cdf0e10cSrcweir 
617*cdf0e10cSrcweir 		try {
618*cdf0e10cSrcweir 			xStreamEncryption.setEncryptionPassword( sNewPass );
619*cdf0e10cSrcweir 		}
620*cdf0e10cSrcweir 		catch( Exception e )
621*cdf0e10cSrcweir 		{
622*cdf0e10cSrcweir 			Error( "Can't change encryption key of the substream '" + sPath + "', exception:" + e );
623*cdf0e10cSrcweir 			return 0;
624*cdf0e10cSrcweir 		}
625*cdf0e10cSrcweir 
626*cdf0e10cSrcweir 		XTransactedObject xTransact =
627*cdf0e10cSrcweir 			(XTransactedObject) UnoRuntime.queryInterface( XTransactedObject.class, xSubStream );
628*cdf0e10cSrcweir 		if ( xTransact == null )
629*cdf0e10cSrcweir 		{
630*cdf0e10cSrcweir 			Error( "Substream '" + sPath + "', stream opened for writing must be transacted!" );
631*cdf0e10cSrcweir 			return 0;
632*cdf0e10cSrcweir 		}
633*cdf0e10cSrcweir 
634*cdf0e10cSrcweir 		if ( bCommit )
635*cdf0e10cSrcweir 		{
636*cdf0e10cSrcweir 			try {
637*cdf0e10cSrcweir 				xTransact.commit();
638*cdf0e10cSrcweir 			} catch( Exception e )
639*cdf0e10cSrcweir 			{
640*cdf0e10cSrcweir 				Error( "Can't commit storage after substream '" + sPath + "' change, exception : " + e + "!" );
641*cdf0e10cSrcweir 				return 0;
642*cdf0e10cSrcweir 			}
643*cdf0e10cSrcweir 		}
644*cdf0e10cSrcweir 
645*cdf0e10cSrcweir 		// free the stream resources, garbage collector may remove the object too late
646*cdf0e10cSrcweir 		if ( !disposeStream( xSubStream, sPath ) )
647*cdf0e10cSrcweir 			return 0;
648*cdf0e10cSrcweir 
649*cdf0e10cSrcweir 		return 1;
650*cdf0e10cSrcweir 	}
651*cdf0e10cSrcweir 
652*cdf0e10cSrcweir 	public boolean setStorageTypeAndCheckProps( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )
653*cdf0e10cSrcweir 	{
654*cdf0e10cSrcweir 		boolean bOk = false;
655*cdf0e10cSrcweir 
656*cdf0e10cSrcweir 		// get access to the XPropertySet interface
657*cdf0e10cSrcweir 		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStorage );
658*cdf0e10cSrcweir 		if ( xPropSet != null )
659*cdf0e10cSrcweir 		{
660*cdf0e10cSrcweir 			try
661*cdf0e10cSrcweir 			{
662*cdf0e10cSrcweir 				// set "MediaType" property to the stream
663*cdf0e10cSrcweir 				xPropSet.setPropertyValue( "MediaType", sMediaType );
664*cdf0e10cSrcweir 
665*cdf0e10cSrcweir 				// get "IsRoot" and "OpenMode" properties and control there values
666*cdf0e10cSrcweir 				boolean bPropIsRoot = AnyConverter.toBoolean( xPropSet.getPropertyValue( "IsRoot" ) );
667*cdf0e10cSrcweir 				int nPropMode = AnyConverter.toInt( xPropSet.getPropertyValue( "OpenMode" ) );
668*cdf0e10cSrcweir 
669*cdf0e10cSrcweir 				bOk = true;
670*cdf0e10cSrcweir 				if ( bPropIsRoot != bIsRoot )
671*cdf0e10cSrcweir 				{
672*cdf0e10cSrcweir 					Error( "'IsRoot' property contains wrong value!" );
673*cdf0e10cSrcweir 					bOk = false;
674*cdf0e10cSrcweir 				}
675*cdf0e10cSrcweir 
676*cdf0e10cSrcweir 				if ( ( bIsRoot
677*cdf0e10cSrcweir 				  && ( nPropMode | ElementModes.READ ) != ( nMode | ElementModes.READ ) )
678*cdf0e10cSrcweir 				  || ( !bIsRoot && ( nPropMode & nMode ) != nMode ) )
679*cdf0e10cSrcweir 				{
680*cdf0e10cSrcweir 					Error( "'OpenMode' property contains wrong value, expected " + nMode + ", in reality " + nPropMode + "!" );
681*cdf0e10cSrcweir 					bOk = false;
682*cdf0e10cSrcweir 				}
683*cdf0e10cSrcweir 			}
684*cdf0e10cSrcweir 			catch( Exception e )
685*cdf0e10cSrcweir 			{
686*cdf0e10cSrcweir 				Error( "Can't control properties of substorage, exception: " + e );
687*cdf0e10cSrcweir 			}
688*cdf0e10cSrcweir 		}
689*cdf0e10cSrcweir 		else
690*cdf0e10cSrcweir 		{
691*cdf0e10cSrcweir 			Error( "Can't get XPropertySet implementation from storage!" );
692*cdf0e10cSrcweir 		}
693*cdf0e10cSrcweir 
694*cdf0e10cSrcweir 		return bOk;
695*cdf0e10cSrcweir 	}
696*cdf0e10cSrcweir 
697*cdf0e10cSrcweir 	public boolean checkStorageProperties( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )
698*cdf0e10cSrcweir 	{
699*cdf0e10cSrcweir 		boolean bOk = false;
700*cdf0e10cSrcweir 
701*cdf0e10cSrcweir 		// get access to the XPropertySet interface
702*cdf0e10cSrcweir 		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStorage );
703*cdf0e10cSrcweir 		if ( xPropSet != null )
704*cdf0e10cSrcweir 		{
705*cdf0e10cSrcweir 			try
706*cdf0e10cSrcweir 			{
707*cdf0e10cSrcweir 				// get "MediaType", "IsRoot" and "OpenMode" properties and control there values
708*cdf0e10cSrcweir 				String sPropMediaType = AnyConverter.toString( xPropSet.getPropertyValue( "MediaType" ) );
709*cdf0e10cSrcweir 				boolean bPropIsRoot = AnyConverter.toBoolean( xPropSet.getPropertyValue( "IsRoot" ) );
710*cdf0e10cSrcweir 				int nPropMode = AnyConverter.toInt( xPropSet.getPropertyValue( "OpenMode" ) );
711*cdf0e10cSrcweir 
712*cdf0e10cSrcweir 				bOk = true;
713*cdf0e10cSrcweir 				if ( !sPropMediaType.equals( sMediaType ) )
714*cdf0e10cSrcweir 				{
715*cdf0e10cSrcweir 					Error( "'MediaType' property contains wrong value, expected '"
716*cdf0e10cSrcweir 							+ sMediaType + "', set '" + sPropMediaType + "' !" );
717*cdf0e10cSrcweir 					bOk = false;
718*cdf0e10cSrcweir 				}
719*cdf0e10cSrcweir 
720*cdf0e10cSrcweir 				if ( bPropIsRoot != bIsRoot )
721*cdf0e10cSrcweir 				{
722*cdf0e10cSrcweir 					Error( "'IsRoot' property contains wrong value!" );
723*cdf0e10cSrcweir 					bOk = false;
724*cdf0e10cSrcweir 				}
725*cdf0e10cSrcweir 
726*cdf0e10cSrcweir 				if ( ( bIsRoot
727*cdf0e10cSrcweir 				  && ( nPropMode | ElementModes.READ ) != ( nMode | ElementModes.READ ) )
728*cdf0e10cSrcweir 				  || ( !bIsRoot && ( nPropMode & nMode ) != nMode ) )
729*cdf0e10cSrcweir 				{
730*cdf0e10cSrcweir 					Error( "'OpenMode' property contains wrong value, expected " + nMode + ", in reality " + nPropMode + "!" );
731*cdf0e10cSrcweir 					bOk = false;
732*cdf0e10cSrcweir 				}
733*cdf0e10cSrcweir 			}
734*cdf0e10cSrcweir 			catch( Exception e )
735*cdf0e10cSrcweir 			{
736*cdf0e10cSrcweir 				Error( "Can't get properties of substorage, exception: " + e );
737*cdf0e10cSrcweir 			}
738*cdf0e10cSrcweir 		}
739*cdf0e10cSrcweir 		else
740*cdf0e10cSrcweir 		{
741*cdf0e10cSrcweir 			Error( "Can't get XPropertySet implementation from storage!" );
742*cdf0e10cSrcweir 		}
743*cdf0e10cSrcweir 
744*cdf0e10cSrcweir 		return bOk;
745*cdf0e10cSrcweir 	}
746*cdf0e10cSrcweir 
747*cdf0e10cSrcweir 	public boolean InternalCheckStream( XStream xStream,
748*cdf0e10cSrcweir 										String sName,
749*cdf0e10cSrcweir 										String sMediaType,
750*cdf0e10cSrcweir                                         boolean bCompressed,
751*cdf0e10cSrcweir 										byte[] pBytes,
752*cdf0e10cSrcweir                                         boolean bCheckCompressed )
753*cdf0e10cSrcweir 	{
754*cdf0e10cSrcweir 		// get input stream of substream
755*cdf0e10cSrcweir 		XInputStream xInput = xStream.getInputStream();
756*cdf0e10cSrcweir 		if ( xInput == null )
757*cdf0e10cSrcweir 		{
758*cdf0e10cSrcweir 			Error( "Can't get XInputStream implementation from substream '" + sName + "'!" );
759*cdf0e10cSrcweir 			return false;
760*cdf0e10cSrcweir 		}
761*cdf0e10cSrcweir 
762*cdf0e10cSrcweir 		byte pContents[][] = new byte[1][]; // ???
763*cdf0e10cSrcweir 
764*cdf0e10cSrcweir 		// read contents
765*cdf0e10cSrcweir 		try
766*cdf0e10cSrcweir 		{
767*cdf0e10cSrcweir 			xInput.readBytes( pContents, pBytes.length + 1 );
768*cdf0e10cSrcweir 		}
769*cdf0e10cSrcweir 		catch( Exception e )
770*cdf0e10cSrcweir 		{
771*cdf0e10cSrcweir 			Error( "Can't read from stream '" + sName + "', exception: " + e );
772*cdf0e10cSrcweir 			return false;
773*cdf0e10cSrcweir 		}
774*cdf0e10cSrcweir 
775*cdf0e10cSrcweir 		// check size of stream data
776*cdf0e10cSrcweir 		if ( pContents.length == 0 )
777*cdf0e10cSrcweir 		{
778*cdf0e10cSrcweir 			Error( "SubStream '" + sName + "' reading produced disaster!"  );
779*cdf0e10cSrcweir 			return false;
780*cdf0e10cSrcweir 		}
781*cdf0e10cSrcweir 
782*cdf0e10cSrcweir 		if ( pBytes.length != pContents[0].length )
783*cdf0e10cSrcweir 		{
784*cdf0e10cSrcweir 			Error( "SubStream '" + sName + "' contains wrong amount of data! (" + pContents[0].length + "/" + pBytes.length + ")" );
785*cdf0e10cSrcweir 			return false;
786*cdf0e10cSrcweir 		}
787*cdf0e10cSrcweir 
788*cdf0e10cSrcweir 		// check stream data
789*cdf0e10cSrcweir 		for ( int ind = 0; ind < pBytes.length; ind++ )
790*cdf0e10cSrcweir 		{
791*cdf0e10cSrcweir 			if ( pBytes[ind] != pContents[0][ind] )
792*cdf0e10cSrcweir 			{
793*cdf0e10cSrcweir 				Error( "SubStream '" + sName + "' contains wrong data! ( byte num. "
794*cdf0e10cSrcweir 						+ ind + " should be " + pBytes[ind] + " but it is " + pContents[0][ind] + ")" );
795*cdf0e10cSrcweir 				return false;
796*cdf0e10cSrcweir 			}
797*cdf0e10cSrcweir 		}
798*cdf0e10cSrcweir 
799*cdf0e10cSrcweir 		// check properties
800*cdf0e10cSrcweir 		boolean bOk = false;
801*cdf0e10cSrcweir 
802*cdf0e10cSrcweir 		// get access to the XPropertySet interface
803*cdf0e10cSrcweir 		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStream );
804*cdf0e10cSrcweir 		if ( xPropSet != null )
805*cdf0e10cSrcweir 		{
806*cdf0e10cSrcweir 			try
807*cdf0e10cSrcweir 			{
808*cdf0e10cSrcweir 				// get "MediaType" and "Size" properties and control there values
809*cdf0e10cSrcweir 				String sPropMediaType = AnyConverter.toString( xPropSet.getPropertyValue( "MediaType" ) );
810*cdf0e10cSrcweir 				int nPropSize = AnyConverter.toInt( xPropSet.getPropertyValue( "Size" ) );
811*cdf0e10cSrcweir                 boolean bPropCompress = AnyConverter.toBoolean( xPropSet.getPropertyValue( "Compressed" ) );
812*cdf0e10cSrcweir 
813*cdf0e10cSrcweir 				bOk = true;
814*cdf0e10cSrcweir 				if ( !sPropMediaType.equals( sMediaType ) )
815*cdf0e10cSrcweir 				{
816*cdf0e10cSrcweir 					Error( "'MediaType' property contains wrong value for stream '" + sName + "',\nexpected: '"
817*cdf0e10cSrcweir 							+ sMediaType + "', set: '" + sPropMediaType + "'!" );
818*cdf0e10cSrcweir 					bOk = false;
819*cdf0e10cSrcweir 				}
820*cdf0e10cSrcweir 
821*cdf0e10cSrcweir 				if ( nPropSize != pBytes.length )
822*cdf0e10cSrcweir 				{
823*cdf0e10cSrcweir 					Error( "'Size' property contains wrong value for stream'" + sName + "'!" );
824*cdf0e10cSrcweir 					bOk = false;
825*cdf0e10cSrcweir 				}
826*cdf0e10cSrcweir 
827*cdf0e10cSrcweir                 if ( bCheckCompressed && bPropCompress != bCompressed )
828*cdf0e10cSrcweir                 {
829*cdf0e10cSrcweir     				Error( "'Compressed' property contains wrong value for stream'" + sName + "'!" );
830*cdf0e10cSrcweir 					bOk = false;
831*cdf0e10cSrcweir                 }
832*cdf0e10cSrcweir 			}
833*cdf0e10cSrcweir 			catch( Exception e )
834*cdf0e10cSrcweir 			{
835*cdf0e10cSrcweir 				Error( "Can't get properties of substream '" + sName + "', exception: " + e );
836*cdf0e10cSrcweir 			}
837*cdf0e10cSrcweir 		}
838*cdf0e10cSrcweir 		else
839*cdf0e10cSrcweir 		{
840*cdf0e10cSrcweir 			Error( "Can't get XPropertySet implementation from stream '" + sName + "'!" );
841*cdf0e10cSrcweir 		}
842*cdf0e10cSrcweir 
843*cdf0e10cSrcweir 		return bOk;
844*cdf0e10cSrcweir 	}
845*cdf0e10cSrcweir 
846*cdf0e10cSrcweir 	public boolean checkStream( XStorage xParentStorage,
847*cdf0e10cSrcweir 								String sName,
848*cdf0e10cSrcweir 								String sMediaType,
849*cdf0e10cSrcweir                                 boolean bCompressed,
850*cdf0e10cSrcweir 								byte[] pBytes )
851*cdf0e10cSrcweir 	{
852*cdf0e10cSrcweir 		// open substream element first
853*cdf0e10cSrcweir 		XStream xSubStream = null;
854*cdf0e10cSrcweir 		try
855*cdf0e10cSrcweir 		{
856*cdf0e10cSrcweir 			Object oSubStream = xParentStorage.openStreamElement( sName, ElementModes.READ );
857*cdf0e10cSrcweir 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
858*cdf0e10cSrcweir 			if ( xSubStream == null )
859*cdf0e10cSrcweir 			{
860*cdf0e10cSrcweir 				Error( "Can't open substream '" + sName + "'!" );
861*cdf0e10cSrcweir 				return false;
862*cdf0e10cSrcweir 			}
863*cdf0e10cSrcweir 		}
864*cdf0e10cSrcweir 		catch( Exception e )
865*cdf0e10cSrcweir 		{
866*cdf0e10cSrcweir 			Error( "Can't open substream '" + sName + "', exception : " + e + "!" );
867*cdf0e10cSrcweir 			return false;
868*cdf0e10cSrcweir 		}
869*cdf0e10cSrcweir 
870*cdf0e10cSrcweir 		boolean bResult = InternalCheckStream( xSubStream, sName, sMediaType, bCompressed, pBytes, true );
871*cdf0e10cSrcweir 
872*cdf0e10cSrcweir 		// free the stream resources, garbage collector may remove the object too late
873*cdf0e10cSrcweir 		if ( !disposeStream( xSubStream, sName ) )
874*cdf0e10cSrcweir 			return false;
875*cdf0e10cSrcweir 
876*cdf0e10cSrcweir 		return bResult;
877*cdf0e10cSrcweir 	}
878*cdf0e10cSrcweir 
879*cdf0e10cSrcweir 	public boolean checkEncrStream( XStorage xParentStorage,
880*cdf0e10cSrcweir 									String sName,
881*cdf0e10cSrcweir 									String sMediaType,
882*cdf0e10cSrcweir 									byte[] pBytes,
883*cdf0e10cSrcweir 									String sPass )
884*cdf0e10cSrcweir 	{
885*cdf0e10cSrcweir 		// Important: a common password for any of parent storage should not be set or
886*cdf0e10cSrcweir 		// 			  should be different from sPass
887*cdf0e10cSrcweir 
888*cdf0e10cSrcweir 		try
889*cdf0e10cSrcweir 		{
890*cdf0e10cSrcweir 			Object oSubStream = xParentStorage.openStreamElement( sName, ElementModes.READ );
891*cdf0e10cSrcweir 			Error( "Encrypted stream '" + sName + "' was opened without password!" );
892*cdf0e10cSrcweir 			return false;
893*cdf0e10cSrcweir 		}
894*cdf0e10cSrcweir 		catch( WrongPasswordException wpe )
895*cdf0e10cSrcweir 		{}
896*cdf0e10cSrcweir 		catch( Exception e )
897*cdf0e10cSrcweir 		{
898*cdf0e10cSrcweir 			Error( "Unexpected exception in case of opening of encrypted stream '" + sName + "' without password: " + e + "!" );
899*cdf0e10cSrcweir 			return false;
900*cdf0e10cSrcweir 		}
901*cdf0e10cSrcweir 
902*cdf0e10cSrcweir 		String sWrongPass = "11";
903*cdf0e10cSrcweir 		sWrongPass += sPass;
904*cdf0e10cSrcweir 		try
905*cdf0e10cSrcweir 		{
906*cdf0e10cSrcweir 			Object oSubStream = xParentStorage.openEncryptedStreamElement( sName, ElementModes.READ, sWrongPass );
907*cdf0e10cSrcweir 			Error( "Encrypted stream '" + sName + "' was opened with wrong password!" );
908*cdf0e10cSrcweir 			return false;
909*cdf0e10cSrcweir 		}
910*cdf0e10cSrcweir 		catch( WrongPasswordException wpe )
911*cdf0e10cSrcweir 		{}
912*cdf0e10cSrcweir 		catch( Exception e )
913*cdf0e10cSrcweir 		{
914*cdf0e10cSrcweir 			Error( "Unexpected exception in case of opening of encrypted stream '" + sName + "' with wrong password: " + e + "!" );
915*cdf0e10cSrcweir 			return false;
916*cdf0e10cSrcweir 		}
917*cdf0e10cSrcweir 
918*cdf0e10cSrcweir 		XStream xSubStream = null;
919*cdf0e10cSrcweir 		try
920*cdf0e10cSrcweir 		{
921*cdf0e10cSrcweir 			Object oSubStream = xParentStorage.openEncryptedStreamElement( sName, ElementModes.READ, sPass );
922*cdf0e10cSrcweir 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
923*cdf0e10cSrcweir 			if ( xSubStream == null )
924*cdf0e10cSrcweir 			{
925*cdf0e10cSrcweir 				Error( "Can't open encrypted substream '" + sName + "'!" );
926*cdf0e10cSrcweir 				return false;
927*cdf0e10cSrcweir 			}
928*cdf0e10cSrcweir 		}
929*cdf0e10cSrcweir 		catch( Exception e )
930*cdf0e10cSrcweir 		{
931*cdf0e10cSrcweir 			Error( "Can't open encrypted substream '" + sName + "', exception : " + e + "!" );
932*cdf0e10cSrcweir 			return false;
933*cdf0e10cSrcweir 		}
934*cdf0e10cSrcweir 
935*cdf0e10cSrcweir         // encrypted streams will be compressed always, so after the storing this property is always true,
936*cdf0e10cSrcweir         // although before the storing it can be set to false ( it is not always clear whether a stream is encrypted
937*cdf0e10cSrcweir         // before the storing )
938*cdf0e10cSrcweir 		boolean bResult = InternalCheckStream( xSubStream, sName, sMediaType, true, pBytes, false );
939*cdf0e10cSrcweir 
940*cdf0e10cSrcweir 		// free the stream resources, garbage collector may remove the object too late
941*cdf0e10cSrcweir 		if ( !disposeStream( xSubStream, sName ) )
942*cdf0e10cSrcweir 			return false;
943*cdf0e10cSrcweir 
944*cdf0e10cSrcweir 		return bResult;
945*cdf0e10cSrcweir 	}
946*cdf0e10cSrcweir 
947*cdf0e10cSrcweir 	public boolean checkStreamH( XStorage xParentStorage,
948*cdf0e10cSrcweir 								String sPath,
949*cdf0e10cSrcweir 								String sMediaType,
950*cdf0e10cSrcweir                                 boolean bCompressed,
951*cdf0e10cSrcweir 								byte[] pBytes )
952*cdf0e10cSrcweir 	{
953*cdf0e10cSrcweir 		// open substream element first
954*cdf0e10cSrcweir 		XStream xSubStream = null;
955*cdf0e10cSrcweir 		try
956*cdf0e10cSrcweir 		{
957*cdf0e10cSrcweir 			XHierarchicalStorageAccess xHStorage =
958*cdf0e10cSrcweir 				(XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xParentStorage );
959*cdf0e10cSrcweir 			if ( xHStorage == null )
960*cdf0e10cSrcweir 			{
961*cdf0e10cSrcweir 				Error( "The storage does not support hierarchical access!" );
962*cdf0e10cSrcweir 				return false;
963*cdf0e10cSrcweir 			}
964*cdf0e10cSrcweir 
965*cdf0e10cSrcweir 			Object oSubStream = xHStorage.openStreamElementByHierarchicalName( sPath, ElementModes.READ );
966*cdf0e10cSrcweir 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
967*cdf0e10cSrcweir 			if ( xSubStream == null )
968*cdf0e10cSrcweir 			{
969*cdf0e10cSrcweir 				Error( "Can't open substream '" + sPath + "'!" );
970*cdf0e10cSrcweir 				return false;
971*cdf0e10cSrcweir 			}
972*cdf0e10cSrcweir 		}
973*cdf0e10cSrcweir 		catch( Exception e )
974*cdf0e10cSrcweir 		{
975*cdf0e10cSrcweir 			Error( "Can't open substream '" + sPath + "', exception : " + e + "!" );
976*cdf0e10cSrcweir 			return false;
977*cdf0e10cSrcweir 		}
978*cdf0e10cSrcweir 
979*cdf0e10cSrcweir 		boolean bResult = InternalCheckStream( xSubStream, sPath, sMediaType, bCompressed, pBytes, true );
980*cdf0e10cSrcweir 
981*cdf0e10cSrcweir 		// free the stream resources, garbage collector may remove the object too late
982*cdf0e10cSrcweir 		if ( !disposeStream( xSubStream, sPath ) )
983*cdf0e10cSrcweir 			return false;
984*cdf0e10cSrcweir 
985*cdf0e10cSrcweir 		return bResult;
986*cdf0e10cSrcweir 	}
987*cdf0e10cSrcweir 
988*cdf0e10cSrcweir 	public boolean checkEncrStreamH( XStorage xParentStorage,
989*cdf0e10cSrcweir 									String sPath,
990*cdf0e10cSrcweir 									String sMediaType,
991*cdf0e10cSrcweir 									byte[] pBytes,
992*cdf0e10cSrcweir 									String sPass )
993*cdf0e10cSrcweir 	{
994*cdf0e10cSrcweir 		// Important: a common password for any of parent storage should not be set or
995*cdf0e10cSrcweir 		// 			  should be different from sPass
996*cdf0e10cSrcweir 		XHierarchicalStorageAccess xHStorage =
997*cdf0e10cSrcweir 			(XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xParentStorage );
998*cdf0e10cSrcweir 		if ( xHStorage == null )
999*cdf0e10cSrcweir 		{
1000*cdf0e10cSrcweir 			Error( "The storage does not support hierarchical access!" );
1001*cdf0e10cSrcweir 			return false;
1002*cdf0e10cSrcweir 		}
1003*cdf0e10cSrcweir 
1004*cdf0e10cSrcweir 		try
1005*cdf0e10cSrcweir 		{
1006*cdf0e10cSrcweir 			Object oSubStream = xHStorage.openStreamElementByHierarchicalName( sPath, ElementModes.READ );
1007*cdf0e10cSrcweir 			XStream xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
1008*cdf0e10cSrcweir 			Error( "Encrypted substream '" + sPath + "' was opened without password!" );
1009*cdf0e10cSrcweir 			return false;
1010*cdf0e10cSrcweir 		}
1011*cdf0e10cSrcweir 		catch( WrongPasswordException wpe )
1012*cdf0e10cSrcweir 		{}
1013*cdf0e10cSrcweir 		catch( Exception e )
1014*cdf0e10cSrcweir 		{
1015*cdf0e10cSrcweir 			Error( "Unexpected exception in case of opening of encrypted stream '" + sPath + "' without password: " + e + "!" );
1016*cdf0e10cSrcweir 			return false;
1017*cdf0e10cSrcweir 		}
1018*cdf0e10cSrcweir 
1019*cdf0e10cSrcweir 		String sWrongPass = "11";
1020*cdf0e10cSrcweir 		sWrongPass += sPass;
1021*cdf0e10cSrcweir 		try
1022*cdf0e10cSrcweir 		{
1023*cdf0e10cSrcweir 			Object oSubStream = xHStorage.openEncryptedStreamElementByHierarchicalName( sPath, ElementModes.READ, sWrongPass );
1024*cdf0e10cSrcweir 			XStream xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
1025*cdf0e10cSrcweir 			Error( "Encrypted substream '" + sPath + "' was opened with wrong password!" );
1026*cdf0e10cSrcweir 			return false;
1027*cdf0e10cSrcweir 		}
1028*cdf0e10cSrcweir 		catch( WrongPasswordException wpe )
1029*cdf0e10cSrcweir 		{}
1030*cdf0e10cSrcweir 		catch( Exception e )
1031*cdf0e10cSrcweir 		{
1032*cdf0e10cSrcweir 			Error( "Unexpected exception in case of opening of encrypted stream '" + sPath + "' with wrong password: " + e + "!" );
1033*cdf0e10cSrcweir 			return false;
1034*cdf0e10cSrcweir 		}
1035*cdf0e10cSrcweir 
1036*cdf0e10cSrcweir 		XStream xSubStream = null;
1037*cdf0e10cSrcweir 		try
1038*cdf0e10cSrcweir 		{
1039*cdf0e10cSrcweir 			Object oSubStream = xHStorage.openEncryptedStreamElementByHierarchicalName( sPath, ElementModes.READ, sPass );
1040*cdf0e10cSrcweir 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
1041*cdf0e10cSrcweir 			if ( xSubStream == null )
1042*cdf0e10cSrcweir 			{
1043*cdf0e10cSrcweir 				Error( "Can't open encrypted substream '" + sPath + "'!" );
1044*cdf0e10cSrcweir 				return false;
1045*cdf0e10cSrcweir 			}
1046*cdf0e10cSrcweir 		}
1047*cdf0e10cSrcweir 		catch( Exception e )
1048*cdf0e10cSrcweir 		{
1049*cdf0e10cSrcweir 			Error( "Can't open encrypted substream '" + sPath + "', exception : " + e + "!" );
1050*cdf0e10cSrcweir 			return false;
1051*cdf0e10cSrcweir 		}
1052*cdf0e10cSrcweir 
1053*cdf0e10cSrcweir         // encrypted streams will be compressed always, so after the storing this property is always true,
1054*cdf0e10cSrcweir         // although before the storing it can be set to false ( it is not always clear whether a stream is encrypted
1055*cdf0e10cSrcweir         // before the storing )
1056*cdf0e10cSrcweir 		boolean bResult = InternalCheckStream( xSubStream, sPath, sMediaType, true, pBytes, false );
1057*cdf0e10cSrcweir 
1058*cdf0e10cSrcweir 		// free the stream resources, garbage collector may remove the object too late
1059*cdf0e10cSrcweir 		if ( !disposeStream( xSubStream, sPath ) )
1060*cdf0e10cSrcweir 			return false;
1061*cdf0e10cSrcweir 
1062*cdf0e10cSrcweir 		return bResult;
1063*cdf0e10cSrcweir 	}
1064*cdf0e10cSrcweir 
1065*cdf0e10cSrcweir 	public boolean copyStorage( XStorage xSourceStorage, XStorage xDestStorage )
1066*cdf0e10cSrcweir 	{
1067*cdf0e10cSrcweir 		// copy xSourceStorage to xDestStorage
1068*cdf0e10cSrcweir 		try
1069*cdf0e10cSrcweir 		{
1070*cdf0e10cSrcweir 			xSourceStorage.copyToStorage( xDestStorage );
1071*cdf0e10cSrcweir 		}
1072*cdf0e10cSrcweir 		catch( Exception e )
1073*cdf0e10cSrcweir 		{
1074*cdf0e10cSrcweir 			Error( "Storage copying failed, exception: " + e );
1075*cdf0e10cSrcweir 			return false;
1076*cdf0e10cSrcweir 		}
1077*cdf0e10cSrcweir 
1078*cdf0e10cSrcweir 		return true;
1079*cdf0e10cSrcweir 	}
1080*cdf0e10cSrcweir 
1081*cdf0e10cSrcweir 	public boolean commitStorage( XStorage xStorage )
1082*cdf0e10cSrcweir 	{
1083*cdf0e10cSrcweir 		// XTransactedObject must be supported by storages
1084*cdf0e10cSrcweir 		XTransactedObject xTransact = (XTransactedObject) UnoRuntime.queryInterface( XTransactedObject.class, xStorage );
1085*cdf0e10cSrcweir 		if ( xTransact == null )
1086*cdf0e10cSrcweir 		{
1087*cdf0e10cSrcweir 			Error( "Storage doesn't implement transacted access!" );
1088*cdf0e10cSrcweir 			return false;
1089*cdf0e10cSrcweir 		}
1090*cdf0e10cSrcweir 
1091*cdf0e10cSrcweir 		try
1092*cdf0e10cSrcweir 		{
1093*cdf0e10cSrcweir 			xTransact.commit();
1094*cdf0e10cSrcweir 		}
1095*cdf0e10cSrcweir 		catch( Exception e )
1096*cdf0e10cSrcweir 		{
1097*cdf0e10cSrcweir 			Error( "Storage commit failed, exception:" + e );
1098*cdf0e10cSrcweir 			return false;
1099*cdf0e10cSrcweir 		}
1100*cdf0e10cSrcweir 
1101*cdf0e10cSrcweir 		return true;
1102*cdf0e10cSrcweir 	}
1103*cdf0e10cSrcweir 
1104*cdf0e10cSrcweir 	public boolean disposeStream( XStream xStream, String sStreamName )
1105*cdf0e10cSrcweir 	{
1106*cdf0e10cSrcweir 		XComponent xComponent = (XComponent) UnoRuntime.queryInterface( XComponent.class, xStream );
1107*cdf0e10cSrcweir 		if ( xComponent == null )
1108*cdf0e10cSrcweir 		{
1109*cdf0e10cSrcweir 			Error( "Can't get XComponent implementation from substream '" + sStreamName + "'!" );
1110*cdf0e10cSrcweir 			return false;
1111*cdf0e10cSrcweir 		}
1112*cdf0e10cSrcweir 
1113*cdf0e10cSrcweir 		try
1114*cdf0e10cSrcweir 		{
1115*cdf0e10cSrcweir 			xComponent.dispose();
1116*cdf0e10cSrcweir 		}
1117*cdf0e10cSrcweir 		catch( Exception e )
1118*cdf0e10cSrcweir 		{
1119*cdf0e10cSrcweir 			Error( "Substream '" + sStreamName + "' disposing throws exception: " + e );
1120*cdf0e10cSrcweir 			return false;
1121*cdf0e10cSrcweir 		}
1122*cdf0e10cSrcweir 
1123*cdf0e10cSrcweir 		return true;
1124*cdf0e10cSrcweir 	}
1125*cdf0e10cSrcweir 
1126*cdf0e10cSrcweir 	public boolean disposeStorage( XStorage xStorage )
1127*cdf0e10cSrcweir 	{
1128*cdf0e10cSrcweir 		// dispose the storage
1129*cdf0e10cSrcweir 		XComponent xComponent = (XComponent) UnoRuntime.queryInterface( XComponent.class, xStorage );
1130*cdf0e10cSrcweir 		if ( xComponent == null )
1131*cdf0e10cSrcweir 		{
1132*cdf0e10cSrcweir 			Error( "Can't retrieve XComponent implementation from storage!" );
1133*cdf0e10cSrcweir 			return false;
1134*cdf0e10cSrcweir 		}
1135*cdf0e10cSrcweir 
1136*cdf0e10cSrcweir 		try
1137*cdf0e10cSrcweir 		{
1138*cdf0e10cSrcweir 			xComponent.dispose();
1139*cdf0e10cSrcweir 		}
1140*cdf0e10cSrcweir 		catch( Exception e )
1141*cdf0e10cSrcweir 		{
1142*cdf0e10cSrcweir 			Error( "Storage disposing failed!" );
1143*cdf0e10cSrcweir 			return false;
1144*cdf0e10cSrcweir 		}
1145*cdf0e10cSrcweir 
1146*cdf0e10cSrcweir 		return true;
1147*cdf0e10cSrcweir 	}
1148*cdf0e10cSrcweir 
1149*cdf0e10cSrcweir 	public XInputStream getInputStream( XStream xStream )
1150*cdf0e10cSrcweir 	{
1151*cdf0e10cSrcweir 		XInputStream xInTemp = null;
1152*cdf0e10cSrcweir 		try
1153*cdf0e10cSrcweir 		{
1154*cdf0e10cSrcweir 			xInTemp = xStream.getInputStream();
1155*cdf0e10cSrcweir 			if ( xInTemp == null )
1156*cdf0e10cSrcweir 				Error( "Can't get the input part of a stream!" );
1157*cdf0e10cSrcweir 		}
1158*cdf0e10cSrcweir 		catch ( Exception e )
1159*cdf0e10cSrcweir 		{
1160*cdf0e10cSrcweir 			Error( "Can't get the input part of a stream, exception :" + e );
1161*cdf0e10cSrcweir 		}
1162*cdf0e10cSrcweir 
1163*cdf0e10cSrcweir 		return xInTemp;
1164*cdf0e10cSrcweir 	}
1165*cdf0e10cSrcweir 
1166*cdf0e10cSrcweir 	public boolean closeOutput( XStream xStream )
1167*cdf0e10cSrcweir 	{
1168*cdf0e10cSrcweir 		XOutputStream xOutTemp = null;
1169*cdf0e10cSrcweir 		try
1170*cdf0e10cSrcweir 		{
1171*cdf0e10cSrcweir 			xOutTemp = xStream.getOutputStream();
1172*cdf0e10cSrcweir 			if ( xOutTemp == null )
1173*cdf0e10cSrcweir 			{
1174*cdf0e10cSrcweir 				Error( "Can't get the output part of a stream!" );
1175*cdf0e10cSrcweir 				return false;
1176*cdf0e10cSrcweir 			}
1177*cdf0e10cSrcweir 		}
1178*cdf0e10cSrcweir 		catch ( Exception e )
1179*cdf0e10cSrcweir 		{
1180*cdf0e10cSrcweir 			Error( "Can't get the output part of a stream, exception :" + e );
1181*cdf0e10cSrcweir 			return false;
1182*cdf0e10cSrcweir 		}
1183*cdf0e10cSrcweir 
1184*cdf0e10cSrcweir 		try
1185*cdf0e10cSrcweir 		{
1186*cdf0e10cSrcweir 			xOutTemp.closeOutput();
1187*cdf0e10cSrcweir 		}
1188*cdf0e10cSrcweir 		catch ( Exception e )
1189*cdf0e10cSrcweir 		{
1190*cdf0e10cSrcweir 			Error( "Can't close output part of a stream, exception :" + e );
1191*cdf0e10cSrcweir 			return false;
1192*cdf0e10cSrcweir 		}
1193*cdf0e10cSrcweir 
1194*cdf0e10cSrcweir 		return true;
1195*cdf0e10cSrcweir 	}
1196*cdf0e10cSrcweir 
1197*cdf0e10cSrcweir 	public XStorage openSubStorage( XStorage xStorage, String sName, int nMode )
1198*cdf0e10cSrcweir 	{
1199*cdf0e10cSrcweir 		// open existing substorage
1200*cdf0e10cSrcweir 		try
1201*cdf0e10cSrcweir 		{
1202*cdf0e10cSrcweir 			Object oSubStorage = xStorage.openStorageElement( sName, nMode );
1203*cdf0e10cSrcweir 			XStorage xSubStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oSubStorage );
1204*cdf0e10cSrcweir 			return xSubStorage;
1205*cdf0e10cSrcweir 		}
1206*cdf0e10cSrcweir 		catch( Exception e )
1207*cdf0e10cSrcweir 		{
1208*cdf0e10cSrcweir 			Error( "Can't open substorage '" + sName + "', exception: " + e );
1209*cdf0e10cSrcweir 		}
1210*cdf0e10cSrcweir 
1211*cdf0e10cSrcweir 		return null;
1212*cdf0e10cSrcweir 	}
1213*cdf0e10cSrcweir 
1214*cdf0e10cSrcweir 	public XStream CreateTempFileStream( XMultiServiceFactory xMSF )
1215*cdf0e10cSrcweir 	{
1216*cdf0e10cSrcweir 		// try to get temporary file representation
1217*cdf0e10cSrcweir 		XStream xTempFileStream = null;
1218*cdf0e10cSrcweir 		try
1219*cdf0e10cSrcweir 		{
1220*cdf0e10cSrcweir 			Object oTempFile = xMSF.createInstance( "com.sun.star.io.TempFile" );
1221*cdf0e10cSrcweir 			xTempFileStream = (XStream)UnoRuntime.queryInterface( XStream.class, oTempFile );
1222*cdf0e10cSrcweir 		}
1223*cdf0e10cSrcweir 		catch( Exception e )
1224*cdf0e10cSrcweir 		{}
1225*cdf0e10cSrcweir 
1226*cdf0e10cSrcweir 		if ( xTempFileStream == null )
1227*cdf0e10cSrcweir 			Error( "Can't create temporary file!" );
1228*cdf0e10cSrcweir 
1229*cdf0e10cSrcweir 		return xTempFileStream;
1230*cdf0e10cSrcweir 	}
1231*cdf0e10cSrcweir 
1232*cdf0e10cSrcweir 	public String CreateTempFile( XMultiServiceFactory xMSF )
1233*cdf0e10cSrcweir 	{
1234*cdf0e10cSrcweir 		String sResult = null;
1235*cdf0e10cSrcweir 
1236*cdf0e10cSrcweir 		// try to get temporary file representation
1237*cdf0e10cSrcweir 		XPropertySet xTempFileProps = null;
1238*cdf0e10cSrcweir 		try
1239*cdf0e10cSrcweir 		{
1240*cdf0e10cSrcweir 			Object oTempFile = xMSF.createInstance( "com.sun.star.io.TempFile" );
1241*cdf0e10cSrcweir 			xTempFileProps = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, oTempFile );
1242*cdf0e10cSrcweir 		}
1243*cdf0e10cSrcweir 		catch( Exception e )
1244*cdf0e10cSrcweir 		{}
1245*cdf0e10cSrcweir 
1246*cdf0e10cSrcweir 		if ( xTempFileProps != null )
1247*cdf0e10cSrcweir 		{
1248*cdf0e10cSrcweir 			try
1249*cdf0e10cSrcweir 			{
1250*cdf0e10cSrcweir 				xTempFileProps.setPropertyValue( "RemoveFile", new Boolean( false ) );
1251*cdf0e10cSrcweir 				sResult = AnyConverter.toString( xTempFileProps.getPropertyValue( "Uri" ) );
1252*cdf0e10cSrcweir 			}
1253*cdf0e10cSrcweir 			catch( Exception e )
1254*cdf0e10cSrcweir 			{
1255*cdf0e10cSrcweir 				Error( "Can't control TempFile properties, exception: " + e );
1256*cdf0e10cSrcweir 			}
1257*cdf0e10cSrcweir 		}
1258*cdf0e10cSrcweir 		else
1259*cdf0e10cSrcweir 		{
1260*cdf0e10cSrcweir 			Error( "Can't create temporary file representation!" );
1261*cdf0e10cSrcweir 		}
1262*cdf0e10cSrcweir 
1263*cdf0e10cSrcweir 		// close temporary file explicitly
1264*cdf0e10cSrcweir 		try
1265*cdf0e10cSrcweir 		{
1266*cdf0e10cSrcweir 			XStream xStream = (XStream)UnoRuntime.queryInterface( XStream.class, xTempFileProps );
1267*cdf0e10cSrcweir 			if ( xStream != null )
1268*cdf0e10cSrcweir 			{
1269*cdf0e10cSrcweir 				XOutputStream xOut = xStream.getOutputStream();
1270*cdf0e10cSrcweir 				if ( xOut != null )
1271*cdf0e10cSrcweir 					xOut.closeOutput();
1272*cdf0e10cSrcweir 
1273*cdf0e10cSrcweir 				XInputStream xIn = xStream.getInputStream();
1274*cdf0e10cSrcweir 				if ( xIn != null )
1275*cdf0e10cSrcweir 					xIn.closeInput();
1276*cdf0e10cSrcweir 			}
1277*cdf0e10cSrcweir 			else
1278*cdf0e10cSrcweir 				Error( "Can't close TempFile!" );
1279*cdf0e10cSrcweir 		}
1280*cdf0e10cSrcweir 		catch( Exception e )
1281*cdf0e10cSrcweir 		{
1282*cdf0e10cSrcweir 			Error( "Can't close TempFile, exception: " + e );
1283*cdf0e10cSrcweir 		}
1284*cdf0e10cSrcweir 
1285*cdf0e10cSrcweir 		return sResult;
1286*cdf0e10cSrcweir 	}
1287*cdf0e10cSrcweir 
1288*cdf0e10cSrcweir 	public boolean copyElementTo( XStorage xSource, String sName, XStorage xDest )
1289*cdf0e10cSrcweir 	{
1290*cdf0e10cSrcweir 		// copy element with name sName from xSource to xDest
1291*cdf0e10cSrcweir 		try
1292*cdf0e10cSrcweir 		{
1293*cdf0e10cSrcweir 			xSource.copyElementTo( sName, xDest, sName );
1294*cdf0e10cSrcweir 		}
1295*cdf0e10cSrcweir 		catch( Exception e )
1296*cdf0e10cSrcweir 		{
1297*cdf0e10cSrcweir 			Error( "Element copying failed, exception: " + e );
1298*cdf0e10cSrcweir 			return false;
1299*cdf0e10cSrcweir 		}
1300*cdf0e10cSrcweir 
1301*cdf0e10cSrcweir 		return true;
1302*cdf0e10cSrcweir 	}
1303*cdf0e10cSrcweir 
1304*cdf0e10cSrcweir 	public boolean copyElementTo( XStorage xSource, String sName, XStorage xDest, String sTargetName )
1305*cdf0e10cSrcweir 	{
1306*cdf0e10cSrcweir 		// copy element with name sName from xSource to xDest
1307*cdf0e10cSrcweir 		try
1308*cdf0e10cSrcweir 		{
1309*cdf0e10cSrcweir 			xSource.copyElementTo( sName, xDest, sTargetName );
1310*cdf0e10cSrcweir 		}
1311*cdf0e10cSrcweir 		catch( Exception e )
1312*cdf0e10cSrcweir 		{
1313*cdf0e10cSrcweir 			Error( "Element copying failed, exception: " + e );
1314*cdf0e10cSrcweir 			return false;
1315*cdf0e10cSrcweir 		}
1316*cdf0e10cSrcweir 
1317*cdf0e10cSrcweir 		return true;
1318*cdf0e10cSrcweir 	}
1319*cdf0e10cSrcweir 
1320*cdf0e10cSrcweir 	public boolean moveElementTo( XStorage xSource, String sName, XStorage xDest )
1321*cdf0e10cSrcweir 	{
1322*cdf0e10cSrcweir 		// move element with name sName from xSource to xDest
1323*cdf0e10cSrcweir 		try
1324*cdf0e10cSrcweir 		{
1325*cdf0e10cSrcweir 			xSource.moveElementTo( sName, xDest, sName );
1326*cdf0e10cSrcweir 		}
1327*cdf0e10cSrcweir 		catch( Exception e )
1328*cdf0e10cSrcweir 		{
1329*cdf0e10cSrcweir 			Error( "Element moving failed, exception: " + e );
1330*cdf0e10cSrcweir 			return false;
1331*cdf0e10cSrcweir 		}
1332*cdf0e10cSrcweir 
1333*cdf0e10cSrcweir 		return true;
1334*cdf0e10cSrcweir 	}
1335*cdf0e10cSrcweir 
1336*cdf0e10cSrcweir 	public boolean renameElement( XStorage xStorage, String sOldName, String sNewName )
1337*cdf0e10cSrcweir 	{
1338*cdf0e10cSrcweir 		// rename element with name sOldName to sNewName
1339*cdf0e10cSrcweir 		try
1340*cdf0e10cSrcweir 		{
1341*cdf0e10cSrcweir 			xStorage.renameElement( sOldName, sNewName );
1342*cdf0e10cSrcweir 		}
1343*cdf0e10cSrcweir 		catch( Exception e )
1344*cdf0e10cSrcweir 		{
1345*cdf0e10cSrcweir 			Error( "Element renaming failed, exception: " + e );
1346*cdf0e10cSrcweir 			return false;
1347*cdf0e10cSrcweir 		}
1348*cdf0e10cSrcweir 
1349*cdf0e10cSrcweir 		return true;
1350*cdf0e10cSrcweir 	}
1351*cdf0e10cSrcweir 
1352*cdf0e10cSrcweir 	public boolean removeElement( XStorage xStorage, String sName )
1353*cdf0e10cSrcweir 	{
1354*cdf0e10cSrcweir 		// remove element with name sName
1355*cdf0e10cSrcweir 		try
1356*cdf0e10cSrcweir 		{
1357*cdf0e10cSrcweir 			xStorage.removeElement( sName );
1358*cdf0e10cSrcweir 		}
1359*cdf0e10cSrcweir 		catch( Exception e )
1360*cdf0e10cSrcweir 		{
1361*cdf0e10cSrcweir 			Error( "Element removing failed, exception: " + e );
1362*cdf0e10cSrcweir 			return false;
1363*cdf0e10cSrcweir 		}
1364*cdf0e10cSrcweir 
1365*cdf0e10cSrcweir 		return true;
1366*cdf0e10cSrcweir 	}
1367*cdf0e10cSrcweir 
1368*cdf0e10cSrcweir 	public XStream OpenStream( XStorage xStorage,
1369*cdf0e10cSrcweir 								String sStreamName,
1370*cdf0e10cSrcweir 								int nMode )
1371*cdf0e10cSrcweir 	{
1372*cdf0e10cSrcweir 		// open substream element
1373*cdf0e10cSrcweir 		XStream xSubStream = null;
1374*cdf0e10cSrcweir 		try
1375*cdf0e10cSrcweir 		{
1376*cdf0e10cSrcweir 			Object oSubStream = xStorage.openStreamElement( sStreamName, nMode );
1377*cdf0e10cSrcweir 			xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
1378*cdf0e10cSrcweir 			if ( xSubStream == null )
1379*cdf0e10cSrcweir 				Error( "Can't create substream '" + sStreamName + "'!" );
1380*cdf0e10cSrcweir 		}
1381*cdf0e10cSrcweir 		catch( Exception e )
1382*cdf0e10cSrcweir 		{
1383*cdf0e10cSrcweir 			Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
1384*cdf0e10cSrcweir 		}
1385*cdf0e10cSrcweir 
1386*cdf0e10cSrcweir 		return xSubStream;
1387*cdf0e10cSrcweir 	}
1388*cdf0e10cSrcweir 
1389*cdf0e10cSrcweir 	public boolean compareRawMethodsOnEncrStream( XStorage xStorage, String sStreamName )
1390*cdf0e10cSrcweir 	{
1391*cdf0e10cSrcweir 
1392*cdf0e10cSrcweir 		XStorageRawAccess xRawStorage;
1393*cdf0e10cSrcweir 		try
1394*cdf0e10cSrcweir 		{
1395*cdf0e10cSrcweir 			xRawStorage = (XStorageRawAccess) UnoRuntime.queryInterface( XStorageRawAccess.class, xStorage );
1396*cdf0e10cSrcweir 		}
1397*cdf0e10cSrcweir 		catch( Exception e )
1398*cdf0e10cSrcweir 		{
1399*cdf0e10cSrcweir 			Error( "Can't get raw access to the storage, exception : " + e + "!" );
1400*cdf0e10cSrcweir 			return false;
1401*cdf0e10cSrcweir 		}
1402*cdf0e10cSrcweir 
1403*cdf0e10cSrcweir 		if ( xRawStorage == null )
1404*cdf0e10cSrcweir 		{
1405*cdf0e10cSrcweir 			Error( "Can't get raw access to the storage!" );
1406*cdf0e10cSrcweir 			return false;
1407*cdf0e10cSrcweir 		}
1408*cdf0e10cSrcweir 
1409*cdf0e10cSrcweir 		XInputStream xHeadRawStream = null;
1410*cdf0e10cSrcweir 		try
1411*cdf0e10cSrcweir 		{
1412*cdf0e10cSrcweir 			xHeadRawStream = xRawStorage.getRawEncrStreamElement( sStreamName );
1413*cdf0e10cSrcweir 		}
1414*cdf0e10cSrcweir 		catch( Exception e )
1415*cdf0e10cSrcweir 		{
1416*cdf0e10cSrcweir 			Error( "Can't open encrypted stream '" + sStreamName + "' in raw mode with header, exception : " + e + "!" );
1417*cdf0e10cSrcweir 		}
1418*cdf0e10cSrcweir 
1419*cdf0e10cSrcweir 		XInputStream xPlainRawStream = null;
1420*cdf0e10cSrcweir 		try
1421*cdf0e10cSrcweir 		{
1422*cdf0e10cSrcweir 			xPlainRawStream = xRawStorage.getPlainRawStreamElement( sStreamName );
1423*cdf0e10cSrcweir 		}
1424*cdf0e10cSrcweir 		catch( Exception e )
1425*cdf0e10cSrcweir 		{
1426*cdf0e10cSrcweir 			Error( "Can't open encrypted stream '" + sStreamName + "' in raw mode with header, exception : " + e + "!" );
1427*cdf0e10cSrcweir 		}
1428*cdf0e10cSrcweir 
1429*cdf0e10cSrcweir 		if ( xHeadRawStream == null || xPlainRawStream == null )
1430*cdf0e10cSrcweir 		{
1431*cdf0e10cSrcweir 			Error( "Can't open encrypted stream '" + sStreamName + "' in raw modes!" );
1432*cdf0e10cSrcweir 			return false;
1433*cdf0e10cSrcweir 		}
1434*cdf0e10cSrcweir 
1435*cdf0e10cSrcweir 		try
1436*cdf0e10cSrcweir 		{
1437*cdf0e10cSrcweir 			byte pData[][] = new byte[1][38];
1438*cdf0e10cSrcweir 			if ( xHeadRawStream.readBytes( pData, 38 ) != 38 )
1439*cdf0e10cSrcweir 			{
1440*cdf0e10cSrcweir 				Error( "Can't read header of encrypted stream '" + sStreamName + "' raw representations!" );
1441*cdf0e10cSrcweir 				return false;
1442*cdf0e10cSrcweir 			}
1443*cdf0e10cSrcweir 
1444*cdf0e10cSrcweir 			if ( pData[0][0] != 0x4d || pData[0][1] != 0x4d || pData[0][2] != 0x02 || pData[0][3] != 0x05 )
1445*cdf0e10cSrcweir 			{
1446*cdf0e10cSrcweir 				Error( "No signature in the header of encrypted stream '" + sStreamName + "' raw representations!" );
1447*cdf0e10cSrcweir 				return false;
1448*cdf0e10cSrcweir 			}
1449*cdf0e10cSrcweir 
1450*cdf0e10cSrcweir 			int nVariableHeaderLength =
1451*cdf0e10cSrcweir 						( pData[0][30] + pData[0][31] * 0x100 ) // salt length
1452*cdf0e10cSrcweir 						+ ( pData[0][32] + pData[0][33] * 0x100 ) // iv length
1453*cdf0e10cSrcweir 						+ ( pData[0][34] + pData[0][35] * 0x100 ) // digest length
1454*cdf0e10cSrcweir 						+ ( pData[0][36] + pData[0][37] * 0x100 ); // mediatype length
1455*cdf0e10cSrcweir 
1456*cdf0e10cSrcweir 			xHeadRawStream.skipBytes( nVariableHeaderLength );
1457*cdf0e10cSrcweir 
1458*cdf0e10cSrcweir 			byte pRawData1[][] = new byte[1][32000];
1459*cdf0e10cSrcweir 			byte pRawData2[][] = new byte[1][32000];
1460*cdf0e10cSrcweir 			int nRead1 = 0;
1461*cdf0e10cSrcweir 			int nRead2 = 0;
1462*cdf0e10cSrcweir 
1463*cdf0e10cSrcweir 			do
1464*cdf0e10cSrcweir 			{
1465*cdf0e10cSrcweir 				nRead1 = xHeadRawStream.readBytes( pRawData1, 32000 );
1466*cdf0e10cSrcweir 				nRead2 = xPlainRawStream.readBytes( pRawData2, 32000 );
1467*cdf0e10cSrcweir 
1468*cdf0e10cSrcweir 				if ( nRead1 != nRead2 )
1469*cdf0e10cSrcweir 				{
1470*cdf0e10cSrcweir 					Error( "The encrypted stream '" + sStreamName + "' raw representations have different size! nRead1 - nRead2 = " + ( new Integer( nRead1 - nRead2 ) ).toString() );
1471*cdf0e10cSrcweir 					return false;
1472*cdf0e10cSrcweir 				}
1473*cdf0e10cSrcweir 
1474*cdf0e10cSrcweir 				for ( int nInd = 0; nInd < nRead1; nInd++ )
1475*cdf0e10cSrcweir 					if ( pRawData1[0][nInd] != pRawData2[0][nInd] )
1476*cdf0e10cSrcweir 					{
1477*cdf0e10cSrcweir 						Error( "The encrypted stream '" + sStreamName + "' raw representations have different data!" );
1478*cdf0e10cSrcweir 						return false;
1479*cdf0e10cSrcweir 					}
1480*cdf0e10cSrcweir 			}
1481*cdf0e10cSrcweir 			while( nRead1 == 32000 );
1482*cdf0e10cSrcweir 		}
1483*cdf0e10cSrcweir 		catch ( Exception e )
1484*cdf0e10cSrcweir 		{
1485*cdf0e10cSrcweir 			Error( "Can't compare stream '" + sStreamName + "' raw representations, exception : " + e + "!" );
1486*cdf0e10cSrcweir 			return false;
1487*cdf0e10cSrcweir 		}
1488*cdf0e10cSrcweir 
1489*cdf0e10cSrcweir 		return true;
1490*cdf0e10cSrcweir 	}
1491*cdf0e10cSrcweir 
1492*cdf0e10cSrcweir 	public boolean cantOpenStorage( XStorage xStorage, String sName )
1493*cdf0e10cSrcweir 	{
1494*cdf0e10cSrcweir 		// try to open an opened substorage, open call must fail
1495*cdf0e10cSrcweir 		try
1496*cdf0e10cSrcweir 		{
1497*cdf0e10cSrcweir 			Object oDummyStorage = xStorage.openStorageElement( sName, ElementModes.READ );
1498*cdf0e10cSrcweir 			Error( "The trying to reopen opened substorage '" + sName + "' must fail!" );
1499*cdf0e10cSrcweir 		}
1500*cdf0e10cSrcweir 		catch( Exception e )
1501*cdf0e10cSrcweir 		{
1502*cdf0e10cSrcweir 			return true;
1503*cdf0e10cSrcweir 		}
1504*cdf0e10cSrcweir 
1505*cdf0e10cSrcweir 		return false;
1506*cdf0e10cSrcweir 	}
1507*cdf0e10cSrcweir 
1508*cdf0e10cSrcweir 	public boolean cantOpenStream( XStorage xStorage, String sName, int nMode )
1509*cdf0e10cSrcweir 	{
1510*cdf0e10cSrcweir 		// try to open the substream with specified mode must fail
1511*cdf0e10cSrcweir 		try
1512*cdf0e10cSrcweir 		{
1513*cdf0e10cSrcweir 			Object oDummyStream = xStorage.openStreamElement( sName, nMode );
1514*cdf0e10cSrcweir 			Error( "The trying to open substream '" + sName + "' must fail!" );
1515*cdf0e10cSrcweir 		}
1516*cdf0e10cSrcweir 		catch( Exception e )
1517*cdf0e10cSrcweir 		{
1518*cdf0e10cSrcweir 			return true;
1519*cdf0e10cSrcweir 		}
1520*cdf0e10cSrcweir 
1521*cdf0e10cSrcweir 		return false;
1522*cdf0e10cSrcweir 	}
1523*cdf0e10cSrcweir 
1524*cdf0e10cSrcweir 	public boolean cantOpenStreamH( XStorage xStorage, String sPath, int nMode )
1525*cdf0e10cSrcweir 	{
1526*cdf0e10cSrcweir 		// try to open the substream with specified mode must fail
1527*cdf0e10cSrcweir 
1528*cdf0e10cSrcweir 		XHierarchicalStorageAccess xHStorage =
1529*cdf0e10cSrcweir 			(XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage );
1530*cdf0e10cSrcweir 		if ( xHStorage == null )
1531*cdf0e10cSrcweir 		{
1532*cdf0e10cSrcweir 			Error( "The storage does not support hierarchical access!" );
1533*cdf0e10cSrcweir 			return false;
1534*cdf0e10cSrcweir 		}
1535*cdf0e10cSrcweir 
1536*cdf0e10cSrcweir 		try
1537*cdf0e10cSrcweir 		{
1538*cdf0e10cSrcweir 			Object oDummyStream = xHStorage.openStreamElementByHierarchicalName( sPath, nMode );
1539*cdf0e10cSrcweir 			Error( "The trying to open substream '" + sPath + "' must fail!" );
1540*cdf0e10cSrcweir 		}
1541*cdf0e10cSrcweir 		catch( Exception e )
1542*cdf0e10cSrcweir 		{
1543*cdf0e10cSrcweir 			return true;
1544*cdf0e10cSrcweir 		}
1545*cdf0e10cSrcweir 
1546*cdf0e10cSrcweir 		return false;
1547*cdf0e10cSrcweir 	}
1548*cdf0e10cSrcweir 
1549*cdf0e10cSrcweir 	public boolean cantOpenEncrStreamH( XStorage xStorage, String sPath, int nMode, String aPass )
1550*cdf0e10cSrcweir 	{
1551*cdf0e10cSrcweir 		// try to open the substream with specified mode must fail
1552*cdf0e10cSrcweir 
1553*cdf0e10cSrcweir 		XHierarchicalStorageAccess xHStorage =
1554*cdf0e10cSrcweir 			(XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage );
1555*cdf0e10cSrcweir 		if ( xHStorage == null )
1556*cdf0e10cSrcweir 		{
1557*cdf0e10cSrcweir 			Error( "The storage does not support hierarchical access!" );
1558*cdf0e10cSrcweir 			return false;
1559*cdf0e10cSrcweir 		}
1560*cdf0e10cSrcweir 
1561*cdf0e10cSrcweir 		try
1562*cdf0e10cSrcweir 		{
1563*cdf0e10cSrcweir 			Object oDummyStream = xHStorage.openEncryptedStreamElementByHierarchicalName( sPath, nMode, aPass );
1564*cdf0e10cSrcweir 			Error( "The trying to open substream '" + sPath + "' must fail!" );
1565*cdf0e10cSrcweir 		}
1566*cdf0e10cSrcweir 		catch( WrongPasswordException wpe )
1567*cdf0e10cSrcweir 		{
1568*cdf0e10cSrcweir 			Error( "The substream '" + sPath + "' must not exist!" );
1569*cdf0e10cSrcweir 			return false;
1570*cdf0e10cSrcweir 		}
1571*cdf0e10cSrcweir 		catch( Exception e )
1572*cdf0e10cSrcweir 		{
1573*cdf0e10cSrcweir 			return true;
1574*cdf0e10cSrcweir 		}
1575*cdf0e10cSrcweir 
1576*cdf0e10cSrcweir 		return false;
1577*cdf0e10cSrcweir 	}
1578*cdf0e10cSrcweir 
1579*cdf0e10cSrcweir 	public XStorage cloneStorage( XSingleServiceFactory xFactory, XStorage xStorage )
1580*cdf0e10cSrcweir 	{
1581*cdf0e10cSrcweir 		// create a copy of a last commited version of specified storage
1582*cdf0e10cSrcweir 		XStorage xResult = null;
1583*cdf0e10cSrcweir 		try
1584*cdf0e10cSrcweir 		{
1585*cdf0e10cSrcweir 			Object oTempStorage = xFactory.createInstance();
1586*cdf0e10cSrcweir 			xResult = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
1587*cdf0e10cSrcweir 			if ( xResult != null )
1588*cdf0e10cSrcweir 				xStorage.copyLastCommitTo( xResult );
1589*cdf0e10cSrcweir 		}
1590*cdf0e10cSrcweir 		catch( Exception e )
1591*cdf0e10cSrcweir 		{
1592*cdf0e10cSrcweir 			Error( "Can't clone storage, exception: " + e );
1593*cdf0e10cSrcweir 			return null;
1594*cdf0e10cSrcweir 		}
1595*cdf0e10cSrcweir 
1596*cdf0e10cSrcweir 		return xResult;
1597*cdf0e10cSrcweir 	}
1598*cdf0e10cSrcweir 
1599*cdf0e10cSrcweir 	public XStorage cloneSubStorage( XSingleServiceFactory xFactory, XStorage xStorage, String sName )
1600*cdf0e10cSrcweir 	{
1601*cdf0e10cSrcweir 		// create a copy of a last commited version of specified substorage
1602*cdf0e10cSrcweir 		XStorage xResult = null;
1603*cdf0e10cSrcweir 		try
1604*cdf0e10cSrcweir 		{
1605*cdf0e10cSrcweir 			Object oTempStorage = xFactory.createInstance();
1606*cdf0e10cSrcweir 			xResult = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
1607*cdf0e10cSrcweir 			if ( xResult != null )
1608*cdf0e10cSrcweir 				xStorage.copyStorageElementLastCommitTo( sName, xResult );
1609*cdf0e10cSrcweir 		}
1610*cdf0e10cSrcweir 		catch( Exception e )
1611*cdf0e10cSrcweir 		{
1612*cdf0e10cSrcweir 			Error( "Can't clone substorage '" + sName + "', exception: " + e );
1613*cdf0e10cSrcweir 			return null;
1614*cdf0e10cSrcweir 		}
1615*cdf0e10cSrcweir 
1616*cdf0e10cSrcweir 		return xResult;
1617*cdf0e10cSrcweir 	}
1618*cdf0e10cSrcweir 
1619*cdf0e10cSrcweir 	public XStream cloneSubStream( XStorage xStorage, String sName )
1620*cdf0e10cSrcweir 	{
1621*cdf0e10cSrcweir 		// clone existing substream
1622*cdf0e10cSrcweir 		try
1623*cdf0e10cSrcweir 		{
1624*cdf0e10cSrcweir 			XStream xStream = xStorage.cloneStreamElement( sName );
1625*cdf0e10cSrcweir 			return xStream;
1626*cdf0e10cSrcweir 		}
1627*cdf0e10cSrcweir 		catch( Exception e )
1628*cdf0e10cSrcweir 		{
1629*cdf0e10cSrcweir 			Error( "Can't clone substream '" + sName + "', exception: " + e );
1630*cdf0e10cSrcweir 		}
1631*cdf0e10cSrcweir 
1632*cdf0e10cSrcweir 		return null;
1633*cdf0e10cSrcweir 	}
1634*cdf0e10cSrcweir 
1635*cdf0e10cSrcweir 	public XStream cloneEncrSubStream( XStorage xStorage, String sName, String sPass )
1636*cdf0e10cSrcweir 	{
1637*cdf0e10cSrcweir 		// clone existing substream
1638*cdf0e10cSrcweir 		try
1639*cdf0e10cSrcweir 		{
1640*cdf0e10cSrcweir 			XStream xStream = xStorage.cloneEncryptedStreamElement( sName, sPass );
1641*cdf0e10cSrcweir 			return xStream;
1642*cdf0e10cSrcweir 		}
1643*cdf0e10cSrcweir 		catch( Exception e )
1644*cdf0e10cSrcweir 		{
1645*cdf0e10cSrcweir 			Error( "Can't clone encrypted substream '" + sName + "', exception: " + e );
1646*cdf0e10cSrcweir 		}
1647*cdf0e10cSrcweir 
1648*cdf0e10cSrcweir 		return null;
1649*cdf0e10cSrcweir 	}
1650*cdf0e10cSrcweir 
1651*cdf0e10cSrcweir 	public void Error( String sError )
1652*cdf0e10cSrcweir 	{
1653*cdf0e10cSrcweir 		m_aLogWriter.println( m_sTestPrefix + "Error: " + sError );
1654*cdf0e10cSrcweir 	}
1655*cdf0e10cSrcweir 
1656*cdf0e10cSrcweir 	public void Message( String sMessage )
1657*cdf0e10cSrcweir 	{
1658*cdf0e10cSrcweir 		m_aLogWriter.println( m_sTestPrefix + sMessage );
1659*cdf0e10cSrcweir 	}
1660*cdf0e10cSrcweir }
1661*cdf0e10cSrcweir 
1662