LibreOffice Module xmlscript (master) 1
xmlbas_export.cxx
Go to the documentation of this file.
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 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include "xmlbas_export.hxx"
23#include <sal/log.hxx>
24#include <xmlscript/xmlns.h>
26#include <com/sun/star/beans/XPropertySet.hpp>
27#include <com/sun/star/script/XLibraryContainer2.hpp>
28#include <com/sun/star/script/XLibraryContainerPassword.hpp>
29#include <com/sun/star/document/XEmbeddedScripts.hpp>
30#include <com/sun/star/uno/XComponentContext.hpp>
31#include <com/sun/star/xml/sax/SAXException.hpp>
34#include <rtl/ref.hxx>
35
36using namespace ::com::sun::star;
37using namespace ::com::sun::star::lang;
38using namespace ::com::sun::star::uno;
39
40namespace xmlscript
41{
42 // XMLBasicExporterBase
43
45 :m_bOasis( bOasis )
46 {
47 }
48
50 {
51 }
52
53 // XServiceInfo
54 sal_Bool XMLBasicExporterBase::supportsService( const OUString& rServiceName )
55 {
56 return cppu::supportsService(this, rServiceName);
57 }
58
59 // XInitialization
60
61 void XMLBasicExporterBase::initialize( const Sequence< Any >& aArguments )
62 {
63 std::scoped_lock aGuard( m_aMutex );
64
65 if ( aArguments.getLength() != 1 )
66 {
67 throw RuntimeException( "XMLBasicExporterBase::initialize: invalid number of arguments!" );
68 }
69
71
72 if ( !m_xHandler.is() )
73 {
74 throw RuntimeException( "XMLBasicExporterBase::initialize: invalid argument format!" );
75 }
76 }
77
78 // XExporter
79
80 void XMLBasicExporterBase::setSourceDocument( const Reference< XComponent >& rxDoc )
81 {
82 std::scoped_lock aGuard( m_aMutex );
83
84 m_xModel.set( rxDoc, UNO_QUERY );
85
86 if ( !m_xModel.is() )
87 {
88 throw IllegalArgumentException( "XMLBasicExporter::setSourceDocument: no document model!", Reference< XInterface >(), 1 );
89 }
90 }
91
92 // XFilter
93
94sal_Bool XMLBasicExporterBase::filter( const Sequence< beans::PropertyValue >& /*aDescriptor*/ )
95 {
96 std::scoped_lock aGuard( m_aMutex );
97
98 bool bReturn = true;
99
100 try
101 {
102 if ( m_xHandler.is() )
103 {
104 m_xHandler->startDocument();
105
106 // ooo/script prefix and URI
107 OUString aPrefix;
108 OUString aURI;
109 if ( m_bOasis )
110 {
111 aPrefix = XMLNS_OOO_PREFIX;
112 aURI = XMLNS_OOO_URI;
113 }
114 else
115 {
116 aPrefix = XMLNS_SCRIPT_PREFIX;
117 aURI = XMLNS_SCRIPT_URI;
118 }
119
120 // ooo/script:libraries element
121 OUString aLibContElementName = aPrefix + ":libraries";
122 rtl::Reference<XMLElement> pLibContElement = new XMLElement( aLibContElementName );
123
124 // ooo/script namespace attribute
125 pLibContElement->addAttribute( "xmlns:" + aPrefix, aURI );
126
127 // xlink namespace attribute
128 pLibContElement->addAttribute( "xmlns:" XMLNS_XLINK_PREFIX, XMLNS_XLINK_URI );
129
130 // <ooo/script:libraries...
131 m_xHandler->ignorableWhitespace( OUString() );
132 m_xHandler->startElement( aLibContElementName, pLibContElement );
133
134 Reference< script::XLibraryContainer2 > xLibContainer;
135
136 // try the XEmbeddedScripts interface
137 Reference< document::XEmbeddedScripts > xDocumentScripts( m_xModel, UNO_QUERY );
138 if ( xDocumentScripts.is() )
139 xLibContainer = xDocumentScripts->getBasicLibraries();
140
141 if ( !xLibContainer.is() )
142 {
143 // try the "BasicLibraries" property (old-style, for compatibility)
144 Reference< beans::XPropertySet > xPSet( m_xModel, UNO_QUERY );
145 if ( xPSet.is() )
146 xPSet->getPropertyValue("BasicLibraries" ) >>= xLibContainer;
147 }
148
149 SAL_WARN_IF( !xLibContainer.is(), "xmlscript.xmlflat", "XMLBasicExporterBase::filter: nowhere to export to!" );
150
151 if ( xLibContainer.is() )
152 {
153 const Sequence< OUString > aLibNames = xLibContainer->getElementNames();
154 for ( const OUString& rLibName : aLibNames )
155 {
156 if ( xLibContainer->hasByName( rLibName ) )
157 {
158 OUString aTrueStr( "true" );
159
160 if ( xLibContainer->isLibraryLink( rLibName ) )
161 {
162 // ooo/script:library-linked element
163 OUString aLibElementName = aPrefix + ":library-linked";
164 rtl::Reference<XMLElement> pLibElement = new XMLElement( aLibElementName );
165
166 // ooo/script:name attribute
167 pLibElement->addAttribute( aPrefix + ":name", rLibName );
168
169 OUString aLinkURL( xLibContainer->getLibraryLinkURL( rLibName ) );
170 if ( !aLinkURL.isEmpty() )
171 {
172 // xlink:href attribute
173 pLibElement->addAttribute( XMLNS_XLINK_PREFIX ":href", aLinkURL );
174
175 // xlink:type attribute
176 pLibElement->addAttribute( XMLNS_XLINK_PREFIX ":type", "simple" );
177 }
178
179 if ( xLibContainer->isLibraryReadOnly( rLibName ) )
180 {
181 // ooo/script:readonly attribute
182 pLibElement->addAttribute( aPrefix + ":readonly", aTrueStr );
183 }
184
185 // <ooo/script:library-linked...
186 m_xHandler->ignorableWhitespace( OUString() );
187 m_xHandler->startElement( aLibElementName, pLibElement );
188
189 // ...ooo/script:library-linked>
190 m_xHandler->ignorableWhitespace( OUString() );
191 m_xHandler->endElement( aLibElementName );
192 }
193 else
194 {
195 // ooo/script:library-embedded element
196 OUString aLibElementName = aPrefix + ":library-embedded";
197 rtl::Reference<XMLElement> pLibElement = new XMLElement( aLibElementName );
198
199 // ooo/script:name attribute
200 pLibElement->addAttribute( aPrefix + ":name", rLibName );
201
202 if ( xLibContainer->isLibraryReadOnly( rLibName ) )
203 {
204 // ooo/script:readonly attribute
205 pLibElement->addAttribute( aPrefix + ":readonly", aTrueStr );
206 }
207
208 // TODO: password protected libraries
209 Reference< script::XLibraryContainerPassword > xPasswd( xLibContainer, UNO_QUERY );
210 if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( rLibName ) )
211 continue;
212
213 // <ooo/script:library-embedded...
214 m_xHandler->ignorableWhitespace( OUString() );
215 m_xHandler->startElement( aLibElementName, pLibElement );
216
217 if ( !xLibContainer->isLibraryLoaded( rLibName ) )
218 xLibContainer->loadLibrary( rLibName );
219
220 Reference< container::XNameContainer > xLib;
221 xLibContainer->getByName( rLibName ) >>= xLib;
222
223 if ( xLib.is() )
224 {
225 const Sequence< OUString > aModNames = xLib->getElementNames();
226 for ( const OUString& rModName : aModNames )
227 {
228 if ( xLib->hasByName( rModName ) )
229 {
230 // ooo/script:module element
231 OUString aModElementName = aPrefix + ":module";
232 rtl::Reference<XMLElement> pModElement = new XMLElement( aModElementName );
233
234 // ooo/script:name attribute
235 pModElement->addAttribute( aPrefix + ":name", rModName );
236
237 // <ooo/script:module...
238 m_xHandler->ignorableWhitespace( OUString() );
239 m_xHandler->startElement( aModElementName, pModElement );
240
241 // ooo/script:source-code element
242 OUString aSourceElementName = aPrefix + ":source-code";
243 rtl::Reference<XMLElement> pSourceElement = new XMLElement( aSourceElementName );
244
245 // <ooo/script:source-code...
246 m_xHandler->ignorableWhitespace( OUString() );
247 m_xHandler->startElement( aSourceElementName, pSourceElement );
248
249 // module data
250 // TODO: write encrypted data for password protected libraries
251 OUString aSource;
252 xLib->getByName( rModName ) >>= aSource;
253 m_xHandler->characters( aSource );
254
255 // TODO: <ooo/script:byte-code>
256
257 // ...ooo/script:source-code>
258 m_xHandler->ignorableWhitespace( OUString() );
259 m_xHandler->endElement( aSourceElementName );
260
261 // ...ooo/script:module>
262 m_xHandler->ignorableWhitespace( OUString() );
263 m_xHandler->endElement( aModElementName );
264 }
265 }
266 }
267
268 // ...ooo/script:library-embedded>
269 m_xHandler->ignorableWhitespace( OUString() );
270 m_xHandler->endElement( aLibElementName );
271 }
272 }
273 }
274 }
275
276 // ...ooo/script:libraries>
277 m_xHandler->ignorableWhitespace( OUString() );
278 m_xHandler->endElement( aLibContElementName );
279
280 m_xHandler->endDocument();
281 }
282 }
283 catch ( const container::NoSuchElementException& )
284 {
285 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "XMLBasicExporterBase::filter" );
286 bReturn = false;
287 }
288 catch ( const lang::IllegalArgumentException& )
289 {
290 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "XMLBasicExporterBase::filter" );
291 bReturn = false;
292 }
293 catch ( const lang::WrappedTargetException& )
294 {
295 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "XMLBasicExporterBase::filter:" );
296 bReturn = false;
297 }
298 catch ( const xml::sax::SAXException& )
299 {
300 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "XMLBasicExporterBase::filter:" );
301 bReturn = false;
302 }
303
304 return bReturn;
305 }
306
308 {
309 // cancel export
310 }
311
312 // XMLBasicExporter
313
315 :XMLBasicExporterBase( false )
316 {
317 }
318
320 {
321 }
322
323 // XServiceInfo
324
326 {
327 return "com.sun.star.comp.xmlscript.XMLBasicExporter";
328 }
329
331 {
332 return { "com.sun.star.document.XMLBasicExporter" };
333 }
334
335 // XMLOasisBasicExporter
336
338 :XMLBasicExporterBase( true )
339 {
340 }
341
343 {
344 }
345
346 // XServiceInfo
347
349 {
350 return "com.sun.star.comp.xmlscript.XMLOasisBasicExporter";
351 }
352
354 {
355 return { "com.sun.star.document.XMLOasisBasicExporter" };
356 }
357
358} // namespace xmlscript
359
360extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
362 css::uno::XComponentContext *,
363 css::uno::Sequence<css::uno::Any> const &)
364{
365 return cppu::acquire(new xmlscript::XMLBasicExporter());
366}
367
368extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
370 css::uno::XComponentContext *,
371 css::uno::Sequence<css::uno::Any> const &)
372{
373
374 return cppu::acquire(new xmlscript::XMLOasisBasicExporter());
375}
376
377/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual ~XMLBasicExporterBase() override
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &aArguments) override
virtual sal_Bool SAL_CALL filter(const css::uno::Sequence< css::beans::PropertyValue > &aDescriptor) override
virtual void SAL_CALL cancel() override
css::uno::Reference< css::frame::XModel > m_xModel
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
css::uno::Reference< css::xml::sax::XDocumentHandler > m_xHandler
virtual void SAL_CALL setSourceDocument(const css::uno::Reference< css::lang::XComponent > &rxDoc) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual OUString SAL_CALL getImplementationName() override
virtual ~XMLBasicExporter() override
virtual ~XMLOasisBasicExporter() override
virtual OUString SAL_CALL getImplementationName() override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
#define TOOLS_INFO_EXCEPTION(area, stream)
Sequence< PropertyValue > aArguments
#define SAL_WARN_IF(condition, area, stream)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
constexpr OUStringLiteral aTrueStr
unsigned char sal_Bool
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_xmlscript_XMLOasisBasicExporter(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_xmlscript_XMLBasicExporter(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
#define XMLNS_SCRIPT_PREFIX
Definition: xmlns.h:27
constexpr OUStringLiteral XMLNS_XLINK_URI
Definition: xmlns.h:35
#define XMLNS_XLINK_PREFIX
Definition: xmlns.h:36
constexpr OUStringLiteral XMLNS_SCRIPT_URI
Definition: xmlns.h:26
constexpr OUStringLiteral XMLNS_OOO_PREFIX
Definition: xmlns.h:30
constexpr OUStringLiteral XMLNS_OOO_URI
Definition: xmlns.h:29