xref: /trunk/main/xmlsecurity/tools/standalone/mscsfit/helper.hxx (revision 914d351e5f5b84e4342a86d6ab8d4aca7308b9bd)
1*ec61c6edSAndrew Rist /**************************************************************
2*ec61c6edSAndrew Rist  *
3*ec61c6edSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ec61c6edSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ec61c6edSAndrew Rist  * distributed with this work for additional information
6*ec61c6edSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ec61c6edSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ec61c6edSAndrew Rist  * "License"); you may not use this file except in compliance
9*ec61c6edSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ec61c6edSAndrew Rist  *
11*ec61c6edSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ec61c6edSAndrew Rist  *
13*ec61c6edSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ec61c6edSAndrew Rist  * software distributed under the License is distributed on an
15*ec61c6edSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ec61c6edSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ec61c6edSAndrew Rist  * specific language governing permissions and limitations
18*ec61c6edSAndrew Rist  * under the License.
19*ec61c6edSAndrew Rist  *
20*ec61c6edSAndrew Rist  *************************************************************/
21*ec61c6edSAndrew Rist 
22cdf0e10cSrcweir #include <stdio.h>
23cdf0e10cSrcweir #include <rtl/ustring.hxx>
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include <cppuhelper/bootstrap.hxx>
26cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
27cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx>
28cdf0e10cSrcweir #include <ucbhelper/contentbroker.hxx>
29cdf0e10cSrcweir #include <ucbhelper/configurationkeys.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
32cdf0e10cSrcweir #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
33cdf0e10cSrcweir #include <com/sun/star/registry/XImplementationRegistration.hpp>
34cdf0e10cSrcweir #include <com/sun/star/document/XFilter.hpp>
35cdf0e10cSrcweir #include <com/sun/star/document/XExporter.hpp>
36cdf0e10cSrcweir #include <com/sun/star/document/XImporter.hpp>
37cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
38cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
39cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
40cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
41cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
42cdf0e10cSrcweir #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #include <com/sun/star/xml/crypto/XUriBinding.hpp>
45cdf0e10cSrcweir #include <com/sun/star/xml/wrapper/XXMLDocumentWrapper.hpp>
46cdf0e10cSrcweir #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
47cdf0e10cSrcweir 
48cdf0e10cSrcweir #include <com/sun/star/xml/sax/XParser.hpp>
49cdf0e10cSrcweir #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
50cdf0e10cSrcweir #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
51cdf0e10cSrcweir #include <com/sun/star/xml/sax/InputSource.hpp>
52cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
53cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
54cdf0e10cSrcweir #include <com/sun/star/uno/XNamingService.hpp>
55cdf0e10cSrcweir 
56cdf0e10cSrcweir using namespace ::rtl ;
57cdf0e10cSrcweir using namespace ::cppu ;
58cdf0e10cSrcweir using namespace ::com::sun::star ;
59cdf0e10cSrcweir using namespace ::com::sun::star::uno ;
60cdf0e10cSrcweir using namespace ::com::sun::star::io ;
61cdf0e10cSrcweir using namespace ::com::sun::star::ucb ;
62cdf0e10cSrcweir using namespace ::com::sun::star::beans ;
63cdf0e10cSrcweir using namespace ::com::sun::star::document ;
64cdf0e10cSrcweir using namespace ::com::sun::star::lang ;
65cdf0e10cSrcweir using namespace ::com::sun::star::bridge ;
66cdf0e10cSrcweir using namespace ::com::sun::star::registry ;
67cdf0e10cSrcweir using namespace ::com::sun::star::task ;
68cdf0e10cSrcweir using namespace ::com::sun::star::xml ;
69cdf0e10cSrcweir using namespace ::com::sun::star::xml::wrapper ;
70cdf0e10cSrcweir using namespace ::com::sun::star::xml::sax ;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 
73cdf0e10cSrcweir /**
74cdf0e10cSrcweir  * Helper: Implementation of XInputStream
75cdf0e10cSrcweir  */
76cdf0e10cSrcweir class OInputStream : public WeakImplHelper1 < XInputStream >
77cdf0e10cSrcweir {
78cdf0e10cSrcweir     public:
OInputStream(const Sequence<sal_Int8> & seq)79cdf0e10cSrcweir         OInputStream( const Sequence< sal_Int8 >&seq ) : m_seq( seq ), nPos( 0 ) {}
80cdf0e10cSrcweir 
readBytes(Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)81cdf0e10cSrcweir         virtual sal_Int32 SAL_CALL readBytes(
82cdf0e10cSrcweir             Sequence< sal_Int8 >& aData ,
83cdf0e10cSrcweir             sal_Int32 nBytesToRead
84cdf0e10cSrcweir         ) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException )
85cdf0e10cSrcweir         {
86cdf0e10cSrcweir             nBytesToRead = ( nBytesToRead > m_seq.getLength() - nPos ) ?
87cdf0e10cSrcweir                 m_seq.getLength() - nPos :
88cdf0e10cSrcweir                 nBytesToRead ;
89cdf0e10cSrcweir             aData = Sequence< sal_Int8 > ( &( m_seq.getConstArray()[nPos] ), nBytesToRead ) ;
90cdf0e10cSrcweir             nPos += nBytesToRead ;
91cdf0e10cSrcweir             return nBytesToRead ;
92cdf0e10cSrcweir         }
93cdf0e10cSrcweir 
readSomeBytes(::com::sun::star::uno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)94cdf0e10cSrcweir         virtual sal_Int32 SAL_CALL readSomeBytes(
95cdf0e10cSrcweir             ::com::sun::star::uno::Sequence< sal_Int8 >& aData ,
96cdf0e10cSrcweir             sal_Int32 nMaxBytesToRead
97cdf0e10cSrcweir         ) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException )
98cdf0e10cSrcweir         {
99cdf0e10cSrcweir             return readBytes( aData, nMaxBytesToRead ) ;
100cdf0e10cSrcweir         }
101cdf0e10cSrcweir 
skipBytes(sal_Int32 nBytesToSkip)102cdf0e10cSrcweir         virtual void SAL_CALL skipBytes(
103cdf0e10cSrcweir             sal_Int32 nBytesToSkip
104cdf0e10cSrcweir         ) throw( NotConnectedException, BufferSizeExceededException, IOException, RuntimeException )
105cdf0e10cSrcweir         {
106cdf0e10cSrcweir             // not implemented
107cdf0e10cSrcweir         }
108cdf0e10cSrcweir 
available(void)109cdf0e10cSrcweir         virtual sal_Int32 SAL_CALL available(
110cdf0e10cSrcweir             void
111cdf0e10cSrcweir         ) throw( NotConnectedException, IOException, RuntimeException )
112cdf0e10cSrcweir         {
113cdf0e10cSrcweir             return m_seq.getLength() - nPos ;
114cdf0e10cSrcweir         }
115cdf0e10cSrcweir 
closeInput(void)116cdf0e10cSrcweir         virtual void SAL_CALL closeInput(
117cdf0e10cSrcweir             void
118cdf0e10cSrcweir         ) throw( NotConnectedException, IOException, RuntimeException )
119cdf0e10cSrcweir         {
120cdf0e10cSrcweir             // not needed
121cdf0e10cSrcweir         }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir     private:
124cdf0e10cSrcweir         sal_Int32 nPos;
125cdf0e10cSrcweir         Sequence< sal_Int8> m_seq;
126cdf0e10cSrcweir } ;
127cdf0e10cSrcweir 
128cdf0e10cSrcweir /**
129cdf0e10cSrcweir  * Helper : create a input stream from a file
130cdf0e10cSrcweir  */
131cdf0e10cSrcweir Reference< XInputStream > createStreamFromFile( const OUString sFile ) ;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir /**
134cdf0e10cSrcweir  * Helper: Implementation of XOutputStream
135cdf0e10cSrcweir  */
136cdf0e10cSrcweir class OOutputStream : public WeakImplHelper1 < XOutputStream >
137cdf0e10cSrcweir {
138cdf0e10cSrcweir     public:
OOutputStream(const char * pcFile)139cdf0e10cSrcweir         OOutputStream( const char *pcFile ) {
140cdf0e10cSrcweir             strcpy( m_pcFile , pcFile ) ;
141cdf0e10cSrcweir             m_f = 0 ;
142cdf0e10cSrcweir         }
143cdf0e10cSrcweir 
writeBytes(const Sequence<sal_Int8> & aData)144cdf0e10cSrcweir         virtual void SAL_CALL writeBytes(
145cdf0e10cSrcweir             const Sequence< sal_Int8 >& aData
146cdf0e10cSrcweir         ) throw( NotConnectedException , BufferSizeExceededException , RuntimeException ) {
147cdf0e10cSrcweir             if( !m_f ) {
148cdf0e10cSrcweir                 m_f = fopen( m_pcFile , "w" ) ;
149cdf0e10cSrcweir             }
150cdf0e10cSrcweir 
151cdf0e10cSrcweir             fwrite( aData.getConstArray() , 1 , aData.getLength() , m_f ) ;
152cdf0e10cSrcweir         }
153cdf0e10cSrcweir 
flush(void)154cdf0e10cSrcweir         virtual void SAL_CALL flush(
155cdf0e10cSrcweir             void
156cdf0e10cSrcweir         ) throw( NotConnectedException , BufferSizeExceededException , RuntimeException ) {
157cdf0e10cSrcweir             fflush( m_f ) ;
158cdf0e10cSrcweir         }
159cdf0e10cSrcweir 
closeOutput(void)160cdf0e10cSrcweir         virtual void SAL_CALL closeOutput(
161cdf0e10cSrcweir             void
162cdf0e10cSrcweir         ) throw( NotConnectedException , BufferSizeExceededException , RuntimeException ) {
163cdf0e10cSrcweir             fclose( m_f ) ;
164cdf0e10cSrcweir             m_f = 0 ;
165cdf0e10cSrcweir         }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir     private:
168cdf0e10cSrcweir         char m_pcFile[256];
169cdf0e10cSrcweir         FILE *m_f;
170cdf0e10cSrcweir } ;
171cdf0e10cSrcweir 
172cdf0e10cSrcweir /**
173cdf0e10cSrcweir  * Helper: Implementation of XUriBinding
174cdf0e10cSrcweir  */
175cdf0e10cSrcweir class OUriBinding : public WeakImplHelper1 < ::com::sun::star::xml::crypto::XUriBinding >
176cdf0e10cSrcweir {
177cdf0e10cSrcweir     public:
OUriBinding()178cdf0e10cSrcweir         OUriBinding() {
179cdf0e10cSrcweir             //Do nothing
180cdf0e10cSrcweir         }
181cdf0e10cSrcweir 
OUriBinding(::rtl::OUString & aUri,::com::sun::star::uno::Reference<com::sun::star::io::XInputStream> & aInputStream)182cdf0e10cSrcweir         OUriBinding(
183cdf0e10cSrcweir             ::rtl::OUString& aUri,
184cdf0e10cSrcweir             ::com::sun::star::uno::Reference< com::sun::star::io::XInputStream >& aInputStream ) {
185cdf0e10cSrcweir             m_vUris.push_back( aUri ) ;
186cdf0e10cSrcweir             m_vStreams.push_back( aInputStream ) ;
187cdf0e10cSrcweir         }
188cdf0e10cSrcweir 
setUriBinding(const::rtl::OUString & aUri,const::com::sun::star::uno::Reference<::com::sun::star::io::XInputStream> & aInputStream)189cdf0e10cSrcweir         virtual void SAL_CALL setUriBinding(
190cdf0e10cSrcweir             const ::rtl::OUString& aUri ,
191cdf0e10cSrcweir             const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& aInputStream
192cdf0e10cSrcweir         ) throw( ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException ) {
193cdf0e10cSrcweir             m_vUris.push_back( aUri ) ;
194cdf0e10cSrcweir             m_vStreams.push_back( aInputStream ) ;
195cdf0e10cSrcweir         }
196cdf0e10cSrcweir 
getUriBinding(const::rtl::OUString & uri)197cdf0e10cSrcweir         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 ) {
198cdf0e10cSrcweir             ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > xInputStream ;
199cdf0e10cSrcweir 
200cdf0e10cSrcweir             int size = m_vUris.size() ;
201cdf0e10cSrcweir             for( int i = 0 ; i<size ; ++i ) {
202cdf0e10cSrcweir                 if( uri == m_vUris[i] ) {
203cdf0e10cSrcweir                     xInputStream = m_vStreams[i];
204cdf0e10cSrcweir                     break;
205cdf0e10cSrcweir                 }
206cdf0e10cSrcweir             }
207cdf0e10cSrcweir 
208cdf0e10cSrcweir             return xInputStream;
209cdf0e10cSrcweir         }
210cdf0e10cSrcweir 
211cdf0e10cSrcweir     private:
212cdf0e10cSrcweir         std::vector< ::rtl::OUString > m_vUris ;
213cdf0e10cSrcweir         std::vector< ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > > m_vStreams ;
214cdf0e10cSrcweir } ;
215cdf0e10cSrcweir 
216cdf0e10cSrcweir /**
217cdf0e10cSrcweir  * Helper : set a output stream to a file
218cdf0e10cSrcweir  */
219cdf0e10cSrcweir Reference< XOutputStream > createStreamToFile( const OUString sFile ) ;
220cdf0e10cSrcweir 
221cdf0e10cSrcweir /**
222cdf0e10cSrcweir  * Helper : get service manager and context
223cdf0e10cSrcweir  */
224cdf0e10cSrcweir Reference< XMultiComponentFactory > serviceManager( Reference< XComponentContext >& xContext , OUString sUnoUrl , OUString sRdbUrl  ) throw( RuntimeException , Exception ) ;
225