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 #include "precompiled_dbaccess.hxx"
25 
26 #include "storagetextstream.hxx"
27 
28 /** === begin UNO includes === **/
29 #include <com/sun/star/io/XTextOutputStream.hpp>
30 #include <com/sun/star/io/XActiveDataSource.hpp>
31 /** === end UNO includes === **/
32 
33 #include <comphelper/componentcontext.hxx>
34 #include <tools/diagnose_ex.h>
35 
36 //......................................................................................................................
37 namespace dbaccess
38 {
39 //......................................................................................................................
40 
41 	/** === begin UNO using === **/
42 	using ::com::sun::star::uno::Reference;
43 	using ::com::sun::star::uno::XInterface;
44 	using ::com::sun::star::uno::UNO_QUERY;
45 	using ::com::sun::star::uno::UNO_QUERY_THROW;
46 	using ::com::sun::star::uno::UNO_SET_THROW;
47 	using ::com::sun::star::uno::Exception;
48 	using ::com::sun::star::uno::RuntimeException;
49 	using ::com::sun::star::uno::Any;
50 	using ::com::sun::star::uno::makeAny;
51 	using ::com::sun::star::uno::Sequence;
52 	using ::com::sun::star::uno::Type;
53     using ::com::sun::star::embed::XStorage;
54     using ::com::sun::star::io::XTextOutputStream;
55     using ::com::sun::star::io::XActiveDataSource;
56 	/** === end UNO using === **/
57 
58 	//==================================================================================================================
59 	//= StorageTextOutputStream_Data
60 	//==================================================================================================================
61     struct StorageTextOutputStream_Data
62     {
63         Reference< XTextOutputStream >  xTextOutput;
64     };
65 
66 	//==================================================================================================================
67 	//= helper
68 	//==================================================================================================================
69     namespace
70     {
71 	    //--------------------------------------------------------------------------------------------------------------
lcl_getTextStreamEncodingName()72         static const ::rtl::OUString& lcl_getTextStreamEncodingName()
73         {
74             static const ::rtl::OUString s_sMapStreamEncodingName( RTL_CONSTASCII_USTRINGPARAM( "UTF-8" ) );
75             return s_sMapStreamEncodingName;
76         }
77 
78         //--------------------------------------------------------------------------------------------------------------
lcl_getLineFeed()79         static const ::rtl::OUString& lcl_getLineFeed()
80         {
81             static const ::rtl::OUString s_sLineFeed( sal_Unicode( '\n' ) );
82             return s_sLineFeed;
83         }
84     }
85 
86 	//==================================================================================================================
87 	//= StorageTextOutputStream
88 	//==================================================================================================================
89 	//------------------------------------------------------------------------------------------------------------------
StorageTextOutputStream(const::comphelper::ComponentContext & i_rContext,const Reference<XStorage> & i_rParentStorage,const::rtl::OUString & i_rStreamName)90     StorageTextOutputStream::StorageTextOutputStream(   const ::comphelper::ComponentContext& i_rContext,
91                                                         const Reference< XStorage >& i_rParentStorage,
92                                                         const ::rtl::OUString& i_rStreamName
93                                                     )
94         :StorageOutputStream( i_rContext, i_rParentStorage, i_rStreamName )
95         ,m_pData( new StorageTextOutputStream_Data )
96     {
97         m_pData->xTextOutput.set( i_rContext.createComponent( "com.sun.star.io.TextOutputStream" ), UNO_QUERY_THROW );
98         m_pData->xTextOutput->setEncoding( lcl_getTextStreamEncodingName() );
99 
100         Reference< XActiveDataSource > xDataSource( m_pData->xTextOutput, UNO_QUERY_THROW );
101         xDataSource->setOutputStream( getOutputStream() );
102     }
103 
104     //------------------------------------------------------------------------------------------------------------------
~StorageTextOutputStream()105     StorageTextOutputStream::~StorageTextOutputStream()
106     {
107     }
108 
109     //------------------------------------------------------------------------------------------------------------------
writeLine(const::rtl::OUString & i_rLine)110     void StorageTextOutputStream::writeLine( const ::rtl::OUString& i_rLine )
111     {
112         ENSURE_OR_RETURN_VOID( m_pData->xTextOutput.is(), "no text output" );
113 
114         m_pData->xTextOutput->writeString( i_rLine );
115         m_pData->xTextOutput->writeString( lcl_getLineFeed() );
116     }
117 
118     //------------------------------------------------------------------------------------------------------------------
writeLine()119     void StorageTextOutputStream::writeLine()
120     {
121         ENSURE_OR_RETURN_VOID( m_pData->xTextOutput.is(), "no text output" );
122 
123         m_pData->xTextOutput->writeString( lcl_getLineFeed() );
124     }
125 
126 //......................................................................................................................
127 } // namespace dbaccess
128 //......................................................................................................................
129