Branch data 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 [ - + ]: 156 : 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 : 78 : UnoBinaryInputStream::UnoBinaryInputStream( BinaryInputStream& rInStrm ) :
72 : 78 : mpInStrm( &rInStrm )
73 : : {
74 : 78 : }
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 : 338 : sal_Int32 SAL_CALL UnoBinaryInputStream::readSomeBytes( Sequence< sal_Int8 >& rData, sal_Int32 nMaxBytesToRead )
84 : : throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
85 : : {
86 : 338 : ensureConnected();
87 : 338 : 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 : 338 : void UnoBinaryInputStream::ensureConnected() const throw (NotConnectedException)
111 : : {
112 [ - + ]: 338 : if( !mpInStrm )
113 [ # # ][ # # ]: 0 : throw NotConnectedException( CREATE_OUSTRING( "Stream closed" ), Reference< XInterface >() );
114 : 338 : }
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 : 78 : TextInputStream::TextInputStream( const Reference< XComponentContext >& rxContext, BinaryInputStream& rInStrm, rtl_TextEncoding eTextEnc )
126 : : {
127 [ + - ][ + - ]: 78 : init( rxContext, new UnoBinaryInputStream( rInStrm ), eTextEnc );
[ + - ][ + - ]
128 : 78 : }
129 : :
130 : 78 : TextInputStream::~TextInputStream()
131 : : {
132 : 78 : }
133 : :
134 : 2042 : bool TextInputStream::isEof() const
135 : : {
136 [ + - ]: 2042 : if( mxTextStrm.is() ) try
137 : : {
138 [ + - ][ + - ]: 2042 : return mxTextStrm->isEOF();
139 : : }
140 : 0 : catch (const Exception&)
141 : : {
142 : : }
143 [ # # ]: 2042 : return true;
144 : : }
145 : :
146 : 1977 : OUString TextInputStream::readLine()
147 : : {
148 [ + - ]: 1977 : 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 [ + - ][ + - ]: 1977 : return createFinalString( mxTextStrm->readLine() );
153 : : }
154 : 0 : catch (const Exception&)
155 : : {
156 : 0 : mxTextStrm.clear();
157 : : }
158 [ # # ]: 1977 : 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 : 84 : /*static*/ Reference< XTextInputStream > TextInputStream::createXTextInputStream(
190 : : const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm, rtl_TextEncoding eTextEnc )
191 : : {
192 : 84 : Reference< XTextInputStream > xTextStrm;
193 [ + - ]: 84 : const char* pcCharset = rtl_getBestMimeCharsetFromTextEncoding( eTextEnc );
194 : : OSL_ENSURE( pcCharset, "TextInputStream::createXTextInputStream - unsupported text encoding" );
195 [ + - ][ + - ]: 84 : if( rxContext.is() && rxInStrm.is() && pcCharset ) try
[ + - ][ + - ]
196 : : {
197 [ + - ][ + - ]: 84 : Reference< XMultiServiceFactory > xFactory( rxContext->getServiceManager(), UNO_QUERY_THROW );
[ + - ]
198 [ + - ][ + - ]: 84 : Reference< XActiveDataSink > xDataSink( xFactory->createInstance( CREATE_OUSTRING( "com.sun.star.io.TextInputStream" ) ), UNO_QUERY_THROW );
[ + - ][ + - ]
199 [ + - ][ + - ]: 84 : xDataSink->setInputStream( rxInStrm );
200 [ + - ]: 84 : xTextStrm.set( xDataSink, UNO_QUERY_THROW );
201 [ + - ][ + - ]: 84 : xTextStrm->setEncoding( OUString::createFromAscii( pcCharset ) );
[ # # ]
202 : : }
203 [ # # ]: 0 : catch (const Exception&)
204 : : {
205 : : }
206 : 84 : return xTextStrm;
207 : : }
208 : :
209 : : // private --------------------------------------------------------------------
210 : :
211 : 1977 : OUString TextInputStream::createFinalString( const OUString& rString )
212 : : {
213 [ + - ]: 1977 : if( mcPendingChar == 0 )
214 : 1977 : return rString;
215 : :
216 : 0 : OUString aString = OUString( mcPendingChar ) + rString;
217 : 0 : mcPendingChar = 0;
218 : 1977 : return aString;
219 : : }
220 : :
221 : 78 : void TextInputStream::init( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm, rtl_TextEncoding eTextEnc )
222 : : {
223 : 78 : mcPendingChar = 0;
224 [ + - ]: 78 : mxTextStrm = createXTextInputStream( rxContext, rxInStrm, eTextEnc );
225 : 78 : }
226 : :
227 : : // ============================================================================
228 : :
229 : : } // namespace oox
230 : :
231 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|