Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : */
9 :
10 : #ifndef INCLUDED_FILTER_SOURCE_XSLTFILTER_LIBXSLTTRANSFORMER_HXX
11 : #define INCLUDED_FILTER_SOURCE_XSLTFILTER_LIBXSLTTRANSFORMER_HXX
12 :
13 : #include <stdio.h>
14 :
15 : #include <list>
16 : #include <map>
17 :
18 : #include <libxml/parser.h>
19 : #include <libxml/tree.h>
20 : #include <libxml/xmlIO.h>
21 : #include <libxslt/transform.h>
22 : #include <libxml/xpathInternals.h>
23 :
24 : #include <cppuhelper/factory.hxx>
25 : #include <cppuhelper/implbase1.hxx>
26 :
27 : #include <rtl/ref.hxx>
28 :
29 : #include <salhelper/thread.hxx>
30 :
31 : #include <com/sun/star/uno/Any.hxx>
32 :
33 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 : #include <com/sun/star/io/XInputStream.hpp>
35 : #include <com/sun/star/io/XOutputStream.hpp>
36 : #include <com/sun/star/io/XStreamListener.hpp>
37 : #include <com/sun/star/beans/NamedValue.hpp>
38 : #include <com/sun/star/xml/xslt/XXSLTTransformer.hpp>
39 :
40 : using namespace ::cppu;
41 : using namespace ::osl;
42 : using namespace ::com::sun::star::beans;
43 : using namespace ::com::sun::star::io;
44 : using namespace ::com::sun::star::uno;
45 :
46 : using ::std::list;
47 : using ::std::map;
48 :
49 : #define EXT_MODULE_OLE_URI "http://libreoffice.org/2011/xslt/ole"
50 :
51 : namespace XSLT
52 : {
53 :
54 : /*
55 : * LibXSLTTransformer provides an transforming pipe service to XSLTFilter.
56 : *
57 : * It implements XActiveDataSource, XActiveDataSink and XActiveDataControl
58 : * to consume data. It also notifies upstream of important events such as
59 : * begin and end of the transformation and of any errors that occur during
60 : * transformation.
61 : *
62 : * TODO: Error reporting leaves room for improvement, currently.
63 : *
64 : * The actual transformation is done by a worker thread.
65 : *
66 : * See Reader below.
67 : */
68 : class LibXSLTTransformer : public WeakImplHelper1<com::sun::star::xml::xslt::XXSLTTransformer>
69 : {
70 : private:
71 : static const char* const PARAM_SOURCE_URL;
72 : static const char* const PARAM_SOURCE_BASE_URL;
73 : static const char* const PARAM_TARGET_URL;
74 : static const char* const PARAM_TARGET_BASE_URL;
75 : static const char* const PARAM_DOCTYPE_PUBLIC;
76 :
77 : // the UNO ServiceFactory
78 : com::sun::star::uno::Reference<com::sun::star::uno::XComponentContext> m_xContext;
79 :
80 : com::sun::star::uno::Reference<XInputStream> m_rInputStream;
81 :
82 : com::sun::star::uno::Reference<XOutputStream> m_rOutputStream;
83 :
84 : typedef ::std::list<com::sun::star::uno::Reference<XStreamListener> > ListenerList;
85 :
86 : ListenerList m_listeners;
87 :
88 : OString m_styleSheetURL;
89 :
90 : ::std::map<const char *, OString> m_parameters;
91 :
92 : rtl::Reference< salhelper::Thread > m_Reader;
93 :
94 : protected:
95 12 : virtual ~LibXSLTTransformer() {
96 4 : if (m_Reader.is()) {
97 4 : m_Reader->terminate();
98 4 : m_Reader->join();
99 : }
100 8 : }
101 :
102 : public:
103 :
104 : // ctor...
105 : LibXSLTTransformer(const com::sun::star::uno::Reference<com::sun::star::uno::XComponentContext> &r);
106 :
107 : // XActiveDataSink
108 : virtual void SAL_CALL
109 : setInputStream(const com::sun::star::uno::Reference<XInputStream>& inputStream)
110 : throw (RuntimeException, std::exception) SAL_OVERRIDE;
111 : virtual com::sun::star::uno::Reference<XInputStream> SAL_CALL
112 : getInputStream() throw (RuntimeException, std::exception) SAL_OVERRIDE;
113 : // XActiveDataSource
114 : virtual void SAL_CALL
115 : setOutputStream(const com::sun::star::uno::Reference<XOutputStream>& outputStream)
116 : throw (RuntimeException, std::exception) SAL_OVERRIDE;
117 : virtual com::sun::star::uno::Reference<XOutputStream> SAL_CALL
118 : getOutputStream() throw (RuntimeException, std::exception) SAL_OVERRIDE;
119 : // XActiveDataControl
120 : virtual void SAL_CALL
121 : addListener(const com::sun::star::uno::Reference<XStreamListener>& listener)
122 : throw (RuntimeException, std::exception) SAL_OVERRIDE;
123 : virtual void SAL_CALL
124 : removeListener(const com::sun::star::uno::Reference<XStreamListener>& listener)
125 : throw (RuntimeException, std::exception) SAL_OVERRIDE;
126 : virtual void SAL_CALL
127 : start() throw (RuntimeException, std::exception) SAL_OVERRIDE;
128 : virtual void SAL_CALL
129 : terminate() throw (RuntimeException, std::exception) SAL_OVERRIDE;
130 : virtual void SAL_CALL
131 : initialize(const Sequence<Any>& params) throw (RuntimeException, std::exception) SAL_OVERRIDE;
132 :
133 : void SAL_CALL
134 : done();
135 :
136 : void SAL_CALL
137 : error(const OUString& msg);
138 :
139 : const OString SAL_CALL
140 4 : getStyleSheetURL() { return m_styleSheetURL; }
141 :
142 : ::std::map<const char*, OString> SAL_CALL
143 4 : getParameters() { return m_parameters; }
144 :
145 : com::sun::star::uno::Reference<com::sun::star::uno::XComponentContext> SAL_CALL
146 4 : getComponentContext() {
147 4 : return m_xContext;
148 : }
149 :
150 : };
151 :
152 : /*
153 : * Reader provides a worker thread to perform the actual transformation.
154 : * It pipes the streams provided by a LibXSLTTransformer
155 : * instance through libxslt.
156 : */
157 : class Reader : public salhelper::Thread
158 : {
159 : public:
160 : Reader(LibXSLTTransformer* transformer);
161 : int SAL_CALL
162 : read(char * buffer, int len);
163 : int SAL_CALL
164 : write(const char * buffer, int len);
165 : int SAL_CALL
166 : closeInput();
167 : int SAL_CALL
168 : closeOutput();
169 :
170 : private:
171 : virtual
172 : ~Reader();
173 :
174 : static const sal_Int32 OUTPUT_BUFFER_SIZE;
175 : static const sal_Int32 INPUT_BUFFER_SIZE;
176 : LibXSLTTransformer* m_transformer;
177 : Sequence<sal_Int8> m_readBuf;
178 : Sequence<sal_Int8> m_writeBuf;
179 :
180 : virtual void
181 : execute() SAL_OVERRIDE;
182 : void SAL_CALL
183 : registerExtensionModule();
184 : };
185 :
186 : }
187 : ;
188 :
189 : #endif // INCLUDED_FILTER_SOURCE_XSLTFILTER_LIBXSLTTRANSFORMER_HXX
190 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|