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