xref: /trunk/main/package/qa/ofopxmlstorages/TestHelper.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
1a740f2aaSAndrew Rist /**************************************************************
2a740f2aaSAndrew Rist  *
3a740f2aaSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4a740f2aaSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5a740f2aaSAndrew Rist  * distributed with this work for additional information
6a740f2aaSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7a740f2aaSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8a740f2aaSAndrew Rist  * "License"); you may not use this file except in compliance
9a740f2aaSAndrew Rist  * with the License.  You may obtain a copy of the License at
10a740f2aaSAndrew Rist  *
11a740f2aaSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12a740f2aaSAndrew Rist  *
13a740f2aaSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14a740f2aaSAndrew Rist  * software distributed under the License is distributed on an
15a740f2aaSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16a740f2aaSAndrew Rist  * KIND, either express or implied.  See the License for the
17a740f2aaSAndrew Rist  * specific language governing permissions and limitations
18a740f2aaSAndrew Rist  * under the License.
19a740f2aaSAndrew Rist  *
20a740f2aaSAndrew Rist  *************************************************************/
21a740f2aaSAndrew Rist 
22cdf0e10cSrcweir package complex.ofopxmlstorages;
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
25cdf0e10cSrcweir import com.sun.star.uno.XInterface;
26cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import com.sun.star.lang.*;
29cdf0e10cSrcweir import com.sun.star.embed.*;
30cdf0e10cSrcweir import com.sun.star.packages.*;
31cdf0e10cSrcweir import com.sun.star.io.*;
32cdf0e10cSrcweir import com.sun.star.beans.*;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir import share.LogWriter;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir public class TestHelper  {
37cdf0e10cSrcweir 
38cdf0e10cSrcweir     LogWriter m_aLogWriter;
39cdf0e10cSrcweir     String m_sTestPrefix;
40cdf0e10cSrcweir 
TestHelper( LogWriter aLogWriter, String sTestPrefix )41cdf0e10cSrcweir     public TestHelper( LogWriter aLogWriter, String sTestPrefix )
42cdf0e10cSrcweir     {
43cdf0e10cSrcweir         m_aLogWriter = aLogWriter;
44cdf0e10cSrcweir         m_sTestPrefix = sTestPrefix;
45cdf0e10cSrcweir     }
46cdf0e10cSrcweir 
WriteBytesToStream( XStream xStream, String sStreamName, String sMediaType, boolean bCompressed, byte[] pBytes, StringPair[][] aRelations )47cdf0e10cSrcweir     public boolean WriteBytesToStream( XStream xStream,
48cdf0e10cSrcweir                                         String sStreamName,
49cdf0e10cSrcweir                                         String sMediaType,
50cdf0e10cSrcweir                                         boolean bCompressed,
51cdf0e10cSrcweir                                         byte[] pBytes,
52cdf0e10cSrcweir                                         StringPair[][] aRelations )
53cdf0e10cSrcweir     {
54cdf0e10cSrcweir         // get output stream of substream
55cdf0e10cSrcweir         XOutputStream xOutput = xStream.getOutputStream();
56cdf0e10cSrcweir         if ( xOutput == null )
57cdf0e10cSrcweir         {
58cdf0e10cSrcweir             Error( "Can't get XOutputStream implementation from substream '" + sStreamName + "'!" );
59cdf0e10cSrcweir             return false;
60cdf0e10cSrcweir         }
61cdf0e10cSrcweir 
62cdf0e10cSrcweir         // get XTrucate implementation from output stream
63cdf0e10cSrcweir         XTruncate xTruncate = (XTruncate) UnoRuntime.queryInterface( XTruncate.class, xOutput );
64cdf0e10cSrcweir         if ( xTruncate == null )
65cdf0e10cSrcweir         {
66cdf0e10cSrcweir             Error( "Can't get XTruncate implementation from substream '" + sStreamName + "'!" );
67cdf0e10cSrcweir             return false;
68cdf0e10cSrcweir         }
69cdf0e10cSrcweir 
70cdf0e10cSrcweir         // write requested byte sequence
71cdf0e10cSrcweir         try
72cdf0e10cSrcweir         {
73cdf0e10cSrcweir             xTruncate.truncate();
74cdf0e10cSrcweir             xOutput.writeBytes( pBytes );
75cdf0e10cSrcweir         }
76cdf0e10cSrcweir         catch( Exception e )
77cdf0e10cSrcweir         {
78cdf0e10cSrcweir             Error( "Can't write to stream '" + sStreamName + "', exception: " + e );
79cdf0e10cSrcweir             return false;
80cdf0e10cSrcweir         }
81cdf0e10cSrcweir 
82cdf0e10cSrcweir         // get access to the XPropertySet interface
83cdf0e10cSrcweir         XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStream );
84cdf0e10cSrcweir         if ( xPropSet == null )
85cdf0e10cSrcweir         {
86cdf0e10cSrcweir             Error( "Can't get XPropertySet implementation from substream '" + sStreamName + "'!" );
87cdf0e10cSrcweir             return false;
88cdf0e10cSrcweir         }
89cdf0e10cSrcweir 
90cdf0e10cSrcweir         // set properties to the stream
91cdf0e10cSrcweir         try
92cdf0e10cSrcweir         {
93cdf0e10cSrcweir             xPropSet.setPropertyValue( "MediaType", sMediaType );
94cdf0e10cSrcweir             xPropSet.setPropertyValue( "Compressed", new Boolean( bCompressed ) );
95cdf0e10cSrcweir         }
96cdf0e10cSrcweir         catch( Exception e )
97cdf0e10cSrcweir         {
98cdf0e10cSrcweir             Error( "Can't set properties to substream '" + sStreamName + "', exception: " + e );
99cdf0e10cSrcweir             return false;
100cdf0e10cSrcweir         }
101cdf0e10cSrcweir 
102cdf0e10cSrcweir         // check size property of the stream
103cdf0e10cSrcweir         try
104cdf0e10cSrcweir         {
105cdf0e10cSrcweir             int nSize = AnyConverter.toInt( xPropSet.getPropertyValue( "Size" ) );
106cdf0e10cSrcweir             if ( nSize != pBytes.length )
107cdf0e10cSrcweir             {
108cdf0e10cSrcweir                 Error( "The 'Size' property of substream '" + sStreamName + "' contains wrong value!" );
109cdf0e10cSrcweir                 return false;
110cdf0e10cSrcweir             }
111cdf0e10cSrcweir         }
112cdf0e10cSrcweir         catch( Exception e )
113cdf0e10cSrcweir         {
114cdf0e10cSrcweir             Error( "Can't get 'Size' property from substream '" + sStreamName + "', exception: " + e );
115cdf0e10cSrcweir             return false;
116cdf0e10cSrcweir         }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir         // get access to the relationship information
119cdf0e10cSrcweir         XRelationshipAccess xRelAccess = (XRelationshipAccess) UnoRuntime.queryInterface( XRelationshipAccess.class, xStream );
120cdf0e10cSrcweir         if ( xRelAccess == null )
121cdf0e10cSrcweir         {
122cdf0e10cSrcweir             Error( "Can't get XRelationshipAccess implementation from substream '" + sStreamName + "'!" );
123cdf0e10cSrcweir             return false;
124cdf0e10cSrcweir         }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir         // set the relationship information
127cdf0e10cSrcweir         try
128cdf0e10cSrcweir         {
129cdf0e10cSrcweir             xRelAccess.insertRelationships( aRelations, false );
130cdf0e10cSrcweir         }
131cdf0e10cSrcweir         catch( Exception e )
132cdf0e10cSrcweir         {
133cdf0e10cSrcweir             Error( "Can't set relationships to substream '" + sStreamName + "', exception: " + e );
134cdf0e10cSrcweir             return false;
135cdf0e10cSrcweir         }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir         // free the stream resources, garbage collector may remove the object too late
138cdf0e10cSrcweir         if ( !disposeStream( xStream, sStreamName ) )
139cdf0e10cSrcweir             return false;
140cdf0e10cSrcweir 
141cdf0e10cSrcweir         return true;
142cdf0e10cSrcweir     }
143cdf0e10cSrcweir 
WriteBytesToSubstream( XStorage xStorage, String sStreamName, String sMediaType, boolean bCompressed, byte[] pBytes, StringPair[][] aRelations )144cdf0e10cSrcweir     public boolean WriteBytesToSubstream( XStorage xStorage,
145cdf0e10cSrcweir                                           String sStreamName,
146cdf0e10cSrcweir                                           String sMediaType,
147cdf0e10cSrcweir                                           boolean bCompressed,
148cdf0e10cSrcweir                                           byte[] pBytes,
149cdf0e10cSrcweir                                           StringPair[][] aRelations )
150cdf0e10cSrcweir     {
151cdf0e10cSrcweir         // open substream element
152cdf0e10cSrcweir         XStream xSubStream = null;
153cdf0e10cSrcweir         try
154cdf0e10cSrcweir         {
155cdf0e10cSrcweir             Object oSubStream = xStorage.openStreamElement( sStreamName, ElementModes.WRITE );
156cdf0e10cSrcweir             xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
157cdf0e10cSrcweir             if ( xSubStream == null )
158cdf0e10cSrcweir             {
159cdf0e10cSrcweir                 Error( "Can't create substream '" + sStreamName + "'!" );
160cdf0e10cSrcweir                 return false;
161cdf0e10cSrcweir             }
162cdf0e10cSrcweir         }
163cdf0e10cSrcweir         catch( Exception e )
164cdf0e10cSrcweir         {
165cdf0e10cSrcweir             Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
166cdf0e10cSrcweir             return false;
167cdf0e10cSrcweir         }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir         return WriteBytesToStream( xSubStream, sStreamName, sMediaType, bCompressed, pBytes, aRelations );
170cdf0e10cSrcweir     }
171cdf0e10cSrcweir 
setStorageTypeAndCheckProps( XStorage xStorage, boolean bIsRoot, int nMode, StringPair[][] aRelations )172cdf0e10cSrcweir     public boolean setStorageTypeAndCheckProps( XStorage xStorage,
173cdf0e10cSrcweir                                                 boolean bIsRoot,
174cdf0e10cSrcweir                                                 int nMode,
175cdf0e10cSrcweir                                                 StringPair[][] aRelations )
176cdf0e10cSrcweir     {
177cdf0e10cSrcweir         boolean bOk = false;
178cdf0e10cSrcweir 
179cdf0e10cSrcweir         // get access to the XPropertySet interface
180cdf0e10cSrcweir         XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStorage );
181cdf0e10cSrcweir         if ( xPropSet != null )
182cdf0e10cSrcweir         {
183cdf0e10cSrcweir             try
184cdf0e10cSrcweir             {
185cdf0e10cSrcweir                 // get "IsRoot" and "OpenMode" properties and control there values
186cdf0e10cSrcweir                 boolean bPropIsRoot = AnyConverter.toBoolean( xPropSet.getPropertyValue( "IsRoot" ) );
187cdf0e10cSrcweir                 int nPropMode = AnyConverter.toInt( xPropSet.getPropertyValue( "OpenMode" ) );
188cdf0e10cSrcweir 
189cdf0e10cSrcweir                 bOk = true;
190cdf0e10cSrcweir                 if ( bPropIsRoot != bIsRoot )
191cdf0e10cSrcweir                 {
192cdf0e10cSrcweir                     Error( "'IsRoot' property contains wrong value!" );
193cdf0e10cSrcweir                     bOk = false;
194cdf0e10cSrcweir                 }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir                 if ( ( bIsRoot
197cdf0e10cSrcweir                   && ( nPropMode | ElementModes.READ ) != ( nMode | ElementModes.READ ) )
198cdf0e10cSrcweir                   || ( !bIsRoot && ( nPropMode & nMode ) != nMode ) )
199cdf0e10cSrcweir                 {
200cdf0e10cSrcweir                     Error( "'OpenMode' property contains wrong value, expected " + nMode + ", in reality " + nPropMode + "!" );
201cdf0e10cSrcweir                     bOk = false;
202cdf0e10cSrcweir                 }
203cdf0e10cSrcweir             }
204cdf0e10cSrcweir             catch( Exception e )
205cdf0e10cSrcweir             {
206cdf0e10cSrcweir                 Error( "Can't control properties of substorage, exception: " + e );
207cdf0e10cSrcweir             }
208cdf0e10cSrcweir         }
209cdf0e10cSrcweir         else
210cdf0e10cSrcweir         {
211cdf0e10cSrcweir             Error( "Can't get XPropertySet implementation from storage!" );
212cdf0e10cSrcweir         }
213cdf0e10cSrcweir 
214cdf0e10cSrcweir         // get access to the relationship information
215cdf0e10cSrcweir         XRelationshipAccess xRelAccess = (XRelationshipAccess) UnoRuntime.queryInterface( XRelationshipAccess.class, xStorage );
216cdf0e10cSrcweir 
217cdf0e10cSrcweir         if ( xRelAccess == null )
218cdf0e10cSrcweir         {
219cdf0e10cSrcweir             Error( "Can't get XRelationshipAccess implementation from the storage!" );
220cdf0e10cSrcweir             return false;
221cdf0e10cSrcweir         }
222cdf0e10cSrcweir 
223cdf0e10cSrcweir         // set the relationship information
224cdf0e10cSrcweir         try
225cdf0e10cSrcweir         {
226cdf0e10cSrcweir             xRelAccess.insertRelationships( aRelations, false );
227cdf0e10cSrcweir         }
228cdf0e10cSrcweir         catch( Exception e )
229cdf0e10cSrcweir         {
230cdf0e10cSrcweir             Error( "Can't set relationships to the storage, exception: " + e );
231cdf0e10cSrcweir             return false;
232cdf0e10cSrcweir         }
233cdf0e10cSrcweir 
234cdf0e10cSrcweir 
235cdf0e10cSrcweir         return bOk;
236cdf0e10cSrcweir     }
237cdf0e10cSrcweir 
checkRelations( StringPair[][] aStorRels, StringPair[][] aTestRels )238cdf0e10cSrcweir     public boolean checkRelations( StringPair[][] aStorRels, StringPair[][] aTestRels )
239cdf0e10cSrcweir     {
240cdf0e10cSrcweir         // Message( "StorageRels:" );
241cdf0e10cSrcweir         // PrintRelations( aStorRels );
242cdf0e10cSrcweir         // Message( "TestRels:" );
243cdf0e10cSrcweir         // PrintRelations( aTestRels );
244cdf0e10cSrcweir 
245cdf0e10cSrcweir         if ( aStorRels.length != aTestRels.length )
246cdf0e10cSrcweir         {
247cdf0e10cSrcweir             Error( "The provided relations sequence has different size than the storage's one!" );
248cdf0e10cSrcweir             return false;
249cdf0e10cSrcweir         }
250cdf0e10cSrcweir 
251cdf0e10cSrcweir         for ( int nStorInd = 0; nStorInd < aStorRels.length; nStorInd++ )
252cdf0e10cSrcweir         {
253cdf0e10cSrcweir             int nStorIDInd = -1;
254cdf0e10cSrcweir             for ( int nStorTagInd = 0; nStorTagInd < aStorRels[nStorInd].length; nStorTagInd++ )
255cdf0e10cSrcweir             {
256cdf0e10cSrcweir                 if ( aStorRels[nStorInd][nStorTagInd].First.equals( "Id" ) )
257cdf0e10cSrcweir                 {
258cdf0e10cSrcweir                     nStorIDInd = nStorTagInd;
259cdf0e10cSrcweir                     break;
260cdf0e10cSrcweir                 }
261cdf0e10cSrcweir             }
262cdf0e10cSrcweir 
263cdf0e10cSrcweir             if ( nStorIDInd == -1 )
264cdf0e10cSrcweir             {
265cdf0e10cSrcweir                 Error( "One of the storage relations entries has no ID!" );
266cdf0e10cSrcweir                 return false;
267cdf0e10cSrcweir             }
268cdf0e10cSrcweir 
269cdf0e10cSrcweir             for ( int nInd = 0; nInd < aTestRels.length; nInd++ )
270cdf0e10cSrcweir             {
271cdf0e10cSrcweir                 int nIDInd = -1;
272cdf0e10cSrcweir                 for ( int nTagInd = 0; nTagInd < aTestRels[nInd].length; nTagInd++ )
273cdf0e10cSrcweir                 {
274cdf0e10cSrcweir                     if ( aTestRels[nInd][nTagInd].First.equals( "Id" ) )
275cdf0e10cSrcweir                     {
276cdf0e10cSrcweir                         nIDInd = nTagInd;
277cdf0e10cSrcweir                         break;
278cdf0e10cSrcweir                     }
279cdf0e10cSrcweir                 }
280cdf0e10cSrcweir 
281cdf0e10cSrcweir                 if ( nIDInd == -1 )
282cdf0e10cSrcweir                 {
283cdf0e10cSrcweir                     Error( "One of the test hardcoded entries has no ID, num = " + nInd + ", length = " + aTestRels[nInd].length + ", global length = " + aTestRels.length + "!" );
284cdf0e10cSrcweir                     return false;
285cdf0e10cSrcweir                 }
286cdf0e10cSrcweir 
287cdf0e10cSrcweir                 if ( aStorRels[nStorInd][nStorIDInd].Second.equals( aTestRels[nInd][nIDInd].Second ) )
288cdf0e10cSrcweir                 {
289cdf0e10cSrcweir                     boolean[] pRelCheckMark = new boolean[ aTestRels[nInd].length ];
290cdf0e10cSrcweir                     for ( int nCheckInd = 0; nCheckInd < pRelCheckMark.length; nCheckInd++ )
291cdf0e10cSrcweir                     {
292cdf0e10cSrcweir                         pRelCheckMark[nCheckInd] = false;
293cdf0e10cSrcweir                     }
294cdf0e10cSrcweir 
295cdf0e10cSrcweir                     for ( int nStorTagInd = 0; nStorTagInd < aStorRels[nStorInd].length; nStorTagInd++ )
296cdf0e10cSrcweir                     {
297cdf0e10cSrcweir                         boolean bFound = false;
298cdf0e10cSrcweir                         for ( int nTagInd = 0; nTagInd < aTestRels[nInd].length; nTagInd++ )
299cdf0e10cSrcweir                         {
300cdf0e10cSrcweir                             if ( aTestRels[nInd][nTagInd].First.equals( aStorRels[nStorInd][nStorTagInd].First ) )
301cdf0e10cSrcweir                             {
302cdf0e10cSrcweir                                 if ( !aTestRels[nInd][nTagInd].Second.equals( aStorRels[nStorInd][nStorTagInd].Second ) )
303cdf0e10cSrcweir                                 {
304cdf0e10cSrcweir                                     Error( "Test rel. num. " + nInd + " has different tag \"" + aTestRels[nInd][nTagInd].First + "\" value!" );
305cdf0e10cSrcweir                                     return false;
306cdf0e10cSrcweir                                 }
307cdf0e10cSrcweir 
308cdf0e10cSrcweir                                 bFound = true;
309cdf0e10cSrcweir                                 pRelCheckMark[nTagInd] = true;
310cdf0e10cSrcweir                                 break;
311cdf0e10cSrcweir                             }
312cdf0e10cSrcweir                         }
313cdf0e10cSrcweir 
314cdf0e10cSrcweir                         if ( !bFound )
315cdf0e10cSrcweir                         {
316cdf0e10cSrcweir                             Error( "Stor rel. num. " + nStorInd + " has unexpected tag \"" + aStorRels[nStorInd][nStorTagInd].First + "\", ID = \"" + aStorRels[nStorInd][nStorIDInd].Second + "\"!" );
317cdf0e10cSrcweir                             return false;
318cdf0e10cSrcweir                         }
319cdf0e10cSrcweir                     }
320cdf0e10cSrcweir 
321cdf0e10cSrcweir                     for ( int nCheckInd = 0; nCheckInd < pRelCheckMark.length; nCheckInd++ )
322cdf0e10cSrcweir                     {
323cdf0e10cSrcweir                         if ( !pRelCheckMark[nCheckInd] && !aTestRels[nInd][nCheckInd].Second.equals( "" ) )
324cdf0e10cSrcweir                         {
325cdf0e10cSrcweir                             Error( "Test rel. num. " + nInd + " has unexpected tag \"" + aTestRels[nInd][nCheckInd].First + "\" with nonempty value!" );
326cdf0e10cSrcweir                             return false;
327cdf0e10cSrcweir                         }
328cdf0e10cSrcweir                     }
329cdf0e10cSrcweir 
330cdf0e10cSrcweir                     break;
331cdf0e10cSrcweir                 }
332cdf0e10cSrcweir             }
333cdf0e10cSrcweir         }
334cdf0e10cSrcweir 
335cdf0e10cSrcweir         return true;
336cdf0e10cSrcweir     }
337cdf0e10cSrcweir 
checkStorageProperties( XStorage xStorage, boolean bIsRoot, int nMode, StringPair[][] aRelations )338cdf0e10cSrcweir     public boolean checkStorageProperties( XStorage xStorage,
339cdf0e10cSrcweir                                             boolean bIsRoot,
340cdf0e10cSrcweir                                             int nMode,
341cdf0e10cSrcweir                                             StringPair[][] aRelations )
342cdf0e10cSrcweir     {
343cdf0e10cSrcweir         boolean bOk = false;
344cdf0e10cSrcweir 
345cdf0e10cSrcweir         // get access to the XPropertySet interface
346cdf0e10cSrcweir         XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStorage );
347cdf0e10cSrcweir         if ( xPropSet != null )
348cdf0e10cSrcweir         {
349cdf0e10cSrcweir             try
350cdf0e10cSrcweir             {
351cdf0e10cSrcweir                 // get "IsRoot" and "OpenMode" properties and control there values
352cdf0e10cSrcweir                 boolean bPropIsRoot = AnyConverter.toBoolean( xPropSet.getPropertyValue( "IsRoot" ) );
353cdf0e10cSrcweir                 int nPropMode = AnyConverter.toInt( xPropSet.getPropertyValue( "OpenMode" ) );
354cdf0e10cSrcweir 
355cdf0e10cSrcweir                 bOk = true;
356cdf0e10cSrcweir                 if ( bPropIsRoot != bIsRoot )
357cdf0e10cSrcweir                 {
358cdf0e10cSrcweir                     Error( "'IsRoot' property contains wrong value!" );
359cdf0e10cSrcweir                     bOk = false;
360cdf0e10cSrcweir                 }
361cdf0e10cSrcweir 
362cdf0e10cSrcweir                 if ( ( bIsRoot
363cdf0e10cSrcweir                   && ( nPropMode | ElementModes.READ ) != ( nMode | ElementModes.READ ) )
364cdf0e10cSrcweir                   || ( !bIsRoot && ( nPropMode & nMode ) != nMode ) )
365cdf0e10cSrcweir                 {
366cdf0e10cSrcweir                     Error( "'OpenMode' property contains wrong value, expected " + nMode + ", in reality " + nPropMode + "!" );
367cdf0e10cSrcweir                     bOk = false;
368cdf0e10cSrcweir                 }
369cdf0e10cSrcweir             }
370cdf0e10cSrcweir             catch( Exception e )
371cdf0e10cSrcweir             {
372cdf0e10cSrcweir                 Error( "Can't get properties of substorage, exception: " + e );
373cdf0e10cSrcweir             }
374cdf0e10cSrcweir         }
375cdf0e10cSrcweir         else
376cdf0e10cSrcweir         {
377cdf0e10cSrcweir             Error( "Can't get XPropertySet implementation from storage!" );
378cdf0e10cSrcweir         }
379cdf0e10cSrcweir 
380cdf0e10cSrcweir         // get access to the relationship information
381cdf0e10cSrcweir         XRelationshipAccess xRelAccess = (XRelationshipAccess) UnoRuntime.queryInterface( XRelationshipAccess.class, xStorage );
382cdf0e10cSrcweir 
383cdf0e10cSrcweir         if ( xRelAccess == null )
384cdf0e10cSrcweir         {
385cdf0e10cSrcweir             Error( "Can't get XRelationshipAccess implementation from the checked storage!" );
386cdf0e10cSrcweir             return false;
387cdf0e10cSrcweir         }
388cdf0e10cSrcweir 
389cdf0e10cSrcweir         // get the relationship information
390cdf0e10cSrcweir         StringPair[][] aStorRels;
391cdf0e10cSrcweir         try
392cdf0e10cSrcweir         {
393cdf0e10cSrcweir             aStorRels = xRelAccess.getAllRelationships();
394cdf0e10cSrcweir         }
395cdf0e10cSrcweir         catch( Exception e )
396cdf0e10cSrcweir         {
397cdf0e10cSrcweir             Error( "Can't get relationships of the checked storage, exception: " + e );
398cdf0e10cSrcweir             return false;
399cdf0e10cSrcweir         }
400cdf0e10cSrcweir 
401cdf0e10cSrcweir         if ( !checkRelations( aStorRels, aRelations ) )
402cdf0e10cSrcweir         {
403cdf0e10cSrcweir             Error( "StorageRelationsChecking has failed!" );
404cdf0e10cSrcweir             return false;
405cdf0e10cSrcweir         }
406cdf0e10cSrcweir 
407cdf0e10cSrcweir         return bOk;
408cdf0e10cSrcweir     }
409cdf0e10cSrcweir 
InternalCheckStream( XStream xStream, String sName, String sMediaType, byte[] pBytes, StringPair[][] aRelations )410cdf0e10cSrcweir     public boolean InternalCheckStream( XStream xStream,
411cdf0e10cSrcweir                                         String sName,
412cdf0e10cSrcweir                                         String sMediaType,
413cdf0e10cSrcweir                                         byte[] pBytes,
414cdf0e10cSrcweir                                         StringPair[][] aRelations )
415cdf0e10cSrcweir     {
416cdf0e10cSrcweir         // get input stream of substream
417cdf0e10cSrcweir         XInputStream xInput = xStream.getInputStream();
418cdf0e10cSrcweir         if ( xInput == null )
419cdf0e10cSrcweir         {
420cdf0e10cSrcweir             Error( "Can't get XInputStream implementation from substream '" + sName + "'!" );
421cdf0e10cSrcweir             return false;
422cdf0e10cSrcweir         }
423cdf0e10cSrcweir 
424cdf0e10cSrcweir         byte pContents[][] = new byte[1][]; // ???
425cdf0e10cSrcweir 
426cdf0e10cSrcweir         // read contents
427cdf0e10cSrcweir         try
428cdf0e10cSrcweir         {
429cdf0e10cSrcweir             xInput.readBytes( pContents, pBytes.length + 1 );
430cdf0e10cSrcweir         }
431cdf0e10cSrcweir         catch( Exception e )
432cdf0e10cSrcweir         {
433cdf0e10cSrcweir             Error( "Can't read from stream '" + sName + "', exception: " + e );
434cdf0e10cSrcweir             return false;
435cdf0e10cSrcweir         }
436cdf0e10cSrcweir 
437cdf0e10cSrcweir         // check size of stream data
438cdf0e10cSrcweir         if ( pContents.length == 0 )
439cdf0e10cSrcweir         {
440cdf0e10cSrcweir             Error( "SubStream '" + sName + "' reading produced disaster!"  );
441cdf0e10cSrcweir             return false;
442cdf0e10cSrcweir         }
443cdf0e10cSrcweir 
444cdf0e10cSrcweir         if ( pBytes.length != pContents[0].length )
445cdf0e10cSrcweir         {
446cdf0e10cSrcweir             Error( "SubStream '" + sName + "' contains wrong amount of data! (" + pContents[0].length + "/" + pBytes.length + ")" );
447cdf0e10cSrcweir             return false;
448cdf0e10cSrcweir         }
449cdf0e10cSrcweir 
450cdf0e10cSrcweir         // check stream data
451cdf0e10cSrcweir         for ( int ind = 0; ind < pBytes.length; ind++ )
452cdf0e10cSrcweir         {
453cdf0e10cSrcweir             if ( pBytes[ind] != pContents[0][ind] )
454cdf0e10cSrcweir             {
455cdf0e10cSrcweir                 Error( "SubStream '" + sName + "' contains wrong data! ( byte num. "
456cdf0e10cSrcweir                         + ind + " should be" + pBytes[ind] + " but it is " + pContents[0][ind] + ")" );
457cdf0e10cSrcweir                 return false;
458cdf0e10cSrcweir             }
459cdf0e10cSrcweir         }
460cdf0e10cSrcweir 
461cdf0e10cSrcweir         // check properties
462cdf0e10cSrcweir         boolean bOk = false;
463cdf0e10cSrcweir 
464cdf0e10cSrcweir         // get access to the XPropertySet interface
465cdf0e10cSrcweir         XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStream );
466cdf0e10cSrcweir         if ( xPropSet != null )
467cdf0e10cSrcweir         {
468cdf0e10cSrcweir             try
469cdf0e10cSrcweir             {
470cdf0e10cSrcweir                 // get "MediaType" and "Size" properties and control there values
471cdf0e10cSrcweir                 String sPropMediaType = AnyConverter.toString( xPropSet.getPropertyValue( "MediaType" ) );
472cdf0e10cSrcweir                 int nPropSize = AnyConverter.toInt( xPropSet.getPropertyValue( "Size" ) );
473cdf0e10cSrcweir 
474cdf0e10cSrcweir                 bOk = true;
475cdf0e10cSrcweir                 if ( !sPropMediaType.equals( sMediaType ) )
476cdf0e10cSrcweir                 {
477cdf0e10cSrcweir                     Error( "'MediaType' property contains wrong value for stream '" + sName + "',\nexpected: '"
478cdf0e10cSrcweir                             + sMediaType + "', set: '" + sPropMediaType + "'!" );
479cdf0e10cSrcweir                     bOk = false;
480cdf0e10cSrcweir                 }
481cdf0e10cSrcweir 
482cdf0e10cSrcweir                 if ( nPropSize != pBytes.length )
483cdf0e10cSrcweir                 {
484cdf0e10cSrcweir                     Error( "'Size' property contains wrong value for stream'" + sName + "'!" );
485cdf0e10cSrcweir                     bOk = false;
486cdf0e10cSrcweir                 }
487cdf0e10cSrcweir             }
488cdf0e10cSrcweir             catch( Exception e )
489cdf0e10cSrcweir             {
490cdf0e10cSrcweir                 Error( "Can't get properties of substream '" + sName + "', exception: " + e );
491cdf0e10cSrcweir             }
492cdf0e10cSrcweir         }
493cdf0e10cSrcweir         else
494cdf0e10cSrcweir         {
495cdf0e10cSrcweir             Error( "Can't get XPropertySet implementation from stream '" + sName + "'!" );
496cdf0e10cSrcweir         }
497cdf0e10cSrcweir 
498cdf0e10cSrcweir 
499cdf0e10cSrcweir         // get access to the relationship information
500cdf0e10cSrcweir         XRelationshipAccess xRelAccess = (XRelationshipAccess) UnoRuntime.queryInterface( XRelationshipAccess.class, xStream );
501cdf0e10cSrcweir 
502cdf0e10cSrcweir         if ( xRelAccess == null )
503cdf0e10cSrcweir         {
504cdf0e10cSrcweir             Error( "Can't get XRelationshipAccess implementation from the stream\"" + sName + "\"!" );
505cdf0e10cSrcweir             return false;
506cdf0e10cSrcweir         }
507cdf0e10cSrcweir 
508cdf0e10cSrcweir         // get the relationship information
509cdf0e10cSrcweir         StringPair[][] aStorRels;
510cdf0e10cSrcweir         try
511cdf0e10cSrcweir         {
512cdf0e10cSrcweir             aStorRels = xRelAccess.getAllRelationships();
513cdf0e10cSrcweir         }
514cdf0e10cSrcweir         catch( Exception e )
515cdf0e10cSrcweir         {
516cdf0e10cSrcweir             Error( "Can't get relationships of the substream '" + sName + "', exception: " + e );
517cdf0e10cSrcweir             return false;
518cdf0e10cSrcweir         }
519cdf0e10cSrcweir 
520cdf0e10cSrcweir         if ( !checkRelations( aStorRels, aRelations ) )
521cdf0e10cSrcweir         {
522cdf0e10cSrcweir             Error( "Stream '" + sName + "' RelationsChecking has failed!" );
523cdf0e10cSrcweir             return false;
524cdf0e10cSrcweir         }
525cdf0e10cSrcweir 
526cdf0e10cSrcweir         return bOk;
527cdf0e10cSrcweir     }
528cdf0e10cSrcweir 
checkStream( XStorage xParentStorage, String sName, String sMediaType, byte[] pBytes, StringPair[][] aRelations )529cdf0e10cSrcweir     public boolean checkStream( XStorage xParentStorage,
530cdf0e10cSrcweir                                 String sName,
531cdf0e10cSrcweir                                 String sMediaType,
532cdf0e10cSrcweir                                 byte[] pBytes,
533cdf0e10cSrcweir                                 StringPair[][] aRelations )
534cdf0e10cSrcweir     {
535cdf0e10cSrcweir         // open substream element first
536cdf0e10cSrcweir         XStream xSubStream = null;
537cdf0e10cSrcweir         try
538cdf0e10cSrcweir         {
539cdf0e10cSrcweir             Object oSubStream = xParentStorage.openStreamElement( sName, ElementModes.READ );
540cdf0e10cSrcweir             xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
541cdf0e10cSrcweir             if ( xSubStream == null )
542cdf0e10cSrcweir             {
543cdf0e10cSrcweir                 Error( "Can't open substream '" + sName + "'!" );
544cdf0e10cSrcweir                 return false;
545cdf0e10cSrcweir             }
546cdf0e10cSrcweir         }
547cdf0e10cSrcweir         catch( Exception e )
548cdf0e10cSrcweir         {
549cdf0e10cSrcweir             Error( "Can't open substream '" + sName + "', exception : " + e + "!" );
550cdf0e10cSrcweir             return false;
551cdf0e10cSrcweir         }
552cdf0e10cSrcweir 
553cdf0e10cSrcweir         boolean bResult = InternalCheckStream( xSubStream, sName, sMediaType, pBytes, aRelations );
554cdf0e10cSrcweir 
555cdf0e10cSrcweir         // free the stream resources, garbage collector may remove the object too late
556cdf0e10cSrcweir         if ( !disposeStream( xSubStream, sName ) )
557cdf0e10cSrcweir             return false;
558cdf0e10cSrcweir 
559cdf0e10cSrcweir         return bResult;
560cdf0e10cSrcweir     }
561cdf0e10cSrcweir 
copyStorage( XStorage xSourceStorage, XStorage xDestStorage )562cdf0e10cSrcweir     public boolean copyStorage( XStorage xSourceStorage, XStorage xDestStorage )
563cdf0e10cSrcweir     {
564cdf0e10cSrcweir         // copy xSourceStorage to xDestStorage
565cdf0e10cSrcweir         try
566cdf0e10cSrcweir         {
567cdf0e10cSrcweir             xSourceStorage.copyToStorage( xDestStorage );
568cdf0e10cSrcweir         }
569cdf0e10cSrcweir         catch( Exception e )
570cdf0e10cSrcweir         {
571cdf0e10cSrcweir             Error( "Storage copying failed, exception: " + e );
572cdf0e10cSrcweir             return false;
573cdf0e10cSrcweir         }
574cdf0e10cSrcweir 
575cdf0e10cSrcweir         return true;
576cdf0e10cSrcweir     }
577cdf0e10cSrcweir 
commitStorage( XStorage xStorage )578cdf0e10cSrcweir     public boolean commitStorage( XStorage xStorage )
579cdf0e10cSrcweir     {
580cdf0e10cSrcweir         // XTransactedObject must be supported by storages
581cdf0e10cSrcweir         XTransactedObject xTransact = (XTransactedObject) UnoRuntime.queryInterface( XTransactedObject.class, xStorage );
582cdf0e10cSrcweir         if ( xTransact == null )
583cdf0e10cSrcweir         {
584cdf0e10cSrcweir             Error( "Storage doesn't implement transacted access!" );
585cdf0e10cSrcweir             return false;
586cdf0e10cSrcweir         }
587cdf0e10cSrcweir 
588cdf0e10cSrcweir         try
589cdf0e10cSrcweir         {
590cdf0e10cSrcweir             xTransact.commit();
591cdf0e10cSrcweir         }
592cdf0e10cSrcweir         catch( Exception e )
593cdf0e10cSrcweir         {
594cdf0e10cSrcweir             Error( "Storage commit failed, exception:" + e );
595cdf0e10cSrcweir             return false;
596cdf0e10cSrcweir         }
597cdf0e10cSrcweir 
598cdf0e10cSrcweir         return true;
599cdf0e10cSrcweir     }
600cdf0e10cSrcweir 
disposeStream( XStream xStream, String sStreamName )601cdf0e10cSrcweir     public boolean disposeStream( XStream xStream, String sStreamName )
602cdf0e10cSrcweir     {
603cdf0e10cSrcweir         XComponent xComponent = (XComponent) UnoRuntime.queryInterface( XComponent.class, xStream );
604cdf0e10cSrcweir         if ( xComponent == null )
605cdf0e10cSrcweir         {
606cdf0e10cSrcweir             Error( "Can't get XComponent implementation from substream '" + sStreamName + "'!" );
607cdf0e10cSrcweir             return false;
608cdf0e10cSrcweir         }
609cdf0e10cSrcweir 
610cdf0e10cSrcweir         try
611cdf0e10cSrcweir         {
612cdf0e10cSrcweir             xComponent.dispose();
613cdf0e10cSrcweir         }
614cdf0e10cSrcweir         catch( Exception e )
615cdf0e10cSrcweir         {
616cdf0e10cSrcweir             Error( "Substream '" + sStreamName + "' disposing throws exception: " + e );
617cdf0e10cSrcweir             return false;
618cdf0e10cSrcweir         }
619cdf0e10cSrcweir 
620cdf0e10cSrcweir         return true;
621cdf0e10cSrcweir     }
622cdf0e10cSrcweir 
disposeStorage( XStorage xStorage )623cdf0e10cSrcweir     public boolean disposeStorage( XStorage xStorage )
624cdf0e10cSrcweir     {
625cdf0e10cSrcweir         // dispose the storage
626cdf0e10cSrcweir         XComponent xComponent = (XComponent) UnoRuntime.queryInterface( XComponent.class, xStorage );
627cdf0e10cSrcweir         if ( xComponent == null )
628cdf0e10cSrcweir         {
629cdf0e10cSrcweir             Error( "Can't retrieve XComponent implementation from storage!" );
630cdf0e10cSrcweir             return false;
631cdf0e10cSrcweir         }
632cdf0e10cSrcweir 
633cdf0e10cSrcweir         try
634cdf0e10cSrcweir         {
635cdf0e10cSrcweir             xComponent.dispose();
636cdf0e10cSrcweir         }
637cdf0e10cSrcweir         catch( Exception e )
638cdf0e10cSrcweir         {
639cdf0e10cSrcweir             Error( "Storage disposing failed!" );
640cdf0e10cSrcweir             return false;
641cdf0e10cSrcweir         }
642cdf0e10cSrcweir 
643cdf0e10cSrcweir         return true;
644cdf0e10cSrcweir     }
645cdf0e10cSrcweir 
getInputStream( XStream xStream )646cdf0e10cSrcweir     public XInputStream getInputStream( XStream xStream )
647cdf0e10cSrcweir     {
648cdf0e10cSrcweir         XInputStream xInTemp = null;
649cdf0e10cSrcweir         try
650cdf0e10cSrcweir         {
651cdf0e10cSrcweir             xInTemp = xStream.getInputStream();
652cdf0e10cSrcweir             if ( xInTemp == null )
653cdf0e10cSrcweir                 Error( "Can't get the input part of a stream!" );
654cdf0e10cSrcweir         }
655cdf0e10cSrcweir         catch ( Exception e )
656cdf0e10cSrcweir         {
657cdf0e10cSrcweir             Error( "Can't get the input part of a stream, exception :" + e );
658cdf0e10cSrcweir         }
659cdf0e10cSrcweir 
660cdf0e10cSrcweir         return xInTemp;
661cdf0e10cSrcweir     }
662cdf0e10cSrcweir 
closeOutput( XStream xStream )663cdf0e10cSrcweir     public boolean closeOutput( XStream xStream )
664cdf0e10cSrcweir     {
665cdf0e10cSrcweir         XOutputStream xOutTemp = null;
666cdf0e10cSrcweir         try
667cdf0e10cSrcweir         {
668cdf0e10cSrcweir             xOutTemp = xStream.getOutputStream();
669cdf0e10cSrcweir             if ( xOutTemp == null )
670cdf0e10cSrcweir             {
671cdf0e10cSrcweir                 Error( "Can't get the output part of a stream!" );
672cdf0e10cSrcweir                 return false;
673cdf0e10cSrcweir             }
674cdf0e10cSrcweir         }
675cdf0e10cSrcweir         catch ( Exception e )
676cdf0e10cSrcweir         {
677cdf0e10cSrcweir             Error( "Can't get the output part of a stream, exception :" + e );
678cdf0e10cSrcweir             return false;
679cdf0e10cSrcweir         }
680cdf0e10cSrcweir 
681cdf0e10cSrcweir         try
682cdf0e10cSrcweir         {
683cdf0e10cSrcweir             xOutTemp.closeOutput();
684cdf0e10cSrcweir         }
685cdf0e10cSrcweir         catch ( Exception e )
686cdf0e10cSrcweir         {
687cdf0e10cSrcweir             Error( "Can't close output part of a stream, exception :" + e );
688cdf0e10cSrcweir             return false;
689cdf0e10cSrcweir         }
690cdf0e10cSrcweir 
691cdf0e10cSrcweir         return true;
692cdf0e10cSrcweir     }
693cdf0e10cSrcweir 
openSubStorage( XStorage xStorage, String sName, int nMode )694cdf0e10cSrcweir     public XStorage openSubStorage( XStorage xStorage, String sName, int nMode )
695cdf0e10cSrcweir     {
696cdf0e10cSrcweir         // open existing substorage
697cdf0e10cSrcweir         try
698cdf0e10cSrcweir         {
699cdf0e10cSrcweir             Object oSubStorage = xStorage.openStorageElement( sName, nMode );
700cdf0e10cSrcweir             XStorage xSubStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oSubStorage );
701cdf0e10cSrcweir             return xSubStorage;
702cdf0e10cSrcweir         }
703cdf0e10cSrcweir         catch( Exception e )
704cdf0e10cSrcweir         {
705cdf0e10cSrcweir             Error( "Can't open substorage '" + sName + "', exception: " + e );
706cdf0e10cSrcweir         }
707cdf0e10cSrcweir 
708cdf0e10cSrcweir         return null;
709cdf0e10cSrcweir     }
710cdf0e10cSrcweir 
CreateTempFileStream( XMultiServiceFactory xMSF )711cdf0e10cSrcweir     public XStream CreateTempFileStream( XMultiServiceFactory xMSF )
712cdf0e10cSrcweir     {
713cdf0e10cSrcweir         // try to get temporary file representation
714cdf0e10cSrcweir         XStream xTempFileStream = null;
715cdf0e10cSrcweir         try
716cdf0e10cSrcweir         {
717cdf0e10cSrcweir             Object oTempFile = xMSF.createInstance( "com.sun.star.io.TempFile" );
718cdf0e10cSrcweir             xTempFileStream = (XStream)UnoRuntime.queryInterface( XStream.class, oTempFile );
719cdf0e10cSrcweir         }
720cdf0e10cSrcweir         catch( Exception e )
721cdf0e10cSrcweir         {}
722cdf0e10cSrcweir 
723cdf0e10cSrcweir         if ( xTempFileStream == null )
724cdf0e10cSrcweir             Error( "Can't create temporary file!" );
725cdf0e10cSrcweir 
726cdf0e10cSrcweir         return xTempFileStream;
727cdf0e10cSrcweir     }
728cdf0e10cSrcweir 
CreateTempFile( XMultiServiceFactory xMSF )729cdf0e10cSrcweir     public String CreateTempFile( XMultiServiceFactory xMSF )
730cdf0e10cSrcweir     {
731cdf0e10cSrcweir         String sResult = null;
732cdf0e10cSrcweir 
733cdf0e10cSrcweir         // try to get temporary file representation
734cdf0e10cSrcweir         XPropertySet xTempFileProps = null;
735cdf0e10cSrcweir         try
736cdf0e10cSrcweir         {
737cdf0e10cSrcweir             Object oTempFile = xMSF.createInstance( "com.sun.star.io.TempFile" );
738cdf0e10cSrcweir             xTempFileProps = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, oTempFile );
739cdf0e10cSrcweir         }
740cdf0e10cSrcweir         catch( Exception e )
741cdf0e10cSrcweir         {}
742cdf0e10cSrcweir 
743cdf0e10cSrcweir         if ( xTempFileProps != null )
744cdf0e10cSrcweir         {
745cdf0e10cSrcweir             try
746cdf0e10cSrcweir             {
747cdf0e10cSrcweir                 xTempFileProps.setPropertyValue( "RemoveFile", new Boolean( false ) );
748cdf0e10cSrcweir                 sResult = AnyConverter.toString( xTempFileProps.getPropertyValue( "Uri" ) );
749cdf0e10cSrcweir             }
750cdf0e10cSrcweir             catch( Exception e )
751cdf0e10cSrcweir             {
752cdf0e10cSrcweir                 Error( "Can't control TempFile properties, exception: " + e );
753cdf0e10cSrcweir             }
754cdf0e10cSrcweir         }
755cdf0e10cSrcweir         else
756cdf0e10cSrcweir         {
757cdf0e10cSrcweir             Error( "Can't create temporary file representation!" );
758cdf0e10cSrcweir         }
759cdf0e10cSrcweir 
760cdf0e10cSrcweir         // close temporary file explicitly
761cdf0e10cSrcweir         try
762cdf0e10cSrcweir         {
763cdf0e10cSrcweir             XStream xStream = (XStream)UnoRuntime.queryInterface( XStream.class, xTempFileProps );
764cdf0e10cSrcweir             if ( xStream != null )
765cdf0e10cSrcweir             {
766cdf0e10cSrcweir                 XOutputStream xOut = xStream.getOutputStream();
767cdf0e10cSrcweir                 if ( xOut != null )
768cdf0e10cSrcweir                     xOut.closeOutput();
769cdf0e10cSrcweir 
770cdf0e10cSrcweir                 XInputStream xIn = xStream.getInputStream();
771cdf0e10cSrcweir                 if ( xIn != null )
772cdf0e10cSrcweir                     xIn.closeInput();
773cdf0e10cSrcweir             }
774cdf0e10cSrcweir             else
775cdf0e10cSrcweir                 Error( "Can't close TempFile!" );
776cdf0e10cSrcweir         }
777cdf0e10cSrcweir         catch( Exception e )
778cdf0e10cSrcweir         {
779cdf0e10cSrcweir             Error( "Can't close TempFile, exception: " + e );
780cdf0e10cSrcweir         }
781cdf0e10cSrcweir 
782cdf0e10cSrcweir         return sResult;
783cdf0e10cSrcweir     }
784cdf0e10cSrcweir 
copyElementTo( XStorage xSource, String sName, XStorage xDest )785cdf0e10cSrcweir     public boolean copyElementTo( XStorage xSource, String sName, XStorage xDest )
786cdf0e10cSrcweir     {
787cdf0e10cSrcweir         // copy element with name sName from xSource to xDest
788cdf0e10cSrcweir         try
789cdf0e10cSrcweir         {
790cdf0e10cSrcweir             xSource.copyElementTo( sName, xDest, sName );
791cdf0e10cSrcweir         }
792cdf0e10cSrcweir         catch( Exception e )
793cdf0e10cSrcweir         {
794cdf0e10cSrcweir             Error( "Element copying failed, exception: " + e );
795cdf0e10cSrcweir             return false;
796cdf0e10cSrcweir         }
797cdf0e10cSrcweir 
798cdf0e10cSrcweir         return true;
799cdf0e10cSrcweir     }
800cdf0e10cSrcweir 
copyElementTo( XStorage xSource, String sName, XStorage xDest, String sTargetName )801cdf0e10cSrcweir     public boolean copyElementTo( XStorage xSource, String sName, XStorage xDest, String sTargetName )
802cdf0e10cSrcweir     {
803cdf0e10cSrcweir         // copy element with name sName from xSource to xDest
804cdf0e10cSrcweir         try
805cdf0e10cSrcweir         {
806cdf0e10cSrcweir             xSource.copyElementTo( sName, xDest, sTargetName );
807cdf0e10cSrcweir         }
808cdf0e10cSrcweir         catch( Exception e )
809cdf0e10cSrcweir         {
810cdf0e10cSrcweir             Error( "Element copying failed, exception: " + e );
811cdf0e10cSrcweir             return false;
812cdf0e10cSrcweir         }
813cdf0e10cSrcweir 
814cdf0e10cSrcweir         return true;
815cdf0e10cSrcweir     }
816cdf0e10cSrcweir 
moveElementTo( XStorage xSource, String sName, XStorage xDest )817cdf0e10cSrcweir     public boolean moveElementTo( XStorage xSource, String sName, XStorage xDest )
818cdf0e10cSrcweir     {
819cdf0e10cSrcweir         // move element with name sName from xSource to xDest
820cdf0e10cSrcweir         try
821cdf0e10cSrcweir         {
822cdf0e10cSrcweir             xSource.moveElementTo( sName, xDest, sName );
823cdf0e10cSrcweir         }
824cdf0e10cSrcweir         catch( Exception e )
825cdf0e10cSrcweir         {
826cdf0e10cSrcweir             Error( "Element moving failed, exception: " + e );
827cdf0e10cSrcweir             return false;
828cdf0e10cSrcweir         }
829cdf0e10cSrcweir 
830cdf0e10cSrcweir         return true;
831cdf0e10cSrcweir     }
832cdf0e10cSrcweir 
renameElement( XStorage xStorage, String sOldName, String sNewName )833cdf0e10cSrcweir     public boolean renameElement( XStorage xStorage, String sOldName, String sNewName )
834cdf0e10cSrcweir     {
835cdf0e10cSrcweir         // rename element with name sOldName to sNewName
836cdf0e10cSrcweir         try
837cdf0e10cSrcweir         {
838cdf0e10cSrcweir             xStorage.renameElement( sOldName, sNewName );
839cdf0e10cSrcweir         }
840cdf0e10cSrcweir         catch( Exception e )
841cdf0e10cSrcweir         {
842cdf0e10cSrcweir             Error( "Element renaming failed, exception: " + e );
843cdf0e10cSrcweir             return false;
844cdf0e10cSrcweir         }
845cdf0e10cSrcweir 
846cdf0e10cSrcweir         return true;
847cdf0e10cSrcweir     }
848cdf0e10cSrcweir 
removeElement( XStorage xStorage, String sName )849cdf0e10cSrcweir     public boolean removeElement( XStorage xStorage, String sName )
850cdf0e10cSrcweir     {
851cdf0e10cSrcweir         // remove element with name sName
852cdf0e10cSrcweir         try
853cdf0e10cSrcweir         {
854cdf0e10cSrcweir             xStorage.removeElement( sName );
855cdf0e10cSrcweir         }
856cdf0e10cSrcweir         catch( Exception e )
857cdf0e10cSrcweir         {
858cdf0e10cSrcweir             Error( "Element removing failed, exception: " + e );
859cdf0e10cSrcweir             return false;
860cdf0e10cSrcweir         }
861cdf0e10cSrcweir 
862cdf0e10cSrcweir         return true;
863cdf0e10cSrcweir     }
864cdf0e10cSrcweir 
OpenStream( XStorage xStorage, String sStreamName, int nMode )865cdf0e10cSrcweir     public XStream OpenStream( XStorage xStorage,
866cdf0e10cSrcweir                                 String sStreamName,
867cdf0e10cSrcweir                                 int nMode )
868cdf0e10cSrcweir     {
869cdf0e10cSrcweir         // open substream element
870cdf0e10cSrcweir         XStream xSubStream = null;
871cdf0e10cSrcweir         try
872cdf0e10cSrcweir         {
873cdf0e10cSrcweir             Object oSubStream = xStorage.openStreamElement( sStreamName, nMode );
874cdf0e10cSrcweir             xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
875cdf0e10cSrcweir             if ( xSubStream == null )
876cdf0e10cSrcweir                 Error( "Can't create substream '" + sStreamName + "'!" );
877cdf0e10cSrcweir         }
878cdf0e10cSrcweir         catch( Exception e )
879cdf0e10cSrcweir         {
880cdf0e10cSrcweir             Error( "Can't create substream '" + sStreamName + "', exception : " + e + "!" );
881cdf0e10cSrcweir         }
882cdf0e10cSrcweir 
883cdf0e10cSrcweir         return xSubStream;
884cdf0e10cSrcweir     }
885cdf0e10cSrcweir 
cantOpenStorage( XStorage xStorage, String sName )886cdf0e10cSrcweir     public boolean cantOpenStorage( XStorage xStorage, String sName )
887cdf0e10cSrcweir     {
888cdf0e10cSrcweir         // try to open an opened substorage, open call must fail
889cdf0e10cSrcweir         try
890cdf0e10cSrcweir         {
891cdf0e10cSrcweir             Object oDummyStorage = xStorage.openStorageElement( sName, ElementModes.READ );
892cdf0e10cSrcweir             Error( "The trying to reopen opened substorage '" + sName + "' must fail!" );
893cdf0e10cSrcweir         }
894cdf0e10cSrcweir         catch( Exception e )
895cdf0e10cSrcweir         {
896cdf0e10cSrcweir             return true;
897cdf0e10cSrcweir         }
898cdf0e10cSrcweir 
899cdf0e10cSrcweir         return false;
900cdf0e10cSrcweir     }
901cdf0e10cSrcweir 
cantOpenStream( XStorage xStorage, String sName, int nMode )902cdf0e10cSrcweir     public boolean cantOpenStream( XStorage xStorage, String sName, int nMode )
903cdf0e10cSrcweir     {
904cdf0e10cSrcweir         // try to open the substream with specified mode must fail
905cdf0e10cSrcweir         try
906cdf0e10cSrcweir         {
907cdf0e10cSrcweir             Object oDummyStream = xStorage.openStreamElement( sName, nMode );
908cdf0e10cSrcweir             Error( "The trying to open substoream '" + sName + "' must fail!" );
909cdf0e10cSrcweir         }
910cdf0e10cSrcweir         catch( Exception e )
911cdf0e10cSrcweir         {
912cdf0e10cSrcweir             return true;
913cdf0e10cSrcweir         }
914cdf0e10cSrcweir 
915cdf0e10cSrcweir         return false;
916cdf0e10cSrcweir     }
917cdf0e10cSrcweir 
createStorageFromURL( XSingleServiceFactory xFactory, String aURL, int nMode )918cdf0e10cSrcweir     public XStorage createStorageFromURL(
919cdf0e10cSrcweir                                 XSingleServiceFactory xFactory,
920cdf0e10cSrcweir                                 String aURL,
921cdf0e10cSrcweir                                 int nMode )
922cdf0e10cSrcweir     {
923cdf0e10cSrcweir         XStorage xResult = null;
924cdf0e10cSrcweir 
925cdf0e10cSrcweir         try
926cdf0e10cSrcweir         {
927cdf0e10cSrcweir             PropertyValue[] aAddArgs = new PropertyValue[1];
928cdf0e10cSrcweir             aAddArgs[0] = new PropertyValue();
929cdf0e10cSrcweir             aAddArgs[0].Name = "StorageFormat";
930cdf0e10cSrcweir             aAddArgs[0].Value = "OFOPXMLFormat";
931cdf0e10cSrcweir 
932cdf0e10cSrcweir             Object pArgs[] = new Object[3];
933cdf0e10cSrcweir             pArgs[0] = (Object) aURL;
934cdf0e10cSrcweir             pArgs[1] = new Integer( nMode );
935cdf0e10cSrcweir             pArgs[2] = (Object) aAddArgs;
936cdf0e10cSrcweir 
937cdf0e10cSrcweir             Object oTempStorage = xFactory.createInstanceWithArguments( pArgs );
938cdf0e10cSrcweir             xResult = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
939cdf0e10cSrcweir         }
940cdf0e10cSrcweir         catch( Exception e )
941cdf0e10cSrcweir         {
942cdf0e10cSrcweir             Error( "Can't create storage from URL, exception: " + e );
943cdf0e10cSrcweir             return null;
944cdf0e10cSrcweir         }
945cdf0e10cSrcweir 
946cdf0e10cSrcweir         if ( xResult == null )
947cdf0e10cSrcweir             Error( "Can't create storage from URL!" );
948cdf0e10cSrcweir 
949cdf0e10cSrcweir         return xResult;
950cdf0e10cSrcweir     }
951cdf0e10cSrcweir 
createStorageFromStream( XSingleServiceFactory xFactory, XStream xStream, int nMode )952cdf0e10cSrcweir     public XStorage createStorageFromStream(
953cdf0e10cSrcweir                                 XSingleServiceFactory xFactory,
954cdf0e10cSrcweir                                 XStream xStream,
955cdf0e10cSrcweir                                 int nMode )
956cdf0e10cSrcweir     {
957cdf0e10cSrcweir         XStorage xResult = null;
958cdf0e10cSrcweir 
959cdf0e10cSrcweir         try
960cdf0e10cSrcweir         {
961cdf0e10cSrcweir             PropertyValue[] aAddArgs = new PropertyValue[1];
962cdf0e10cSrcweir             aAddArgs[0] = new PropertyValue();
963cdf0e10cSrcweir             aAddArgs[0].Name = "StorageFormat";
964cdf0e10cSrcweir             aAddArgs[0].Value = "OFOPXMLFormat";
965cdf0e10cSrcweir 
966cdf0e10cSrcweir             Object pArgs[] = new Object[3];
967cdf0e10cSrcweir             pArgs[0] = (Object) xStream;
968cdf0e10cSrcweir             pArgs[1] = new Integer( nMode );
969cdf0e10cSrcweir             pArgs[2] = (Object) aAddArgs;
970cdf0e10cSrcweir 
971cdf0e10cSrcweir             Object oTempStorage = xFactory.createInstanceWithArguments( pArgs );
972cdf0e10cSrcweir             xResult = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
973cdf0e10cSrcweir         }
974cdf0e10cSrcweir         catch( Exception e )
975cdf0e10cSrcweir         {
976cdf0e10cSrcweir             Error( "Can't create storage from stream, exception: " + e );
977cdf0e10cSrcweir             return null;
978cdf0e10cSrcweir         }
979cdf0e10cSrcweir 
980cdf0e10cSrcweir         if ( xResult == null )
981cdf0e10cSrcweir             Error( "Can't create storage from stream!" );
982cdf0e10cSrcweir 
983cdf0e10cSrcweir         return xResult;
984cdf0e10cSrcweir     }
985cdf0e10cSrcweir 
createStorageFromInputStream( XSingleServiceFactory xFactory, XInputStream xInStream )986cdf0e10cSrcweir     public XStorage createStorageFromInputStream(
987cdf0e10cSrcweir                                 XSingleServiceFactory xFactory,
988cdf0e10cSrcweir                                 XInputStream xInStream )
989cdf0e10cSrcweir     {
990cdf0e10cSrcweir         XStorage xResult = null;
991cdf0e10cSrcweir 
992cdf0e10cSrcweir         try
993cdf0e10cSrcweir         {
994cdf0e10cSrcweir             PropertyValue[] aAddArgs = new PropertyValue[1];
995cdf0e10cSrcweir             aAddArgs[0] = new PropertyValue();
996cdf0e10cSrcweir             aAddArgs[0].Name = "StorageFormat";
997cdf0e10cSrcweir             aAddArgs[0].Value = "OFOPXMLFormat";
998cdf0e10cSrcweir 
999cdf0e10cSrcweir             Object pArgs[] = new Object[3];
1000cdf0e10cSrcweir             pArgs[0] = (Object) xInStream;
1001cdf0e10cSrcweir             pArgs[1] = new Integer( ElementModes.READ );
1002cdf0e10cSrcweir             pArgs[2] = (Object) aAddArgs;
1003cdf0e10cSrcweir 
1004cdf0e10cSrcweir             Object oTempStorage = xFactory.createInstanceWithArguments( pArgs );
1005cdf0e10cSrcweir             xResult = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage );
1006cdf0e10cSrcweir         }
1007cdf0e10cSrcweir         catch( Exception e )
1008cdf0e10cSrcweir         {
1009cdf0e10cSrcweir             Error( "Can't create storage from input stream, exception: " + e );
1010cdf0e10cSrcweir             return null;
1011cdf0e10cSrcweir         }
1012cdf0e10cSrcweir 
1013cdf0e10cSrcweir         if ( xResult == null )
1014cdf0e10cSrcweir             Error( "Can't create storage from input stream!" );
1015cdf0e10cSrcweir 
1016cdf0e10cSrcweir         return xResult;
1017cdf0e10cSrcweir     }
1018cdf0e10cSrcweir 
createTempStorage( XMultiServiceFactory xMSF, XSingleServiceFactory xFactory )1019cdf0e10cSrcweir     public XStorage createTempStorage( XMultiServiceFactory xMSF, XSingleServiceFactory xFactory )
1020cdf0e10cSrcweir     {
1021cdf0e10cSrcweir         // create a temporary storage
1022cdf0e10cSrcweir         XStorage xResult = null;
1023cdf0e10cSrcweir         XStream xStream = CreateTempFileStream( xMSF );
1024cdf0e10cSrcweir         if ( xStream == null )
1025cdf0e10cSrcweir         {
1026cdf0e10cSrcweir             Error( "Can't create temp file stream!" );
1027cdf0e10cSrcweir             return null;
1028cdf0e10cSrcweir         }
1029cdf0e10cSrcweir 
1030cdf0e10cSrcweir         try
1031cdf0e10cSrcweir         {
1032cdf0e10cSrcweir             xResult = createStorageFromStream( xFactory, xStream, ElementModes.WRITE );
1033cdf0e10cSrcweir         }
1034cdf0e10cSrcweir         catch( Exception e )
1035cdf0e10cSrcweir         {
1036cdf0e10cSrcweir             Error( "Can't create temp storage, exception: " + e );
1037cdf0e10cSrcweir         }
1038cdf0e10cSrcweir 
1039cdf0e10cSrcweir         return xResult;
1040cdf0e10cSrcweir     }
1041cdf0e10cSrcweir 
cloneStorage( XMultiServiceFactory xMSF, XSingleServiceFactory xFactory, XStorage xStorage )1042cdf0e10cSrcweir     public XStorage cloneStorage( XMultiServiceFactory xMSF, XSingleServiceFactory xFactory, XStorage xStorage )
1043cdf0e10cSrcweir     {
1044*a893be29SPedro Giffuni         // create a copy of a last committed version of specified storage
1045cdf0e10cSrcweir         XStorage xResult = null;
1046cdf0e10cSrcweir         try
1047cdf0e10cSrcweir         {
1048cdf0e10cSrcweir             xResult = createTempStorage( xMSF, xFactory );
1049cdf0e10cSrcweir             if ( xResult != null )
1050cdf0e10cSrcweir                 xStorage.copyLastCommitTo( xResult );
1051cdf0e10cSrcweir         }
1052cdf0e10cSrcweir         catch( Exception e )
1053cdf0e10cSrcweir         {
1054cdf0e10cSrcweir             Error( "Can't clone storage, exception: " + e );
1055cdf0e10cSrcweir             return null;
1056cdf0e10cSrcweir         }
1057cdf0e10cSrcweir 
1058cdf0e10cSrcweir         return xResult;
1059cdf0e10cSrcweir     }
1060cdf0e10cSrcweir 
cloneSubStorage( XMultiServiceFactory xMSF, XSingleServiceFactory xFactory, XStorage xStorage, String sName )1061cdf0e10cSrcweir     public XStorage cloneSubStorage( XMultiServiceFactory xMSF, XSingleServiceFactory xFactory, XStorage xStorage, String sName )
1062cdf0e10cSrcweir     {
1063*a893be29SPedro Giffuni         // create a copy of a last committed version of specified substorage
1064cdf0e10cSrcweir         XStorage xResult = null;
1065cdf0e10cSrcweir         try
1066cdf0e10cSrcweir         {
1067cdf0e10cSrcweir             xResult = createTempStorage( xMSF, xFactory );
1068cdf0e10cSrcweir             if ( xResult != null )
1069cdf0e10cSrcweir                 xStorage.copyStorageElementLastCommitTo( sName, xResult );
1070cdf0e10cSrcweir         }
1071cdf0e10cSrcweir         catch( Exception e )
1072cdf0e10cSrcweir         {
1073cdf0e10cSrcweir             Error( "Can't clone substorage '" + sName + "', exception: " + e );
1074cdf0e10cSrcweir             return null;
1075cdf0e10cSrcweir         }
1076cdf0e10cSrcweir 
1077cdf0e10cSrcweir         return xResult;
1078cdf0e10cSrcweir     }
1079cdf0e10cSrcweir 
cloneSubStream( XStorage xStorage, String sName )1080cdf0e10cSrcweir     public XStream cloneSubStream( XStorage xStorage, String sName )
1081cdf0e10cSrcweir     {
1082cdf0e10cSrcweir         // clone existing substream
1083cdf0e10cSrcweir         try
1084cdf0e10cSrcweir         {
1085cdf0e10cSrcweir             XStream xStream = xStorage.cloneStreamElement( sName );
1086cdf0e10cSrcweir             return xStream;
1087cdf0e10cSrcweir         }
1088cdf0e10cSrcweir         catch( Exception e )
1089cdf0e10cSrcweir         {
1090cdf0e10cSrcweir             Error( "Can't clone substream '" + sName + "', exception: " + e );
1091cdf0e10cSrcweir         }
1092cdf0e10cSrcweir 
1093cdf0e10cSrcweir         return null;
1094cdf0e10cSrcweir     }
1095cdf0e10cSrcweir 
Error( String sError )1096cdf0e10cSrcweir     public void Error( String sError )
1097cdf0e10cSrcweir     {
1098cdf0e10cSrcweir         m_aLogWriter.println( m_sTestPrefix + "Error: " + sError );
1099cdf0e10cSrcweir     }
1100cdf0e10cSrcweir 
Message( String sMessage )1101cdf0e10cSrcweir     public void Message( String sMessage )
1102cdf0e10cSrcweir     {
1103cdf0e10cSrcweir         m_aLogWriter.println( m_sTestPrefix + sMessage );
1104cdf0e10cSrcweir     }
1105cdf0e10cSrcweir 
PrintRelations( StringPair[][] aRels )1106cdf0e10cSrcweir     public void PrintRelations( StringPair[][] aRels )
1107cdf0e10cSrcweir     {
1108cdf0e10cSrcweir         m_aLogWriter.println( "========" );
1109cdf0e10cSrcweir         for ( int nInd1 = 0; nInd1 < aRels.length; nInd1++ )
1110cdf0e10cSrcweir         {
1111cdf0e10cSrcweir             for ( int nInd2 = 0; nInd2 < aRels[nInd1].length; nInd2++ )
1112cdf0e10cSrcweir             {
1113cdf0e10cSrcweir                 m_aLogWriter.println( "\"" + aRels[nInd1][nInd2].First + "\" = \"" + aRels[nInd1][nInd2].Second + "\", " );
1114cdf0e10cSrcweir             }
1115cdf0e10cSrcweir             m_aLogWriter.println( "========" );
1116cdf0e10cSrcweir         }
1117cdf0e10cSrcweir     }
1118cdf0e10cSrcweir }
1119