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