PageRenderTime 9ms CodeModel.GetById 8ms app.highlight 56ms RepoModel.GetById 1ms app.codeStats 1ms

/package/qa/storages/TestHelper.java

https://bitbucket.org/markjenkins/libreoffice_ubuntu-debian-fixes
Java | 1679 lines | 1376 code | 200 blank | 103 comment | 183 complexity | fb5ea408be7db9246fe7a32dd8a262eb MD5 | raw file

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file