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