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 :
21 : #include <osl/diagnose.h>
22 :
23 : #include <uno/mapping.hxx>
24 :
25 : #include <cppuhelper/factory.hxx>
26 : #include <cppuhelper/implbase2.hxx>
27 : #include <cppuhelper/implementationentry.hxx>
28 :
29 : #include <rtl/textenc.h>
30 : #include <rtl/tencinfo.h>
31 :
32 : #include <com/sun/star/io/XTextOutputStream2.hpp>
33 : #include <com/sun/star/lang/XServiceInfo.hpp>
34 :
35 :
36 : #define IMPLEMENTATION_NAME "com.sun.star.comp.io.TextOutputStream"
37 : #define SERVICE_NAME "com.sun.star.io.TextOutputStream"
38 :
39 : using namespace ::osl;
40 : using namespace ::rtl;
41 : using namespace ::cppu;
42 : using namespace ::com::sun::star::uno;
43 : using namespace ::com::sun::star::lang;
44 : using namespace ::com::sun::star::io;
45 : using namespace ::com::sun::star::registry;
46 :
47 : namespace io_TextOutputStream
48 : {
49 : //===========================================================================
50 : // Implementation XTextOutputStream
51 :
52 : typedef WeakImplHelper2< XTextOutputStream2, XServiceInfo > TextOutputStreamHelper;
53 :
54 : class OTextOutputStream : public TextOutputStreamHelper
55 : {
56 : Reference< XOutputStream > mxStream;
57 :
58 : // Encoding
59 : OUString mEncoding;
60 : sal_Bool mbEncodingInitialized;
61 : rtl_UnicodeToTextConverter mConvUnicode2Text;
62 : rtl_UnicodeToTextContext mContextUnicode2Text;
63 :
64 : Sequence<sal_Int8> implConvert( const OUString& rSource );
65 :
66 : public:
67 : OTextOutputStream();
68 : ~OTextOutputStream();
69 :
70 : // Methods XTextOutputStream
71 : virtual void SAL_CALL writeString( const OUString& aString )
72 : throw(IOException, RuntimeException);
73 : virtual void SAL_CALL setEncoding( const OUString& Encoding )
74 : throw(RuntimeException);
75 :
76 : // Methods XOutputStream
77 : virtual void SAL_CALL writeBytes( const Sequence< sal_Int8 >& aData )
78 : throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
79 : virtual void SAL_CALL flush( )
80 : throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
81 : virtual void SAL_CALL closeOutput( )
82 : throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
83 :
84 : // Methods XActiveDataSource
85 : virtual void SAL_CALL setOutputStream( const Reference< XOutputStream >& aStream )
86 : throw(RuntimeException);
87 : virtual Reference< XOutputStream > SAL_CALL getOutputStream( )
88 : throw(RuntimeException);
89 :
90 : // Methods XServiceInfo
91 : virtual OUString SAL_CALL getImplementationName() throw();
92 : virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(void) throw();
93 : virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw();
94 : };
95 :
96 0 : OTextOutputStream::OTextOutputStream()
97 : {
98 0 : mbEncodingInitialized = false;
99 0 : }
100 :
101 0 : OTextOutputStream::~OTextOutputStream()
102 : {
103 0 : if( mbEncodingInitialized )
104 : {
105 0 : rtl_destroyUnicodeToTextContext( mConvUnicode2Text, mContextUnicode2Text );
106 0 : rtl_destroyUnicodeToTextConverter( mConvUnicode2Text );
107 : }
108 0 : }
109 :
110 0 : Sequence<sal_Int8> OTextOutputStream::implConvert( const OUString& rSource )
111 : {
112 0 : const sal_Unicode *puSource = rSource.getStr();
113 0 : sal_Int32 nSourceSize = rSource.getLength();
114 :
115 0 : sal_Size nTargetCount = 0;
116 0 : sal_Size nSourceCount = 0;
117 :
118 : sal_uInt32 uiInfo;
119 : sal_Size nSrcCvtChars;
120 :
121 : // take nSourceSize * 3 as preference
122 : // this is an upper boundary for converting to utf8,
123 : // which most often used as the target.
124 0 : sal_Int32 nSeqSize = nSourceSize * 3;
125 :
126 0 : Sequence<sal_Int8> seqText( nSeqSize );
127 0 : sal_Char *pTarget = (sal_Char *) seqText.getArray();
128 : while( true )
129 : {
130 : nTargetCount += rtl_convertUnicodeToText(
131 : mConvUnicode2Text,
132 : mContextUnicode2Text,
133 : &( puSource[nSourceCount] ),
134 : nSourceSize - nSourceCount ,
135 : &( pTarget[nTargetCount] ),
136 : nSeqSize - nTargetCount,
137 : RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT |
138 : RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT ,
139 : &uiInfo,
140 0 : &nSrcCvtChars);
141 0 : nSourceCount += nSrcCvtChars;
142 :
143 0 : if( uiInfo & RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL )
144 : {
145 0 : nSeqSize *= 2;
146 0 : seqText.realloc( nSeqSize ); // double array size
147 0 : pTarget = (sal_Char*) seqText.getArray();
148 0 : continue;
149 : }
150 0 : break;
151 : }
152 :
153 : // reduce the size of the buffer (fast, no copy necessary)
154 0 : seqText.realloc( nTargetCount );
155 0 : return seqText;
156 : }
157 :
158 :
159 : //===========================================================================
160 : // XTextOutputStream
161 :
162 0 : void OTextOutputStream::writeString( const OUString& aString )
163 : throw(IOException, RuntimeException)
164 : {
165 0 : if( !mbEncodingInitialized )
166 : {
167 0 : OUString aUtf8Str("utf8");
168 0 : setEncoding( aUtf8Str );
169 : }
170 0 : if( !mbEncodingInitialized )
171 0 : return;
172 :
173 0 : Sequence<sal_Int8> aByteSeq = implConvert( aString );
174 0 : mxStream->writeBytes( aByteSeq );
175 : }
176 :
177 0 : void OTextOutputStream::setEncoding( const OUString& Encoding )
178 : throw(RuntimeException)
179 : {
180 0 : OString aOEncodingStr = OUStringToOString( Encoding, RTL_TEXTENCODING_ASCII_US );
181 0 : rtl_TextEncoding encoding = rtl_getTextEncodingFromMimeCharset( aOEncodingStr.getStr() );
182 0 : if( RTL_TEXTENCODING_DONTKNOW == encoding )
183 0 : return;
184 :
185 0 : mbEncodingInitialized = true;
186 0 : mConvUnicode2Text = rtl_createUnicodeToTextConverter( encoding );
187 0 : mContextUnicode2Text = rtl_createUnicodeToTextContext( mConvUnicode2Text );
188 0 : mEncoding = Encoding;
189 : }
190 :
191 : //===========================================================================
192 : // XOutputStream
193 0 : void OTextOutputStream::writeBytes( const Sequence< sal_Int8 >& aData )
194 : throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
195 : {
196 0 : mxStream->writeBytes( aData );
197 0 : }
198 :
199 0 : void OTextOutputStream::flush( )
200 : throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
201 : {
202 0 : mxStream->flush();
203 0 : }
204 :
205 0 : void OTextOutputStream::closeOutput( )
206 : throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
207 : {
208 0 : mxStream->closeOutput();
209 0 : }
210 :
211 :
212 : //===========================================================================
213 : // XActiveDataSource
214 :
215 0 : void OTextOutputStream::setOutputStream( const Reference< XOutputStream >& aStream )
216 : throw(RuntimeException)
217 : {
218 0 : mxStream = aStream;
219 0 : }
220 :
221 0 : Reference< XOutputStream > OTextOutputStream::getOutputStream()
222 : throw(RuntimeException)
223 : {
224 0 : return mxStream;
225 : }
226 :
227 :
228 0 : Reference< XInterface > SAL_CALL TextOutputStream_CreateInstance(
229 : SAL_UNUSED_PARAMETER const Reference< XComponentContext > &)
230 : {
231 0 : return Reference < XInterface >( ( OWeakObject * ) new OTextOutputStream() );
232 : }
233 :
234 0 : OUString TextOutputStream_getImplementationName() SAL_THROW( () )
235 : {
236 0 : return OUString( IMPLEMENTATION_NAME );
237 : }
238 :
239 :
240 0 : Sequence< OUString > TextOutputStream_getSupportedServiceNames()
241 : {
242 0 : Sequence< OUString > seqNames(1);
243 0 : seqNames.getArray()[0] = SERVICE_NAME;
244 0 : return seqNames;
245 : }
246 :
247 0 : OUString OTextOutputStream::getImplementationName() throw()
248 : {
249 0 : return TextOutputStream_getImplementationName();
250 : }
251 :
252 0 : sal_Bool OTextOutputStream::supportsService(const OUString& ServiceName) throw()
253 : {
254 0 : Sequence< OUString > aSNL = getSupportedServiceNames();
255 0 : const OUString * pArray = aSNL.getConstArray();
256 :
257 0 : for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
258 0 : if( pArray[i] == ServiceName )
259 0 : return sal_True;
260 :
261 0 : return sal_False;
262 : }
263 :
264 0 : Sequence< OUString > OTextOutputStream::getSupportedServiceNames(void) throw()
265 : {
266 0 : return TextOutputStream_getSupportedServiceNames();
267 : }
268 :
269 :
270 : }
271 :
272 : using namespace io_TextOutputStream;
273 :
274 : static struct ImplementationEntry g_entries[] =
275 : {
276 : {
277 : TextOutputStream_CreateInstance, TextOutputStream_getImplementationName ,
278 : TextOutputStream_getSupportedServiceNames, createSingleComponentFactory ,
279 : 0, 0
280 : },
281 : { 0, 0, 0, 0, 0, 0 }
282 : };
283 :
284 0 : extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL textoutstream_component_getFactory(
285 : const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
286 : {
287 0 : return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
288 : }
289 :
290 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|