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 : // ============================================================================
37 :
38 : namespace {
39 :
40 : typedef ::cppu::WeakImplHelper1< XInputStream > UnoBinaryInputStream_BASE;
41 :
42 : /** Implementation of a UNO input stream wrapping a binary input stream.
43 : */
44 36 : class UnoBinaryInputStream : public UnoBinaryInputStream_BASE
45 : {
46 : public:
47 : explicit UnoBinaryInputStream( BinaryInputStream& rInStrm );
48 :
49 : virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& rData, sal_Int32 nBytesToRead )
50 : throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
51 : virtual sal_Int32 SAL_CALL readSomeBytes( Sequence< sal_Int8 >& rData, sal_Int32 nMaxBytesToRead )
52 : throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
53 : virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip )
54 : throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
55 : virtual sal_Int32 SAL_CALL available()
56 : throw (NotConnectedException, IOException, RuntimeException);
57 : virtual void SAL_CALL closeInput()
58 : throw (NotConnectedException, IOException, RuntimeException);
59 :
60 : private:
61 : void ensureConnected() const throw (NotConnectedException);
62 :
63 : private:
64 : BinaryInputStream* mpInStrm;
65 : };
66 :
67 : // ----------------------------------------------------------------------------
68 :
69 18 : UnoBinaryInputStream::UnoBinaryInputStream( BinaryInputStream& rInStrm ) :
70 18 : mpInStrm( &rInStrm )
71 : {
72 18 : }
73 :
74 0 : sal_Int32 SAL_CALL UnoBinaryInputStream::readBytes( Sequence< sal_Int8 >& rData, sal_Int32 nBytesToRead )
75 : throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
76 : {
77 0 : ensureConnected();
78 0 : return mpInStrm->readData( rData, nBytesToRead, 1 );
79 : }
80 :
81 74 : sal_Int32 SAL_CALL UnoBinaryInputStream::readSomeBytes( Sequence< sal_Int8 >& rData, sal_Int32 nMaxBytesToRead )
82 : throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
83 : {
84 74 : ensureConnected();
85 74 : return mpInStrm->readData( rData, nMaxBytesToRead, 1 );
86 : }
87 :
88 0 : void SAL_CALL UnoBinaryInputStream::skipBytes( sal_Int32 nBytesToSkip )
89 : throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
90 : {
91 0 : ensureConnected();
92 0 : mpInStrm->skip( nBytesToSkip, 1 );
93 0 : }
94 :
95 0 : sal_Int32 SAL_CALL UnoBinaryInputStream::available() throw (NotConnectedException, IOException, RuntimeException)
96 : {
97 0 : ensureConnected();
98 0 : throw RuntimeException( CREATE_OUSTRING( "Functionality not supported" ), Reference< XInputStream >() );
99 : }
100 :
101 0 : void SAL_CALL UnoBinaryInputStream::closeInput() throw (NotConnectedException, IOException, RuntimeException)
102 : {
103 0 : ensureConnected();
104 0 : mpInStrm->close();
105 0 : mpInStrm = 0;
106 0 : }
107 :
108 74 : void UnoBinaryInputStream::ensureConnected() const throw (NotConnectedException)
109 : {
110 74 : if( !mpInStrm )
111 0 : throw NotConnectedException( CREATE_OUSTRING( "Stream closed" ), Reference< XInterface >() );
112 74 : }
113 :
114 : } // namespace
115 :
116 : // ============================================================================
117 :
118 0 : TextInputStream::TextInputStream( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm, rtl_TextEncoding eTextEnc )
119 : {
120 0 : init( rxContext, rxInStrm, eTextEnc );
121 0 : }
122 :
123 18 : TextInputStream::TextInputStream( const Reference< XComponentContext >& rxContext, BinaryInputStream& rInStrm, rtl_TextEncoding eTextEnc )
124 : {
125 18 : init( rxContext, new UnoBinaryInputStream( rInStrm ), eTextEnc );
126 18 : }
127 :
128 18 : TextInputStream::~TextInputStream()
129 : {
130 18 : }
131 :
132 508 : bool TextInputStream::isEof() const
133 : {
134 508 : if( mxTextStrm.is() ) try
135 : {
136 508 : return mxTextStrm->isEOF();
137 : }
138 0 : catch (const Exception&)
139 : {
140 : }
141 0 : return true;
142 : }
143 :
144 493 : OUString TextInputStream::readLine()
145 : {
146 493 : if( mxTextStrm.is() ) try
147 : {
148 : /* The function createFinalString() adds a character that may have
149 : been buffered in the previous call of readToChar() (see below). */
150 493 : return createFinalString( mxTextStrm->readLine() );
151 : }
152 0 : catch (const Exception&)
153 : {
154 0 : mxTextStrm.clear();
155 : }
156 0 : return OUString();
157 : }
158 :
159 0 : OUString TextInputStream::readToChar( sal_Unicode cChar, bool bIncludeChar )
160 : {
161 0 : if( mxTextStrm.is() ) try
162 : {
163 0 : Sequence< sal_Unicode > aDelimiters( 1 );
164 0 : aDelimiters[ 0 ] = cChar;
165 : /* Always get the delimiter character from the UNO text input stream.
166 : In difference to this implementation, it will not return it in the
167 : next call but silently skip it. If caller specifies to exclude the
168 : character in this call, it will be returned in the next call of one
169 : of the own member functions. The function createFinalString() adds
170 : a character that has been buffered in the previous call. */
171 0 : OUString aString = createFinalString( mxTextStrm->readString( aDelimiters, sal_False ) );
172 : // remove last character from string and remember it for next call
173 0 : if( !bIncludeChar && !aString.isEmpty() && (aString[ aString.getLength() - 1 ] == cChar) )
174 : {
175 0 : mcPendingChar = cChar;
176 0 : aString = aString.copy( 0, aString.getLength() - 1 );
177 : }
178 0 : return aString;
179 : }
180 0 : catch (const Exception&)
181 : {
182 0 : mxTextStrm.clear();
183 : }
184 0 : return OUString();
185 : }
186 :
187 20 : /*static*/ Reference< XTextInputStream > TextInputStream::createXTextInputStream(
188 : const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm, rtl_TextEncoding eTextEnc )
189 : {
190 20 : Reference< XTextInputStream > xTextStrm;
191 20 : const char* pcCharset = rtl_getBestMimeCharsetFromTextEncoding( eTextEnc );
192 : OSL_ENSURE( pcCharset, "TextInputStream::createXTextInputStream - unsupported text encoding" );
193 20 : if( rxContext.is() && rxInStrm.is() && pcCharset ) try
194 : {
195 20 : Reference< XMultiServiceFactory > xFactory( rxContext->getServiceManager(), UNO_QUERY_THROW );
196 20 : Reference< XActiveDataSink > xDataSink( xFactory->createInstance( CREATE_OUSTRING( "com.sun.star.io.TextInputStream" ) ), UNO_QUERY_THROW );
197 20 : xDataSink->setInputStream( rxInStrm );
198 20 : xTextStrm.set( xDataSink, UNO_QUERY_THROW );
199 20 : xTextStrm->setEncoding( OUString::createFromAscii( pcCharset ) );
200 : }
201 0 : catch (const Exception&)
202 : {
203 : }
204 20 : return xTextStrm;
205 : }
206 :
207 : // private --------------------------------------------------------------------
208 :
209 493 : OUString TextInputStream::createFinalString( const OUString& rString )
210 : {
211 493 : if( mcPendingChar == 0 )
212 493 : return rString;
213 :
214 0 : OUString aString = OUString( mcPendingChar ) + rString;
215 0 : mcPendingChar = 0;
216 0 : return aString;
217 : }
218 :
219 18 : void TextInputStream::init( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm, rtl_TextEncoding eTextEnc )
220 : {
221 18 : mcPendingChar = 0;
222 18 : mxTextStrm = createXTextInputStream( rxContext, rxInStrm, eTextEnc );
223 18 : }
224 :
225 : // ============================================================================
226 :
227 : } // namespace oox
228 :
229 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|