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