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