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