Bump for 4.0-15
[LibreOffice.git] / filter / source / xsltfilter / XSLTFilter.cxx
blob85add7168f134186a251a9d5c4e0008299a7a814
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <stdio.h>
23 #include <cppuhelper/factory.hxx>
24 #include <cppuhelper/servicefactory.hxx>
25 #include <cppuhelper/implbase4.hxx>
26 #include <cppuhelper/implbase.hxx>
28 #include <sax/tools/documenthandleradapter.hxx>
30 #include <osl/time.h>
31 #include <osl/conditn.h>
32 #include <rtl/strbuf.hxx>
33 #include <tools/urlobj.hxx>
35 #include <comphelper/interaction.hxx>
36 #include <comphelper/processfactory.hxx>
38 #include <com/sun/star/lang/XComponent.hpp>
39 #include <com/sun/star/lang/EventObject.hpp>
41 #include <com/sun/star/uno/Any.hxx>
43 #include <com/sun/star/beans/PropertyValue.hpp>
45 #include <com/sun/star/xml/sax/Parser.hpp>
46 #include <com/sun/star/xml/sax/InputSource.hpp>
47 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
48 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
49 #include <com/sun/star/xml/sax/SAXException.hpp>
50 #include <com/sun/star/xml/sax/Writer.hpp>
51 #include <com/sun/star/xml/XImportFilter.hpp>
52 #include <com/sun/star/xml/XExportFilter.hpp>
54 #include <com/sun/star/util/XMacroExpander.hpp>
56 #include <com/sun/star/io/Pipe.hpp>
57 #include <com/sun/star/io/XInputStream.hpp>
58 #include <com/sun/star/io/XOutputStream.hpp>
59 #include <com/sun/star/io/XActiveDataSource.hpp>
60 #include <com/sun/star/io/XActiveDataSink.hpp>
61 #include <com/sun/star/io/XActiveDataControl.hpp>
62 #include <com/sun/star/io/XStreamListener.hpp>
63 #include <com/sun/star/util/PathSubstitution.hpp>
64 #include <com/sun/star/util/XStringSubstitution.hpp>
65 #include <com/sun/star/beans/NamedValue.hpp>
66 #include <com/sun/star/task/XInteractionHandler.hpp>
67 #include <com/sun/star/task/XInteractionRequest.hpp>
68 #include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
69 #include <com/sun/star/xml/xslt/XSLT2Transformer.hpp>
70 #include <com/sun/star/xml/xslt/XSLTTransformer.hpp>
72 #include <xmloff/attrlist.hxx>
74 #include <LibXSLTTransformer.hxx>
76 #define TRANSFORMATION_TIMEOUT_SEC 60
78 using namespace ::rtl;
79 using namespace ::cppu;
80 using namespace ::osl;
81 using namespace ::sax;
82 using namespace ::com::sun::star::beans;
83 using namespace ::com::sun::star::io;
84 using namespace ::com::sun::star::uno;
85 using namespace ::com::sun::star::lang;
86 using namespace ::com::sun::star::registry;
87 using namespace ::com::sun::star::xml;
88 using namespace ::com::sun::star::xml::sax;
89 using namespace ::com::sun::star::util;
90 using namespace ::com::sun::star::task;
92 namespace XSLT
95 * XSLTFilter reads flat XML streams from the XML filter framework and passes
96 * them to an XSLT transformation service. XSLT transformation errors are
97 * reported to XSLTFilter.
99 * Currently, our transformation service is libxslt based, so it
100 * only supports XSLT 1.0. There is a possibility to use XSLT 2.0
101 * supporting service from an extension for a specific filter; the
102 * service must support com.sun.star.xml.xslt.XSLT2Transformer.
104 class XSLTFilter : public WeakImplHelper4<XImportFilter, XExportFilter,
105 XStreamListener, ExtendedDocumentHandlerAdapter>
107 private:
109 // the UNO ServiceFactory
110 css::uno::Reference<XMultiServiceFactory> m_rServiceFactory;
112 // DocumentHandler interface of the css::xml::sax::Writer service
113 css::uno::Reference<XOutputStream> m_rOutputStream;
115 css::uno::Reference<xslt::XXSLTTransformer> m_tcontrol;
117 oslCondition m_cTransformed;
118 sal_Bool m_bTerminated;
119 sal_Bool m_bError;
121 OUString m_aExportBaseUrl;
123 OUString
124 rel2abs(const OUString&);
125 OUString
126 expandUrl(const OUString&);
128 css::uno::Reference<xslt::XXSLTTransformer> impl_createTransformer(const rtl::OUString& rTransformer, const Sequence<Any>& rArgs);
130 public:
132 // ctor...
133 XSLTFilter(const css::uno::Reference<XMultiServiceFactory> &r);
135 // XStreamListener
136 virtual void SAL_CALL
137 error(const Any& a) throw (RuntimeException);
138 virtual void SAL_CALL
139 closed() throw (RuntimeException);
140 virtual void SAL_CALL
141 terminated() throw (RuntimeException);
142 virtual void SAL_CALL
143 started() throw (RuntimeException);
144 virtual void SAL_CALL
145 disposing(const EventObject& e) throw (RuntimeException);
147 // XImportFilter
148 virtual sal_Bool SAL_CALL
149 importer(const Sequence<PropertyValue>& aSourceData, const css::uno::Reference<
150 XDocumentHandler>& xHandler,
151 const Sequence<OUString>& msUserData) throw (RuntimeException);
153 // XExportFilter
154 virtual sal_Bool SAL_CALL
155 exporter(const Sequence<PropertyValue>& aSourceData, const Sequence<
156 OUString>& msUserData) throw (RuntimeException);
158 // XDocumentHandler
159 virtual void SAL_CALL
160 startDocument() throw (SAXException, RuntimeException);
161 virtual void SAL_CALL
162 endDocument() throw (SAXException, RuntimeException);
165 XSLTFilter::XSLTFilter(const css::uno::Reference<XMultiServiceFactory> &r):
166 m_rServiceFactory(r), m_bTerminated(sal_False), m_bError(sal_False)
168 m_cTransformed = osl_createCondition();
171 void
172 XSLTFilter::disposing(const EventObject&) throw (RuntimeException)
176 ::rtl::OUString
177 XSLTFilter::expandUrl(const ::rtl::OUString& sUrl)
179 ::rtl::OUString sExpandedUrl;
182 css::uno::Reference<XComponentContext> xContext(
183 comphelper::getComponentContext(m_rServiceFactory));
184 css::uno::Reference<XMacroExpander>
185 xMacroExpander(
186 xContext->getValueByName(
187 ::rtl::OUString(
188 "/singletons/com.sun.star.util.theMacroExpander" )),
189 UNO_QUERY_THROW);
190 sExpandedUrl = xMacroExpander->expandMacros(sUrl);
191 sal_Int32 nPos = sExpandedUrl.indexOf( "vnd.sun.star.expand:" );
192 if (nPos != -1)
193 sExpandedUrl = sExpandedUrl.copy(nPos + 20);
195 catch (const Exception&)
198 return sExpandedUrl;
201 css::uno::Reference<xslt::XXSLTTransformer>
202 XSLTFilter::impl_createTransformer(const rtl::OUString& rTransformer, const Sequence<Any>& rArgs)
204 css::uno::Reference<xslt::XXSLTTransformer> xTransformer;
206 // check if the filter needs XSLT-2.0-capable transformer
207 // COMPATIBILITY: libreoffice 3.5/3.6 used to save the impl.
208 // name of the XSLT 2.0 transformation service there, so check
209 // for that too (it is sufficient to check that there is _a_
210 // service name there)
211 if (rTransformer.toBoolean() || rTransformer.startsWith("com.sun."))
215 xTransformer = xslt::XSLT2Transformer::create(
216 comphelper::getComponentContext(m_rServiceFactory), rArgs);
218 catch (const Exception&)
220 // TODO: put a dialog telling about the need to install
221 // xslt2-transformer extension here
222 SAL_WARN("filter.xslt", "could not create XSLT 2.0 transformer");
223 throw;
227 // instantiation of XSLT 2.0 transformer service failed, or the
228 // filter does not need it
229 if (!xTransformer.is())
231 xTransformer = xslt::XSLTTransformer::create(
232 comphelper::getComponentContext(m_rServiceFactory), rArgs);
235 return xTransformer;
238 void
239 XSLTFilter::started() throw (RuntimeException)
241 osl_resetCondition(m_cTransformed);
243 void
244 XSLTFilter::error(const Any& a) throw (RuntimeException)
246 Exception e;
247 if (a >>= e)
249 rtl::OStringBuffer aMessage(RTL_CONSTASCII_STRINGPARAM("XSLTFilter::error was called: "));
250 aMessage.append(rtl::OUStringToOString(e.Message, RTL_TEXTENCODING_ASCII_US));
251 OSL_FAIL(aMessage.getStr());
253 m_bError = sal_True;
254 osl_setCondition(m_cTransformed);
256 void
257 XSLTFilter::closed() throw (RuntimeException)
259 osl_setCondition(m_cTransformed);
261 void
262 XSLTFilter::terminated() throw (RuntimeException)
264 m_bTerminated = sal_True;
265 osl_setCondition(m_cTransformed);
268 OUString
269 XSLTFilter::rel2abs(const OUString& s)
272 css::uno::Reference< css::uno::XComponentContext > xContext( comphelper::getComponentContext(m_rServiceFactory) );
273 css::uno::Reference<XStringSubstitution>
274 subs(css::util::PathSubstitution::create(xContext));
275 OUString aWorkingDir(subs->getSubstituteVariableValue(OUString( "$(progurl)")));
276 INetURLObject aObj(aWorkingDir);
277 aObj.setFinalSlash();
278 bool bWasAbsolute;
279 INetURLObject aURL = aObj.smartRel2Abs(s, bWasAbsolute, false,
280 INetURLObject::WAS_ENCODED, RTL_TEXTENCODING_UTF8, true);
281 return aURL.GetMainURL(INetURLObject::NO_DECODE);
284 sal_Bool
285 XSLTFilter::importer(const Sequence<PropertyValue>& aSourceData,
286 const css::uno::Reference<XDocumentHandler>& xHandler, const Sequence<
287 OUString>& msUserData) throw (RuntimeException)
289 if (msUserData.getLength() < 5)
290 return sal_False;
292 OUString udImport = msUserData[2];
293 OUString udStyleSheet = rel2abs(msUserData[4]);
295 // get information from media descriptor
296 // the imput stream that represents the imported file
297 // is most important here since we need to supply it to
298 // the sax parser that drives the supplied document handler
299 sal_Int32 nLength = aSourceData.getLength();
300 OUString aName, aFileName, aURL;
301 css::uno::Reference<XInputStream> xInputStream;
302 css::uno::Reference<XInteractionHandler> xInterActionHandler;
303 for (sal_Int32 i = 0; i < nLength; i++)
305 aName = aSourceData[i].Name;
306 Any value = aSourceData[i].Value;
307 if ( aName == "InputStream" )
308 value >>= xInputStream;
309 else if ( aName == "FileName" )
310 value >>= aFileName;
311 else if ( aName == "URL" )
312 value >>= aURL;
313 else if ( aName == "InteractionHandler" )
314 value >>= xInterActionHandler;
316 OSL_ASSERT(xInputStream.is());
317 if (!xInputStream.is())
318 return sal_False;
320 // create SAX parser that will read the document file
321 // and provide events to xHandler passed to this call
322 css::uno::Reference<XParser> xSaxParser = Parser::create(comphelper::getComponentContext(m_rServiceFactory));
324 // create transformer
325 Sequence<Any> args(3);
326 NamedValue nv;
328 nv.Name = OUString( "StylesheetURL" );
329 nv.Value <<= expandUrl(udStyleSheet);
330 args[0] <<= nv;
331 nv.Name = OUString( "SourceURL" );
332 nv.Value <<= aURL;
333 args[1] <<= nv;
334 nv.Name = OUString( "SourceBaseURL" );
335 nv.Value <<= OUString(INetURLObject(aURL).getBase());
336 args[2] <<= nv;
338 m_tcontrol = impl_createTransformer(msUserData[1], args);
340 OSL_ASSERT(xHandler.is());
341 OSL_ASSERT(xInputStream.is());
342 OSL_ASSERT(m_tcontrol.is());
343 if (xHandler.is() && xInputStream.is() && m_tcontrol.is())
347 // we want to be notfied when the processing is done...
348 m_tcontrol->addListener(css::uno::Reference<XStreamListener> (
349 this));
351 // connect input to transformer
352 css::uno::Reference<XActiveDataSink> tsink(m_tcontrol, UNO_QUERY);
353 tsink->setInputStream(xInputStream);
355 // create pipe
356 css::uno::Reference<XOutputStream> pipeout(
357 Pipe::create(comphelper::getComponentContext(m_rServiceFactory)),
358 UNO_QUERY);
359 css::uno::Reference<XInputStream> pipein(pipeout, UNO_QUERY);
361 //connect transformer to pipe
362 css::uno::Reference<XActiveDataSource> tsource(m_tcontrol,
363 UNO_QUERY);
364 tsource->setOutputStream(pipeout);
366 // connect pipe to sax parser
367 InputSource aInput;
368 aInput.sSystemId = aURL;
369 aInput.sPublicId = aURL;
370 aInput.aInputStream = pipein;
372 // set doc handler
373 xSaxParser->setDocumentHandler(xHandler);
375 // transform
376 m_tcontrol->start();
377 TimeValue timeout = { TRANSFORMATION_TIMEOUT_SEC, 0};
378 oslConditionResult result(osl_waitCondition(m_cTransformed, &timeout));
379 while (osl_cond_result_timeout == result) {
380 if (xInterActionHandler.is()) {
381 Sequence<Any> excArgs(0);
382 ::com::sun::star::ucb::InteractiveAugmentedIOException exc(
383 rtl::OUString("Timeout!"),
384 static_cast< OWeakObject * >( this ),
385 InteractionClassification_ERROR,
386 ::com::sun::star::ucb::IOErrorCode_GENERAL,
387 excArgs);
388 Any r;
389 r <<= exc;
390 ::comphelper::OInteractionRequest* pRequest = new ::comphelper::OInteractionRequest(r);
391 css::uno::Reference< XInteractionRequest > xRequest(pRequest);
392 ::comphelper::OInteractionRetry* pRetry = new ::comphelper::OInteractionRetry;
393 ::comphelper::OInteractionAbort* pAbort = new ::comphelper::OInteractionAbort;
394 pRequest->addContinuation(pRetry);
395 pRequest->addContinuation(pAbort);
396 xInterActionHandler->handle(xRequest);
397 if (pAbort->wasSelected()) {
398 m_bError = sal_True;
399 osl_setCondition(m_cTransformed);
402 result = osl_waitCondition(m_cTransformed, &timeout);
404 if (!m_bError) {
405 xSaxParser->parseStream(aInput);
407 m_tcontrol->terminate();
408 return !m_bError;
410 #if OSL_DEBUG_LEVEL > 0
411 catch( const Exception& exc)
412 #else
413 catch (const Exception&)
414 #endif
416 // something went wrong
417 OSL_FAIL(OUStringToOString(exc.Message, RTL_TEXTENCODING_ASCII_US).getStr());
418 return sal_False;
421 else
423 return sal_False;
427 sal_Bool
428 XSLTFilter::exporter(const Sequence<PropertyValue>& aSourceData,
429 const Sequence<OUString>& msUserData) throw (RuntimeException)
431 if (msUserData.getLength() < 6)
432 return sal_False;
434 // get interesting values from user data
435 OUString udImport = msUserData[2];
436 OUString udStyleSheet = rel2abs(msUserData[5]);
438 // read source data
439 // we are especialy interested in the output stream
440 // since that is where our xml-writer will push the data
441 // from it's data-source interface
442 OUString aName, sURL;
443 sal_Bool bIndent = sal_False;
444 OUString aDoctypePublic;
445 // css::uno::Reference<XOutputStream> rOutputStream;
446 sal_Int32 nLength = aSourceData.getLength();
447 for (sal_Int32 i = 0; i < nLength; i++)
449 aName = aSourceData[i].Name;
450 if ( aName == "Indent" )
451 aSourceData[i].Value >>= bIndent;
452 if ( aName == "DocType_Public" )
453 aSourceData[i].Value >>= aDoctypePublic;
454 if ( aName == "OutputStream" )
455 aSourceData[i].Value >>= m_rOutputStream;
456 else if ( aName == "URL" )
457 aSourceData[i].Value >>= sURL;
460 if (!getDelegate().is())
462 // get the document writer
463 setDelegate(css::uno::Reference<XExtendedDocumentHandler>(
464 Writer::create(comphelper::getComponentContext(m_rServiceFactory)),
465 UNO_QUERY_THROW));
468 // create transformer
469 Sequence<Any> args(4);
470 NamedValue nv;
471 nv.Name = OUString( "StylesheetURL" );
472 nv.Value <<= expandUrl(udStyleSheet);
473 args[0] <<= nv;
474 nv.Name = OUString( "TargetURL" );
475 nv.Value <<= sURL;
476 args[1] <<= nv;
477 nv.Name = OUString( "DoctypePublic" );
478 nv.Value <<= aDoctypePublic;
479 args[2] <<= nv;
480 nv.Name = OUString( "TargetBaseURL" );
481 INetURLObject ineturl(sURL);
482 ineturl.removeSegment();
483 m_aExportBaseUrl = ineturl.GetMainURL(INetURLObject::NO_DECODE);
484 nv.Value <<= m_aExportBaseUrl;
485 args[3] <<= nv;
487 m_tcontrol = impl_createTransformer(msUserData[1], args);
489 OSL_ASSERT(m_rOutputStream.is());
490 OSL_ASSERT(m_tcontrol.is());
491 if (m_tcontrol.is() && m_rOutputStream.is())
493 // we want to be notfied when the processing is done...
494 m_tcontrol->addListener(css::uno::Reference<XStreamListener> (this));
496 // create pipe
497 css::uno::Reference<XOutputStream> pipeout(
498 Pipe::create(comphelper::getComponentContext(m_rServiceFactory)),
499 UNO_QUERY);
500 css::uno::Reference<XInputStream> pipein(pipeout, UNO_QUERY);
502 // connect sax writer to pipe
503 css::uno::Reference<XActiveDataSource> xmlsource(getDelegate(),
504 UNO_QUERY);
505 xmlsource->setOutputStream(pipeout);
507 // connect pipe to transformer
508 css::uno::Reference<XActiveDataSink> tsink(m_tcontrol, UNO_QUERY);
509 tsink->setInputStream(pipein);
511 // connect transformer to output
512 css::uno::Reference<XActiveDataSource> tsource(m_tcontrol, UNO_QUERY);
513 tsource->setOutputStream(m_rOutputStream);
515 // we will start receiving events after returning 'true'.
516 // we will start the transformation as soon as we receive the startDocument
517 // event.
518 return sal_True;
520 else
522 return sal_False;
526 // for the DocumentHandler implementation, we just proxy the the
527 // events to the XML writer that we created upon the output stream
528 // that was provided by the XMLFilterAdapter
529 void
530 XSLTFilter::startDocument() throw (SAXException, RuntimeException)
532 ExtendedDocumentHandlerAdapter::startDocument();
533 m_tcontrol->start();
536 void
537 XSLTFilter::endDocument() throw (SAXException, RuntimeException)
539 ExtendedDocumentHandlerAdapter::endDocument();
540 // wait for the transformer to finish
541 osl_waitCondition(m_cTransformed, 0);
542 m_tcontrol->terminate();
543 if (!m_bError && !m_bTerminated)
545 return;
547 else
549 throw RuntimeException();
555 // --------------------------------------
556 // Component management
557 // --------------------------------------
558 #define FILTER_SERVICE_NAME "com.sun.star.documentconversion.XSLTFilter"
559 #define FILTER_IMPL_NAME "com.sun.star.comp.documentconversion.XSLTFilter"
560 #define TRANSFORMER_SERVICE_NAME "com.sun.star.xml.xslt.XSLTTransformer"
561 #define TRANSFORMER_IMPL_NAME "com.sun.star.comp.documentconversion.LibXSLTTransformer"
563 static css::uno::Reference<XInterface> SAL_CALL
564 CreateTransformerInstance(const css::uno::Reference<XMultiServiceFactory> &r)
566 return css::uno::Reference<XInterface> ((OWeakObject *) new LibXSLTTransformer(r));
569 static css::uno::Reference<XInterface> SAL_CALL
570 CreateFilterInstance(const css::uno::Reference<XMultiServiceFactory> &r)
572 return css::uno::Reference<XInterface> ((OWeakObject *) new XSLTFilter(r));
577 using namespace XSLT;
579 extern "C"
581 SAL_DLLPUBLIC_EXPORT void * SAL_CALL xsltfilter_component_getFactory(const sal_Char * pImplName,
582 void * pServiceManager, void * /* pRegistryKey */)
584 void * pRet = 0;
586 if (pServiceManager)
588 if (rtl_str_compare(pImplName, FILTER_IMPL_NAME) == 0)
590 Sequence<OUString> serviceNames(1);
591 serviceNames.getArray()[0]
592 = OUString(
593 FILTER_SERVICE_NAME );
595 css::uno::Reference<XSingleServiceFactory>
596 xFactory(
597 createSingleFactory(
598 reinterpret_cast<XMultiServiceFactory *> (pServiceManager),
599 OUString::createFromAscii(
600 pImplName),
601 CreateFilterInstance,
602 serviceNames));
604 if (xFactory.is())
606 xFactory->acquire();
607 pRet = xFactory.get();
610 else if (rtl_str_compare(pImplName, TRANSFORMER_IMPL_NAME) == 0)
612 Sequence<OUString> serviceNames(1);
613 serviceNames.getArray()[0]
614 = OUString(
615 TRANSFORMER_SERVICE_NAME );
616 css::uno::Reference<XSingleServiceFactory>
617 xFactory(
618 createSingleFactory(
619 reinterpret_cast<XMultiServiceFactory *> (pServiceManager),
620 OUString::createFromAscii(
621 pImplName),
622 CreateTransformerInstance,
623 serviceNames));
625 if (xFactory.is())
627 xFactory->acquire();
628 pRet = xFactory.get();
633 return pRet;
636 } // extern "C"
638 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */