merge the formfield patch from ooo-build
[ooovba.git] / extensions / source / xmlextract / xmxtrct.cxx
blob5cc0b83aff4e0adfe310abe76cd62216426cd03e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xmxtrct.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
34 #include "xmxtrct.hxx"
36 #include <rtl/memory.h>
37 #include <tools/zcodec.hxx>
38 #include <unotools/streamhelper.hxx>
39 #include <sot/storage.hxx>
41 // ----------------
42 // - XMXLockBytes -
43 // ----------------
45 class XMXLockBytes : public SvLockBytes
47 REF( NMSP_IO::XInputStream ) mxIStm;
48 SEQ( sal_Int8 ) maSeq;
50 XMXLockBytes();
52 public:
54 XMXLockBytes( const REF( NMSP_IO::XInputStream )& rxIStm );
55 virtual ~XMXLockBytes();
57 virtual ErrCode ReadAt( sal_Size nPos, void* pBuffer, sal_Size nCount, sal_Size* pRead ) const;
58 virtual ErrCode WriteAt( sal_Size nPos, const void* pBuffer, sal_Size nCount, sal_Size* pWritten );
59 virtual ErrCode Flush() const;
60 virtual ErrCode SetSize( sal_Size nSize );
61 virtual ErrCode Stat( SvLockBytesStat*, SvLockBytesStatFlag ) const;
64 // ------------------------------------------------------------------------
66 XMXLockBytes::XMXLockBytes( const REF( NMSP_IO::XInputStream )& rxIStm ) :
67 mxIStm( rxIStm )
69 if( mxIStm.is() )
71 const sal_uInt32 nBytesToRead = 65535;
72 sal_uInt32 nRead;
76 SEQ( sal_Int8 ) aReadSeq;
78 nRead = mxIStm->readSomeBytes( aReadSeq, nBytesToRead );
80 if( nRead )
82 const sal_uInt32 nOldLength = maSeq.getLength();
83 maSeq.realloc( nOldLength + nRead );
84 rtl_copyMemory( maSeq.getArray() + nOldLength, aReadSeq.getConstArray(), aReadSeq.getLength() );
87 while( nBytesToRead == nRead );
91 // ------------------------------------------------------------------------
93 XMXLockBytes::~XMXLockBytes()
97 // ------------------------------------------------------------------------
99 ErrCode XMXLockBytes::ReadAt( sal_Size nPos, void* pBuffer, sal_Size nCount, sal_Size* pRead ) const
101 const sal_Size nSeqLen = maSeq.getLength();
102 ErrCode nErr = ERRCODE_NONE;
104 if( nPos < nSeqLen )
106 if( ( nPos + nCount ) > nSeqLen )
107 nCount = nSeqLen - nPos;
109 rtl_copyMemory( pBuffer, maSeq.getConstArray() + nPos, nCount );
110 *pRead = nCount;
112 else
113 *pRead = 0UL;
115 return nErr;
118 // ------------------------------------------------------------------------
120 ErrCode XMXLockBytes::WriteAt( sal_Size /*nPos*/, const void* /*pBuffer*/, sal_Size /*nCount*/, sal_Size* /*pWritten*/ )
122 return ERRCODE_IO_CANTWRITE;
125 // ------------------------------------------------------------------------
127 ErrCode XMXLockBytes::Flush() const
129 return ERRCODE_NONE;
132 // ------------------------------------------------------------------------
134 ErrCode XMXLockBytes::SetSize( sal_Size /*nSize*/ )
136 return ERRCODE_IO_CANTWRITE;
139 // ------------------------------------------------------------------------
141 ErrCode XMXLockBytes::Stat( SvLockBytesStat* pStat, SvLockBytesStatFlag /*eFlag*/ ) const
143 pStat->nSize = maSeq.getLength();
144 return ERRCODE_NONE;
147 // ----------------
148 // - XMLExtractor -
149 // ----------------
151 XMLExtractor::XMLExtractor( const REF( NMSP_LANG::XMultiServiceFactory )& rxMgr ) :
152 mxFact( rxMgr )
156 // -----------------------------------------------------------------------------
158 XMLExtractor::~XMLExtractor()
162 // -----------------------------------------------------------------------------
164 REF( NMSP_IO::XInputStream ) SAL_CALL XMLExtractor::extract( const REF( NMSP_IO::XInputStream )& rxIStm ) throw( NMSP_UNO::RuntimeException )
166 REF( NMSP_IO::XInputStream ) xRet;
168 if( rxIStm.is() )
170 SvStream aIStm( new XMXLockBytes( rxIStm ) );
171 SvStorageRef aStorage( new SvStorage( aIStm ) );
172 String aStmName;
173 const String aFormat1( String::CreateFromAscii( "XMLFormat" ) );
174 const String aFormat2( String::CreateFromAscii( "XMLFormat2" ) );
176 if( aStorage->IsContained( aFormat2 ) )
177 aStmName = aFormat2;
178 else if( aStorage->IsContained( aFormat1 ) )
179 aStmName = aFormat1;
181 if( !aStorage->GetError() && aStmName.Len() && aStorage->IsStream( aStmName ) )
183 SvStorageStreamRef xStream( aStorage->OpenSotStream( aStmName ) );
185 if( xStream.Is() )
187 SvMemoryStream* pMemStm = new SvMemoryStream( 65535, 65535 );
188 ZCodec aCodec;
190 aCodec.BeginCompression( ZCODEC_BEST_COMPRESSION );
191 aCodec.Decompress( *xStream, *pMemStm );
192 aCodec.EndCompression();
194 xRet = new ::utl::OInputStreamHelper( new SvLockBytes( pMemStm, TRUE ), 65535 );
199 return xRet;