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