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/TextInputStream.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 0 : 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, std::exception) SAL_OVERRIDE;
51 : virtual sal_Int32 SAL_CALL readSomeBytes( Sequence< sal_Int8 >& rData, sal_Int32 nMaxBytesToRead )
52 : throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
53 : virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip )
54 : throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
55 : virtual sal_Int32 SAL_CALL available()
56 : throw (NotConnectedException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
57 : virtual void SAL_CALL closeInput()
58 : throw (NotConnectedException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
59 :
60 : private:
61 : void ensureConnected() const throw (NotConnectedException);
62 :
63 : private:
64 : BinaryInputStream* mpInStrm;
65 : };
66 :
67 :
68 :
69 0 : UnoBinaryInputStream::UnoBinaryInputStream( BinaryInputStream& rInStrm ) :
70 0 : mpInStrm( &rInStrm )
71 : {
72 0 : }
73 :
74 0 : sal_Int32 SAL_CALL UnoBinaryInputStream::readBytes( Sequence< sal_Int8 >& rData, sal_Int32 nBytesToRead )
75 : throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
76 : {
77 0 : ensureConnected();
78 0 : return mpInStrm->readData( rData, nBytesToRead, 1 );
79 : }
80 :
81 0 : sal_Int32 SAL_CALL UnoBinaryInputStream::readSomeBytes( Sequence< sal_Int8 >& rData, sal_Int32 nMaxBytesToRead )
82 : throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
83 : {
84 0 : ensureConnected();
85 0 : return mpInStrm->readData( rData, nMaxBytesToRead, 1 );
86 : }
87 :
88 0 : void SAL_CALL UnoBinaryInputStream::skipBytes( sal_Int32 nBytesToSkip )
89 : throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
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, std::exception)
96 : {
97 0 : ensureConnected();
98 0 : throw RuntimeException( "Functionality not supported", Reference< XInputStream >() );
99 : }
100 :
101 0 : void SAL_CALL UnoBinaryInputStream::closeInput() throw (NotConnectedException, IOException, RuntimeException, std::exception)
102 : {
103 0 : ensureConnected();
104 0 : mpInStrm->close();
105 0 : mpInStrm = 0;
106 0 : }
107 :
108 0 : void UnoBinaryInputStream::ensureConnected() const throw (NotConnectedException)
109 : {
110 0 : if( !mpInStrm )
111 0 : throw NotConnectedException( "Stream closed", Reference< XInterface >() );
112 0 : }
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 0 : TextInputStream::TextInputStream( const Reference< XComponentContext >& rxContext, BinaryInputStream& rInStrm, rtl_TextEncoding eTextEnc )
124 : {
125 0 : init( rxContext, new UnoBinaryInputStream( rInStrm ), eTextEnc );
126 0 : }
127 :
128 0 : TextInputStream::~TextInputStream()
129 : {
130 0 : }
131 :
132 0 : bool TextInputStream::isEof() const
133 : {
134 0 : if( mxTextStrm.is() ) try
135 : {
136 0 : return mxTextStrm->isEOF();
137 : }
138 0 : catch (const Exception&)
139 : {
140 : }
141 0 : return true;
142 : }
143 :
144 0 : OUString TextInputStream::readLine()
145 : {
146 0 : 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 0 : 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 0 : Reference< XTextInputStream2 > TextInputStream::createXTextInputStream(
188 : const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm, rtl_TextEncoding eTextEnc )
189 : {
190 0 : Reference< XTextInputStream2 > xTextStrm;
191 0 : const char* pcCharset = rtl_getBestMimeCharsetFromTextEncoding( eTextEnc );
192 : OSL_ENSURE( pcCharset, "TextInputStream::createXTextInputStream - unsupported text encoding" );
193 0 : if( rxContext.is() && rxInStrm.is() && pcCharset ) try
194 : {
195 0 : xTextStrm = com::sun::star::io::TextInputStream::create( rxContext );
196 0 : xTextStrm->setInputStream( rxInStrm );
197 0 : xTextStrm->setEncoding( OUString::createFromAscii( pcCharset ) );
198 : }
199 0 : catch (const Exception&)
200 : {
201 : }
202 0 : return xTextStrm;
203 : }
204 :
205 : // private --------------------------------------------------------------------
206 :
207 0 : OUString TextInputStream::createFinalString( const OUString& rString )
208 : {
209 0 : if( mcPendingChar == 0 )
210 0 : return rString;
211 :
212 0 : OUString aString = OUString( mcPendingChar ) + rString;
213 0 : mcPendingChar = 0;
214 0 : return aString;
215 : }
216 :
217 0 : void TextInputStream::init( const Reference< XComponentContext >& rxContext, const Reference< XInputStream >& rxInStrm, rtl_TextEncoding eTextEnc )
218 : {
219 0 : mcPendingChar = 0;
220 0 : mxTextStrm = createXTextInputStream( rxContext, rxInStrm, eTextEnc );
221 0 : }
222 :
223 :
224 :
225 : } // namespace oox
226 :
227 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|