xref: /trunk/main/io/source/TextOutputStream/TextOutputStream.cxx (revision f90c092ab5c040ba698a3b784978f9837c57fdaf)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_io.hxx"
26 
27 
28 #include <osl/mutex.hxx>
29 #include <osl/diagnose.h>
30 
31 #include <uno/mapping.hxx>
32 
33 #include <cppuhelper/factory.hxx>
34 #include <cppuhelper/implbase3.hxx>
35 #include <cppuhelper/implementationentry.hxx>
36 
37 #include <rtl/textenc.h>
38 #include <rtl/tencinfo.h>
39 #include <rtl/unload.h>
40 
41 #include "io/dllapi.h"
42 
43 #include <com/sun/star/io/XTextOutputStream.hpp>
44 #include <com/sun/star/io/XActiveDataSource.hpp>
45 #include <com/sun/star/lang/XServiceInfo.hpp>
46 
47 
48 #define IMPLEMENTATION_NAME "com.sun.star.comp.io.TextOutputStream"
49 #define SERVICE_NAME "com.sun.star.io.TextOutputStream"
50 
51 using namespace ::osl;
52 using namespace ::rtl;
53 using namespace ::cppu;
54 using namespace ::com::sun::star::uno;
55 using namespace ::com::sun::star::lang;
56 using namespace ::com::sun::star::io;
57 using namespace ::com::sun::star::registry;
58 
59 namespace io_TextOutputStream
60 {
61     rtl_StandardModuleCount g_moduleCount = MODULE_COUNT_INIT;
62 //===========================================================================
63 // Implementation XTextOutputStream
64 
65 typedef WeakImplHelper3< XTextOutputStream, XActiveDataSource, XServiceInfo > TextOutputStreamHelper;
66 class OCommandEnvironment;
67 
68 class OTextOutputStream : public TextOutputStreamHelper
69 {
70     Reference< XOutputStream > mxStream;
71 
72     // Encoding
73     OUString mEncoding;
74     sal_Bool mbEncodingInitialized;
75     rtl_UnicodeToTextConverter  mConvUnicode2Text;
76     rtl_UnicodeToTextContext    mContextUnicode2Text;
77 
78     Sequence<sal_Int8> implConvert( const OUString& rSource );
79     void checkOutputStream() throw(IOException);
80 
81 public:
82     OTextOutputStream();
83     ~OTextOutputStream();
84 
85     // Methods XTextOutputStream
86     virtual void SAL_CALL writeString( const OUString& aString )
87         throw(IOException, RuntimeException);
88     virtual void SAL_CALL setEncoding( const OUString& Encoding )
89         throw(RuntimeException);
90 
91     // Methods XOutputStream
92     virtual void SAL_CALL writeBytes( const Sequence< sal_Int8 >& aData )
93         throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
94     virtual void SAL_CALL flush(  )
95         throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
96     virtual void SAL_CALL closeOutput(  )
97         throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
98 
99     // Methods XActiveDataSource
100     virtual void SAL_CALL setOutputStream( const Reference< XOutputStream >& aStream )
101         throw(RuntimeException);
102     virtual Reference< XOutputStream > SAL_CALL getOutputStream(  )
103         throw(RuntimeException);
104 
105     // Methods XServiceInfo
106         virtual OUString              SAL_CALL getImplementationName() throw();
107         virtual Sequence< OUString >  SAL_CALL getSupportedServiceNames(void) throw();
108         virtual sal_Bool              SAL_CALL supportsService(const OUString& ServiceName) throw();
109 };
110 
111 OTextOutputStream::OTextOutputStream()
112 {
113     mbEncodingInitialized = false;
114 }
115 
116 OTextOutputStream::~OTextOutputStream()
117 {
118     if( mbEncodingInitialized )
119     {
120         rtl_destroyUnicodeToTextContext( mConvUnicode2Text, mContextUnicode2Text );
121         rtl_destroyUnicodeToTextConverter( mConvUnicode2Text );
122     }
123 }
124 
125 Sequence<sal_Int8> OTextOutputStream::implConvert( const OUString& rSource )
126 {
127     const sal_Unicode *puSource = rSource.getStr();
128     sal_Int32 nSourceSize = rSource.getLength();
129 
130     sal_Size nTargetCount = 0;
131     sal_Size nSourceCount = 0;
132 
133     sal_uInt32 uiInfo;
134     sal_Size nSrcCvtChars;
135 
136     // take nSourceSize * 3 as preference
137     // this is an upper boundary for converting to utf8,
138     // which most often used as the target.
139     sal_Int32 nSeqSize =  nSourceSize * 3;
140 
141     Sequence<sal_Int8> seqText( nSeqSize );
142     sal_Char *pTarget = (sal_Char *) seqText.getArray();
143     while( sal_True )
144     {
145         nTargetCount += rtl_convertUnicodeToText(
146                                     mConvUnicode2Text,
147                                     mContextUnicode2Text,
148                                     &( puSource[nSourceCount] ),
149                                     nSourceSize - nSourceCount ,
150                                     &( pTarget[nTargetCount] ),
151                                     nSeqSize - nTargetCount,
152                                     RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT |
153                                     RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT ,
154                                     &uiInfo,
155                                     &nSrcCvtChars);
156         nSourceCount += nSrcCvtChars;
157 
158         if( uiInfo & RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL )
159         {
160             nSeqSize *= 2;
161             seqText.realloc( nSeqSize );  // double array size
162             pTarget = (sal_Char*) seqText.getArray();
163             continue;
164         }
165         break;
166     }
167 
168     // reduce the size of the buffer (fast, no copy necessary)
169     seqText.realloc( nTargetCount );
170     return seqText;
171 }
172 
173 
174 //===========================================================================
175 // XTextOutputStream
176 
177 void OTextOutputStream::writeString( const OUString& aString )
178     throw(IOException, RuntimeException)
179 {
180     checkOutputStream();
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     checkOutputStream();
213     mxStream->writeBytes( aData );
214 }
215 
216 void OTextOutputStream::flush(  )
217     throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
218 {
219     checkOutputStream();
220     mxStream->flush();
221 }
222 
223 void OTextOutputStream::closeOutput(  )
224     throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
225 {
226     checkOutputStream();
227     mxStream->closeOutput();
228 }
229 
230 
231 void OTextOutputStream::checkOutputStream()
232     throw(IOException)
233 {
234     if (! mxStream.is() )
235         throw IOException(
236             OUString(RTL_CONSTASCII_USTRINGPARAM("output stream is not initialized, you have to use setOutputStream first")),
237             Reference<XInterface>());
238 }
239 
240 
241 //===========================================================================
242 // XActiveDataSource
243 
244 void OTextOutputStream::setOutputStream( const Reference< XOutputStream >& aStream )
245     throw(RuntimeException)
246 {
247     mxStream = aStream;
248 }
249 
250 Reference< XOutputStream > OTextOutputStream::getOutputStream()
251     throw(RuntimeException)
252 {
253     return mxStream;
254 }
255 
256 
257 Reference< XInterface > SAL_CALL TextOutputStream_CreateInstance( const Reference< XComponentContext > &)
258 {
259     return Reference < XInterface >( ( OWeakObject * ) new OTextOutputStream() );
260 }
261 
262 OUString TextOutputStream_getImplementationName() SAL_THROW(  () )
263 {
264     return OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) );
265 }
266 
267 
268 Sequence< OUString > TextOutputStream_getSupportedServiceNames()
269 {
270     static Sequence < OUString > *pNames = 0;
271     if( ! pNames )
272     {
273         MutexGuard guard( Mutex::getGlobalMutex() );
274         if( !pNames )
275         {
276             static Sequence< OUString > seqNames(1);
277             seqNames.getArray()[0] = OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICE_NAME ) );
278             pNames = &seqNames;
279         }
280     }
281     return *pNames;
282 }
283 
284 OUString OTextOutputStream::getImplementationName() throw()
285 {
286     return TextOutputStream_getImplementationName();
287 }
288 
289 sal_Bool OTextOutputStream::supportsService(const OUString& ServiceName) throw()
290 {
291     Sequence< OUString > aSNL = getSupportedServiceNames();
292     const OUString * pArray = aSNL.getConstArray();
293 
294     for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
295         if( pArray[i] == ServiceName )
296             return sal_True;
297 
298     return sal_False;
299 }
300 
301 Sequence< OUString > OTextOutputStream::getSupportedServiceNames(void) throw()
302 {
303     return TextOutputStream_getSupportedServiceNames();
304 }
305 
306 
307 }
308 
309 using namespace io_TextOutputStream;
310 
311 static struct ImplementationEntry g_entries[] =
312 {
313     {
314         TextOutputStream_CreateInstance, TextOutputStream_getImplementationName ,
315         TextOutputStream_getSupportedServiceNames, createSingleComponentFactory ,
316         &g_moduleCount.modCnt , 0
317     },
318     { 0, 0, 0, 0, 0, 0 }
319 };
320 
321 extern "C"
322 {
323 IO_DLLPUBLIC sal_Bool SAL_CALL component_canUnload( TimeValue *pTime )
324 {
325     return g_moduleCount.canUnload( &g_moduleCount , pTime );
326 }
327 
328 //==================================================================================================
329 IO_DLLPUBLIC void SAL_CALL component_getImplementationEnvironment(
330     const sal_Char ** ppEnvTypeName, uno_Environment ** )
331 {
332     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
333 }
334 //==================================================================================================
335 IO_DLLPUBLIC void * SAL_CALL component_getFactory(
336     const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
337 {
338     return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
339 }
340 }
341 
342 
343