1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xmlbas_export.cxx,v $
10 * $Revision: 1.6.10.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlscript.hxx"
33 #include "xmlbas_export.hxx"
34 #include "xmlscript/xmlns.h"
35 #include "xmlscript/xml_helper.hxx"
36 #include <com/sun/star/beans/XPropertySet.hpp>
37 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
38 #include <com/sun/star/script/XLibraryContainer2.hpp>
39 #include <com/sun/star/script/XLibraryContainerPassword.hpp>
40 #include <com/sun/star/document/XEmbeddedScripts.hpp>
41 #include <cppuhelper/implementationentry.hxx>
43 using namespace ::com::sun::star
;
44 using namespace ::com::sun::star::lang
;
45 using namespace ::com::sun::star::uno
;
48 //.........................................................................
51 //.........................................................................
53 // =============================================================================
54 // component operations
55 // =============================================================================
57 ::rtl::OUString
getImplementationName_XMLBasicExporter()
59 static ::rtl::OUString
* pImplName
= 0;
62 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
65 static ::rtl::OUString
aImplName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.xmlscript.XMLBasicExporter" ) );
66 pImplName
= &aImplName
;
72 // -----------------------------------------------------------------------------
74 Sequence
< ::rtl::OUString
> getSupportedServiceNames_XMLBasicExporter()
76 static Sequence
< ::rtl::OUString
>* pNames
= 0;
79 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
82 static Sequence
< ::rtl::OUString
> aNames(1);
83 aNames
.getArray()[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.XMLBasicExporter" ) );
90 // -----------------------------------------------------------------------------
92 ::rtl::OUString
getImplementationName_XMLOasisBasicExporter()
94 static ::rtl::OUString
* pImplName
= 0;
97 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
100 static ::rtl::OUString
aImplName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.xmlscript.XMLOasisBasicExporter" ) );
101 pImplName
= &aImplName
;
107 // -----------------------------------------------------------------------------
109 Sequence
< ::rtl::OUString
> getSupportedServiceNames_XMLOasisBasicExporter()
111 static Sequence
< ::rtl::OUString
>* pNames
= 0;
114 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
117 static Sequence
< ::rtl::OUString
> aNames(1);
118 aNames
.getArray()[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.XMLOasisBasicExporter" ) );
126 // =============================================================================
127 // XMLBasicExporterBase
128 // =============================================================================
130 XMLBasicExporterBase::XMLBasicExporterBase( const Reference
< XComponentContext
>& rxContext
, sal_Bool bOasis
)
131 :m_xContext( rxContext
)
136 // -----------------------------------------------------------------------------
138 XMLBasicExporterBase::~XMLBasicExporterBase()
142 // -----------------------------------------------------------------------------
144 // -----------------------------------------------------------------------------
146 sal_Bool
XMLBasicExporterBase::supportsService( const ::rtl::OUString
& rServiceName
) throw (RuntimeException
)
148 Sequence
< ::rtl::OUString
> aNames( getSupportedServiceNames() );
149 const ::rtl::OUString
* pNames
= aNames
.getConstArray();
150 const ::rtl::OUString
* pEnd
= pNames
+ aNames
.getLength();
151 for ( ; pNames
!= pEnd
&& !pNames
->equals( rServiceName
); ++pNames
)
154 return pNames
!= pEnd
;
157 // -----------------------------------------------------------------------------
159 // -----------------------------------------------------------------------------
161 void XMLBasicExporterBase::initialize( const Sequence
< Any
>& aArguments
) throw (Exception
, RuntimeException
)
163 ::osl::MutexGuard
aGuard( m_aMutex
);
165 if ( aArguments
.getLength() == 1 )
167 aArguments
[0] >>= m_xHandler
;
169 if ( !m_xHandler
.is() )
171 throw RuntimeException(
172 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "XMLBasicExporterBase::initialize: invalid argument format!" ) ),
173 Reference
< XInterface
>() );
178 throw RuntimeException(
179 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "XMLBasicExporterBase::initialize: invalid number of arguments!" ) ),
180 Reference
< XInterface
>() );
184 // -----------------------------------------------------------------------------
186 // -----------------------------------------------------------------------------
188 void XMLBasicExporterBase::setSourceDocument( const Reference
< XComponent
>& rxDoc
)
189 throw (IllegalArgumentException
, RuntimeException
)
191 ::osl::MutexGuard
aGuard( m_aMutex
);
193 m_xModel
.set( rxDoc
, UNO_QUERY
);
195 if ( !m_xModel
.is() )
197 throw IllegalArgumentException(
198 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "XMLBasicExporter::setSourceDocument: no document model!" ) ),
199 Reference
< XInterface
>(), 1 );
203 // -----------------------------------------------------------------------------
205 // -----------------------------------------------------------------------------
207 sal_Bool
XMLBasicExporterBase::filter( const Sequence
< beans::PropertyValue
>& /*aDescriptor*/ )
208 throw (RuntimeException
)
210 ::osl::MutexGuard
aGuard( m_aMutex
);
212 sal_Bool bReturn
= sal_True
;
216 if ( m_xHandler
.is() )
218 m_xHandler
->startDocument();
220 // ooo/script prefix and URI
221 ::rtl::OUString aPrefix
;
222 ::rtl::OUString aURI
;
225 aPrefix
= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( XMLNS_OOO_PREFIX
) );
226 aURI
= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( XMLNS_OOO_URI
) );
230 aPrefix
= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( XMLNS_SCRIPT_PREFIX
) );
231 aURI
= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( XMLNS_SCRIPT_URI
) );
234 // ooo/script:libraries element
235 ::rtl::OUString
aLibContElementName( aPrefix
);
236 aLibContElementName
+= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ":libraries" ) );
237 XMLElement
* pLibContElement
= new XMLElement( aLibContElementName
);
238 Reference
< xml::sax::XAttributeList
> xLibContAttribs( pLibContElement
);
240 // ooo/script namespace attribute
241 pLibContElement
->addAttribute( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "xmlns:" ) ) + aPrefix
,
244 // xlink namespace attribute
245 pLibContElement
->addAttribute( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "xmlns:" XMLNS_XLINK_PREFIX
) ),
246 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( XMLNS_XLINK_URI
) ) );
248 // <ooo/script:libraries...
249 m_xHandler
->ignorableWhitespace( ::rtl::OUString() );
250 m_xHandler
->startElement( aLibContElementName
, xLibContAttribs
);
252 Reference
< script::XLibraryContainer2
> xLibContainer
;
254 // try the XEmbeddedScripts interface
255 Reference
< document::XEmbeddedScripts
> xDocumentScripts( m_xModel
, UNO_QUERY
);
256 if ( xDocumentScripts
.is() )
257 xLibContainer
.set( xDocumentScripts
->getBasicLibraries().get() );
259 if ( !xLibContainer
.is() )
261 // try the "BasicLibraries" property (old-style, for compatibility)
262 Reference
< beans::XPropertySet
> xPSet( m_xModel
, UNO_QUERY
);
264 xPSet
->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "BasicLibraries" ) ) ) >>= xLibContainer
;
267 OSL_ENSURE( xLibContainer
.is(), "XMLBasicExporterBase::filter: nowhere to export to!" );
269 if ( xLibContainer
.is() )
271 Sequence
< ::rtl::OUString
> aLibNames
= xLibContainer
->getElementNames();
272 sal_Int32 nLibCount
= aLibNames
.getLength();
273 const ::rtl::OUString
* pLibNames
= aLibNames
.getConstArray();
274 for ( sal_Int32 i
= 0 ; i
< nLibCount
; ++i
)
276 ::rtl::OUString
aLibName( pLibNames
[i
] );
278 if ( xLibContainer
->hasByName( aLibName
) )
280 ::rtl::OUString
aTrueStr( RTL_CONSTASCII_USTRINGPARAM( "true" ) );
282 if ( xLibContainer
->isLibraryLink( aLibName
) )
284 // ooo/script:library-linked element
285 ::rtl::OUString
aLibElementName( aPrefix
);
286 aLibElementName
+= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ":library-linked" ) );
287 XMLElement
* pLibElement
= new XMLElement( aLibElementName
);
288 Reference
< xml::sax::XAttributeList
> xLibAttribs
;
289 xLibAttribs
= static_cast< xml::sax::XAttributeList
* >( pLibElement
);
291 // ooo/script:name attribute
292 pLibElement
->addAttribute( aPrefix
+ ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ":name" ) ),
295 ::rtl::OUString
aLinkURL( xLibContainer
->getLibraryLinkURL( aLibName
) );
296 if ( aLinkURL
.getLength() )
298 // xlink:href attribute
299 pLibElement
->addAttribute( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( XMLNS_XLINK_PREFIX
":href" ) ),
302 // xlink:type attribute
303 pLibElement
->addAttribute( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( XMLNS_XLINK_PREFIX
":type" ) ),
304 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "simple" ) ) );
307 if ( xLibContainer
->isLibraryReadOnly( aLibName
) )
309 // ooo/script:readonly attribute
310 pLibElement
->addAttribute( aPrefix
+ ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ":readonly" ) ),
314 // <ooo/script:library-linked...
315 m_xHandler
->ignorableWhitespace( ::rtl::OUString() );
316 m_xHandler
->startElement( aLibElementName
, xLibAttribs
);
318 // ...ooo/script:library-linked>
319 m_xHandler
->ignorableWhitespace( ::rtl::OUString() );
320 m_xHandler
->endElement( aLibElementName
);
324 // ooo/script:library-embedded element
325 ::rtl::OUString
aLibElementName( aPrefix
);
326 aLibElementName
+= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ":library-embedded" ) );
327 XMLElement
* pLibElement
= new XMLElement( aLibElementName
);
328 Reference
< xml::sax::XAttributeList
> xLibAttribs
;
329 xLibAttribs
= static_cast< xml::sax::XAttributeList
* >( pLibElement
);
331 // ooo/script:name attribute
332 pLibElement
->addAttribute( aPrefix
+ ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ":name" ) ),
335 if ( xLibContainer
->isLibraryReadOnly( aLibName
) )
337 // ooo/script:readonly attribute
338 pLibElement
->addAttribute( aPrefix
+ ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ":readonly" ) ),
342 // TODO: password protected libraries
343 Reference
< script::XLibraryContainerPassword
> xPasswd( xLibContainer
, UNO_QUERY
);
344 if ( xPasswd
.is() && xPasswd
->isLibraryPasswordProtected( aLibName
) )
347 // <ooo/script:library-embedded...
348 m_xHandler
->ignorableWhitespace( ::rtl::OUString() );
349 m_xHandler
->startElement( aLibElementName
, xLibAttribs
);
351 if ( !xLibContainer
->isLibraryLoaded( aLibName
) )
352 xLibContainer
->loadLibrary( aLibName
);
354 Reference
< container::XNameContainer
> xLib
;
355 xLibContainer
->getByName( aLibName
) >>= xLib
;
359 Sequence
< ::rtl::OUString
> aModNames
= xLib
->getElementNames();
360 sal_Int32 nModCount
= aModNames
.getLength();
361 const ::rtl::OUString
* pModNames
= aModNames
.getConstArray();
362 for ( sal_Int32 j
= 0 ; j
< nModCount
; ++j
)
364 ::rtl::OUString
aModName( pModNames
[j
] );
365 if ( xLib
->hasByName( aModName
) )
367 // ooo/script:module element
368 ::rtl::OUString
aModElementName( aPrefix
);
369 aModElementName
+= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ":module" ) );
370 XMLElement
* pModElement
= new XMLElement( aModElementName
);
371 Reference
< xml::sax::XAttributeList
> xModAttribs
;
372 xModAttribs
= static_cast< xml::sax::XAttributeList
* >( pModElement
);
374 // ooo/script:name attribute
375 pModElement
->addAttribute( aPrefix
+ ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ":name" ) ),
378 // <ooo/script:module...
379 m_xHandler
->ignorableWhitespace( ::rtl::OUString() );
380 m_xHandler
->startElement( aModElementName
, xModAttribs
);
382 // ooo/script:source-code element
383 ::rtl::OUString
aSourceElementName( aPrefix
);
384 aSourceElementName
+= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ":source-code" ) );
385 XMLElement
* pSourceElement
= new XMLElement( aSourceElementName
);
386 Reference
< xml::sax::XAttributeList
> xSourceAttribs
;
387 xSourceAttribs
= static_cast< xml::sax::XAttributeList
* >( pSourceElement
);
389 // <ooo/script:source-code...
390 m_xHandler
->ignorableWhitespace( ::rtl::OUString() );
391 m_xHandler
->startElement( aSourceElementName
, xSourceAttribs
);
394 // TODO: write encrypted data for password protected libraries
395 ::rtl::OUString aSource
;
396 xLib
->getByName( aModName
) >>= aSource
;
397 m_xHandler
->characters( aSource
);
399 // TODO: <ooo/script:byte-code>
401 // ...ooo/script:source-code>
402 m_xHandler
->ignorableWhitespace( ::rtl::OUString() );
403 m_xHandler
->endElement( aSourceElementName
);
405 // ...ooo/script:module>
406 m_xHandler
->ignorableWhitespace( ::rtl::OUString() );
407 m_xHandler
->endElement( aModElementName
);
412 // ...ooo/script:library-embedded>
413 m_xHandler
->ignorableWhitespace( ::rtl::OUString() );
414 m_xHandler
->endElement( aLibElementName
);
420 // ...ooo/script:libraries>
421 m_xHandler
->ignorableWhitespace( ::rtl::OUString() );
422 m_xHandler
->endElement( aLibContElementName
);
424 m_xHandler
->endDocument();
427 catch ( container::NoSuchElementException
& e
)
429 OSL_TRACE( "XMLBasicExporterBase::filter: caught NoSuchElementException reason %s",
430 ::rtl::OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
433 catch ( lang::IllegalArgumentException
& e
)
435 OSL_TRACE( "XMLBasicExporterBase::filter: caught IllegalArgumentException reason %s",
436 ::rtl::OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
439 catch ( lang::WrappedTargetException
& e
)
441 OSL_TRACE( "XMLBasicExporterBase::filter: caught WrappedTargetException reason %s",
442 ::rtl::OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
445 catch ( xml::sax::SAXException
& e
)
447 OSL_TRACE( "XMLBasicExporterBase::filter: caught SAXException reason %s",
448 ::rtl::OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
).pData
->buffer
);
455 // -----------------------------------------------------------------------------
457 void XMLBasicExporterBase::cancel()
458 throw (RuntimeException
)
460 ::osl::MutexGuard
aGuard( m_aMutex
);
466 // =============================================================================
468 // =============================================================================
470 XMLBasicExporter::XMLBasicExporter( const Reference
< XComponentContext
>& rxContext
)
471 :XMLBasicExporterBase( rxContext
, sal_False
)
475 // -----------------------------------------------------------------------------
477 XMLBasicExporter::~XMLBasicExporter()
481 // -----------------------------------------------------------------------------
483 // -----------------------------------------------------------------------------
485 ::rtl::OUString
XMLBasicExporter::getImplementationName( ) throw (RuntimeException
)
487 return getImplementationName_XMLBasicExporter();
490 // -----------------------------------------------------------------------------
492 Sequence
< ::rtl::OUString
> XMLBasicExporter::getSupportedServiceNames( ) throw (RuntimeException
)
494 return getSupportedServiceNames_XMLBasicExporter();
498 // =============================================================================
499 // XMLOasisBasicExporter
500 // =============================================================================
502 XMLOasisBasicExporter::XMLOasisBasicExporter( const Reference
< XComponentContext
>& rxContext
)
503 :XMLBasicExporterBase( rxContext
, sal_True
)
507 // -----------------------------------------------------------------------------
509 XMLOasisBasicExporter::~XMLOasisBasicExporter()
513 // -----------------------------------------------------------------------------
515 // -----------------------------------------------------------------------------
517 ::rtl::OUString
XMLOasisBasicExporter::getImplementationName( ) throw (RuntimeException
)
519 return getImplementationName_XMLOasisBasicExporter();
522 // -----------------------------------------------------------------------------
524 Sequence
< ::rtl::OUString
> XMLOasisBasicExporter::getSupportedServiceNames( ) throw (RuntimeException
)
526 return getSupportedServiceNames_XMLOasisBasicExporter();
530 // =============================================================================
531 // component operations
532 // =============================================================================
534 Reference
< XInterface
> SAL_CALL
create_XMLBasicExporter(
535 Reference
< XComponentContext
> const & xContext
)
538 return static_cast< lang::XTypeProvider
* >( new XMLBasicExporter( xContext
) );
541 // -----------------------------------------------------------------------------
543 Reference
< XInterface
> SAL_CALL
create_XMLOasisBasicExporter(
544 Reference
< XComponentContext
> const & xContext
)
547 return static_cast< lang::XTypeProvider
* >( new XMLOasisBasicExporter( xContext
) );
550 // -----------------------------------------------------------------------------
552 //.........................................................................
553 } // namespace xmlscript
554 //.........................................................................