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