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 : * 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 "unoservices.hxx"
23 : #include "xmlbas_export.hxx"
24 : #include <sal/log.hxx>
25 : #include <xmlscript/xmlns.h>
26 : #include <xmlscript/xml_helper.hxx>
27 : #include <com/sun/star/beans/XPropertySet.hpp>
28 : #include <com/sun/star/lang/XMultiComponentFactory.hpp>
29 : #include <com/sun/star/script/XLibraryContainer2.hpp>
30 : #include <com/sun/star/script/XLibraryContainerPassword.hpp>
31 : #include <com/sun/star/document/XEmbeddedScripts.hpp>
32 : #include <cppuhelper/implementationentry.hxx>
33 : #include <cppuhelper/supportsservice.hxx>
34 :
35 : using namespace ::com::sun::star;
36 : using namespace ::com::sun::star::lang;
37 : using namespace ::com::sun::star::uno;
38 :
39 : namespace xmlscript
40 : {
41 :
42 : // component operations
43 :
44 7 : OUString getImplementationName_XMLBasicExporter()
45 : {
46 7 : return OUString( "com.sun.star.comp.xmlscript.XMLBasicExporter" );
47 : }
48 :
49 0 : Sequence< OUString > getSupportedServiceNames_XMLBasicExporter()
50 : {
51 0 : Sequence< OUString > aNames(1);
52 0 : aNames.getArray()[0] = "com.sun.star.document.XMLBasicExporter";
53 0 : return aNames;
54 : }
55 :
56 7 : OUString getImplementationName_XMLOasisBasicExporter()
57 : {
58 7 : return OUString( "com.sun.star.comp.xmlscript.XMLOasisBasicExporter" );
59 : }
60 :
61 2 : Sequence< OUString > getSupportedServiceNames_XMLOasisBasicExporter()
62 : {
63 2 : Sequence< OUString > aNames(1);
64 2 : aNames.getArray()[0] = "com.sun.star.document.XMLOasisBasicExporter";
65 2 : return aNames;
66 : }
67 :
68 : // XMLBasicExporterBase
69 :
70 4 : XMLBasicExporterBase::XMLBasicExporterBase( const Reference< XComponentContext >& rxContext, bool bOasis )
71 : :m_xContext( rxContext )
72 4 : ,m_bOasis( bOasis )
73 : {
74 4 : }
75 :
76 4 : XMLBasicExporterBase::~XMLBasicExporterBase()
77 : {
78 4 : }
79 :
80 : // XServiceInfo
81 0 : sal_Bool XMLBasicExporterBase::supportsService( const OUString& rServiceName ) throw (RuntimeException, std::exception)
82 : {
83 0 : return cppu::supportsService(this, rServiceName);
84 : }
85 :
86 : // XInitialization
87 :
88 4 : void XMLBasicExporterBase::initialize( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException, std::exception)
89 : {
90 4 : ::osl::MutexGuard aGuard( m_aMutex );
91 :
92 4 : if ( aArguments.getLength() == 1 )
93 : {
94 4 : aArguments[0] >>= m_xHandler;
95 :
96 4 : if ( !m_xHandler.is() )
97 : {
98 0 : throw RuntimeException( "XMLBasicExporterBase::initialize: invalid argument format!" );
99 : }
100 : }
101 : else
102 : {
103 0 : throw RuntimeException( "XMLBasicExporterBase::initialize: invalid number of arguments!" );
104 4 : }
105 4 : }
106 :
107 : // XExporter
108 :
109 4 : void XMLBasicExporterBase::setSourceDocument( const Reference< XComponent >& rxDoc )
110 : throw (IllegalArgumentException, RuntimeException, std::exception)
111 : {
112 4 : ::osl::MutexGuard aGuard( m_aMutex );
113 :
114 4 : m_xModel.set( rxDoc, UNO_QUERY );
115 :
116 4 : if ( !m_xModel.is() )
117 : {
118 0 : throw IllegalArgumentException( "XMLBasicExporter::setSourceDocument: no document model!", Reference< XInterface >(), 1 );
119 4 : }
120 4 : }
121 :
122 : // XFilter
123 :
124 4 : sal_Bool XMLBasicExporterBase::filter( const Sequence< beans::PropertyValue >& /*aDescriptor*/ )
125 : throw (RuntimeException, std::exception)
126 : {
127 4 : ::osl::MutexGuard aGuard( m_aMutex );
128 :
129 4 : bool bReturn = true;
130 :
131 : try
132 : {
133 4 : if ( m_xHandler.is() )
134 : {
135 4 : m_xHandler->startDocument();
136 :
137 : // ooo/script prefix and URI
138 4 : OUString aPrefix;
139 8 : OUString aURI;
140 4 : if ( m_bOasis )
141 : {
142 4 : aPrefix = XMLNS_OOO_PREFIX;
143 4 : aURI = XMLNS_OOO_URI;
144 : }
145 : else
146 : {
147 0 : aPrefix = XMLNS_SCRIPT_PREFIX;
148 0 : aURI = XMLNS_SCRIPT_URI;
149 : }
150 :
151 : // ooo/script:libraries element
152 8 : OUString aLibContElementName( aPrefix );
153 4 : aLibContElementName += ":libraries";
154 4 : XMLElement* pLibContElement = new XMLElement( aLibContElementName );
155 8 : Reference< xml::sax::XAttributeList > xLibContAttribs( pLibContElement );
156 :
157 : // ooo/script namespace attribute
158 4 : pLibContElement->addAttribute( "xmlns:" + aPrefix, aURI );
159 :
160 : // xlink namespace attribute
161 4 : pLibContElement->addAttribute( "xmlns:" XMLNS_XLINK_PREFIX, XMLNS_XLINK_URI );
162 :
163 : // <ooo/script:libraries...
164 4 : m_xHandler->ignorableWhitespace( OUString() );
165 4 : m_xHandler->startElement( aLibContElementName, xLibContAttribs );
166 :
167 8 : Reference< script::XLibraryContainer2 > xLibContainer;
168 :
169 : // try the XEmbeddedScripts interface
170 8 : Reference< document::XEmbeddedScripts > xDocumentScripts( m_xModel, UNO_QUERY );
171 4 : if ( xDocumentScripts.is() )
172 4 : xLibContainer.set( xDocumentScripts->getBasicLibraries().get() );
173 :
174 4 : if ( !xLibContainer.is() )
175 : {
176 : // try the "BasicLibraries" property (old-style, for compatibility)
177 0 : Reference< beans::XPropertySet > xPSet( m_xModel, UNO_QUERY );
178 0 : if ( xPSet.is() )
179 0 : xPSet->getPropertyValue("BasicLibraries" ) >>= xLibContainer;
180 : }
181 :
182 : SAL_WARN_IF( !xLibContainer.is(), "xmlscript.xmlflat", "XMLBasicExporterBase::filter: nowhere to export to!" );
183 :
184 4 : if ( xLibContainer.is() )
185 : {
186 4 : Sequence< OUString > aLibNames = xLibContainer->getElementNames();
187 4 : sal_Int32 nLibCount = aLibNames.getLength();
188 4 : const OUString* pLibNames = aLibNames.getConstArray();
189 6 : for ( sal_Int32 i = 0 ; i < nLibCount ; ++i )
190 : {
191 2 : OUString aLibName( pLibNames[i] );
192 :
193 2 : if ( xLibContainer->hasByName( aLibName ) )
194 : {
195 2 : OUString aTrueStr( "true" );
196 :
197 2 : if ( xLibContainer->isLibraryLink( aLibName ) )
198 : {
199 : // ooo/script:library-linked element
200 0 : OUString aLibElementName( aPrefix );
201 0 : aLibElementName += ":library-linked";
202 0 : XMLElement* pLibElement = new XMLElement( aLibElementName );
203 0 : Reference< xml::sax::XAttributeList > xLibAttribs;
204 0 : xLibAttribs = static_cast< xml::sax::XAttributeList* >( pLibElement );
205 :
206 : // ooo/script:name attribute
207 0 : pLibElement->addAttribute( aPrefix + ":name", aLibName );
208 :
209 0 : OUString aLinkURL( xLibContainer->getLibraryLinkURL( aLibName ) );
210 0 : if ( !aLinkURL.isEmpty() )
211 : {
212 : // xlink:href attribute
213 0 : pLibElement->addAttribute( XMLNS_XLINK_PREFIX ":href", aLinkURL );
214 :
215 : // xlink:type attribute
216 0 : pLibElement->addAttribute( XMLNS_XLINK_PREFIX ":type", "simple" );
217 : }
218 :
219 0 : if ( xLibContainer->isLibraryReadOnly( aLibName ) )
220 : {
221 : // ooo/script:readonly attribute
222 0 : pLibElement->addAttribute( aPrefix + ":readonly", aTrueStr );
223 : }
224 :
225 : // <ooo/script:library-linked...
226 0 : m_xHandler->ignorableWhitespace( OUString() );
227 0 : m_xHandler->startElement( aLibElementName, xLibAttribs );
228 :
229 : // ...ooo/script:library-linked>
230 0 : m_xHandler->ignorableWhitespace( OUString() );
231 0 : m_xHandler->endElement( aLibElementName );
232 : }
233 : else
234 : {
235 : // ooo/script:library-embedded element
236 2 : OUString aLibElementName( aPrefix );
237 2 : aLibElementName += ":library-embedded";
238 2 : XMLElement* pLibElement = new XMLElement( aLibElementName );
239 4 : Reference< xml::sax::XAttributeList > xLibAttribs;
240 2 : xLibAttribs = static_cast< xml::sax::XAttributeList* >( pLibElement );
241 :
242 : // ooo/script:name attribute
243 2 : pLibElement->addAttribute( aPrefix + ":name", aLibName );
244 :
245 2 : if ( xLibContainer->isLibraryReadOnly( aLibName ) )
246 : {
247 : // ooo/script:readonly attribute
248 0 : pLibElement->addAttribute( aPrefix + ":readonly", aTrueStr );
249 : }
250 :
251 : // TODO: password protected libraries
252 4 : Reference< script::XLibraryContainerPassword > xPasswd( xLibContainer, UNO_QUERY );
253 2 : if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aLibName ) )
254 0 : continue;
255 :
256 : // <ooo/script:library-embedded...
257 2 : m_xHandler->ignorableWhitespace( OUString() );
258 2 : m_xHandler->startElement( aLibElementName, xLibAttribs );
259 :
260 2 : if ( !xLibContainer->isLibraryLoaded( aLibName ) )
261 0 : xLibContainer->loadLibrary( aLibName );
262 :
263 4 : Reference< container::XNameContainer > xLib;
264 2 : xLibContainer->getByName( aLibName ) >>= xLib;
265 :
266 2 : if ( xLib.is() )
267 : {
268 2 : Sequence< OUString > aModNames = xLib->getElementNames();
269 2 : sal_Int32 nModCount = aModNames.getLength();
270 2 : const OUString* pModNames = aModNames.getConstArray();
271 2 : for ( sal_Int32 j = 0 ; j < nModCount ; ++j )
272 : {
273 0 : OUString aModName( pModNames[j] );
274 0 : if ( xLib->hasByName( aModName ) )
275 : {
276 : // ooo/script:module element
277 0 : OUString aModElementName( aPrefix );
278 0 : aModElementName += ":module";
279 0 : XMLElement* pModElement = new XMLElement( aModElementName );
280 0 : Reference< xml::sax::XAttributeList > xModAttribs;
281 0 : xModAttribs = static_cast< xml::sax::XAttributeList* >( pModElement );
282 :
283 : // ooo/script:name attribute
284 0 : pModElement->addAttribute( aPrefix + ":name", aModName );
285 :
286 : // <ooo/script:module...
287 0 : m_xHandler->ignorableWhitespace( OUString() );
288 0 : m_xHandler->startElement( aModElementName, xModAttribs );
289 :
290 : // ooo/script:source-code element
291 0 : OUString aSourceElementName( aPrefix );
292 0 : aSourceElementName += ":source-code";
293 0 : XMLElement* pSourceElement = new XMLElement( aSourceElementName );
294 0 : Reference< xml::sax::XAttributeList > xSourceAttribs;
295 0 : xSourceAttribs = static_cast< xml::sax::XAttributeList* >( pSourceElement );
296 :
297 : // <ooo/script:source-code...
298 0 : m_xHandler->ignorableWhitespace( OUString() );
299 0 : m_xHandler->startElement( aSourceElementName, xSourceAttribs );
300 :
301 : // module data
302 : // TODO: write encrypted data for password protected libraries
303 0 : OUString aSource;
304 0 : xLib->getByName( aModName ) >>= aSource;
305 0 : m_xHandler->characters( aSource );
306 :
307 : // TODO: <ooo/script:byte-code>
308 :
309 : // ...ooo/script:source-code>
310 0 : m_xHandler->ignorableWhitespace( OUString() );
311 0 : m_xHandler->endElement( aSourceElementName );
312 :
313 : // ...ooo/script:module>
314 0 : m_xHandler->ignorableWhitespace( OUString() );
315 0 : m_xHandler->endElement( aModElementName );
316 : }
317 2 : }
318 : }
319 :
320 : // ...ooo/script:library-embedded>
321 2 : m_xHandler->ignorableWhitespace( OUString() );
322 4 : m_xHandler->endElement( aLibElementName );
323 2 : }
324 : }
325 6 : }
326 : }
327 :
328 : // ...ooo/script:libraries>
329 4 : m_xHandler->ignorableWhitespace( OUString() );
330 4 : m_xHandler->endElement( aLibContElementName );
331 :
332 8 : m_xHandler->endDocument();
333 : }
334 : }
335 0 : catch ( const container::NoSuchElementException& e )
336 : {
337 : SAL_INFO("xmlscript.xmlflat", "XMLBasicExporterBase::filter: caught NoSuchElementException reason " << e.Message );
338 0 : bReturn = false;
339 : }
340 0 : catch ( const lang::IllegalArgumentException& e )
341 : {
342 : SAL_INFO("xmlscript.xmlflat", "XMLBasicExporterBase::filter: caught IllegalArgumentException reason " << e.Message );
343 0 : bReturn = false;
344 : }
345 0 : catch ( const lang::WrappedTargetException& e )
346 : {
347 : SAL_INFO("xmlscript.xmlflat", "XMLBasicExporterBase::filter: caught WrappedTargetException reason " << e.Message );
348 0 : bReturn = false;
349 : }
350 0 : catch ( const xml::sax::SAXException& e )
351 : {
352 : SAL_INFO("xmlscript.xmlflat", "XMLBasicExporterBase::filter: caught SAXException reason " << e.Message );
353 0 : bReturn = false;
354 : }
355 :
356 4 : return bReturn;
357 : }
358 :
359 0 : void XMLBasicExporterBase::cancel()
360 : throw (RuntimeException, std::exception)
361 : {
362 0 : ::osl::MutexGuard aGuard( m_aMutex );
363 :
364 : // cancel export
365 0 : }
366 :
367 : // XMLBasicExporter
368 :
369 0 : XMLBasicExporter::XMLBasicExporter( const Reference< XComponentContext >& rxContext )
370 0 : :XMLBasicExporterBase( rxContext, false )
371 : {
372 0 : }
373 :
374 0 : XMLBasicExporter::~XMLBasicExporter()
375 : {
376 0 : }
377 :
378 : // XServiceInfo
379 :
380 0 : OUString XMLBasicExporter::getImplementationName( ) throw (RuntimeException, std::exception)
381 : {
382 0 : return getImplementationName_XMLBasicExporter();
383 : }
384 :
385 0 : Sequence< OUString > XMLBasicExporter::getSupportedServiceNames( ) throw (RuntimeException, std::exception)
386 : {
387 0 : return getSupportedServiceNames_XMLBasicExporter();
388 : }
389 :
390 : // XMLOasisBasicExporter
391 :
392 4 : XMLOasisBasicExporter::XMLOasisBasicExporter( const Reference< XComponentContext >& rxContext )
393 4 : :XMLBasicExporterBase( rxContext, true )
394 : {
395 4 : }
396 :
397 8 : XMLOasisBasicExporter::~XMLOasisBasicExporter()
398 : {
399 8 : }
400 :
401 : // XServiceInfo
402 :
403 0 : OUString XMLOasisBasicExporter::getImplementationName( ) throw (RuntimeException, std::exception)
404 : {
405 0 : return getImplementationName_XMLOasisBasicExporter();
406 : }
407 :
408 0 : Sequence< OUString > XMLOasisBasicExporter::getSupportedServiceNames( ) throw (RuntimeException, std::exception)
409 : {
410 0 : return getSupportedServiceNames_XMLOasisBasicExporter();
411 : }
412 :
413 : // component operations
414 :
415 0 : Reference< XInterface > SAL_CALL create_XMLBasicExporter(
416 : Reference< XComponentContext > const & xContext )
417 : {
418 0 : return static_cast< lang::XTypeProvider * >( new XMLBasicExporter( xContext ) );
419 : }
420 :
421 4 : Reference< XInterface > SAL_CALL create_XMLOasisBasicExporter(
422 : Reference< XComponentContext > const & xContext )
423 : {
424 4 : return static_cast< lang::XTypeProvider * >( new XMLOasisBasicExporter( xContext ) );
425 : }
426 :
427 : } // namespace xmlscript
428 :
429 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|