13716f815SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 33716f815SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 43716f815SAndrew Rist * or more contributor license agreements. See the NOTICE file 53716f815SAndrew Rist * distributed with this work for additional information 63716f815SAndrew Rist * regarding copyright ownership. The ASF licenses this file 73716f815SAndrew Rist * to you under the Apache License, Version 2.0 (the 83716f815SAndrew Rist * "License"); you may not use this file except in compliance 93716f815SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 113716f815SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 133716f815SAndrew Rist * Unless required by applicable law or agreed to in writing, 143716f815SAndrew Rist * software distributed under the License is distributed on an 153716f815SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 163716f815SAndrew Rist * KIND, either express or implied. See the License for the 173716f815SAndrew Rist * specific language governing permissions and limitations 183716f815SAndrew Rist * under the License. 19cdf0e10cSrcweir * 203716f815SAndrew Rist *************************************************************/ 213716f815SAndrew Rist 223716f815SAndrew 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 ); 77*fa267792SJürgen Schmidt void checkOutputStream() throw(IOException); 78cdf0e10cSrcweir 79cdf0e10cSrcweir public: 80cdf0e10cSrcweir OTextOutputStream(); 81cdf0e10cSrcweir ~OTextOutputStream(); 82cdf0e10cSrcweir 83cdf0e10cSrcweir // Methods XTextOutputStream 84cdf0e10cSrcweir virtual void SAL_CALL writeString( const OUString& aString ) 85cdf0e10cSrcweir throw(IOException, RuntimeException); 86cdf0e10cSrcweir virtual void SAL_CALL setEncoding( const OUString& Encoding ) 87cdf0e10cSrcweir throw(RuntimeException); 88cdf0e10cSrcweir 89cdf0e10cSrcweir // Methods XOutputStream 90cdf0e10cSrcweir virtual void SAL_CALL writeBytes( const Sequence< sal_Int8 >& aData ) 91cdf0e10cSrcweir throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException); 92cdf0e10cSrcweir virtual void SAL_CALL flush( ) 93cdf0e10cSrcweir throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException); 94cdf0e10cSrcweir virtual void SAL_CALL closeOutput( ) 95cdf0e10cSrcweir throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException); 96cdf0e10cSrcweir 97cdf0e10cSrcweir // Methods XActiveDataSource 98cdf0e10cSrcweir virtual void SAL_CALL setOutputStream( const Reference< XOutputStream >& aStream ) 99cdf0e10cSrcweir throw(RuntimeException); 100cdf0e10cSrcweir virtual Reference< XOutputStream > SAL_CALL getOutputStream( ) 101cdf0e10cSrcweir throw(RuntimeException); 102cdf0e10cSrcweir 103cdf0e10cSrcweir // Methods XServiceInfo 104cdf0e10cSrcweir virtual OUString SAL_CALL getImplementationName() throw(); 105cdf0e10cSrcweir virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(void) throw(); 106cdf0e10cSrcweir virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw(); 107cdf0e10cSrcweir }; 108cdf0e10cSrcweir 109cdf0e10cSrcweir OTextOutputStream::OTextOutputStream() 110cdf0e10cSrcweir { 111cdf0e10cSrcweir mbEncodingInitialized = false; 112cdf0e10cSrcweir } 113cdf0e10cSrcweir 114cdf0e10cSrcweir OTextOutputStream::~OTextOutputStream() 115cdf0e10cSrcweir { 116cdf0e10cSrcweir if( mbEncodingInitialized ) 117cdf0e10cSrcweir { 118cdf0e10cSrcweir rtl_destroyUnicodeToTextContext( mConvUnicode2Text, mContextUnicode2Text ); 119cdf0e10cSrcweir rtl_destroyUnicodeToTextConverter( mConvUnicode2Text ); 120cdf0e10cSrcweir } 121cdf0e10cSrcweir } 122cdf0e10cSrcweir 123cdf0e10cSrcweir Sequence<sal_Int8> OTextOutputStream::implConvert( const OUString& rSource ) 124cdf0e10cSrcweir { 125cdf0e10cSrcweir const sal_Unicode *puSource = rSource.getStr(); 126cdf0e10cSrcweir sal_Int32 nSourceSize = rSource.getLength(); 127cdf0e10cSrcweir 128cdf0e10cSrcweir sal_Size nTargetCount = 0; 129cdf0e10cSrcweir sal_Size nSourceCount = 0; 130cdf0e10cSrcweir 131cdf0e10cSrcweir sal_uInt32 uiInfo; 132cdf0e10cSrcweir sal_Size nSrcCvtChars; 133cdf0e10cSrcweir 134cdf0e10cSrcweir // take nSourceSize * 3 as preference 135cdf0e10cSrcweir // this is an upper boundary for converting to utf8, 136cdf0e10cSrcweir // which most often used as the target. 137cdf0e10cSrcweir sal_Int32 nSeqSize = nSourceSize * 3; 138cdf0e10cSrcweir 139cdf0e10cSrcweir Sequence<sal_Int8> seqText( nSeqSize ); 140cdf0e10cSrcweir sal_Char *pTarget = (sal_Char *) seqText.getArray(); 141cdf0e10cSrcweir while( sal_True ) 142cdf0e10cSrcweir { 143cdf0e10cSrcweir nTargetCount += rtl_convertUnicodeToText( 144cdf0e10cSrcweir mConvUnicode2Text, 145cdf0e10cSrcweir mContextUnicode2Text, 146cdf0e10cSrcweir &( puSource[nSourceCount] ), 147cdf0e10cSrcweir nSourceSize - nSourceCount , 148cdf0e10cSrcweir &( pTarget[nTargetCount] ), 149cdf0e10cSrcweir nSeqSize - nTargetCount, 150cdf0e10cSrcweir RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT | 151cdf0e10cSrcweir RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT , 152cdf0e10cSrcweir &uiInfo, 153cdf0e10cSrcweir &nSrcCvtChars); 154cdf0e10cSrcweir nSourceCount += nSrcCvtChars; 155cdf0e10cSrcweir 156cdf0e10cSrcweir if( uiInfo & RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL ) 157cdf0e10cSrcweir { 158cdf0e10cSrcweir nSeqSize *= 2; 159cdf0e10cSrcweir seqText.realloc( nSeqSize ); // double array size 160cdf0e10cSrcweir pTarget = (sal_Char*) seqText.getArray(); 161cdf0e10cSrcweir continue; 162cdf0e10cSrcweir } 163cdf0e10cSrcweir break; 164cdf0e10cSrcweir } 165cdf0e10cSrcweir 166cdf0e10cSrcweir // reduce the size of the buffer (fast, no copy necessary) 167cdf0e10cSrcweir seqText.realloc( nTargetCount ); 168cdf0e10cSrcweir return seqText; 169cdf0e10cSrcweir } 170cdf0e10cSrcweir 171cdf0e10cSrcweir 172cdf0e10cSrcweir //=========================================================================== 173cdf0e10cSrcweir // XTextOutputStream 174cdf0e10cSrcweir 175cdf0e10cSrcweir void OTextOutputStream::writeString( const OUString& aString ) 176cdf0e10cSrcweir throw(IOException, RuntimeException) 177cdf0e10cSrcweir { 178*fa267792SJürgen Schmidt checkOutputStream(); 179cdf0e10cSrcweir if( !mbEncodingInitialized ) 180cdf0e10cSrcweir { 181cdf0e10cSrcweir OUString aUtf8Str( RTL_CONSTASCII_USTRINGPARAM("utf8") ); 182cdf0e10cSrcweir setEncoding( aUtf8Str ); 183cdf0e10cSrcweir } 184cdf0e10cSrcweir if( !mbEncodingInitialized ) 185cdf0e10cSrcweir return; 186cdf0e10cSrcweir 187cdf0e10cSrcweir Sequence<sal_Int8> aByteSeq = implConvert( aString ); 188cdf0e10cSrcweir mxStream->writeBytes( aByteSeq ); 189cdf0e10cSrcweir } 190cdf0e10cSrcweir 191cdf0e10cSrcweir void OTextOutputStream::setEncoding( const OUString& Encoding ) 192cdf0e10cSrcweir throw(RuntimeException) 193cdf0e10cSrcweir { 194cdf0e10cSrcweir OString aOEncodingStr = OUStringToOString( Encoding, RTL_TEXTENCODING_ASCII_US ); 195cdf0e10cSrcweir rtl_TextEncoding encoding = rtl_getTextEncodingFromMimeCharset( aOEncodingStr.getStr() ); 196cdf0e10cSrcweir if( RTL_TEXTENCODING_DONTKNOW == encoding ) 197cdf0e10cSrcweir return; 198cdf0e10cSrcweir 199cdf0e10cSrcweir mbEncodingInitialized = true; 200cdf0e10cSrcweir mConvUnicode2Text = rtl_createUnicodeToTextConverter( encoding ); 201cdf0e10cSrcweir mContextUnicode2Text = rtl_createUnicodeToTextContext( mConvUnicode2Text ); 202cdf0e10cSrcweir mEncoding = Encoding; 203cdf0e10cSrcweir } 204cdf0e10cSrcweir 205cdf0e10cSrcweir //=========================================================================== 206cdf0e10cSrcweir // XOutputStream 207cdf0e10cSrcweir void OTextOutputStream::writeBytes( const Sequence< sal_Int8 >& aData ) 208cdf0e10cSrcweir throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException) 209cdf0e10cSrcweir { 210*fa267792SJürgen Schmidt checkOutputStream(); 211cdf0e10cSrcweir mxStream->writeBytes( aData ); 212cdf0e10cSrcweir } 213cdf0e10cSrcweir 214cdf0e10cSrcweir void OTextOutputStream::flush( ) 215cdf0e10cSrcweir throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException) 216cdf0e10cSrcweir { 217*fa267792SJürgen Schmidt checkOutputStream(); 218cdf0e10cSrcweir mxStream->flush(); 219cdf0e10cSrcweir } 220cdf0e10cSrcweir 221cdf0e10cSrcweir void OTextOutputStream::closeOutput( ) 222cdf0e10cSrcweir throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException) 223cdf0e10cSrcweir { 224*fa267792SJürgen Schmidt checkOutputStream(); 225cdf0e10cSrcweir mxStream->closeOutput(); 226cdf0e10cSrcweir } 227cdf0e10cSrcweir 228cdf0e10cSrcweir 229*fa267792SJürgen Schmidt void OTextOutputStream::checkOutputStream() 230*fa267792SJürgen Schmidt throw(IOException) 231*fa267792SJürgen Schmidt { 232*fa267792SJürgen Schmidt if (! mxStream.is() ) 233*fa267792SJürgen Schmidt throw IOException( 234*fa267792SJürgen Schmidt OUString(RTL_CONSTASCII_USTRINGPARAM("output stream is not initialized, you have to use setOutputStream first")), 235*fa267792SJürgen Schmidt Reference<XInterface>()); 236*fa267792SJürgen Schmidt } 237*fa267792SJürgen Schmidt 238*fa267792SJürgen Schmidt 239cdf0e10cSrcweir //=========================================================================== 240cdf0e10cSrcweir // XActiveDataSource 241cdf0e10cSrcweir 242cdf0e10cSrcweir void OTextOutputStream::setOutputStream( const Reference< XOutputStream >& aStream ) 243cdf0e10cSrcweir throw(RuntimeException) 244cdf0e10cSrcweir { 245cdf0e10cSrcweir mxStream = aStream; 246cdf0e10cSrcweir } 247cdf0e10cSrcweir 248cdf0e10cSrcweir Reference< XOutputStream > OTextOutputStream::getOutputStream() 249cdf0e10cSrcweir throw(RuntimeException) 250cdf0e10cSrcweir { 251cdf0e10cSrcweir return mxStream; 252cdf0e10cSrcweir } 253cdf0e10cSrcweir 254cdf0e10cSrcweir 255cdf0e10cSrcweir Reference< XInterface > SAL_CALL TextOutputStream_CreateInstance( const Reference< XComponentContext > &) 256cdf0e10cSrcweir { 257cdf0e10cSrcweir return Reference < XInterface >( ( OWeakObject * ) new OTextOutputStream() ); 258cdf0e10cSrcweir } 259cdf0e10cSrcweir 260cdf0e10cSrcweir OUString TextOutputStream_getImplementationName() SAL_THROW( () ) 261cdf0e10cSrcweir { 262cdf0e10cSrcweir return OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) ); 263cdf0e10cSrcweir } 264cdf0e10cSrcweir 265cdf0e10cSrcweir 266cdf0e10cSrcweir Sequence< OUString > TextOutputStream_getSupportedServiceNames() 267cdf0e10cSrcweir { 268cdf0e10cSrcweir static Sequence < OUString > *pNames = 0; 269cdf0e10cSrcweir if( ! pNames ) 270cdf0e10cSrcweir { 271cdf0e10cSrcweir MutexGuard guard( Mutex::getGlobalMutex() ); 272cdf0e10cSrcweir if( !pNames ) 273cdf0e10cSrcweir { 274cdf0e10cSrcweir static Sequence< OUString > seqNames(1); 275cdf0e10cSrcweir seqNames.getArray()[0] = OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICE_NAME ) ); 276cdf0e10cSrcweir pNames = &seqNames; 277cdf0e10cSrcweir } 278cdf0e10cSrcweir } 279cdf0e10cSrcweir return *pNames; 280cdf0e10cSrcweir } 281cdf0e10cSrcweir 282cdf0e10cSrcweir OUString OTextOutputStream::getImplementationName() throw() 283cdf0e10cSrcweir { 284cdf0e10cSrcweir return TextOutputStream_getImplementationName(); 285cdf0e10cSrcweir } 286cdf0e10cSrcweir 287cdf0e10cSrcweir sal_Bool OTextOutputStream::supportsService(const OUString& ServiceName) throw() 288cdf0e10cSrcweir { 289cdf0e10cSrcweir Sequence< OUString > aSNL = getSupportedServiceNames(); 290cdf0e10cSrcweir const OUString * pArray = aSNL.getConstArray(); 291cdf0e10cSrcweir 292cdf0e10cSrcweir for( sal_Int32 i = 0; i < aSNL.getLength(); i++ ) 293cdf0e10cSrcweir if( pArray[i] == ServiceName ) 294cdf0e10cSrcweir return sal_True; 295cdf0e10cSrcweir 296cdf0e10cSrcweir return sal_False; 297cdf0e10cSrcweir } 298cdf0e10cSrcweir 299cdf0e10cSrcweir Sequence< OUString > OTextOutputStream::getSupportedServiceNames(void) throw() 300cdf0e10cSrcweir { 301cdf0e10cSrcweir return TextOutputStream_getSupportedServiceNames(); 302cdf0e10cSrcweir } 303cdf0e10cSrcweir 304cdf0e10cSrcweir 305cdf0e10cSrcweir } 306cdf0e10cSrcweir 307cdf0e10cSrcweir using namespace io_TextOutputStream; 308cdf0e10cSrcweir 309cdf0e10cSrcweir static struct ImplementationEntry g_entries[] = 310cdf0e10cSrcweir { 311cdf0e10cSrcweir { 312cdf0e10cSrcweir TextOutputStream_CreateInstance, TextOutputStream_getImplementationName , 313cdf0e10cSrcweir TextOutputStream_getSupportedServiceNames, createSingleComponentFactory , 314cdf0e10cSrcweir &g_moduleCount.modCnt , 0 315cdf0e10cSrcweir }, 316cdf0e10cSrcweir { 0, 0, 0, 0, 0, 0 } 317cdf0e10cSrcweir }; 318cdf0e10cSrcweir 319cdf0e10cSrcweir extern "C" 320cdf0e10cSrcweir { 321cdf0e10cSrcweir sal_Bool SAL_CALL component_canUnload( TimeValue *pTime ) 322cdf0e10cSrcweir { 323cdf0e10cSrcweir return g_moduleCount.canUnload( &g_moduleCount , pTime ); 324cdf0e10cSrcweir } 325cdf0e10cSrcweir 326cdf0e10cSrcweir //================================================================================================== 327cdf0e10cSrcweir void SAL_CALL component_getImplementationEnvironment( 328cdf0e10cSrcweir const sal_Char ** ppEnvTypeName, uno_Environment ** ) 329cdf0e10cSrcweir { 330cdf0e10cSrcweir *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME; 331cdf0e10cSrcweir } 332cdf0e10cSrcweir //================================================================================================== 333cdf0e10cSrcweir void * SAL_CALL component_getFactory( 334cdf0e10cSrcweir const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey ) 335cdf0e10cSrcweir { 336cdf0e10cSrcweir return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries ); 337cdf0e10cSrcweir } 338cdf0e10cSrcweir } 339cdf0e10cSrcweir 340cdf0e10cSrcweir 341