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