1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <sal/config.h>
22 #include "xmlbas_export.hxx"
23 #include <sal/log.hxx>
24 #include <xmlscript/xmlns.h>
25 #include <xmlscript/xml_helper.hxx>
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>
32 #include <cppuhelper/supportsservice.hxx>
33 #include <tools/diagnose_ex.h>
35 using namespace ::com::sun::star
;
36 using namespace ::com::sun::star::lang
;
37 using namespace ::com::sun::star::uno
;
41 // XMLBasicExporterBase
43 XMLBasicExporterBase::XMLBasicExporterBase( bool bOasis
)
48 XMLBasicExporterBase::~XMLBasicExporterBase()
53 sal_Bool
XMLBasicExporterBase::supportsService( const OUString
& rServiceName
)
55 return cppu::supportsService(this, rServiceName
);
60 void XMLBasicExporterBase::initialize( const Sequence
< Any
>& aArguments
)
62 ::osl::MutexGuard
aGuard( m_aMutex
);
64 if ( aArguments
.getLength() != 1 )
66 throw RuntimeException( "XMLBasicExporterBase::initialize: invalid number of arguments!" );
69 aArguments
[0] >>= m_xHandler
;
71 if ( !m_xHandler
.is() )
73 throw RuntimeException( "XMLBasicExporterBase::initialize: invalid argument format!" );
79 void XMLBasicExporterBase::setSourceDocument( const Reference
< XComponent
>& rxDoc
)
81 ::osl::MutexGuard
aGuard( m_aMutex
);
83 m_xModel
.set( rxDoc
, UNO_QUERY
);
87 throw IllegalArgumentException( "XMLBasicExporter::setSourceDocument: no document model!", Reference
< XInterface
>(), 1 );
93 sal_Bool
XMLBasicExporterBase::filter( const Sequence
< beans::PropertyValue
>& /*aDescriptor*/ )
95 ::osl::MutexGuard
aGuard( m_aMutex
);
101 if ( m_xHandler
.is() )
103 m_xHandler
->startDocument();
105 // ooo/script prefix and URI
110 aPrefix
= XMLNS_OOO_PREFIX
;
111 aURI
= XMLNS_OOO_URI
;
115 aPrefix
= XMLNS_SCRIPT_PREFIX
;
116 aURI
= XMLNS_SCRIPT_URI
;
119 // ooo/script:libraries element
120 OUString aLibContElementName
= aPrefix
+ ":libraries";
121 XMLElement
* pLibContElement
= new XMLElement( aLibContElementName
);
122 Reference
< xml::sax::XAttributeList
> xLibContAttribs( pLibContElement
);
124 // ooo/script namespace attribute
125 pLibContElement
->addAttribute( "xmlns:" + aPrefix
, aURI
);
127 // xlink namespace attribute
128 pLibContElement
->addAttribute( "xmlns:" XMLNS_XLINK_PREFIX
, XMLNS_XLINK_URI
);
130 // <ooo/script:libraries...
131 m_xHandler
->ignorableWhitespace( OUString() );
132 m_xHandler
->startElement( aLibContElementName
, xLibContAttribs
);
134 Reference
< script::XLibraryContainer2
> xLibContainer
;
136 // try the XEmbeddedScripts interface
137 Reference
< document::XEmbeddedScripts
> xDocumentScripts( m_xModel
, UNO_QUERY
);
138 if ( xDocumentScripts
.is() )
139 xLibContainer
.set( xDocumentScripts
->getBasicLibraries().get() );
141 if ( !xLibContainer
.is() )
143 // try the "BasicLibraries" property (old-style, for compatibility)
144 Reference
< beans::XPropertySet
> xPSet( m_xModel
, UNO_QUERY
);
146 xPSet
->getPropertyValue("BasicLibraries" ) >>= xLibContainer
;
149 SAL_WARN_IF( !xLibContainer
.is(), "xmlscript.xmlflat", "XMLBasicExporterBase::filter: nowhere to export to!" );
151 if ( xLibContainer
.is() )
153 const Sequence
< OUString
> aLibNames
= xLibContainer
->getElementNames();
154 for ( const OUString
& rLibName
: aLibNames
)
156 if ( xLibContainer
->hasByName( rLibName
) )
158 OUString
aTrueStr( "true" );
160 if ( xLibContainer
->isLibraryLink( rLibName
) )
162 // ooo/script:library-linked element
163 OUString aLibElementName
= aPrefix
+ ":library-linked";
164 XMLElement
* pLibElement
= new XMLElement( aLibElementName
);
165 Reference
< xml::sax::XAttributeList
> xLibAttribs
= static_cast< xml::sax::XAttributeList
* >( pLibElement
);
167 // ooo/script:name attribute
168 pLibElement
->addAttribute( aPrefix
+ ":name", rLibName
);
170 OUString
aLinkURL( xLibContainer
->getLibraryLinkURL( rLibName
) );
171 if ( !aLinkURL
.isEmpty() )
173 // xlink:href attribute
174 pLibElement
->addAttribute( XMLNS_XLINK_PREFIX
":href", aLinkURL
);
176 // xlink:type attribute
177 pLibElement
->addAttribute( XMLNS_XLINK_PREFIX
":type", "simple" );
180 if ( xLibContainer
->isLibraryReadOnly( rLibName
) )
182 // ooo/script:readonly attribute
183 pLibElement
->addAttribute( aPrefix
+ ":readonly", aTrueStr
);
186 // <ooo/script:library-linked...
187 m_xHandler
->ignorableWhitespace( OUString() );
188 m_xHandler
->startElement( aLibElementName
, xLibAttribs
);
190 // ...ooo/script:library-linked>
191 m_xHandler
->ignorableWhitespace( OUString() );
192 m_xHandler
->endElement( aLibElementName
);
196 // ooo/script:library-embedded element
197 OUString aLibElementName
= aPrefix
+ ":library-embedded";
198 XMLElement
* pLibElement
= new XMLElement( aLibElementName
);
199 Reference
< xml::sax::XAttributeList
> xLibAttribs
= static_cast< xml::sax::XAttributeList
* >( pLibElement
);
201 // ooo/script:name attribute
202 pLibElement
->addAttribute( aPrefix
+ ":name", rLibName
);
204 if ( xLibContainer
->isLibraryReadOnly( rLibName
) )
206 // ooo/script:readonly attribute
207 pLibElement
->addAttribute( aPrefix
+ ":readonly", aTrueStr
);
210 // TODO: password protected libraries
211 Reference
< script::XLibraryContainerPassword
> xPasswd( xLibContainer
, UNO_QUERY
);
212 if ( xPasswd
.is() && xPasswd
->isLibraryPasswordProtected( rLibName
) )
215 // <ooo/script:library-embedded...
216 m_xHandler
->ignorableWhitespace( OUString() );
217 m_xHandler
->startElement( aLibElementName
, xLibAttribs
);
219 if ( !xLibContainer
->isLibraryLoaded( rLibName
) )
220 xLibContainer
->loadLibrary( rLibName
);
222 Reference
< container::XNameContainer
> xLib
;
223 xLibContainer
->getByName( rLibName
) >>= xLib
;
227 const Sequence
< OUString
> aModNames
= xLib
->getElementNames();
228 for ( const OUString
& rModName
: aModNames
)
230 if ( xLib
->hasByName( rModName
) )
232 // ooo/script:module element
233 OUString aModElementName
= aPrefix
+ ":module";
234 XMLElement
* pModElement
= new XMLElement( aModElementName
);
235 Reference
< xml::sax::XAttributeList
> xModAttribs
= static_cast< xml::sax::XAttributeList
* >( pModElement
);
237 // ooo/script:name attribute
238 pModElement
->addAttribute( aPrefix
+ ":name", rModName
);
240 // <ooo/script:module...
241 m_xHandler
->ignorableWhitespace( OUString() );
242 m_xHandler
->startElement( aModElementName
, xModAttribs
);
244 // ooo/script:source-code element
245 OUString aSourceElementName
= aPrefix
+ ":source-code";
246 XMLElement
* pSourceElement
= new XMLElement( aSourceElementName
);
247 Reference
< xml::sax::XAttributeList
> xSourceAttribs
= static_cast< xml::sax::XAttributeList
* >( pSourceElement
);
249 // <ooo/script:source-code...
250 m_xHandler
->ignorableWhitespace( OUString() );
251 m_xHandler
->startElement( aSourceElementName
, xSourceAttribs
);
254 // TODO: write encrypted data for password protected libraries
256 xLib
->getByName( rModName
) >>= aSource
;
257 m_xHandler
->characters( aSource
);
259 // TODO: <ooo/script:byte-code>
261 // ...ooo/script:source-code>
262 m_xHandler
->ignorableWhitespace( OUString() );
263 m_xHandler
->endElement( aSourceElementName
);
265 // ...ooo/script:module>
266 m_xHandler
->ignorableWhitespace( OUString() );
267 m_xHandler
->endElement( aModElementName
);
272 // ...ooo/script:library-embedded>
273 m_xHandler
->ignorableWhitespace( OUString() );
274 m_xHandler
->endElement( aLibElementName
);
280 // ...ooo/script:libraries>
281 m_xHandler
->ignorableWhitespace( OUString() );
282 m_xHandler
->endElement( aLibContElementName
);
284 m_xHandler
->endDocument();
287 catch ( const container::NoSuchElementException
& )
289 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "XMLBasicExporterBase::filter" );
292 catch ( const lang::IllegalArgumentException
& )
294 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "XMLBasicExporterBase::filter" );
297 catch ( const lang::WrappedTargetException
& )
299 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "XMLBasicExporterBase::filter:" );
302 catch ( const xml::sax::SAXException
& )
304 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "XMLBasicExporterBase::filter:" );
311 void XMLBasicExporterBase::cancel()
313 ::osl::MutexGuard
aGuard( m_aMutex
);
320 XMLBasicExporter::XMLBasicExporter()
321 :XMLBasicExporterBase( false )
325 XMLBasicExporter::~XMLBasicExporter()
331 OUString
XMLBasicExporter::getImplementationName( )
333 return "com.sun.star.comp.xmlscript.XMLBasicExporter";
336 Sequence
< OUString
> XMLBasicExporter::getSupportedServiceNames( )
338 Sequence
< OUString
> aNames
{ "com.sun.star.document.XMLBasicExporter" };
342 // XMLOasisBasicExporter
344 XMLOasisBasicExporter::XMLOasisBasicExporter()
345 :XMLBasicExporterBase( true )
349 XMLOasisBasicExporter::~XMLOasisBasicExporter()
355 OUString
XMLOasisBasicExporter::getImplementationName( )
357 return "com.sun.star.comp.xmlscript.XMLOasisBasicExporter";
360 Sequence
< OUString
> XMLOasisBasicExporter::getSupportedServiceNames( )
362 Sequence
< OUString
> aNames
{ "com.sun.star.document.XMLOasisBasicExporter" };
366 } // namespace xmlscript
368 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
369 com_sun_star_comp_xmlscript_XMLBasicExporter(
370 css::uno::XComponentContext
*,
371 css::uno::Sequence
<css::uno::Any
> const &)
373 return cppu::acquire(new xmlscript::XMLBasicExporter());
376 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
377 com_sun_star_comp_xmlscript_XMLOasisBasicExporter(
378 css::uno::XComponentContext
*,
379 css::uno::Sequence
<css::uno::Any
> const &)
382 return cppu::acquire(new xmlscript::XMLOasisBasicExporter());
385 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */