Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Initial Developer of the Original Code is
16 : * [ Peter Jentsch <pjotr@guineapics.de> ]
17 : * Portions created by the Initial Developer are Copyright (C) 2010 the
18 : * Initial Developer. All Rights Reserved.
19 : *
20 : * Contributor(s): Peter Jentsch <pjotr@guineapics.de>
21 : *
22 : * Alternatively, the contents of this file may be used under the terms of
23 : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
24 : * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
25 : * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
26 : * instead of those above.
27 : */
28 :
29 : #ifndef __LIBXSLTTRANSFORMER_HXX__
30 : #define __LIBXSLTTRANSFORMER_HXX__
31 :
32 : #include <stdio.h>
33 :
34 : #include <list>
35 : #include <map>
36 :
37 : #include <libxml/parser.h>
38 : #include <libxml/tree.h>
39 : #include <libxml/xmlIO.h>
40 : #include <libxslt/transform.h>
41 : #include <libxml/xpathInternals.h>
42 :
43 : #include <cppuhelper/factory.hxx>
44 : #include <cppuhelper/implbase1.hxx>
45 : #include <cppuhelper/implbase.hxx>
46 :
47 : #include <rtl/ref.hxx>
48 :
49 : #include <salhelper/thread.hxx>
50 :
51 : #include <com/sun/star/uno/Any.hxx>
52 :
53 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
54 : #include <com/sun/star/io/XInputStream.hpp>
55 : #include <com/sun/star/io/XOutputStream.hpp>
56 : #include <com/sun/star/io/XStreamListener.hpp>
57 : #include <com/sun/star/beans/NamedValue.hpp>
58 : #include <com/sun/star/xml/xslt/XXSLTTransformer.hpp>
59 :
60 : using namespace ::rtl;
61 : using namespace ::cppu;
62 : using namespace ::osl;
63 : using namespace ::com::sun::star::beans;
64 : using namespace ::com::sun::star::io;
65 : using namespace ::com::sun::star::uno;
66 :
67 : using ::std::list;
68 : using ::std::map;
69 :
70 : #define EXT_MODULE_OLE_URI "http://libreoffice.org/2011/xslt/ole"
71 :
72 : namespace XSLT
73 : {
74 :
75 : /*
76 : * LibXSLTTransformer provides an transforming pipe service to XSLTFilter.
77 : *
78 : * It implements XActiveDataSource, XActiveDataSink and XActiveDataControl
79 : * to consume data. It also notifies upstream of important events such as
80 : * begin and end of the transformation and of any errors that occur during
81 : * transformation.
82 : *
83 : * TODO: Error reporting leaves room for improvement, currently.
84 : *
85 : * The actual transformation is done by a worker thread.
86 : *
87 : * See Reader below.
88 : */
89 : class LibXSLTTransformer : public WeakImplHelper1<com::sun::star::xml::xslt::XXSLTTransformer>
90 : {
91 : private:
92 : static const char* const PARAM_SOURCE_URL;
93 : static const char* const PARAM_SOURCE_BASE_URL;
94 : static const char* const PARAM_TARGET_URL;
95 : static const char* const PARAM_TARGET_BASE_URL;
96 : static const char* const PARAM_DOCTYPE_PUBLIC;
97 :
98 : // the UNO ServiceFactory
99 : com::sun::star::uno::Reference<com::sun::star::lang::XMultiServiceFactory> m_rServiceFactory;
100 :
101 : com::sun::star::uno::Reference<XInputStream> m_rInputStream;
102 :
103 : com::sun::star::uno::Reference<XOutputStream> m_rOutputStream;
104 :
105 : typedef ::std::list<com::sun::star::uno::Reference<XStreamListener> > ListenerList;
106 :
107 : ListenerList m_listeners;
108 :
109 : OString m_styleSheetURL;
110 :
111 : ::std::map<const char *, OString> m_parameters;
112 :
113 : rtl::Reference< salhelper::Thread > m_Reader;
114 :
115 : protected:
116 0 : virtual ~LibXSLTTransformer() {
117 0 : if (m_Reader.is()) {
118 0 : m_Reader->terminate();
119 0 : m_Reader->join();
120 : }
121 0 : }
122 :
123 : public:
124 :
125 : // ctor...
126 : LibXSLTTransformer(const com::sun::star::uno::Reference<com::sun::star::lang::XMultiServiceFactory> &r);
127 :
128 : // XActiveDataSink
129 : virtual void SAL_CALL
130 : setInputStream(const com::sun::star::uno::Reference<XInputStream>& inputStream)
131 : throw (RuntimeException);
132 : virtual com::sun::star::uno::Reference<XInputStream> SAL_CALL
133 : getInputStream() throw (RuntimeException);
134 : // XActiveDataSource
135 : virtual void SAL_CALL
136 : setOutputStream(const com::sun::star::uno::Reference<XOutputStream>& outputStream)
137 : throw (RuntimeException);
138 : virtual com::sun::star::uno::Reference<XOutputStream> SAL_CALL
139 : getOutputStream() throw (RuntimeException);
140 : // XActiveDataControl
141 : virtual void SAL_CALL
142 : addListener(const com::sun::star::uno::Reference<XStreamListener>& listener)
143 : throw (RuntimeException);
144 : virtual void SAL_CALL
145 : removeListener(const com::sun::star::uno::Reference<XStreamListener>& listener)
146 : throw (RuntimeException);
147 : virtual void SAL_CALL
148 : start() throw (RuntimeException);
149 : virtual void SAL_CALL
150 : terminate() throw (RuntimeException);
151 : virtual void SAL_CALL
152 : initialize(const Sequence<Any>& params) throw (RuntimeException);
153 :
154 : void SAL_CALL
155 : done();
156 :
157 : void SAL_CALL
158 : error(const OUString& msg);
159 :
160 : const OString SAL_CALL
161 : getStyleSheetURL();
162 :
163 : ::std::map<const char*, OString> SAL_CALL
164 : getParameters();
165 :
166 : virtual com::sun::star::uno::Reference<com::sun::star::lang::XMultiServiceFactory> SAL_CALL
167 0 : getServiceFactory() {
168 0 : return m_rServiceFactory;
169 : }
170 :
171 : };
172 :
173 : /*
174 : * Reader provides a worker thread to perform the actual transformation.
175 : * It pipes the streams provided by a LibXSLTTransformer
176 : * instance through libxslt.
177 : */
178 : class Reader : public salhelper::Thread
179 : {
180 : public:
181 : Reader(LibXSLTTransformer* transformer);
182 : int SAL_CALL
183 : read(char * buffer, int len);
184 : int SAL_CALL
185 : write(const char * buffer, int len);
186 : int SAL_CALL
187 : closeInput();
188 : int SAL_CALL
189 : closeOutput();
190 :
191 : private:
192 : virtual
193 : ~Reader();
194 :
195 : static const sal_Int32 OUTPUT_BUFFER_SIZE;
196 : static const sal_Int32 INPUT_BUFFER_SIZE;
197 : LibXSLTTransformer* m_transformer;
198 : Sequence<sal_Int8> m_readBuf;
199 : Sequence<sal_Int8> m_writeBuf;
200 :
201 : virtual void
202 : execute();
203 : void SAL_CALL
204 : registerExtensionModule();
205 : };
206 :
207 : }
208 : ;
209 :
210 : #endif // __LIBXSLTTRANSFORMER_HXX__
211 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|