1*2a97ec55SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2a97ec55SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2a97ec55SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2a97ec55SAndrew Rist  * distributed with this work for additional information
6*2a97ec55SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2a97ec55SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2a97ec55SAndrew Rist  * "License"); you may not use this file except in compliance
9*2a97ec55SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2a97ec55SAndrew Rist  *
11*2a97ec55SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2a97ec55SAndrew Rist  *
13*2a97ec55SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2a97ec55SAndrew Rist  * software distributed under the License is distributed on an
15*2a97ec55SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2a97ec55SAndrew Rist  * KIND, either express or implied.  See the License for the
17*2a97ec55SAndrew Rist  * specific language governing permissions and limitations
18*2a97ec55SAndrew Rist  * under the License.
19*2a97ec55SAndrew Rist  *
20*2a97ec55SAndrew Rist  *************************************************************/
21*2a97ec55SAndrew Rist 
22*2a97ec55SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_extensions.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "xmxtrct.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <rtl/memory.h>
30cdf0e10cSrcweir #include <tools/zcodec.hxx>
31cdf0e10cSrcweir #include <unotools/streamhelper.hxx>
32cdf0e10cSrcweir #include <sot/storage.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir // ----------------
35cdf0e10cSrcweir // - XMXLockBytes -
36cdf0e10cSrcweir // ----------------
37cdf0e10cSrcweir 
38cdf0e10cSrcweir class XMXLockBytes : public SvLockBytes
39cdf0e10cSrcweir {
40cdf0e10cSrcweir 	REF( NMSP_IO::XInputStream ) 	mxIStm;
41cdf0e10cSrcweir 	SEQ( sal_Int8 )					maSeq;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir 									XMXLockBytes();
44cdf0e10cSrcweir 
45cdf0e10cSrcweir public:
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 									XMXLockBytes( const REF( NMSP_IO::XInputStream )& rxIStm );
48cdf0e10cSrcweir 	virtual							~XMXLockBytes();
49cdf0e10cSrcweir 
50cdf0e10cSrcweir 	virtual ErrCode					ReadAt( sal_Size nPos, void* pBuffer, sal_Size nCount, sal_Size* pRead ) const;
51cdf0e10cSrcweir 	virtual ErrCode					WriteAt( sal_Size nPos, const void* pBuffer, sal_Size nCount, sal_Size* pWritten );
52cdf0e10cSrcweir 	virtual ErrCode					Flush() const;
53cdf0e10cSrcweir 	virtual ErrCode					SetSize( sal_Size nSize );
54cdf0e10cSrcweir 	virtual ErrCode					Stat( SvLockBytesStat*, SvLockBytesStatFlag ) const;
55cdf0e10cSrcweir };
56cdf0e10cSrcweir 
57cdf0e10cSrcweir // ------------------------------------------------------------------------
58cdf0e10cSrcweir 
XMXLockBytes(const REF (NMSP_IO::XInputStream)& rxIStm)59cdf0e10cSrcweir XMXLockBytes::XMXLockBytes( const REF( NMSP_IO::XInputStream )& rxIStm ) :
60cdf0e10cSrcweir 	mxIStm( rxIStm )
61cdf0e10cSrcweir {
62cdf0e10cSrcweir 	if( mxIStm.is() )
63cdf0e10cSrcweir 	{
64cdf0e10cSrcweir 		const sal_uInt32	nBytesToRead = 65535;
65cdf0e10cSrcweir 		sal_uInt32			nRead;
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 		do
68cdf0e10cSrcweir 		{
69cdf0e10cSrcweir 			SEQ( sal_Int8 ) aReadSeq;
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 			nRead = mxIStm->readSomeBytes( aReadSeq, nBytesToRead );
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 			if( nRead )
74cdf0e10cSrcweir 			{
75cdf0e10cSrcweir 				const sal_uInt32 nOldLength = maSeq.getLength();
76cdf0e10cSrcweir 				maSeq.realloc( nOldLength + nRead );
77cdf0e10cSrcweir 				rtl_copyMemory( maSeq.getArray() + nOldLength, aReadSeq.getConstArray(), aReadSeq.getLength() );
78cdf0e10cSrcweir 			}
79cdf0e10cSrcweir 		}
80cdf0e10cSrcweir 		while( nBytesToRead == nRead );
81cdf0e10cSrcweir 	}
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir // ------------------------------------------------------------------------
85cdf0e10cSrcweir 
~XMXLockBytes()86cdf0e10cSrcweir XMXLockBytes::~XMXLockBytes()
87cdf0e10cSrcweir {
88cdf0e10cSrcweir }
89cdf0e10cSrcweir 
90cdf0e10cSrcweir // ------------------------------------------------------------------------
91cdf0e10cSrcweir 
ReadAt(sal_Size nPos,void * pBuffer,sal_Size nCount,sal_Size * pRead) const92cdf0e10cSrcweir ErrCode XMXLockBytes::ReadAt( sal_Size nPos, void* pBuffer, sal_Size nCount, sal_Size* pRead ) const
93cdf0e10cSrcweir {
94cdf0e10cSrcweir 	const sal_Size		nSeqLen = maSeq.getLength();
95cdf0e10cSrcweir 	ErrCode				nErr = ERRCODE_NONE;
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 	if( nPos < nSeqLen )
98cdf0e10cSrcweir 	{
99cdf0e10cSrcweir 		if( ( nPos + nCount ) > nSeqLen )
100cdf0e10cSrcweir 			nCount = nSeqLen - nPos;
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 		rtl_copyMemory( pBuffer, maSeq.getConstArray() + nPos, nCount );
103cdf0e10cSrcweir 		*pRead = nCount;
104cdf0e10cSrcweir 	}
105cdf0e10cSrcweir 	else
106cdf0e10cSrcweir 		*pRead = 0UL;
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 	return nErr;
109cdf0e10cSrcweir }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir // ------------------------------------------------------------------------
112cdf0e10cSrcweir 
WriteAt(sal_Size,const void *,sal_Size,sal_Size *)113cdf0e10cSrcweir ErrCode XMXLockBytes::WriteAt( sal_Size /*nPos*/, const void* /*pBuffer*/, sal_Size /*nCount*/, sal_Size* /*pWritten*/ )
114cdf0e10cSrcweir {
115cdf0e10cSrcweir 	return ERRCODE_IO_CANTWRITE;
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir // ------------------------------------------------------------------------
119cdf0e10cSrcweir 
Flush() const120cdf0e10cSrcweir ErrCode XMXLockBytes::Flush() const
121cdf0e10cSrcweir {
122cdf0e10cSrcweir 	return ERRCODE_NONE;
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir // ------------------------------------------------------------------------
126cdf0e10cSrcweir 
SetSize(sal_Size)127cdf0e10cSrcweir ErrCode XMXLockBytes::SetSize( sal_Size /*nSize*/ )
128cdf0e10cSrcweir {
129cdf0e10cSrcweir 	return ERRCODE_IO_CANTWRITE;
130cdf0e10cSrcweir }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir // ------------------------------------------------------------------------
133cdf0e10cSrcweir 
Stat(SvLockBytesStat * pStat,SvLockBytesStatFlag) const134cdf0e10cSrcweir ErrCode XMXLockBytes::Stat( SvLockBytesStat* pStat, SvLockBytesStatFlag /*eFlag*/ ) const
135cdf0e10cSrcweir {
136cdf0e10cSrcweir 	pStat->nSize = maSeq.getLength();
137cdf0e10cSrcweir 	return ERRCODE_NONE;
138cdf0e10cSrcweir }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir // ----------------
141cdf0e10cSrcweir // - XMLExtractor -
142cdf0e10cSrcweir // ----------------
143cdf0e10cSrcweir 
XMLExtractor(const REF (NMSP_LANG::XMultiServiceFactory)& rxMgr)144cdf0e10cSrcweir XMLExtractor::XMLExtractor( const REF( NMSP_LANG::XMultiServiceFactory )& rxMgr ) :
145cdf0e10cSrcweir 	mxFact( rxMgr )
146cdf0e10cSrcweir {
147cdf0e10cSrcweir }
148cdf0e10cSrcweir 
149cdf0e10cSrcweir // -----------------------------------------------------------------------------
150cdf0e10cSrcweir 
~XMLExtractor()151cdf0e10cSrcweir XMLExtractor::~XMLExtractor()
152cdf0e10cSrcweir {
153cdf0e10cSrcweir }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir // -----------------------------------------------------------------------------
156cdf0e10cSrcweir 
REF(NMSP_IO::XInputStream)157cdf0e10cSrcweir REF( NMSP_IO::XInputStream ) SAL_CALL XMLExtractor::extract( const REF( NMSP_IO::XInputStream )& rxIStm ) throw( NMSP_UNO::RuntimeException )
158cdf0e10cSrcweir {
159cdf0e10cSrcweir 	REF( NMSP_IO::XInputStream ) xRet;
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 	if( rxIStm.is() )
162cdf0e10cSrcweir 	{
163cdf0e10cSrcweir 		SvStream		aIStm( new XMXLockBytes( rxIStm ) );
164cdf0e10cSrcweir 		SvStorageRef	aStorage( new SvStorage( aIStm ) );
165cdf0e10cSrcweir 		String			aStmName;
166cdf0e10cSrcweir 		const String	aFormat1( String::CreateFromAscii( "XMLFormat" ) );
167cdf0e10cSrcweir 		const String	aFormat2( String::CreateFromAscii( "XMLFormat2" ) );
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 		if( aStorage->IsContained( aFormat2 ) )
170cdf0e10cSrcweir 			aStmName = aFormat2;
171cdf0e10cSrcweir 		else if( aStorage->IsContained( aFormat1 ) )
172cdf0e10cSrcweir 			aStmName = aFormat1;
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 		if( !aStorage->GetError() && aStmName.Len() && aStorage->IsStream( aStmName ) )
175cdf0e10cSrcweir 		{
176cdf0e10cSrcweir             SvStorageStreamRef xStream( aStorage->OpenSotStream( aStmName ) );
177cdf0e10cSrcweir 
178cdf0e10cSrcweir 			if( xStream.Is() )
179cdf0e10cSrcweir 			{
180cdf0e10cSrcweir 				SvMemoryStream*	pMemStm = new SvMemoryStream( 65535, 65535 );
181cdf0e10cSrcweir 				ZCodec			aCodec;
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 				aCodec.BeginCompression( ZCODEC_BEST_COMPRESSION );
184cdf0e10cSrcweir 				aCodec.Decompress( *xStream, *pMemStm );
185cdf0e10cSrcweir 				aCodec.EndCompression();
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 				xRet = new ::utl::OInputStreamHelper( new SvLockBytes( pMemStm, sal_True ), 65535 );
188cdf0e10cSrcweir 			}
189cdf0e10cSrcweir 		}
190cdf0e10cSrcweir 	}
191cdf0e10cSrcweir 
192cdf0e10cSrcweir 	return xRet;
193cdf0e10cSrcweir }
194