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