Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / xmlscript / source / xmlflat_imexp / xmlbas_export.cxx
blob1bf90bf8d5191c47d8dbe729e5efd0cf409dc7db
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 .
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 <comphelper/diagnose_ex.hxx>
34 #include <rtl/ref.hxx>
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::lang;
38 using namespace ::com::sun::star::uno;
40 namespace xmlscript
42 // XMLBasicExporterBase
44 XMLBasicExporterBase::XMLBasicExporterBase( bool bOasis )
45 :m_bOasis( bOasis )
49 XMLBasicExporterBase::~XMLBasicExporterBase()
53 // XServiceInfo
54 sal_Bool XMLBasicExporterBase::supportsService( const OUString& rServiceName )
56 return cppu::supportsService(this, rServiceName);
59 // XInitialization
61 void XMLBasicExporterBase::initialize( const Sequence< Any >& aArguments )
63 std::scoped_lock aGuard( m_aMutex );
65 if ( aArguments.getLength() != 1 )
67 throw RuntimeException( "XMLBasicExporterBase::initialize: invalid number of arguments!" );
70 aArguments[0] >>= m_xHandler;
72 if ( !m_xHandler.is() )
74 throw RuntimeException( "XMLBasicExporterBase::initialize: invalid argument format!" );
78 // XExporter
80 void XMLBasicExporterBase::setSourceDocument( const Reference< XComponent >& rxDoc )
82 std::scoped_lock aGuard( m_aMutex );
84 m_xModel.set( rxDoc, UNO_QUERY );
86 if ( !m_xModel.is() )
88 throw IllegalArgumentException( "XMLBasicExporter::setSourceDocument: no document model!", Reference< XInterface >(), 1 );
92 // XFilter
94 sal_Bool XMLBasicExporterBase::filter( const Sequence< beans::PropertyValue >& /*aDescriptor*/ )
96 std::scoped_lock aGuard( m_aMutex );
98 bool bReturn = true;
102 if ( m_xHandler.is() )
104 m_xHandler->startDocument();
106 // ooo/script prefix and URI
107 OUString aPrefix;
108 OUString aURI;
109 if ( m_bOasis )
111 aPrefix = XMLNS_OOO_PREFIX;
112 aURI = XMLNS_OOO_URI;
114 else
116 aPrefix = XMLNS_SCRIPT_PREFIX;
117 aURI = XMLNS_SCRIPT_URI;
120 // ooo/script:libraries element
121 OUString aLibContElementName = aPrefix + ":libraries";
122 rtl::Reference<XMLElement> pLibContElement = new XMLElement( aLibContElementName );
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, pLibContElement );
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 = xDocumentScripts->getBasicLibraries();
141 if ( !xLibContainer.is() )
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;
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 rtl::Reference<XMLElement> pLibElement = new XMLElement( aLibElementName );
166 // ooo/script:name attribute
167 pLibElement->addAttribute( aPrefix + ":name", rLibName );
169 OUString aLinkURL( xLibContainer->getLibraryLinkURL( rLibName ) );
170 if ( !aLinkURL.isEmpty() )
172 // xlink:href attribute
173 pLibElement->addAttribute( XMLNS_XLINK_PREFIX ":href", aLinkURL );
175 // xlink:type attribute
176 pLibElement->addAttribute( XMLNS_XLINK_PREFIX ":type", "simple" );
179 if ( xLibContainer->isLibraryReadOnly( rLibName ) )
181 // ooo/script:readonly attribute
182 pLibElement->addAttribute( aPrefix + ":readonly", aTrueStr );
185 // <ooo/script:library-linked...
186 m_xHandler->ignorableWhitespace( OUString() );
187 m_xHandler->startElement( aLibElementName, pLibElement );
189 // ...ooo/script:library-linked>
190 m_xHandler->ignorableWhitespace( OUString() );
191 m_xHandler->endElement( aLibElementName );
193 else
195 // ooo/script:library-embedded element
196 OUString aLibElementName = aPrefix + ":library-embedded";
197 rtl::Reference<XMLElement> pLibElement = new XMLElement( aLibElementName );
199 // ooo/script:name attribute
200 pLibElement->addAttribute( aPrefix + ":name", rLibName );
202 if ( xLibContainer->isLibraryReadOnly( rLibName ) )
204 // ooo/script:readonly attribute
205 pLibElement->addAttribute( aPrefix + ":readonly", aTrueStr );
208 // TODO: password protected libraries
209 Reference< script::XLibraryContainerPassword > xPasswd( xLibContainer, UNO_QUERY );
210 if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( rLibName ) )
211 continue;
213 // <ooo/script:library-embedded...
214 m_xHandler->ignorableWhitespace( OUString() );
215 m_xHandler->startElement( aLibElementName, pLibElement );
217 if ( !xLibContainer->isLibraryLoaded( rLibName ) )
218 xLibContainer->loadLibrary( rLibName );
220 Reference< container::XNameContainer > xLib;
221 xLibContainer->getByName( rLibName ) >>= xLib;
223 if ( xLib.is() )
225 const Sequence< OUString > aModNames = xLib->getElementNames();
226 for ( const OUString& rModName : aModNames )
228 if ( xLib->hasByName( rModName ) )
230 // ooo/script:module element
231 OUString aModElementName = aPrefix + ":module";
232 rtl::Reference<XMLElement> pModElement = new XMLElement( aModElementName );
234 // ooo/script:name attribute
235 pModElement->addAttribute( aPrefix + ":name", rModName );
237 // <ooo/script:module...
238 m_xHandler->ignorableWhitespace( OUString() );
239 m_xHandler->startElement( aModElementName, pModElement );
241 // ooo/script:source-code element
242 OUString aSourceElementName = aPrefix + ":source-code";
243 rtl::Reference<XMLElement> pSourceElement = new XMLElement( aSourceElementName );
245 // <ooo/script:source-code...
246 m_xHandler->ignorableWhitespace( OUString() );
247 m_xHandler->startElement( aSourceElementName, pSourceElement );
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 );
255 // TODO: <ooo/script:byte-code>
257 // ...ooo/script:source-code>
258 m_xHandler->ignorableWhitespace( OUString() );
259 m_xHandler->endElement( aSourceElementName );
261 // ...ooo/script:module>
262 m_xHandler->ignorableWhitespace( OUString() );
263 m_xHandler->endElement( aModElementName );
268 // ...ooo/script:library-embedded>
269 m_xHandler->ignorableWhitespace( OUString() );
270 m_xHandler->endElement( aLibElementName );
276 // ...ooo/script:libraries>
277 m_xHandler->ignorableWhitespace( OUString() );
278 m_xHandler->endElement( aLibContElementName );
280 m_xHandler->endDocument();
283 catch ( const container::NoSuchElementException& )
285 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "XMLBasicExporterBase::filter" );
286 bReturn = false;
288 catch ( const lang::IllegalArgumentException& )
290 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "XMLBasicExporterBase::filter" );
291 bReturn = false;
293 catch ( const lang::WrappedTargetException& )
295 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "XMLBasicExporterBase::filter:" );
296 bReturn = false;
298 catch ( const xml::sax::SAXException& )
300 TOOLS_INFO_EXCEPTION("xmlscript.xmlflat", "XMLBasicExporterBase::filter:" );
301 bReturn = false;
304 return bReturn;
307 void XMLBasicExporterBase::cancel()
309 // cancel export
312 // XMLBasicExporter
314 XMLBasicExporter::XMLBasicExporter()
315 :XMLBasicExporterBase( false )
319 XMLBasicExporter::~XMLBasicExporter()
323 // XServiceInfo
325 OUString XMLBasicExporter::getImplementationName( )
327 return "com.sun.star.comp.xmlscript.XMLBasicExporter";
330 Sequence< OUString > XMLBasicExporter::getSupportedServiceNames( )
332 return { "com.sun.star.document.XMLBasicExporter" };
335 // XMLOasisBasicExporter
337 XMLOasisBasicExporter::XMLOasisBasicExporter()
338 :XMLBasicExporterBase( true )
342 XMLOasisBasicExporter::~XMLOasisBasicExporter()
346 // XServiceInfo
348 OUString XMLOasisBasicExporter::getImplementationName( )
350 return "com.sun.star.comp.xmlscript.XMLOasisBasicExporter";
353 Sequence< OUString > XMLOasisBasicExporter::getSupportedServiceNames( )
355 return { "com.sun.star.document.XMLOasisBasicExporter" };
358 } // namespace xmlscript
360 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
361 com_sun_star_comp_xmlscript_XMLBasicExporter(
362 css::uno::XComponentContext *,
363 css::uno::Sequence<css::uno::Any> const &)
365 return cppu::acquire(new xmlscript::XMLBasicExporter());
368 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
369 com_sun_star_comp_xmlscript_XMLOasisBasicExporter(
370 css::uno::XComponentContext *,
371 css::uno::Sequence<css::uno::Any> const &)
374 return cppu::acquire(new xmlscript::XMLOasisBasicExporter());
377 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */