/* * 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, xStream ); if ( xPropSet == null )
{
Error( "Can't get XPropertySet implementation from substream '" + sStreamName + "'!" ); returnfalse;
}
// set properties to the stream try
{
xPropSet.setPropertyValue( "MediaType", sMediaType );
xPropSet.setPropertyValue( "Compressed", Boolean.valueOf( bCompressed ) );
} 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;
}
// get access to the relationship information
XRelationshipAccess xRelAccess = (XRelationshipAccess) UnoRuntime.queryInterface( XRelationshipAccess.class, xStream ); if ( xRelAccess == null )
{
Error( "Can't get XRelationshipAccess implementation from substream '" + sStreamName + "'!" ); returnfalse;
}
// set the relationship information try
{
xRelAccess.insertRelationships( aRelations, false );
} catch( Exception e )
{
Error( "Can't set relationships to substream '" + sStreamName + "', exception: " + e ); returnfalse;
}
// free the stream resources, garbage collector may remove the object too late if ( !disposeStream( xStream, sStreamName ) ) returnfalse;
publicboolean setStorageTypeAndCheckProps( XStorage xStorage, boolean bIsRoot, int nMode,
StringPair[][] aRelations )
{ boolean bOk = false;
// get access to the XPropertySet interface
XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStorage ); if ( xPropSet != null )
{ try
{ // 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!" );
}
// get access to the relationship information
XRelationshipAccess xRelAccess = (XRelationshipAccess) UnoRuntime.queryInterface( XRelationshipAccess.class, xStorage );
if ( xRelAccess == null )
{
Error( "Can't get XRelationshipAccess implementation from the storage!" ); returnfalse;
}
// set the relationship information try
{
xRelAccess.insertRelationships( aRelations, false );
} catch( Exception e )
{
Error( "Can't set relationships to the storage, exception: " + e ); returnfalse;
}
return bOk;
}
publicboolean checkRelations( StringPair[][] aStorRels, StringPair[][] aTestRels )
{ if ( aStorRels.length != aTestRels.length )
{
Error( "The provided relations sequence has different size than the storage's one!" ); returnfalse;
}
for ( int nStorInd = 0; nStorInd < aStorRels.length; nStorInd++ )
{ int nStorIDInd = -1; for ( int nStorTagInd = 0; nStorTagInd < aStorRels[nStorInd].length; nStorTagInd++ )
{ if ( aStorRels[nStorInd][nStorTagInd].First.equals( "Id" ) )
{
nStorIDInd = nStorTagInd; break;
}
}
if ( nStorIDInd == -1 )
{
Error( "One of the storage relations entries has no ID!" ); returnfalse;
}
for ( int nInd = 0; nInd < aTestRels.length; nInd++ )
{ int nIDInd = -1; for ( int nTagInd = 0; nTagInd < aTestRels[nInd].length; nTagInd++ )
{ if ( aTestRels[nInd][nTagInd].First.equals( "Id" ) )
{
nIDInd = nTagInd; break;
}
}
if ( nIDInd == -1 )
{
Error( "One of the test hardcoded entries has no ID, num = " + nInd + ", length = " + aTestRels[nInd].length + ", global length = " + aTestRels.length + "!" ); returnfalse;
}
if ( aStorRels[nStorInd][nStorIDInd].Second.equals( aTestRels[nInd][nIDInd].Second ) )
{ boolean[] pRelCheckMark = newboolean[ aTestRels[nInd].length ]; for ( int nCheckInd = 0; nCheckInd < pRelCheckMark.length; nCheckInd++ )
{
pRelCheckMark[nCheckInd] = false;
}
for ( int nStorTagInd = 0; nStorTagInd < aStorRels[nStorInd].length; nStorTagInd++ )
{ boolean bFound = false; for ( int nTagInd = 0; nTagInd < aTestRels[nInd].length; nTagInd++ )
{ if ( aTestRels[nInd][nTagInd].First.equals( aStorRels[nStorInd][nStorTagInd].First ) )
{ if ( !aTestRels[nInd][nTagInd].Second.equals( aStorRels[nStorInd][nStorTagInd].Second ) )
{
Error( "Test rel. num. " + nInd + " has different tag \"" + aTestRels[nInd][nTagInd].First + "\" value!" ); returnfalse;
}
if ( !bFound )
{
Error( "Stor rel. num. " + nStorInd + " has unexpected tag \"" + aStorRels[nStorInd][nStorTagInd].First + "\", ID = \"" + aStorRels[nStorInd][nStorIDInd].Second + "\"!" ); returnfalse;
}
}
for ( int nCheckInd = 0; nCheckInd < pRelCheckMark.length; nCheckInd++ )
{ if ( !pRelCheckMark[nCheckInd] && !aTestRels[nInd][nCheckInd].Second.equals( "" ) )
{
Error( "Test rel. num. " + nInd + " has unexpected tag \"" + aTestRels[nInd][nCheckInd].First + "\" with nonempty value!" ); returnfalse;
}
}
break;
}
}
}
returntrue;
}
publicboolean checkStorageProperties( XStorage xStorage, boolean bIsRoot, int nMode,
StringPair[][] aRelations )
{ boolean bOk = false;
// get access to the XPropertySet interface
XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xStorage ); if ( xPropSet != null )
{ try
{ // 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 get properties of substorage, exception: " + e );
}
} else
{
Error( "Can't get XPropertySet implementation from storage!" );
}
// get access to the relationship information
XRelationshipAccess xRelAccess = (XRelationshipAccess) UnoRuntime.queryInterface( XRelationshipAccess.class, xStorage );
if ( xRelAccess == null )
{
Error( "Can't get XRelationshipAccess implementation from the checked storage!" ); returnfalse;
}
// get the relationship information
StringPair[][] aStorRels; try
{
aStorRels = xRelAccess.getAllRelationships();
} catch( Exception e )
{
Error( "Can't get relationships of the checked storage, exception: " + e ); returnfalse;
}
if ( !checkRelations( aStorRels, aRelations ) )
{
Error( "StorageRelationsChecking has failed!" ); returnfalse;
}
return bOk;
}
publicboolean InternalCheckStream( XStream xStream,
String sName,
String sMediaType, byte[] pBytes,
StringPair[][] aRelations )
{ // 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" ) );
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;
}
} catch( Exception e )
{
Error( "Can't get properties of substream '" + sName + "', exception: " + e );
}
} else
{
Error( "Can't get XPropertySet implementation from stream '" + sName + "'!" );
}
// get access to the relationship information
XRelationshipAccess xRelAccess = (XRelationshipAccess) UnoRuntime.queryInterface( XRelationshipAccess.class, xStream );
if ( xRelAccess == null )
{
Error( "Can't get XRelationshipAccess implementation from the stream\"" + sName + "\"!" ); returnfalse;
}
// get the relationship information
StringPair[][] aStorRels; try
{
aStorRels = xRelAccess.getAllRelationships();
} catch( Exception e )
{
Error( "Can't get relationships of the substream '" + sName + "', exception: " + e ); 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 )
{}
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;
}
public XStorage createStorageFromURL(
XSingleServiceFactory xFactory,
String aURL, int nMode )
{
XStorage xResult = null;
try
{
PropertyValue[] aAddArgs = new PropertyValue[1];
aAddArgs[0] = new PropertyValue();
aAddArgs[0].Name = "StorageFormat";
aAddArgs[0].Value = "OFOPXMLFormat";
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.