xref: /AOO41X/main/xmlsecurity/tools/standalone/mscsfit/helper.hxx (revision ec61c6ed669d81c7df08233ccc38d0040f129ece)
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 #include <stdio.h>
23 #include <rtl/ustring.hxx>
24 
25 #include <cppuhelper/bootstrap.hxx>
26 #include <cppuhelper/implbase1.hxx>
27 #include <cppuhelper/servicefactory.hxx>
28 #include <ucbhelper/contentbroker.hxx>
29 #include <ucbhelper/configurationkeys.hxx>
30 
31 #include <com/sun/star/lang/XComponent.hpp>
32 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
33 #include <com/sun/star/registry/XImplementationRegistration.hpp>
34 #include <com/sun/star/document/XFilter.hpp>
35 #include <com/sun/star/document/XExporter.hpp>
36 #include <com/sun/star/document/XImporter.hpp>
37 #include <com/sun/star/io/XInputStream.hpp>
38 #include <com/sun/star/io/XOutputStream.hpp>
39 #include <com/sun/star/io/XActiveDataSource.hpp>
40 #include <com/sun/star/beans/PropertyValue.hpp>
41 #include <com/sun/star/beans/XPropertySet.hpp>
42 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
43 
44 #include <com/sun/star/xml/crypto/XUriBinding.hpp>
45 #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
46 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
47 
48 #include <com/sun/star/xml/sax/XParser.hpp>
49 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
50 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
51 #include <com/sun/star/xml/sax/InputSource.hpp>
52 #include <com/sun/star/io/XInputStream.hpp>
53 #include <com/sun/star/io/XOutputStream.hpp>
54 #include <com/sun/star/uno/XNamingService.hpp>
55 
56 using namespace ::rtl ;
57 using namespace ::cppu ;
58 using namespace ::com::sun::star ;
59 using namespace ::com::sun::star::uno ;
60 using namespace ::com::sun::star::io ;
61 using namespace ::com::sun::star::ucb ;
62 using namespace ::com::sun::star::beans ;
63 using namespace ::com::sun::star::document ;
64 using namespace ::com::sun::star::lang ;
65 using namespace ::com::sun::star::bridge ;
66 using namespace ::com::sun::star::registry ;
67 using namespace ::com::sun::star::task ;
68 using namespace ::com::sun::star::xml ;
69 using namespace ::com::sun::star::xml::wrapper ;
70 using namespace ::com::sun::star::xml::sax ;
71 
72 
73 /**
74  * Helper: Implementation of XInputStream
75  */
76 class OInputStream : public WeakImplHelper1 < XInputStream >
77 {
78     public:
OInputStream(const Sequence<sal_Int8> & seq)79         OInputStream( const Sequence< sal_Int8 >&seq ) : m_seq( seq ), nPos( 0 ) {}
80 
readBytes(Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)81         virtual sal_Int32 SAL_CALL readBytes(
82             Sequence< sal_Int8 >& aData ,
83             sal_Int32 nBytesToRead
84         ) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException )
85         {
86             nBytesToRead = ( nBytesToRead > m_seq.getLength() - nPos ) ?
87                 m_seq.getLength() - nPos :
88                 nBytesToRead ;
89             aData = Sequence< sal_Int8 > ( &( m_seq.getConstArray()[nPos] ), nBytesToRead ) ;
90             nPos += nBytesToRead ;
91             return nBytesToRead ;
92         }
93 
readSomeBytes(::com::sun::star::uno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)94         virtual sal_Int32 SAL_CALL readSomeBytes(
95             ::com::sun::star::uno::Sequence< sal_Int8 >& aData ,
96             sal_Int32 nMaxBytesToRead
97         ) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException )
98         {
99             return readBytes( aData, nMaxBytesToRead ) ;
100         }
101 
skipBytes(sal_Int32 nBytesToSkip)102         virtual void SAL_CALL skipBytes(
103             sal_Int32 nBytesToSkip
104         ) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException )
105         {
106             // not implemented
107         }
108 
available(void)109         virtual sal_Int32 SAL_CALL available(
110             void
111         ) throw( NotConnectedException, IOException, RuntimeException )
112         {
113             return m_seq.getLength() - nPos ;
114         }
115 
closeInput(void)116         virtual void SAL_CALL closeInput(
117             void
118         ) throw( NotConnectedException, IOException, RuntimeException )
119         {
120             // not needed
121         }
122 
123     private:
124         sal_Int32 nPos;
125         Sequence< sal_Int8> m_seq;
126 } ;
127 
128 /**
129  * Helper : create a input stream from a file
130  */
131 Reference< XInputStream > createStreamFromFile( const OUString sFile ) ;
132 
133 /**
134  * Helper: Implementation of XOutputStream
135  */
136 class OOutputStream : public WeakImplHelper1 < XOutputStream >
137 {
138     public:
OOutputStream(const char * pcFile)139         OOutputStream( const char *pcFile ) {
140             strcpy( m_pcFile , pcFile ) ;
141             m_f = 0 ;
142         }
143 
writeBytes(const Sequence<sal_Int8> & aData)144         virtual void SAL_CALL writeBytes(
145             const Sequence< sal_Int8 >& aData
146         ) throw( NotConnectedException , BufferSizeExceededException , RuntimeException ) {
147             if( !m_f ) {
148                 m_f = fopen( m_pcFile , "w" ) ;
149             }
150 
151             fwrite( aData.getConstArray() , 1 , aData.getLength() , m_f ) ;
152         }
153 
flush(void)154         virtual void SAL_CALL flush(
155             void
156         ) throw( NotConnectedException , BufferSizeExceededException , RuntimeException ) {
157             fflush( m_f ) ;
158         }
159 
closeOutput(void)160         virtual void SAL_CALL closeOutput(
161             void
162         ) throw( NotConnectedException , BufferSizeExceededException , RuntimeException ) {
163             fclose( m_f ) ;
164             m_f = 0 ;
165         }
166 
167     private:
168         char m_pcFile[256];
169         FILE *m_f;
170 } ;
171 
172 /**
173  * Helper: Implementation of XUriBinding
174  */
175 class OUriBinding : public WeakImplHelper1 < ::com::sun::star::xml::crypto::XUriBinding >
176 {
177     public:
OUriBinding()178         OUriBinding() {
179             //Do nothing
180         }
181 
OUriBinding(::rtl::OUString & aUri,::com::sun::star::uno::Reference<com::sun::star::io::XInputStream> & aInputStream)182         OUriBinding(
183             ::rtl::OUString& aUri,
184             ::com::sun::star::uno::Reference< com::sun::star::io::XInputStream >& aInputStream ) {
185             m_vUris.push_back( aUri ) ;
186             m_vStreams.push_back( aInputStream ) ;
187         }
188 
setUriBinding(const::rtl::OUString & aUri,const::com::sun::star::uno::Reference<::com::sun::star::io::XInputStream> & aInputStream)189         virtual void SAL_CALL setUriBinding(
190             const ::rtl::OUString& aUri ,
191             const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& aInputStream
192         ) throw( ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException ) {
193             m_vUris.push_back( aUri ) ;
194             m_vStreams.push_back( aInputStream ) ;
195         }
196 
getUriBinding(const::rtl::OUString & uri)197         virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL getUriBinding( const ::rtl::OUString& uri ) throw( ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException ) {
198             ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > xInputStream ;
199 
200             int size = m_vUris.size() ;
201             for( int i = 0 ; i<size ; ++i ) {
202                 if( uri == m_vUris[i] ) {
203                     xInputStream = m_vStreams[i];
204                     break;
205                 }
206             }
207 
208             return xInputStream;
209         }
210 
211     private:
212         std::vector< ::rtl::OUString > m_vUris ;
213         std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > > m_vStreams ;
214 } ;
215 
216 /**
217  * Helper : set a output stream to a file
218  */
219 Reference< XOutputStream > createStreamToFile( const OUString sFile ) ;
220 
221 /**
222  * Helper : get service manager and context
223  */
224 Reference< XMultiComponentFactory > serviceManager( Reference< XComponentContext >& xContext , OUString sUnoUrl , OUString sRdbUrl  ) throw( RuntimeException , Exception ) ;
225 
226