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