xref: /trunk/main/io/source/TextOutputStream/TextOutputStream.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_io.hxx"
30 
31 
32 #include <osl/mutex.hxx>
33 #include <osl/diagnose.h>
34 
35 #include <uno/mapping.hxx>
36 
37 #include <cppuhelper/factory.hxx>
38 #include <cppuhelper/implbase3.hxx>
39 #include <cppuhelper/implementationentry.hxx>
40 
41 #include <rtl/textenc.h>
42 #include <rtl/tencinfo.h>
43 #include <rtl/unload.h>
44 
45 #include <com/sun/star/io/XTextOutputStream.hpp>
46 #include <com/sun/star/io/XActiveDataSource.hpp>
47 #include <com/sun/star/lang/XServiceInfo.hpp>
48 
49 
50 #define IMPLEMENTATION_NAME "com.sun.star.comp.io.TextOutputStream"
51 #define SERVICE_NAME "com.sun.star.io.TextOutputStream"
52 
53 using namespace ::osl;
54 using namespace ::rtl;
55 using namespace ::cppu;
56 using namespace ::com::sun::star::uno;
57 using namespace ::com::sun::star::lang;
58 using namespace ::com::sun::star::io;
59 using namespace ::com::sun::star::registry;
60 
61 namespace io_TextOutputStream
62 {
63     rtl_StandardModuleCount g_moduleCount = MODULE_COUNT_INIT;
64 //===========================================================================
65 // Implementation XTextOutputStream
66 
67 typedef WeakImplHelper3< XTextOutputStream, XActiveDataSource, XServiceInfo > TextOutputStreamHelper;
68 class OCommandEnvironment;
69 
70 class OTextOutputStream : public TextOutputStreamHelper
71 {
72     Reference< XOutputStream > mxStream;
73 
74     // Encoding
75     OUString mEncoding;
76     sal_Bool mbEncodingInitialized;
77     rtl_UnicodeToTextConverter  mConvUnicode2Text;
78     rtl_UnicodeToTextContext    mContextUnicode2Text;
79 
80     Sequence<sal_Int8> implConvert( const OUString& rSource );
81 
82 public:
83     OTextOutputStream();
84     ~OTextOutputStream();
85 
86     // Methods XTextOutputStream
87     virtual void SAL_CALL writeString( const OUString& aString )
88         throw(IOException, RuntimeException);
89     virtual void SAL_CALL setEncoding( const OUString& Encoding )
90         throw(RuntimeException);
91 
92     // Methods XOutputStream
93     virtual void SAL_CALL writeBytes( const Sequence< sal_Int8 >& aData )
94         throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
95     virtual void SAL_CALL flush(  )
96         throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
97     virtual void SAL_CALL closeOutput(  )
98         throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
99 
100     // Methods XActiveDataSource
101     virtual void SAL_CALL setOutputStream( const Reference< XOutputStream >& aStream )
102         throw(RuntimeException);
103     virtual Reference< XOutputStream > SAL_CALL getOutputStream(  )
104         throw(RuntimeException);
105 
106     // Methods XServiceInfo
107         virtual OUString              SAL_CALL getImplementationName() throw();
108         virtual Sequence< OUString >  SAL_CALL getSupportedServiceNames(void) throw();
109         virtual sal_Bool              SAL_CALL supportsService(const OUString& ServiceName) throw();
110 };
111 
112 OTextOutputStream::OTextOutputStream()
113 {
114     mbEncodingInitialized = false;
115 }
116 
117 OTextOutputStream::~OTextOutputStream()
118 {
119     if( mbEncodingInitialized )
120     {
121         rtl_destroyUnicodeToTextContext( mConvUnicode2Text, mContextUnicode2Text );
122         rtl_destroyUnicodeToTextConverter( mConvUnicode2Text );
123     }
124 }
125 
126 Sequence<sal_Int8> OTextOutputStream::implConvert( const OUString& rSource )
127 {
128     const sal_Unicode *puSource = rSource.getStr();
129     sal_Int32 nSourceSize = rSource.getLength();
130 
131     sal_Size nTargetCount = 0;
132     sal_Size nSourceCount = 0;
133 
134     sal_uInt32 uiInfo;
135     sal_Size nSrcCvtChars;
136 
137     // take nSourceSize * 3 as preference
138     // this is an upper boundary for converting to utf8,
139     // which most often used as the target.
140     sal_Int32 nSeqSize =  nSourceSize * 3;
141 
142     Sequence<sal_Int8> seqText( nSeqSize );
143     sal_Char *pTarget = (sal_Char *) seqText.getArray();
144     while( sal_True )
145     {
146         nTargetCount += rtl_convertUnicodeToText(
147                                     mConvUnicode2Text,
148                                     mContextUnicode2Text,
149                                     &( puSource[nSourceCount] ),
150                                     nSourceSize - nSourceCount ,
151                                     &( pTarget[nTargetCount] ),
152                                     nSeqSize - nTargetCount,
153                                     RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT |
154                                     RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT ,
155                                     &uiInfo,
156                                     &nSrcCvtChars);
157         nSourceCount += nSrcCvtChars;
158 
159         if( uiInfo & RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL )
160         {
161             nSeqSize *= 2;
162             seqText.realloc( nSeqSize );  // double array size
163             pTarget = (sal_Char*) seqText.getArray();
164             continue;
165         }
166         break;
167     }
168 
169     // reduce the size of the buffer (fast, no copy necessary)
170     seqText.realloc( nTargetCount );
171     return seqText;
172 }
173 
174 
175 //===========================================================================
176 // XTextOutputStream
177 
178 void OTextOutputStream::writeString( const OUString& aString )
179     throw(IOException, RuntimeException)
180 {
181     if( !mbEncodingInitialized )
182     {
183         OUString aUtf8Str( RTL_CONSTASCII_USTRINGPARAM("utf8") );
184         setEncoding( aUtf8Str );
185     }
186     if( !mbEncodingInitialized )
187         return;
188 
189     Sequence<sal_Int8> aByteSeq = implConvert( aString );
190     mxStream->writeBytes( aByteSeq );
191 }
192 
193 void OTextOutputStream::setEncoding( const OUString& Encoding )
194     throw(RuntimeException)
195 {
196     OString aOEncodingStr = OUStringToOString( Encoding, RTL_TEXTENCODING_ASCII_US );
197     rtl_TextEncoding encoding = rtl_getTextEncodingFromMimeCharset( aOEncodingStr.getStr() );
198     if( RTL_TEXTENCODING_DONTKNOW == encoding )
199         return;
200 
201     mbEncodingInitialized = true;
202     mConvUnicode2Text   = rtl_createUnicodeToTextConverter( encoding );
203     mContextUnicode2Text = rtl_createUnicodeToTextContext( mConvUnicode2Text );
204     mEncoding = Encoding;
205 }
206 
207 //===========================================================================
208 // XOutputStream
209 void OTextOutputStream::writeBytes( const Sequence< sal_Int8 >& aData )
210     throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
211 {
212     mxStream->writeBytes( aData );
213 }
214 
215 void OTextOutputStream::flush(  )
216     throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
217 {
218     mxStream->flush();
219 }
220 
221 void OTextOutputStream::closeOutput(  )
222     throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
223 {
224     mxStream->closeOutput();
225 }
226 
227 
228 //===========================================================================
229 // XActiveDataSource
230 
231 void OTextOutputStream::setOutputStream( const Reference< XOutputStream >& aStream )
232     throw(RuntimeException)
233 {
234     mxStream = aStream;
235 }
236 
237 Reference< XOutputStream > OTextOutputStream::getOutputStream()
238     throw(RuntimeException)
239 {
240     return mxStream;
241 }
242 
243 
244 Reference< XInterface > SAL_CALL TextOutputStream_CreateInstance( const Reference< XComponentContext > &)
245 {
246     return Reference < XInterface >( ( OWeakObject * ) new OTextOutputStream() );
247 }
248 
249 OUString TextOutputStream_getImplementationName() SAL_THROW(  () )
250 {
251     return OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) );
252 }
253 
254 
255 Sequence< OUString > TextOutputStream_getSupportedServiceNames()
256 {
257     static Sequence < OUString > *pNames = 0;
258     if( ! pNames )
259     {
260         MutexGuard guard( Mutex::getGlobalMutex() );
261         if( !pNames )
262         {
263             static Sequence< OUString > seqNames(1);
264             seqNames.getArray()[0] = OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICE_NAME ) );
265             pNames = &seqNames;
266         }
267     }
268     return *pNames;
269 }
270 
271 OUString OTextOutputStream::getImplementationName() throw()
272 {
273     return TextOutputStream_getImplementationName();
274 }
275 
276 sal_Bool OTextOutputStream::supportsService(const OUString& ServiceName) throw()
277 {
278     Sequence< OUString > aSNL = getSupportedServiceNames();
279     const OUString * pArray = aSNL.getConstArray();
280 
281     for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
282         if( pArray[i] == ServiceName )
283             return sal_True;
284 
285     return sal_False;
286 }
287 
288 Sequence< OUString > OTextOutputStream::getSupportedServiceNames(void) throw()
289 {
290     return TextOutputStream_getSupportedServiceNames();
291 }
292 
293 
294 }
295 
296 using namespace io_TextOutputStream;
297 
298 static struct ImplementationEntry g_entries[] =
299 {
300     {
301         TextOutputStream_CreateInstance, TextOutputStream_getImplementationName ,
302         TextOutputStream_getSupportedServiceNames, createSingleComponentFactory ,
303         &g_moduleCount.modCnt , 0
304     },
305     { 0, 0, 0, 0, 0, 0 }
306 };
307 
308 extern "C"
309 {
310 sal_Bool SAL_CALL component_canUnload( TimeValue *pTime )
311 {
312     return g_moduleCount.canUnload( &g_moduleCount , pTime );
313 }
314 
315 //==================================================================================================
316 void SAL_CALL component_getImplementationEnvironment(
317     const sal_Char ** ppEnvTypeName, uno_Environment ** )
318 {
319     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
320 }
321 //==================================================================================================
322 void * SAL_CALL component_getFactory(
323     const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
324 {
325     return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
326 }
327 }
328 
329 
330