/* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
// get access to the XPropertySet interface
XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xSubStream ); if ( xPropSet == null )
{
Error( "Can't get XPropertySet implementation from substream '" + sStreamName + "'!" ); returnfalse;
}
// set properties to the stream // do not set the compressed property try
{
xPropSet.setPropertyValue( "MediaType", sMediaType );
} catch( Exception e )
{
Error( "Can't set properties to substream '" + sStreamName + "', exception: " + e ); returnfalse;
}
// check size property of the stream try
{ long nSize = AnyConverter.toLong( xPropSet.getPropertyValue( "Size" ) ); if ( nSize != pBytes.length )
{
Error( "The 'Size' property of substream '" + sStreamName + "' contains wrong value!"); returnfalse;
}
} catch( Exception e )
{
Error( "Can't get 'Size' property from substream '" + sStreamName + "', exception: " + e ); returnfalse;
}
// free the stream resources, garbage collector may remove the object too late if ( !disposeStream( xSubStream, sStreamName ) ) returnfalse;
// change the password for the stream
XEncryptionProtectedSource xStreamEncryption =
(XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xSubStream );
if ( xStreamEncryption == null )
{
Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" ); return -1;
}
try {
xStreamEncryption.setEncryptionPassword( sNewPass );
} catch( Exception e )
{
Error( "Can't change encryption key of the substream '" + sStreamName + "', exception:" + e ); return 0;
}
// free the stream resources, garbage collector may remove the object too late if ( !disposeStream( xSubStream, sStreamName ) ) return 0;
return 1;
}
publicint ChangeStreamPassH( XStorage xStorage,
String sPath,
String sOldPass,
String sNewPass, boolean bCommit )
{ // open substream element
XHierarchicalStorageAccess xHStorage =
(XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage ); if ( xHStorage == null )
{
Error( "The storage does not support hierarchical access!" ); return 0;
}
// change the password for the stream
XEncryptionProtectedSource xStreamEncryption =
(XEncryptionProtectedSource) UnoRuntime.queryInterface( XEncryptionProtectedSource.class, xSubStream );
if ( xStreamEncryption == null )
{
Message( "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!" ); return -1;
}
try {
xStreamEncryption.setEncryptionPassword( sNewPass );
} catch( Exception e )
{
Error( "Can't change encryption key of the substream '" + sPath + "', exception:" + e ); return 0;
}
XTransactedObject xTransact =
(XTransactedObject) UnoRuntime.queryInterface( XTransactedObject.class, xSubStream ); if ( xTransact == null )
{
Error( "Substream '" + sPath + "', stream opened for writing must be transacted!" ); return 0;
}
if ( bCommit )
{ try {
xTransact.commit();
} catch( Exception e )
{
Error( "Can't commit storage after substream '" + sPath + "' change, exception : " + e + "!" ); return 0;
}
}
// free the stream resources, garbage collector may remove the object too late if ( !disposeStream( xSubStream, sPath ) ) return 0;
return 1;
}
publicboolean setStorageTypeAndCheckProps( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )
{ boolean bOk = false;
// get access to the XPropertySet interface
XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStorage ); if ( xPropSet != null )
{ try
{ // set "MediaType" property to the stream
xPropSet.setPropertyValue( "MediaType", sMediaType );
// get "IsRoot" and "OpenMode" properties and control there values boolean bPropIsRoot = AnyConverter.toBoolean( xPropSet.getPropertyValue( "IsRoot" ) ); int nPropMode = AnyConverter.toInt( xPropSet.getPropertyValue( "OpenMode" ) );
bOk = true; if ( bPropIsRoot != bIsRoot )
{
Error( "'IsRoot' property contains wrong value!" );
bOk = false;
}
if ( ( bIsRoot
&& ( nPropMode | ElementModes.READ ) != ( nMode | ElementModes.READ ) )
|| ( !bIsRoot && ( nPropMode & nMode ) != nMode ) )
{
Error( "'OpenMode' property contains wrong value, expected " + nMode + ", in reality " + nPropMode + "!" );
bOk = false;
}
} catch( Exception e )
{
Error( "Can't control properties of substorage, exception: " + e );
}
} else
{
Error( "Can't get XPropertySet implementation from storage!" );
}
return bOk;
}
publicboolean checkStorageProperties( XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode )
{ boolean bOk = false;
// get access to the XPropertySet interface
XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStorage ); if ( xPropSet != null )
{ try
{ // get "MediaType", "IsRoot" and "OpenMode" properties and control there values
String sPropMediaType = AnyConverter.toString( xPropSet.getPropertyValue( "MediaType") ); boolean bPropIsRoot = AnyConverter.toBoolean( xPropSet.getPropertyValue( "IsRoot" ) ); int nPropMode = AnyConverter.toInt( xPropSet.getPropertyValue( "OpenMode" ) );
bOk = true; if ( !sPropMediaType.equals( sMediaType ) )
{
Error( "'MediaType' property contains wrong value, expected '"
+ sMediaType + "', set '" + sPropMediaType + "' !" );
bOk = false;
}
if ( bPropIsRoot != bIsRoot )
{
Error( "'IsRoot' property contains wrong value!" );
bOk = false;
}
if ( ( bIsRoot
&& ( nPropMode | ElementModes.READ ) != ( nMode | ElementModes.READ ) )
|| ( !bIsRoot && ( nPropMode & nMode ) != nMode ) )
{
Error( "'OpenMode' property contains wrong value, expected " + nMode + ", in reality " + nPropMode + "!" );
bOk = false;
}
} catch( Exception e )
{
Error( "Can't get properties of substorage, exception: " + e );
}
} else
{
Error( "Can't get XPropertySet implementation from storage!" );
}
return bOk;
}
publicboolean InternalCheckStream( XStream xStream,
String sName,
String sMediaType, boolean bCompressed, byte[] pBytes, boolean bCheckCompressed )
{ // get input stream of substream
XInputStream xInput = xStream.getInputStream(); if ( xInput == null )
{
Error( "Can't get XInputStream implementation from substream '" + sName + "'!" ); returnfalse;
}
// check stream data for ( int ind = 0; ind < pBytes.length; ind++ )
{ if ( pBytes[ind] != pContents[0][ind] )
{
Error( "SubStream '" + sName + "' contains wrong data! ( byte num. "
+ ind + " should be " + pBytes[ind] + " but it is " + pContents[0][ind] + ")" ); returnfalse;
}
}
// check properties boolean bOk = false;
// get access to the XPropertySet interface
XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStream ); if ( xPropSet != null )
{ try
{ // get "MediaType" and "Size" properties and control there values
String sPropMediaType = AnyConverter.toString( xPropSet.getPropertyValue( "MediaType") ); long nPropSize = AnyConverter.toLong( xPropSet.getPropertyValue( "Size" ) ); boolean bPropCompress = AnyConverter.toBoolean( xPropSet.getPropertyValue( "Compressed" ) );
bOk = true; if ( !sPropMediaType.equals( sMediaType ) )
{
Error( "'MediaType' property contains wrong value for stream '" + sName + "',\nexpected: '"
+ sMediaType + "', set: '" + sPropMediaType + "'!" );
bOk = false;
}
if ( nPropSize != pBytes.length )
{
Error( "'Size' property contains wrong value for stream'" + sName + "'!" );
bOk = false;
}
if ( bCheckCompressed && bPropCompress != bCompressed )
{
Error( "'Compressed' property contains wrong value for stream'" + sName + "'!" );
bOk = false;
}
} catch( Exception e )
{
Error( "Can't get properties of substream '" + sName + "', exception: " + e );
}
} else
{
Error( "Can't get XPropertySet implementation from stream '" + sName + "'!" );
}
// free the stream resources, garbage collector may remove the object too late if ( !disposeStream( xSubStream, sName ) ) returnfalse;
return bResult;
}
publicboolean checkEncrStream( XStorage xParentStorage,
String sName,
String sMediaType, byte[] pBytes,
String sPass )
{ // Important: a common password for any of parent storage should not be set or // should be different from sPass
try
{
Object oSubStream = xParentStorage.openStreamElement( sName, ElementModes.READ );
Error( "Encrypted stream '" + sName + "' was opened without password!" ); returnfalse;
} catch( WrongPasswordException wpe )
{} catch( Exception e )
{
Error( "Unexpected exception in case of opening of encrypted stream '" + sName + "' without password: " + e + "!" ); returnfalse;
}
String sWrongPass = "11";
sWrongPass += sPass; try
{
Object oSubStream = xParentStorage.openEncryptedStreamElement( sName, ElementModes.READ, sWrongPass );
Error( "Encrypted stream '" + sName + "' was opened with wrong password!" ); returnfalse;
} catch( WrongPasswordException wpe )
{} catch( Exception e )
{
Error( "Unexpected exception in case of opening of encrypted stream '" + sName + "' with wrong password: " + e + "!" ); returnfalse;
}
// encrypted streams will be compressed always, so after the storing this property is always true, // although before the storing it can be set to false ( it is not always clear whether a stream is encrypted // before the storing ) boolean bResult = InternalCheckStream( xSubStream, sName, sMediaType, true, pBytes, false);
// free the stream resources, garbage collector may remove the object too late if ( !disposeStream( xSubStream, sName ) ) returnfalse;
return bResult;
}
publicboolean checkStreamH( XStorage xParentStorage,
String sPath,
String sMediaType, boolean bCompressed, byte[] pBytes )
{ // open substream element first
XStream xSubStream = null; try
{
XHierarchicalStorageAccess xHStorage =
(XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xParentStorage ); if ( xHStorage == null )
{
Error( "The storage does not support hierarchical access!" ); returnfalse;
}
// free the stream resources, garbage collector may remove the object too late if ( !disposeStream( xSubStream, sPath ) ) returnfalse;
return bResult;
}
publicboolean checkEncrStreamH( XStorage xParentStorage,
String sPath,
String sMediaType, byte[] pBytes,
String sPass )
{ // Important: a common password for any of parent storage should not be set or // should be different from sPass
XHierarchicalStorageAccess xHStorage =
(XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xParentStorage ); if ( xHStorage == null )
{
Error( "The storage does not support hierarchical access!" ); returnfalse;
}
try
{
Object oSubStream = xHStorage.openStreamElementByHierarchicalName( sPath, ElementModes.READ );
XStream xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
Error( "Encrypted substream '" + sPath + "' was opened without password!" ); returnfalse;
} catch( WrongPasswordException wpe )
{} catch( Exception e )
{
Error( "Unexpected exception in case of opening of encrypted stream '" + sPath + "' without password: " + e + "!" ); returnfalse;
}
String sWrongPass = "11";
sWrongPass += sPass; try
{
Object oSubStream = xHStorage.openEncryptedStreamElementByHierarchicalName( sPath, ElementModes.READ, sWrongPass );
XStream xSubStream = (XStream) UnoRuntime.queryInterface( XStream.class, oSubStream );
Error( "Encrypted substream '" + sPath + "' was opened with wrong password!" ); returnfalse;
} catch( WrongPasswordException wpe )
{} catch( Exception e )
{
Error( "Unexpected exception in case of opening of encrypted stream '" + sPath + "' with wrong password: " + e + "!" ); returnfalse;
}
// encrypted streams will be compressed always, so after the storing this property is always true, // although before the storing it can be set to false ( it is not always clear whether a stream is encrypted // before the storing ) boolean bResult = InternalCheckStream( xSubStream, sPath, sMediaType, true, pBytes, false);
// free the stream resources, garbage collector may remove the object too late if ( !disposeStream( xSubStream, sPath ) ) returnfalse;
public XInputStream getInputStream( XStream xStream )
{
XInputStream xInTemp = null; try
{
xInTemp = xStream.getInputStream(); if ( xInTemp == null )
Error( "Can't get the input part of a stream!" );
} catch ( Exception e )
{
Error( "Can't get the input part of a stream, exception :" + e );
}
return xInTemp;
}
publicboolean closeOutput( XStream xStream )
{
XOutputStream xOutTemp = null; try
{
xOutTemp = xStream.getOutputStream(); if ( xOutTemp == null )
{
Error( "Can't get the output part of a stream!" ); returnfalse;
}
} catch ( Exception e )
{
Error( "Can't get the output part of a stream, exception :" + e ); returnfalse;
}
try
{
xOutTemp.closeOutput();
} catch ( Exception e )
{
Error( "Can't close output part of a stream, exception :" + e ); returnfalse;
}
returntrue;
}
public XStorage openSubStorage( XStorage xStorage, String sName, int nMode )
{ // open existing substorage try
{
Object oSubStorage = xStorage.openStorageElement( sName, nMode );
XStorage xSubStorage = (XStorage) UnoRuntime.queryInterface( XStorage.class, oSubStorage ); return xSubStorage;
} catch( Exception e )
{
Error( "Can't open substorage '" + sName + "', exception: " + e );
}
returnnull;
}
public XStream CreateTempFileStream( XMultiServiceFactory xMSF )
{ // try to get temporary file representation
XStream xTempFileStream = null; try
{
Object oTempFile = xMSF.createInstance( "com.sun.star.io.TempFile" );
xTempFileStream = (XStream)UnoRuntime.queryInterface( XStream.class, oTempFile );
} catch( Exception e )
{}
if ( nRead1 != nRead2 )
{
Error( "The encrypted stream '" + sStreamName + "' raw representations have different size! nRead1 - nRead2 = " + ( Integer.valueOf( nRead1 - nRead2 ) ).toString() ); returnfalse;
}
for ( int nInd = 0; nInd < nRead1; nInd++ ) if ( pRawData1[0][nInd] != pRawData2[0][nInd] )
{
Error( "The encrypted stream '" + sStreamName + "' raw representations have different data!" ); returnfalse;
}
} while( nRead1 == 32000 );
} catch ( Exception e )
{
Error( "Can't compare stream '" + sStreamName + "' raw representations, exception : "+ e + "!" ); returnfalse;
}
returntrue;
}
publicboolean cantOpenStorage( XStorage xStorage, String sName )
{ // try to open an opened substorage, open call must fail try
{
Object oDummyStorage = xStorage.openStorageElement( sName, ElementModes.READ );
Error( "The trying to reopen opened substorage '" + sName + "' must fail!" );
} catch( Exception e )
{ returntrue;
}
returnfalse;
}
publicboolean cantOpenStream( XStorage xStorage, String sName, int nMode )
{ // try to open the substream with specified mode must fail try
{
Object oDummyStream = xStorage.openStreamElement( sName, nMode );
Error( "The trying to open substream '" + sName + "' must fail!" );
} catch( Exception e )
{ returntrue;
}
returnfalse;
}
publicboolean cantOpenStreamH( XStorage xStorage, String sPath, int nMode )
{ // try to open the substream with specified mode must fail
XHierarchicalStorageAccess xHStorage =
(XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage ); if ( xHStorage == null )
{
Error( "The storage does not support hierarchical access!" ); returnfalse;
}
try
{
Object oDummyStream = xHStorage.openStreamElementByHierarchicalName( sPath, nMode );
Error( "The trying to open substream '" + sPath + "' must fail!" );
} catch( Exception e )
{ returntrue;
}
returnfalse;
}
publicboolean cantOpenEncrStreamH( XStorage xStorage, String sPath, int nMode, String aPass )
{ // try to open the substream with specified mode must fail
XHierarchicalStorageAccess xHStorage =
(XHierarchicalStorageAccess) UnoRuntime.queryInterface( XHierarchicalStorageAccess.class, xStorage ); if ( xHStorage == null )
{
Error( "The storage does not support hierarchical access!" ); returnfalse;
}
try
{
Object oDummyStream = xHStorage.openEncryptedStreamElementByHierarchicalName( sPath, nMode, aPass );
Error( "The trying to open substream '" + sPath + "' must fail!" );
} catch( WrongPasswordException wpe )
{
Error( "The substream '" + sPath + "' must not exist!" ); returnfalse;
} catch( Exception e )
{ returntrue;
}
returnfalse;
}
public XStorage cloneStorage( XSingleServiceFactory xFactory, XStorage xStorage )
{ // create a copy of a last committed version of specified storage
XStorage xResult = null; try
{
Object oTempStorage = xFactory.createInstance();
xResult = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage ); if ( xResult != null )
xStorage.copyLastCommitTo( xResult );
} catch( Exception e )
{
Error( "Can't clone storage, exception: " + e ); returnnull;
}
return xResult;
}
public XStorage cloneSubStorage( XSingleServiceFactory xFactory, XStorage xStorage, String sName )
{ // create a copy of a last committed version of specified substorage
XStorage xResult = null; try
{
Object oTempStorage = xFactory.createInstance();
xResult = (XStorage) UnoRuntime.queryInterface( XStorage.class, oTempStorage ); if ( xResult != null )
xStorage.copyStorageElementLastCommitTo( sName, xResult );
} catch( Exception e )
{
Error( "Can't clone substorage '" + sName + "', exception: " + e ); returnnull;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.