1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
31 #include "xmxtrct.hxx"
33 #include <rtl/memory.h>
34 #include <tools/zcodec.hxx>
35 #include <unotools/streamhelper.hxx>
36 #include <sot/storage.hxx>
42 class XMXLockBytes
: public SvLockBytes
44 REF( NMSP_IO::XInputStream
) mxIStm
;
45 SEQ( sal_Int8
) maSeq
;
51 XMXLockBytes( const REF( NMSP_IO::XInputStream
)& rxIStm
);
52 virtual ~XMXLockBytes();
54 virtual ErrCode
ReadAt( sal_Size nPos
, void* pBuffer
, sal_Size nCount
, sal_Size
* pRead
) const;
55 virtual ErrCode
WriteAt( sal_Size nPos
, const void* pBuffer
, sal_Size nCount
, sal_Size
* pWritten
);
56 virtual ErrCode
Flush() const;
57 virtual ErrCode
SetSize( sal_Size nSize
);
58 virtual ErrCode
Stat( SvLockBytesStat
*, SvLockBytesStatFlag
) const;
61 // ------------------------------------------------------------------------
63 XMXLockBytes::XMXLockBytes( const REF( NMSP_IO::XInputStream
)& rxIStm
) :
68 const sal_uInt32 nBytesToRead
= 65535;
73 SEQ( sal_Int8
) aReadSeq
;
75 nRead
= mxIStm
->readSomeBytes( aReadSeq
, nBytesToRead
);
79 const sal_uInt32 nOldLength
= maSeq
.getLength();
80 maSeq
.realloc( nOldLength
+ nRead
);
81 rtl_copyMemory( maSeq
.getArray() + nOldLength
, aReadSeq
.getConstArray(), aReadSeq
.getLength() );
84 while( nBytesToRead
== nRead
);
88 // ------------------------------------------------------------------------
90 XMXLockBytes::~XMXLockBytes()
94 // ------------------------------------------------------------------------
96 ErrCode
XMXLockBytes::ReadAt( sal_Size nPos
, void* pBuffer
, sal_Size nCount
, sal_Size
* pRead
) const
98 const sal_Size nSeqLen
= maSeq
.getLength();
99 ErrCode nErr
= ERRCODE_NONE
;
103 if( ( nPos
+ nCount
) > nSeqLen
)
104 nCount
= nSeqLen
- nPos
;
106 rtl_copyMemory( pBuffer
, maSeq
.getConstArray() + nPos
, nCount
);
115 // ------------------------------------------------------------------------
117 ErrCode
XMXLockBytes::WriteAt( sal_Size
/*nPos*/, const void* /*pBuffer*/, sal_Size
/*nCount*/, sal_Size
* /*pWritten*/ )
119 return ERRCODE_IO_CANTWRITE
;
122 // ------------------------------------------------------------------------
124 ErrCode
XMXLockBytes::Flush() const
129 // ------------------------------------------------------------------------
131 ErrCode
XMXLockBytes::SetSize( sal_Size
/*nSize*/ )
133 return ERRCODE_IO_CANTWRITE
;
136 // ------------------------------------------------------------------------
138 ErrCode
XMXLockBytes::Stat( SvLockBytesStat
* pStat
, SvLockBytesStatFlag
/*eFlag*/ ) const
140 pStat
->nSize
= maSeq
.getLength();
148 XMLExtractor::XMLExtractor( const REF( NMSP_LANG::XMultiServiceFactory
)& rxMgr
) :
153 // -----------------------------------------------------------------------------
155 XMLExtractor::~XMLExtractor()
159 // -----------------------------------------------------------------------------
161 REF( NMSP_IO::XInputStream
) SAL_CALL
XMLExtractor::extract( const REF( NMSP_IO::XInputStream
)& rxIStm
) throw( NMSP_UNO::RuntimeException
)
163 REF( NMSP_IO::XInputStream
) xRet
;
167 SvStream
aIStm( new XMXLockBytes( rxIStm
) );
168 SvStorageRef
aStorage( new SvStorage( aIStm
) );
170 const String
aFormat1( String::CreateFromAscii( "XMLFormat" ) );
171 const String
aFormat2( String::CreateFromAscii( "XMLFormat2" ) );
173 if( aStorage
->IsContained( aFormat2
) )
175 else if( aStorage
->IsContained( aFormat1
) )
178 if( !aStorage
->GetError() && aStmName
.Len() && aStorage
->IsStream( aStmName
) )
180 SvStorageStreamRef
xStream( aStorage
->OpenSotStream( aStmName
) );
184 SvMemoryStream
* pMemStm
= new SvMemoryStream( 65535, 65535 );
187 aCodec
.BeginCompression( ZCODEC_BEST_COMPRESSION
);
188 aCodec
.Decompress( *xStream
, *pMemStm
);
189 aCodec
.EndCompression();
191 xRet
= new ::utl::OInputStreamHelper( new SvLockBytes( pMemStm
, sal_True
), 65535 );