office-gobmx/package/source/zippackage/ZipPackageFolder.cxx

592 lines
22 KiB
C++
Raw Normal View History

/*************************************************************************
*
* $RCSfile: ZipPackageFolder.cxx,v $
*
2001-02-07 03:51:26 -06:00
* $Revision: 1.30 $
*
2001-02-07 03:51:26 -06:00
* last change: $Author: mtg $ $Date: 2001-02-07 10:51:26 $
*
* The Contents of this file are made available subject to the terms of
* either of the following licenses
*
* - GNU Lesser General Public License Version 2.1
* - Sun Industry Standards Source License Version 1.1
*
* Sun Microsystems Inc., October, 2000
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2000 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*
* Sun Industry Standards Source License Version 1.1
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.1 (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.openoffice.org/license.html.
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2000 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
* Contributor(s): Martin Gallwey (gallwey@sun.com)
*
*
************************************************************************/
#ifndef _ZIP_PACKAGE_FOLDER_HXX
#include "ZipPackageFolder.hxx"
#endif
using namespace com::sun::star::package::ZipConstants;
using namespace com::sun::star;
using namespace cppu;
using namespace rtl;
2000-12-04 04:30:09 -06:00
ZipPackageFolder::ZipPackageFolder (void)
: pPackage( NULL )
{
aEntry.nVersion = -1;
aEntry.nFlag = 0;
aEntry.nMethod = STORED;
aEntry.nTime = -1;
aEntry.nCrc = 0;
aEntry.nCompressedSize = 0;
aEntry.nSize = 0;
aEntry.nOffset = -1;
}
ZipPackageFolder::~ZipPackageFolder( void )
{
}
void ZipPackageFolder::copyZipEntry( com::sun::star::package::ZipEntry &rDest, const com::sun::star::package::ZipEntry &rSource)
{
rDest.nVersion = rSource.nVersion;
rDest.nFlag = rSource.nFlag;
rDest.nMethod = rSource.nMethod;
rDest.nTime = rSource.nTime;
rDest.nCrc = rSource.nCrc;
rDest.nCompressedSize = rSource.nCompressedSize;
rDest.nSize = rSource.nSize;
rDest.nOffset = rSource.nOffset;
rDest.sName = rSource.sName;
rDest.extra = rSource.extra;
rDest.sComment = rSource.sComment;
}
uno::Any SAL_CALL ZipPackageFolder::queryInterface( const uno::Type& rType )
throw(uno::RuntimeException)
{
// cppu::queryInterface is an inline template so it's fast
// unfortunately, it always creates an Any...we should be able to optimise
// this with a class static containing supported interfaces
// ...will research this further ...mtg 15/12/00
return ::cppu::queryInterface ( rType ,
// OWeakObject interfaces
reinterpret_cast< uno::XInterface* > ( this ) ,
static_cast< uno::XWeak* > ( this ) ,
// ZipPackageEntry interfaces
static_cast< container::XNamed* > ( this ) ,
static_cast< container::XChild* > ( this ) ,
static_cast< lang::XUnoTunnel* > ( this ) ,
// my own interfaces
static_cast< container::XNameContainer* > ( this ) ,
static_cast< container::XEnumerationAccess* > ( this ) ,
static_cast< beans::XPropertySet* > ( this ) );
}
void SAL_CALL ZipPackageFolder::acquire( )
throw()
{
OWeakObject::acquire();
}
void SAL_CALL ZipPackageFolder::release( )
throw()
{
OWeakObject::release();
}
// XNameContainer
void SAL_CALL ZipPackageFolder::insertByName( const ::rtl::OUString& aName, const uno::Any& aElement )
throw(lang::IllegalArgumentException, container::ElementExistException, lang::WrappedTargetException, uno::RuntimeException)
{
2000-12-20 05:36:39 -06:00
OUString sName;
if (aName.indexOf('/', 0 ) == 0)
sName = aName.copy(1, aName.getLength());
else
sName = aName;
if (hasByName(sName))
throw container::ElementExistException();
else
{
uno::Reference < lang::XUnoTunnel > xRef;
aElement >>= xRef;
uno::Reference < container::XNamed > xNamed (xRef, uno::UNO_QUERY);
2000-11-29 06:47:18 -06:00
uno::Reference < container::XChild > xChild (xRef, uno::UNO_QUERY);
uno::Reference < uno::XInterface > xInterface (*this);
xNamed->setName (sName);
aContents[sName] = xRef;
2000-12-04 04:30:09 -06:00
try
{
xChild->setParent (xInterface);
}
catch ( lang::NoSupportException& )
{
VOS_ENSURE( 0, "setParent threw an exception: attempted to set Parent to non-existing interface!");
2000-12-04 04:30:09 -06:00
}
}
}
void SAL_CALL ZipPackageFolder::removeByName( const ::rtl::OUString& Name )
throw(container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
{
2000-12-20 05:36:39 -06:00
OUString sName;
if (Name.indexOf('/', 0 ) == 0)
sName = Name.copy(1, Name.getLength());
else
sName = Name;
2000-11-28 20:21:56 -06:00
if (!aContents.count(sName))
2000-12-07 08:48:04 -06:00
throw container::NoSuchElementException();
aContents.erase(sName);
}
// XEnumerationAccess
uno::Reference< container::XEnumeration > SAL_CALL ZipPackageFolder::createEnumeration( )
throw(uno::RuntimeException)
{
return uno::Reference < container::XEnumeration> (new ZipPackageFolderEnumeration(aContents));
}
// XElementAccess
uno::Type SAL_CALL ZipPackageFolder::getElementType( )
throw(uno::RuntimeException)
{
return ::getCppuType ((const uno::Reference< lang::XUnoTunnel > *) 0);
}
sal_Bool SAL_CALL ZipPackageFolder::hasElements( )
throw(uno::RuntimeException)
{
return aContents.size() > 0;
}
// XNameAccess
uno::Any SAL_CALL ZipPackageFolder::getByName( const ::rtl::OUString& aName )
throw(container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
{
uno::Any aAny;
2000-12-20 05:36:39 -06:00
OUString sName;
if (aName.indexOf('/', 0 ) == 0)
sName = aName.copy(1, aName.getLength());
else
sName = aName;
2000-11-28 20:21:56 -06:00
if (!aContents.count(sName))
2000-12-07 08:48:04 -06:00
throw container::NoSuchElementException();
2000-11-28 20:21:56 -06:00
TunnelHash::const_iterator aCI = aContents.find(sName);
aAny <<= (*aCI).second;
return aAny;
}
uno::Sequence< ::rtl::OUString > SAL_CALL ZipPackageFolder::getElementNames( )
throw(uno::RuntimeException)
{
sal_uInt32 i=0, nSize = aContents.size();
OUString *pNames = new OUString[nSize];
for (TunnelHash::const_iterator aIterator = aContents.begin() ; aIterator != aContents.end(); i++,aIterator++)
pNames[i] = (*aIterator).first;
return uno::Sequence < OUString > (pNames, nSize);
}
sal_Bool SAL_CALL ZipPackageFolder::hasByName( const ::rtl::OUString& aName )
throw(uno::RuntimeException)
{
2000-12-20 05:36:39 -06:00
OUString sName;
if (aName.indexOf('/', 0 ) == 0)
sName = aName.copy(1, aName.getLength());
else
sName = aName;
2000-11-28 09:49:34 -06:00
return aContents.count(sName);
}
// XNameReplace
void SAL_CALL ZipPackageFolder::replaceByName( const ::rtl::OUString& aName, const uno::Any& aElement )
throw(lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
{
2000-12-20 05:36:39 -06:00
OUString sName;
if (aName.indexOf('/', 0 ) == 0)
sName = aName.copy(1, aName.getLength());
else
sName = aName;
if (hasByName(aName))
removeByName(aName);
2000-11-28 20:21:56 -06:00
else
2000-12-07 08:48:04 -06:00
throw container::NoSuchElementException();
insertByName(aName, aElement);
}
2000-11-28 20:21:56 -06:00
void ZipPackageFolder::saveContents(rtl::OUString &rPath, std::vector < ManifestEntry *> &rManList, ZipOutputStream & rZipOut)
2000-12-04 04:30:09 -06:00
throw(uno::RuntimeException)
{
uno::Reference < lang::XUnoTunnel > xTunnel;
ZipPackageFolder *pFolder = NULL;
ZipPackageStream *pStream = NULL;
2000-12-04 04:30:09 -06:00
sal_Bool bIsFolder = sal_False;
TunnelHash::const_iterator aCI = aContents.begin();
for (;aCI!=aContents.end();aCI++)
{
xTunnel = uno::Reference < lang::XUnoTunnel> ((*aCI).second, uno::UNO_QUERY);
2000-12-04 04:30:09 -06:00
sal_Int64 nTest=0;
if ((nTest = xTunnel->getSomething(ZipPackageFolder::getUnoTunnelImplementationId())) != 0)
{
2000-12-04 04:30:09 -06:00
pFolder = reinterpret_cast < ZipPackageFolder* > ( nTest );
bIsFolder = sal_True;
}
2000-12-04 04:30:09 -06:00
else
{
2000-12-04 04:30:09 -06:00
// If this getSomething call returns 0, it means that
// something evil has crept into the contents hash_map, which
// should mean that something has gone very wrong somewhere, and someone
// else should deal with it
2000-12-04 07:52:27 -06:00
nTest = xTunnel->getSomething(ZipPackageStream::getUnoTunnelImplementationId());
2000-12-04 04:30:09 -06:00
if (nTest == 0)
2000-12-07 08:48:04 -06:00
throw uno::RuntimeException();
2000-12-04 04:30:09 -06:00
pStream = reinterpret_cast < ZipPackageStream* > ( nTest );
bIsFolder = sal_False;
}
if (bIsFolder)
{
if (pFolder->pPackage)
2000-12-04 04:30:09 -06:00
{
// This ZipPackageFolder is the root folder of a zipfile contained within
// this zipfile. Things will get a little strange from here on in...
ManifestEntry *pMan = new ManifestEntry;
pMan->sShortName = (*aCI).first;
ZipPackageFolder::copyZipEntry(pMan->aEntry, pFolder->aEntry);
pMan->aEntry.sName = rPath + pMan->sShortName;
pMan->aEntry.nCrc = -1;
pMan->aEntry.nSize = -1;
pMan->aEntry.nCompressedSize = -1;
pMan->aEntry.nMethod = STORED;
try
{
rZipOut.putNextEntry(pMan->aEntry);
ZipPackageBuffer &rBuffer = pFolder->pPackage->writeToBuffer();
pMan->aEntry.nSize = pMan->aEntry.nCompressedSize = static_cast < sal_Int32 > (rBuffer.getLength());
rZipOut.write(rBuffer.aBuffer, 0, static_cast < sal_Int32 > (rBuffer.getLength()));
rZipOut.closeEntry();
}
catch (::com::sun::star::io::IOException & )
{
VOS_ENSURE( 0, "Error writing ZipOutputStream" );
}
// Then copy it back
try
{
2001-02-07 03:51:26 -06:00
uno::Any aAny = pFolder->getPropertyValue(OUString( RTL_CONSTASCII_USTRINGPARAM ("MediaType") ) );
aAny >>= pMan->sMediaType;
}
catch (::com::sun::star::beans::UnknownPropertyException & )
{
VOS_ENSURE( 0, "MediaType is an unknown property!!" );
}
ZipPackageFolder::copyZipEntry(pFolder->aEntry, pMan->aEntry);
pFolder->aEntry.nOffset *= -1;
rManList.push_back (pMan);
2000-12-04 04:30:09 -06:00
}
else
2000-12-04 04:30:09 -06:00
{
// In case the entry we are reading is also the entry we are writing, we will
// store the ZipEntry data in the ManifestEntry struct and then update the
// ZipEntry data in the ZipPackageFolder later
time_t nTime = time(NULL);
ManifestEntry *pMan = new ManifestEntry;
pMan->sShortName = (*aCI).first;
// First copy current data to ManifestEntry
ZipPackageFolder::copyZipEntry(pMan->aEntry, pFolder->aEntry);
2001-02-07 03:51:26 -06:00
pMan->aEntry.sName = rPath + pMan->sShortName + OUString( RTL_CONSTASCII_USTRINGPARAM ( "/" ) );
pMan->aEntry.nTime = ZipOutputStream::tmDateToDosDate ( *localtime(&nTime));
pMan->aEntry.nCrc = 0;
pMan->aEntry.nSize = 0;
pMan->aEntry.nCompressedSize = 0;
pMan->aEntry.nMethod = STORED;
try
{
rZipOut.putNextEntry(pMan->aEntry);
rZipOut.closeEntry();
}
catch (::com::sun::star::io::IOException & )
{
VOS_ENSURE( 0, "Error writing ZipOutputStream" );
}
try
{
2001-02-07 03:51:26 -06:00
uno::Any aAny = pFolder->getPropertyValue(OUString( RTL_CONSTASCII_USTRINGPARAM ( "MediaType") ) );
aAny >>= pMan->sMediaType;
}
catch (::com::sun::star::beans::UnknownPropertyException & )
{
VOS_ENSURE( 0, "MediaType is an unknown property!!" );
}
// Then copy it back
ZipPackageFolder::copyZipEntry(pFolder->aEntry, pMan->aEntry);
pFolder->aEntry.nOffset *= -1;
pFolder->saveContents(pFolder->aEntry.sName, rManList, rZipOut);
rManList.push_back (pMan);
2000-12-04 04:30:09 -06:00
}
}
else
{
// In case the entry we are reading is also the entry we are writing, we will
// store the ZipEntry data in the ManifestEntry struct and then update the
// ZipEntry data in the ZipPackageStream later
2000-11-28 20:21:56 -06:00
ManifestEntry *pMan = new ManifestEntry;
2000-11-28 22:19:35 -06:00
pMan->sShortName = (*aCI).first;
#if SUPD>617
2001-02-07 03:51:26 -06:00
uno::Any aAny = pStream->getPropertyValue(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Compressed") ) );
#else
2001-02-07 03:51:26 -06:00
uno::Any aAny = pStream->getPropertyValue(OUString( RTL_CONSTASCII_USTRINGPARAM ( "Compress") ) );
#endif
sal_Bool bToBeCompressed;
aAny >>= bToBeCompressed;
// Copy current info to pMan...
ZipPackageFolder::copyZipEntry(pMan->aEntry, pStream->aEntry);
pMan->aEntry.sName = rPath + pMan->sShortName;
2000-11-28 20:21:56 -06:00
// If the entry is already stored in the zip file in the format we
// want for this write...copy it raw
if (pStream->bPackageMember &&
( (pMan->aEntry.nMethod == DEFLATED && bToBeCompressed) ||
(pMan->aEntry.nMethod == STORED && !bToBeCompressed) ) )
2000-11-28 20:21:56 -06:00
{
try
{
uno::Reference < io::XInputStream > xStream = pStream->getRawStream( pMan->aEntry );
try
{
rZipOut.putNextEntry(pMan->aEntry);
while (1)
{
uno::Sequence < sal_Int8 > aSeq (65535);
sal_Int32 nLength;
nLength = xStream->readBytes(aSeq, 65535);
if (nLength < 65535)
aSeq.realloc(nLength);
rZipOut.rawWrite(aSeq);
if (nLength < 65535) // EOF
break;
}
rZipOut.rawCloseEntry();
}
catch (package::ZipException&)
{
}
}
catch (::com::sun::star::io::IOException & )
2000-11-28 20:21:56 -06:00
{
VOS_ENSURE( 0, "Error writing ZipOutputStream" );
2000-11-28 20:21:56 -06:00
}
}
else
{
uno::Reference < io::XInputStream > xStream = pStream->getInputStream();
uno::Reference < io::XSeekable > xSeek (xStream, uno::UNO_QUERY);
sal_Bool bTrackLength = sal_True;
pMan->aEntry.nCrc = -1;
pMan->aEntry.nSize = -1;
pMan->aEntry.nCompressedSize = -1;
if (bToBeCompressed)
pMan->aEntry.nMethod = DEFLATED;
else
pMan->aEntry.nMethod = STORED;
if (xSeek.is())
{
xSeek->seek(0);
if (pMan->aEntry.nMethod == STORED)
{
pMan->aEntry.nSize = pMan->aEntry.nCompressedSize = static_cast < sal_Int32 > (xSeek->getLength());
bTrackLength = sal_False;
}
// Some implementations of XInputStream/XSeekable change the current position
// in the stream! Evil evil bad bad!
xSeek->seek(0);
}
try
2000-12-04 04:30:09 -06:00
{
rZipOut.putNextEntry(pMan->aEntry);
while (1)
{
uno::Sequence < sal_Int8 > aSeq (65535);
sal_Int32 nLength;
nLength = xStream->readBytes(aSeq, 65535);
if (nLength < 65535)
aSeq.realloc(nLength);
rZipOut.write(aSeq, 0, nLength);
if (bTrackLength)
pMan->aEntry.nSize+=nLength;
if (nLength < 65535) // EOF
break;
}
2000-12-04 04:30:09 -06:00
if (bTrackLength)
pMan->aEntry.nCompressedSize = pStream->aEntry.nSize;
pStream->bPackageMember = sal_True;
rZipOut.closeEntry();
}
catch (::com::sun::star::io::IOException & )
{
VOS_ENSURE( 0, "Error writing ZipOutputStream" );
2000-12-04 04:30:09 -06:00
}
}
2000-12-04 04:30:09 -06:00
try
{
2001-02-07 03:51:26 -06:00
uno::Any aAny = pStream->getPropertyValue(OUString( RTL_CONSTASCII_USTRINGPARAM ( "MediaType" ) ) );
2000-12-04 04:30:09 -06:00
aAny >>= pMan->sMediaType;
}
catch (::com::sun::star::beans::UnknownPropertyException & )
{
VOS_ENSURE( 0, "MediaType is an unknown property!!" );
}
// Then copy it back afterwards...
ZipPackageFolder::copyZipEntry(pStream->aEntry, pMan->aEntry);
pStream->aEntry.nOffset *= -1;
rManList.push_back (pMan);
}
}
}
void ZipPackageFolder::releaseUpwardRef( void )
{
uno::Reference < lang::XUnoTunnel > xTunnel;
ZipPackageFolder *pFolder = NULL;
ZipPackageStream *pStream = NULL;
sal_Bool bIsFolder;
TunnelHash::const_iterator aCI = aContents.begin();
for (;aCI!=aContents.end();aCI++)
{
xTunnel = uno::Reference < lang::XUnoTunnel> ((*aCI).second, uno::UNO_QUERY);
sal_Int64 nTest=0;
if ((nTest = xTunnel->getSomething(ZipPackageFolder::getUnoTunnelImplementationId())) != 0)
{
pFolder = reinterpret_cast < ZipPackageFolder* > ( nTest );
bIsFolder = sal_True;
}
else
{
nTest = xTunnel->getSomething(ZipPackageStream::getUnoTunnelImplementationId());
pStream = reinterpret_cast < ZipPackageStream* > ( nTest );
bIsFolder = sal_False;
}
if (bIsFolder)
{
pFolder->releaseUpwardRef();
pFolder->clearParent();
}
else
pStream->clearParent();
}
}
2000-11-28 20:21:56 -06:00
void ZipPackageFolder::updateReferences( ZipFile * pNewZipFile)
{
uno::Reference < lang::XUnoTunnel > xTunnel;
ZipPackageFolder *pFolder = NULL;
ZipPackageStream *pStream = NULL;
sal_Bool bIsFolder;
TunnelHash::const_iterator aCI = aContents.begin();
for (;aCI!=aContents.end();aCI++)
{
xTunnel = uno::Reference < lang::XUnoTunnel> ((*aCI).second, uno::UNO_QUERY);
2000-12-04 04:30:09 -06:00
sal_Int64 nTest=0;
if ((nTest = xTunnel->getSomething(ZipPackageFolder::getUnoTunnelImplementationId())) != 0)
2000-11-28 20:21:56 -06:00
{
2000-12-04 04:30:09 -06:00
pFolder = reinterpret_cast < ZipPackageFolder* > ( nTest );
2000-11-28 20:21:56 -06:00
bIsFolder = sal_True;
}
2000-12-04 04:30:09 -06:00
else
2000-11-28 20:21:56 -06:00
{
2000-12-04 04:30:09 -06:00
// If this getSomething call returns 0, it means that
// something evil has crept into the contents hash_map, which
// should mean that something has gone very wrong somewhere, and someone
// else should deal with it
2000-12-04 07:52:27 -06:00
nTest = xTunnel->getSomething(ZipPackageStream::getUnoTunnelImplementationId());
2000-12-04 04:30:09 -06:00
if (nTest == 0)
2000-12-07 08:48:04 -06:00
throw uno::RuntimeException();
2000-12-04 04:30:09 -06:00
pStream = reinterpret_cast < ZipPackageStream* > ( nTest );
2000-11-28 20:21:56 -06:00
bIsFolder = sal_False;
}
if (bIsFolder)
{
//if pPackage is set,then this is the root folder of a different ZipPackage and
// should not be changed
if (!pFolder->pPackage)
pFolder->updateReferences(pNewZipFile);
}
2000-11-28 20:21:56 -06:00
else
pStream->pZipFile = pNewZipFile;
}
}
uno::Sequence< sal_Int8 > ZipPackageFolder::getUnoTunnelImplementationId( void )
throw (uno::RuntimeException)
{
static ::cppu::OImplementationId * pId = 0;
if (! pId)
{
::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
if (! pId)
{
static ::cppu::OImplementationId aId;
pId = &aId;
}
}
return pId->getImplementationId();
}
sal_Int64 SAL_CALL ZipPackageFolder::getSomething( const uno::Sequence< sal_Int8 >& aIdentifier )
throw(uno::RuntimeException)
{
if (aIdentifier.getLength() == 16 && 0 == rtl_compareMemory(getUnoTunnelImplementationId().getConstArray(), aIdentifier.getConstArray(), 16 ) )
2000-12-04 04:30:09 -06:00
return reinterpret_cast < sal_Int64 > ( this );
2000-12-04 04:30:09 -06:00
return 0;
}