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 
78 public:
79 	OTextOutputStream();
80 	~OTextOutputStream();
81 
82     // Methods XTextOutputStream
83     virtual void SAL_CALL writeString( const OUString& aString )
84 		throw(IOException, RuntimeException);
85     virtual void SAL_CALL setEncoding( const OUString& Encoding )
86 		throw(RuntimeException);
87 
88     // Methods XOutputStream
89     virtual void SAL_CALL writeBytes( const Sequence< sal_Int8 >& aData )
90 		throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
91     virtual void SAL_CALL flush(  )
92 		throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
93     virtual void SAL_CALL closeOutput(  )
94 		throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
95 
96     // Methods XActiveDataSource
97     virtual void SAL_CALL setOutputStream( const Reference< XOutputStream >& aStream )
98 		throw(RuntimeException);
99     virtual Reference< XOutputStream > SAL_CALL getOutputStream(  )
100 		throw(RuntimeException);
101 
102 	// Methods XServiceInfo
103         virtual OUString              SAL_CALL getImplementationName() throw();
104         virtual Sequence< OUString >  SAL_CALL getSupportedServiceNames(void) throw();
105         virtual sal_Bool              SAL_CALL supportsService(const OUString& ServiceName) throw();
106 };
107 
108 OTextOutputStream::OTextOutputStream()
109 {
110 	mbEncodingInitialized = false;
111 }
112 
113 OTextOutputStream::~OTextOutputStream()
114 {
115 	if( mbEncodingInitialized )
116 	{
117 		rtl_destroyUnicodeToTextContext( mConvUnicode2Text, mContextUnicode2Text );
118 		rtl_destroyUnicodeToTextConverter( mConvUnicode2Text );
119 	}
120 }
121 
122 Sequence<sal_Int8> OTextOutputStream::implConvert( const OUString& rSource )
123 {
124 	const sal_Unicode *puSource = rSource.getStr();
125 	sal_Int32 nSourceSize = rSource.getLength();
126 
127 	sal_Size nTargetCount = 0;
128 	sal_Size nSourceCount = 0;
129 
130 	sal_uInt32 uiInfo;
131 	sal_Size nSrcCvtChars;
132 
133 	// take nSourceSize * 3 as preference
134 	// this is an upper boundary for converting to utf8,
135 	// which most often used as the target.
136 	sal_Int32 nSeqSize =  nSourceSize * 3;
137 
138 	Sequence<sal_Int8> seqText( nSeqSize );
139 	sal_Char *pTarget = (sal_Char *) seqText.getArray();
140 	while( sal_True )
141 	{
142 		nTargetCount += rtl_convertUnicodeToText(
143 									mConvUnicode2Text,
144 									mContextUnicode2Text,
145 									&( puSource[nSourceCount] ),
146 									nSourceSize - nSourceCount ,
147 									&( pTarget[nTargetCount] ),
148 									nSeqSize - nTargetCount,
149 									RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT |
150 									RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT ,
151 									&uiInfo,
152 									&nSrcCvtChars);
153 		nSourceCount += nSrcCvtChars;
154 
155 		if( uiInfo & RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL )
156 		{
157 			nSeqSize *= 2;
158 			seqText.realloc( nSeqSize );  // double array size
159 			pTarget = (sal_Char*) seqText.getArray();
160 			continue;
161 		}
162 		break;
163 	}
164 
165 	// reduce the size of the buffer (fast, no copy necessary)
166 	seqText.realloc( nTargetCount );
167 	return seqText;
168 }
169 
170 
171 //===========================================================================
172 // XTextOutputStream
173 
174 void OTextOutputStream::writeString( const OUString& aString )
175 	throw(IOException, RuntimeException)
176 {
177 	if( !mbEncodingInitialized )
178 	{
179 		OUString aUtf8Str( RTL_CONSTASCII_USTRINGPARAM("utf8") );
180 		setEncoding( aUtf8Str );
181 	}
182 	if( !mbEncodingInitialized )
183 		return;
184 
185 	Sequence<sal_Int8> aByteSeq = implConvert( aString );
186 	mxStream->writeBytes( aByteSeq );
187 }
188 
189 void OTextOutputStream::setEncoding( const OUString& Encoding )
190 	throw(RuntimeException)
191 {
192 	OString aOEncodingStr = OUStringToOString( Encoding, RTL_TEXTENCODING_ASCII_US );
193 	rtl_TextEncoding encoding = rtl_getTextEncodingFromMimeCharset( aOEncodingStr.getStr() );
194 	if( RTL_TEXTENCODING_DONTKNOW == encoding )
195 		return;
196 
197 	mbEncodingInitialized = true;
198 	mConvUnicode2Text 	= rtl_createUnicodeToTextConverter( encoding );
199 	mContextUnicode2Text = rtl_createUnicodeToTextContext( mConvUnicode2Text );
200 	mEncoding = Encoding;
201 }
202 
203 //===========================================================================
204 // XOutputStream
205 void OTextOutputStream::writeBytes( const Sequence< sal_Int8 >& aData )
206 	throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
207 {
208 	mxStream->writeBytes( aData );
209 }
210 
211 void OTextOutputStream::flush(  )
212 	throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
213 {
214 	mxStream->flush();
215 }
216 
217 void OTextOutputStream::closeOutput(  )
218 	throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
219 {
220 	mxStream->closeOutput();
221 }
222 
223 
224 //===========================================================================
225 // XActiveDataSource
226 
227 void OTextOutputStream::setOutputStream( const Reference< XOutputStream >& aStream )
228 	throw(RuntimeException)
229 {
230 	mxStream = aStream;
231 }
232 
233 Reference< XOutputStream > OTextOutputStream::getOutputStream()
234 	throw(RuntimeException)
235 {
236 	return mxStream;
237 }
238 
239 
240 Reference< XInterface > SAL_CALL TextOutputStream_CreateInstance( const Reference< XComponentContext > &)
241 {
242 	return Reference < XInterface >( ( OWeakObject * ) new OTextOutputStream() );
243 }
244 
245 OUString TextOutputStream_getImplementationName() SAL_THROW(  () )
246 {
247 	return OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) );
248 }
249 
250 
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 
267 OUString OTextOutputStream::getImplementationName() throw()
268 {
269 	return TextOutputStream_getImplementationName();
270 }
271 
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 
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 {
306 sal_Bool SAL_CALL component_canUnload( TimeValue *pTime )
307 {
308 	return g_moduleCount.canUnload( &g_moduleCount , pTime );
309 }
310 
311 //==================================================================================================
312 void SAL_CALL component_getImplementationEnvironment(
313 	const sal_Char ** ppEnvTypeName, uno_Environment ** )
314 {
315 	*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
316 }
317 //==================================================================================================
318 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 
325 
326