Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "oox/helper/textinputstream.hxx"
21 :
22 : #include <com/sun/star/io/XActiveDataSink.hpp>
23 : #include <com/sun/star/io/XTextInputStream.hpp>
24 : #include <cppuhelper/implbase1.hxx>
25 : #include <rtl/tencinfo.h>
26 : #include "oox/helper/binaryinputstream.hxx"
27 :
28 : namespace oox {
29 :
30 : // ============================================================================
31 :
32 : using namespace ::com::sun::star::io;
33 : using namespace ::com::sun::star::lang;
34 : using namespace ::com::sun::star::uno;
35 :
36 : using ::rtl::OUString;
37 :
38 : // ============================================================================
39 :
40 : namespace {
41 :
42 : typedef ::cppu::WeakImplHelper1< XInputStream > UnoBinaryInputStream_BASE;
43 :
44 : /** Implementation of a UNO input stream wrapping a binary input stream.
45 : */
46 120 : class UnoBinaryInputStream : public UnoBinaryInputStream_BASE
47 : {
48 : public:
49 : explicit UnoBinaryInputStream( BinaryInputStream& rInStrm );
50 :
51 : virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& rData, sal_Int32 nBytesToRead )
52 : throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
53 : virtual sal_Int32 SAL_CALL readSomeBytes( Sequence< sal_Int8 >& rData, sal_Int32 nMaxBytesToRead )
54 : throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
55 : virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip )
56 : throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
57 : virtual sal_Int32 SAL_CALL available()
58 : throw (NotConnectedException, IOException, RuntimeException);
59 : virtual void SAL_CALL closeInput()
60 : throw (NotConnectedException, IOException, RuntimeException);
61 :
62 : private:
63 : void ensureConnected() const throw (NotConnectedException);
64 :
65 : private:
66 : BinaryInputStream* mpInStrm;
67 : };
68 :
69 : // ----------------------------------------------------------------------------
70 :
71 60 : UnoBinaryInputStream::UnoBinaryInputStream( BinaryInputStream& rInStrm ) :
72 60 : mpInStrm( &rInStrm )
73 : {
74 60 : }
75 :
76 0 : sal_Int32 SAL_CALL UnoBinaryInputStream::readBytes( Sequence< sal_Int8 >& rData, sal_Int32 nBytesToRead )
77 : throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
78 : {
79 0 : ensureConnected();
80 0 : return mpInStrm->readData( rData, nBytesToRead, 1 );
81 : }
82 :
83 264 : sal_Int32 SAL_CALL UnoBinaryInputStream::readSomeBytes( Sequence< sal_Int8 >& rData, sal_Int32 nMaxBytesToRead )
84 : throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
85 : {
86 264 : ensureConnected();
87 264 : return mpInStrm->readData( rData, nMaxBytesToRead, 1 );
88 : }
89 :
90 0 : void SAL_CALL UnoBinaryInputStream::skipBytes( sal_Int32 nBytesToSkip )
91 : throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
92 : {
93 0 : ensureConnected();
94 0 : mpInStrm->skip( nBytesToSkip, 1 );
95 0 : }
96 :
97 0 : sal_Int32 SAL_CALL UnoBinaryInputStream::available() throw (NotConnectedException, IOException, RuntimeException)
98 : {
99 0 : ensureConnected();
100 0 : throw RuntimeException( CREATE_OUSTRING( "Functionality not supported" ), Reference< XInputStream >() );
101 : }
102 :
103 0 : void SAL_CALL UnoBinaryInputStream::closeInput() throw (NotConnectedException, IOException, RuntimeException)
104 : {
105 0 : ensureConnected();
106 0 : mpInStrm->close();
107 0 : mpInStrm = 0;
108 0 : }
109 :
110 264 : void UnoBinaryInputStream::ensureConnected() const throw (NotConnectedException)
111 : {
112 264 : if( !mpInStrm )
113 0 : throw NotConnectedException( CREATE_OUSTRING( "Stream closed" ), Reference< XInterface >() );
114 264 : }
115 :
116 : } // namespace
117 :
118 : // ============================================================================
119 :
120 0 : TextInputStream::TextInputStream( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm, rtl_TextEncoding eTextEnc )
121 : {
122 0 : init( rxContext, rxInStrm, eTextEnc );
123 0 : }
124 :
125 60 : TextInputStream::TextInputStream( const Reference< XComponentContext >& rxContext, BinaryInputStream& rInStrm, rtl_TextEncoding eTextEnc )
126 : {
127 60 : init( rxContext, new UnoBinaryInputStream( rInStrm ), eTextEnc );
128 60 : }
129 :
130 60 : TextInputStream::~TextInputStream()
131 : {
132 60 : }
133 :
134 1534 : bool TextInputStream::isEof() const
135 : {
136 1534 : if( mxTextStrm.is() ) try
137 : {
138 1534 : return mxTextStrm->isEOF();
139 : }
140 0 : catch (const Exception&)
141 : {
142 : }
143 0 : return true;
144 : }
145 :
146 1484 : OUString TextInputStream::readLine()
147 : {
148 1484 : if( mxTextStrm.is() ) try
149 : {
150 : /* The function createFinalString() adds a character that may have
151 : been buffered in the previous call of readToChar() (see below). */
152 1484 : return createFinalString( mxTextStrm->readLine() );
153 : }
154 0 : catch (const Exception&)
155 : {
156 0 : mxTextStrm.clear();
157 : }
158 0 : return OUString();
159 : }
160 :
161 0 : OUString TextInputStream::readToChar( sal_Unicode cChar, bool bIncludeChar )
162 : {
163 0 : if( mxTextStrm.is() ) try
164 : {
165 0 : Sequence< sal_Unicode > aDelimiters( 1 );
166 0 : aDelimiters[ 0 ] = cChar;
167 : /* Always get the delimiter character from the UNO text input stream.
168 : In difference to this implementation, it will not return it in the
169 : next call but silently skip it. If caller specifies to exclude the
170 : character in this call, it will be returned in the next call of one
171 : of the own member functions. The function createFinalString() adds
172 : a character that has been buffered in the previous call. */
173 0 : OUString aString = createFinalString( mxTextStrm->readString( aDelimiters, sal_False ) );
174 : // remove last character from string and remember it for next call
175 0 : if( !bIncludeChar && !aString.isEmpty() && (aString[ aString.getLength() - 1 ] == cChar) )
176 : {
177 0 : mcPendingChar = cChar;
178 0 : aString = aString.copy( 0, aString.getLength() - 1 );
179 : }
180 0 : return aString;
181 : }
182 0 : catch (const Exception&)
183 : {
184 0 : mxTextStrm.clear();
185 : }
186 0 : return OUString();
187 : }
188 :
189 64 : /*static*/ Reference< XTextInputStream > TextInputStream::createXTextInputStream(
190 : const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm, rtl_TextEncoding eTextEnc )
191 : {
192 64 : Reference< XTextInputStream > xTextStrm;
193 64 : const char* pcCharset = rtl_getBestMimeCharsetFromTextEncoding( eTextEnc );
194 : OSL_ENSURE( pcCharset, "TextInputStream::createXTextInputStream - unsupported text encoding" );
195 64 : if( rxContext.is() && rxInStrm.is() && pcCharset ) try
196 : {
197 64 : Reference< XMultiServiceFactory > xFactory( rxContext->getServiceManager(), UNO_QUERY_THROW );
198 64 : Reference< XActiveDataSink > xDataSink( xFactory->createInstance( CREATE_OUSTRING( "com.sun.star.io.TextInputStream" ) ), UNO_QUERY_THROW );
199 64 : xDataSink->setInputStream( rxInStrm );
200 64 : xTextStrm.set( xDataSink, UNO_QUERY_THROW );
201 64 : xTextStrm->setEncoding( OUString::createFromAscii( pcCharset ) );
202 : }
203 0 : catch (const Exception&)
204 : {
205 : }
206 64 : return xTextStrm;
207 : }
208 :
209 : // private --------------------------------------------------------------------
210 :
211 1484 : OUString TextInputStream::createFinalString( const OUString& rString )
212 : {
213 1484 : if( mcPendingChar == 0 )
214 1484 : return rString;
215 :
216 0 : OUString aString = OUString( mcPendingChar ) + rString;
217 0 : mcPendingChar = 0;
218 0 : return aString;
219 : }
220 :
221 60 : void TextInputStream::init( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm, rtl_TextEncoding eTextEnc )
222 : {
223 60 : mcPendingChar = 0;
224 60 : mxTextStrm = createXTextInputStream( rxContext, rxInStrm, eTextEnc );
225 60 : }
226 :
227 : // ============================================================================
228 :
229 : } // namespace oox
230 :
231 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|