xref: /trunk/main/xmlsecurity/tools/standalone/csfit/helper.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include <stdio.h>
29 /*#include <libxml/xmlstring.h>
30 */
31 
32 #include <rtl/ustring.hxx>
33 #include <pk11func.h>
34 
35 #include <cppuhelper/bootstrap.hxx>
36 #include <cppuhelper/implbase1.hxx>
37 #include <cppuhelper/servicefactory.hxx>
38 #include <ucbhelper/contentbroker.hxx>
39 #include <ucbhelper/configurationkeys.hxx>
40 
41 #include <com/sun/star/lang/XComponent.hpp>
42 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
43 #include <com/sun/star/registry/XImplementationRegistration.hpp>
44 #include <com/sun/star/document/XFilter.hpp>
45 #include <com/sun/star/document/XExporter.hpp>
46 #include <com/sun/star/document/XImporter.hpp>
47 #include <com/sun/star/io/XInputStream.hpp>
48 #include <com/sun/star/io/XOutputStream.hpp>
49 #include <com/sun/star/io/XActiveDataSource.hpp>
50 #include <com/sun/star/beans/PropertyValue.hpp>
51 #include <com/sun/star/beans/XPropertySet.hpp>
52 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
53 
54 #include <com/sun/star/xml/crypto/XUriBinding.hpp>
55 #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
56 #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
57 
58 #include <com/sun/star/xml/sax/XParser.hpp>
59 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
60 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
61 #include <com/sun/star/xml/sax/InputSource.hpp>
62 #include <com/sun/star/io/XInputStream.hpp>
63 #include <com/sun/star/io/XOutputStream.hpp>
64 #include <com/sun/star/uno/XNamingService.hpp>
65 
66 using namespace ::rtl ;
67 using namespace ::cppu ;
68 using namespace ::com::sun::star ;
69 using namespace ::com::sun::star::uno ;
70 using namespace ::com::sun::star::io ;
71 using namespace ::com::sun::star::ucb ;
72 using namespace ::com::sun::star::beans ;
73 using namespace ::com::sun::star::document ;
74 using namespace ::com::sun::star::lang ;
75 using namespace ::com::sun::star::bridge ;
76 using namespace ::com::sun::star::registry ;
77 using namespace ::com::sun::star::task ;
78 using namespace ::com::sun::star::xml ;
79 using namespace ::com::sun::star::xml::wrapper ;
80 using namespace ::com::sun::star::xml::sax ;
81 
82 
83 /**
84  * Helper: Implementation of XInputStream
85  */
86 class OInputStream : public WeakImplHelper1 < XInputStream >
87 {
88     public:
89         OInputStream( const Sequence< sal_Int8 >&seq ) : m_seq( seq ), nPos( 0 ) {}
90 
91         virtual sal_Int32 SAL_CALL readBytes(
92             Sequence< sal_Int8 >& aData ,
93             sal_Int32 nBytesToRead
94         ) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException )
95         {
96             nBytesToRead = ( nBytesToRead > m_seq.getLength() - nPos ) ?
97                 m_seq.getLength() - nPos :
98                 nBytesToRead ;
99             aData = Sequence< sal_Int8 > ( &( m_seq.getConstArray()[nPos] ), nBytesToRead ) ;
100             nPos += nBytesToRead ;
101             return nBytesToRead ;
102         }
103 
104         virtual sal_Int32 SAL_CALL readSomeBytes(
105             ::com::sun::star::uno::Sequence< sal_Int8 >& aData ,
106             sal_Int32 nMaxBytesToRead
107         ) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException )
108         {
109             return readBytes( aData, nMaxBytesToRead ) ;
110         }
111 
112         virtual void SAL_CALL skipBytes(
113             sal_Int32 nBytesToSkip
114         ) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException )
115         {
116             // not implemented
117         }
118 
119         virtual sal_Int32 SAL_CALL available(
120             void
121         ) throw( NotConnectedException, IOException, RuntimeException )
122         {
123             return m_seq.getLength() - nPos ;
124         }
125 
126         virtual void SAL_CALL closeInput(
127             void
128         ) throw( NotConnectedException, IOException, RuntimeException )
129         {
130             // not needed
131         }
132 
133     private:
134         sal_Int32 nPos;
135         Sequence< sal_Int8> m_seq;
136 } ;
137 
138 /**
139  * Helper : create a input stream from a file
140  */
141 Reference< XInputStream > createStreamFromFile( const OUString sFile ) ;
142 
143 /**
144  * Helper: Implementation of XOutputStream
145  */
146 class OOutputStream : public WeakImplHelper1 < XOutputStream >
147 {
148     public:
149         OOutputStream( const char *pcFile ) {
150             strcpy( m_pcFile , pcFile ) ;
151             m_f = 0 ;
152         }
153 
154         virtual void SAL_CALL writeBytes(
155             const Sequence< sal_Int8 >& aData
156         ) throw( NotConnectedException , BufferSizeExceededException , RuntimeException ) {
157             if( !m_f ) {
158                 m_f = fopen( m_pcFile , "w" ) ;
159             }
160 
161             fwrite( aData.getConstArray() , 1 , aData.getLength() , m_f ) ;
162         }
163 
164         virtual void SAL_CALL flush(
165             void
166         ) throw( NotConnectedException , BufferSizeExceededException , RuntimeException ) {
167             fflush( m_f ) ;
168         }
169 
170         virtual void SAL_CALL closeOutput(
171             void
172         ) throw( NotConnectedException , BufferSizeExceededException , RuntimeException ) {
173             fclose( m_f ) ;
174             m_f = 0 ;
175         }
176 
177     private:
178         char m_pcFile[256];
179         FILE *m_f;
180 } ;
181 
182 /**
183  * Helper: Implementation of XUriBinding
184  */
185 class OUriBinding : public WeakImplHelper1 < ::com::sun::star::xml::crypto::XUriBinding >
186 {
187     public:
188         OUriBinding() {
189             //Do nothing
190         }
191 
192         OUriBinding(
193             ::rtl::OUString& aUri,
194             ::com::sun::star::uno::Reference< com::sun::star::io::XInputStream >& aInputStream ) {
195             m_vUris.push_back( aUri ) ;
196             m_vStreams.push_back( aInputStream ) ;
197         }
198 
199         virtual void SAL_CALL setUriBinding(
200             const ::rtl::OUString& aUri ,
201             const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& aInputStream
202         ) throw( ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException ) {
203             m_vUris.push_back( aUri ) ;
204             m_vStreams.push_back( aInputStream ) ;
205         }
206 
207         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 ) {
208             ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > xInputStream ;
209 
210             int size = m_vUris.size() ;
211             for( int i = 0 ; i<size ; ++i ) {
212                 if( uri == m_vUris[i] ) {
213                     xInputStream = m_vStreams[i];
214                     break;
215                 }
216             }
217 
218             return xInputStream;
219         }
220 
221     private:
222         std::vector< ::rtl::OUString > m_vUris ;
223         std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > > m_vStreams ;
224 } ;
225 
226 /**
227  * Helper : set a output stream to a file
228  */
229 Reference< XOutputStream > createStreamToFile( const OUString sFile ) ;
230 
231 /**
232  * Helper : get service manager and context
233  */
234 Reference< XMultiComponentFactory > serviceManager( Reference< XComponentContext >& xContext , OUString sUnoUrl , OUString sRdbUrl  ) throw( RuntimeException , Exception ) ;
235 
236 /**
237  * Helper : Get password function for PKCS11 slot
238  */
239 char* PriPK11PasswordFunc( PK11SlotInfo *slot , PRBool retry , void* arg ) ;
240 
241 
242