1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlscript.hxx"
30 #include "xmlbas_import.hxx"
31 #include "xmlscript/xmlns.h"
32 #include "xmlscript/xml_helper.hxx"
33 #include <com/sun/star/beans/XPropertySet.hpp>
34 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
35 #include <com/sun/star/script/XLibraryContainerPassword.hpp>
36 #include <com/sun/star/document/XEmbeddedScripts.hpp>
37 #include <cppuhelper/implementationentry.hxx>
38 
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::lang;
41 using namespace ::com::sun::star::uno;
42 
43 
44 //.........................................................................
45 namespace xmlscript
46 {
47 //.........................................................................
48 
49     // =============================================================================
50     // BasicElementBase
51     // =============================================================================
52 
53     BasicElementBase::BasicElementBase( const ::rtl::OUString& rLocalName,
54             const Reference< xml::input::XAttributes >& xAttributes,
55             BasicElementBase* pParent, BasicImport* pImport )
56         :m_pImport( pImport )
57         ,m_pParent( pParent )
58         ,m_aLocalName( rLocalName )
59         ,m_xAttributes( xAttributes )
60     {
61         if ( m_pImport )
62             m_pImport->acquire();
63         if ( m_pParent )
64             m_pParent->acquire();
65     }
66 
67     // -----------------------------------------------------------------------------
68 
69     BasicElementBase::~BasicElementBase()
70     {
71         if ( m_pImport )
72             m_pImport->release();
73         if ( m_pParent )
74             m_pParent->release();
75     }
76 
77     // -----------------------------------------------------------------------------
78 
79     bool BasicElementBase::getBoolAttr( sal_Bool* pRet, const ::rtl::OUString& rAttrName,
80         const ::com::sun::star::uno::Reference< ::com::sun::star::xml::input::XAttributes >& xAttributes,
81         sal_Int32 nUid )
82     {
83         if ( xAttributes.is() )
84         {
85             ::rtl::OUString aValue( xAttributes->getValueByUidName( nUid, rAttrName ) );
86             if ( aValue.getLength() )
87             {
88                 if ( aValue.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "true" ) ) )
89                 {
90                     *pRet = sal_True;
91                     return true;
92                 }
93                 else if ( aValue.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "false" ) ) )
94                 {
95                     *pRet = sal_False;
96                     return true;
97                 }
98                 else
99                 {
100                     throw xml::sax::SAXException(
101                         rAttrName + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ": no boolean value (true|false)!" ) ),
102                         Reference< XInterface >(), Any() );
103                 }
104             }
105         }
106         return false;
107     }
108 
109     // -----------------------------------------------------------------------------
110     // XElement
111     // -----------------------------------------------------------------------------
112 
113     Reference< xml::input::XElement > BasicElementBase::getParent()
114         throw (RuntimeException)
115     {
116         return static_cast< xml::input::XElement* >( m_pParent );
117     }
118 
119     // -----------------------------------------------------------------------------
120 
121     ::rtl::OUString BasicElementBase::getLocalName()
122         throw (RuntimeException)
123     {
124         return m_aLocalName;
125     }
126 
127     // -----------------------------------------------------------------------------
128 
129     sal_Int32 BasicElementBase::getUid()
130         throw (RuntimeException)
131     {
132         sal_Int32 nId = -1;
133         if ( m_pImport )
134             nId = m_pImport->XMLNS_UID;
135         return nId;
136     }
137 
138     // -----------------------------------------------------------------------------
139 
140     Reference< xml::input::XAttributes > BasicElementBase::getAttributes()
141         throw (RuntimeException)
142     {
143         return m_xAttributes;
144     }
145 
146     // -----------------------------------------------------------------------------
147 
148     Reference< xml::input::XElement > BasicElementBase::startChildElement(
149         sal_Int32 /*nUid*/, const ::rtl::OUString& /*rLocalName*/,
150         const Reference< xml::input::XAttributes >& /*xAttributes*/ )
151         throw (xml::sax::SAXException, RuntimeException)
152     {
153         throw xml::sax::SAXException(
154             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "unexpected element!" ) ),
155             Reference< XInterface >(), Any() );
156     }
157 
158     // -----------------------------------------------------------------------------
159 
160 void BasicElementBase::characters( const ::rtl::OUString& /*rChars*/ )
161         throw (xml::sax::SAXException, RuntimeException)
162     {
163         // not used, all characters ignored
164     }
165 
166     // -----------------------------------------------------------------------------
167 
168 void BasicElementBase::ignorableWhitespace( const ::rtl::OUString& /*rWhitespaces*/ )
169         throw (xml::sax::SAXException, RuntimeException)
170     {
171     }
172 
173     // -----------------------------------------------------------------------------
174 
175 void BasicElementBase::processingInstruction( const ::rtl::OUString& /*rTarget*/, const ::rtl::OUString& /*rData*/ )
176         throw (xml::sax::SAXException, RuntimeException)
177     {
178     }
179 
180     // -----------------------------------------------------------------------------
181 
182     void BasicElementBase::endElement()
183         throw (xml::sax::SAXException, RuntimeException)
184     {
185     }
186 
187 
188     // =============================================================================
189     // BasicLibrariesElement
190     // =============================================================================
191 
192     BasicLibrariesElement::BasicLibrariesElement( const ::rtl::OUString& rLocalName,
193             const Reference< xml::input::XAttributes >& xAttributes,
194             BasicElementBase* pParent, BasicImport* pImport,
195             const Reference< script::XLibraryContainer2 >& rxLibContainer )
196         :BasicElementBase( rLocalName, xAttributes, pParent, pImport )
197         ,m_xLibContainer( rxLibContainer )
198     {
199     }
200 
201     // -----------------------------------------------------------------------------
202     // XElement
203     // -----------------------------------------------------------------------------
204 
205     Reference< xml::input::XElement > BasicLibrariesElement::startChildElement(
206             sal_Int32 nUid, const ::rtl::OUString& rLocalName,
207             const Reference< xml::input::XAttributes >& xAttributes )
208         throw (xml::sax::SAXException, RuntimeException)
209     {
210         Reference< xml::input::XElement > xElement;
211 
212         if ( nUid != m_pImport->XMLNS_UID )
213         {
214             throw xml::sax::SAXException(
215                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "illegal namespace!" ) ),
216                 Reference< XInterface >(), Any() );
217         }
218         else if ( rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "library-linked" ) ) )
219         {
220             if ( xAttributes.is() )
221             {
222                 ::rtl::OUString aName = xAttributes->getValueByUidName(
223                     m_pImport->XMLNS_UID,
224                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "name" ) ) );
225 
226                 ::rtl::OUString aStorageURL = xAttributes->getValueByUidName(
227                     m_pImport->XMLNS_XLINK_UID,
228                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "href" ) ) );
229 
230                 sal_Bool bReadOnly = sal_False;
231                 getBoolAttr( &bReadOnly,
232                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "readonly" ) ),
233                     xAttributes, m_pImport->XMLNS_UID );
234 
235                 if ( m_xLibContainer.is() )
236                 {
237                     try
238                     {
239                         Reference< container::XNameAccess > xLib(
240                             m_xLibContainer->createLibraryLink( aName, aStorageURL, bReadOnly ) );
241                         if ( xLib.is() )
242                             xElement.set( new BasicElementBase( rLocalName, xAttributes, this, m_pImport ) );
243                     }
244                     catch ( container::ElementExistException& e )
245                     {
246                         OSL_TRACE( "BasicLibrariesElement::startChildElement: caught ElementExceptionExist reason %s",
247                             ::rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
248                     }
249                     catch ( lang::IllegalArgumentException& e )
250                     {
251                         OSL_TRACE( "BasicLibrariesElement::startChildElement: caught IllegalArgumentException reason %s",
252                             ::rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
253                     }
254                 }
255             }
256         }
257         else if ( rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "library-embedded" ) ) )
258         {
259             // TODO: create password protected libraries
260 
261             if ( xAttributes.is() )
262             {
263                 ::rtl::OUString aName = xAttributes->getValueByUidName(
264                     m_pImport->XMLNS_UID,
265                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "name" ) ) );
266 
267                 sal_Bool bReadOnly = sal_False;
268                 getBoolAttr( &bReadOnly,
269                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "readonly" ) ),
270                     xAttributes, m_pImport->XMLNS_UID );
271 
272                 if ( m_xLibContainer.is() )
273                 {
274                     try
275                     {
276                         Reference< container::XNameContainer > xLib;
277                         if ( m_xLibContainer->hasByName( aName ) )
278                         {
279                             // Standard library
280                             m_xLibContainer->getByName( aName ) >>= xLib;
281                         }
282                         else
283                         {
284                             xLib.set( m_xLibContainer->createLibrary( aName ) );
285                         }
286 
287                         if ( xLib.is() )
288                             xElement.set( new BasicEmbeddedLibraryElement( rLocalName, xAttributes, this, m_pImport, m_xLibContainer, aName, bReadOnly ) );
289                     }
290                     catch ( lang::IllegalArgumentException& e )
291                     {
292                         OSL_TRACE( "BasicLibrariesElement::startChildElement: caught IllegalArgumentException reason %s",
293                             ::rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
294                     }
295                 }
296             }
297         }
298         else
299         {
300             throw xml::sax::SAXException(
301                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "expected library-linked or library-embedded element!" ) ),
302                 Reference< XInterface >(), Any() );
303         }
304 
305         return xElement;
306     }
307 
308     // -----------------------------------------------------------------------------
309 
310     void BasicLibrariesElement::endElement()
311         throw (xml::sax::SAXException, RuntimeException)
312     {
313     }
314 
315 
316     // =============================================================================
317     // BasicEmbeddedLibraryElement
318     // =============================================================================
319 
320     BasicEmbeddedLibraryElement::BasicEmbeddedLibraryElement( const ::rtl::OUString& rLocalName,
321             const Reference< xml::input::XAttributes >& xAttributes,
322             BasicElementBase* pParent, BasicImport* pImport,
323             const Reference< script::XLibraryContainer2 >& rxLibContainer,
324             const ::rtl::OUString& rLibName, bool bReadOnly )
325         :BasicElementBase( rLocalName, xAttributes, pParent, pImport )
326         ,m_xLibContainer( rxLibContainer )
327         ,m_aLibName( rLibName )
328         ,m_bReadOnly( bReadOnly )
329     {
330         try
331         {
332             if ( m_xLibContainer.is() && m_xLibContainer->hasByName( m_aLibName ) )
333                 m_xLibContainer->getByName( m_aLibName ) >>= m_xLib;
334         }
335         catch ( lang::WrappedTargetException& e )
336         {
337             OSL_TRACE( "BasicEmbeddedLibraryElement CTOR: caught WrappedTargetException reason %s",
338                 ::rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
339         }
340     }
341 
342     // -----------------------------------------------------------------------------
343     // XElement
344     // -----------------------------------------------------------------------------
345 
346     Reference< xml::input::XElement > BasicEmbeddedLibraryElement::startChildElement(
347             sal_Int32 nUid, const ::rtl::OUString& rLocalName,
348             const Reference< xml::input::XAttributes >& xAttributes )
349         throw (xml::sax::SAXException, RuntimeException)
350     {
351         Reference< xml::input::XElement > xElement;
352 
353         if ( nUid != m_pImport->XMLNS_UID )
354         {
355             throw xml::sax::SAXException(
356                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "illegal namespace!" ) ),
357                 Reference< XInterface >(), Any() );
358         }
359         else if ( rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "module" ) ) )
360         {
361             if ( xAttributes.is() )
362             {
363                 ::rtl::OUString aName = xAttributes->getValueByUidName(
364                     m_pImport->XMLNS_UID,
365                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "name" ) ) );
366 
367                 if ( m_xLib.is() && aName.getLength() )
368                     xElement.set( new BasicModuleElement( rLocalName, xAttributes, this, m_pImport, m_xLib, aName ) );
369             }
370         }
371         else
372         {
373             throw xml::sax::SAXException(
374                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "expected module element!" ) ),
375                 Reference< XInterface >(), Any() );
376         }
377 
378         return xElement;
379     }
380 
381     // -----------------------------------------------------------------------------
382 
383     void BasicEmbeddedLibraryElement::endElement()
384         throw (xml::sax::SAXException, RuntimeException)
385     {
386         if ( m_xLibContainer.is() && m_xLibContainer->hasByName( m_aLibName ) && m_bReadOnly )
387             m_xLibContainer->setLibraryReadOnly( m_aLibName, m_bReadOnly );
388     }
389 
390 
391     // =============================================================================
392     // BasicModuleElement
393     // =============================================================================
394 
395     BasicModuleElement::BasicModuleElement( const ::rtl::OUString& rLocalName,
396             const Reference< xml::input::XAttributes >& xAttributes,
397             BasicElementBase* pParent, BasicImport* pImport,
398             const Reference< container::XNameContainer >& rxLib, const ::rtl::OUString& rName )
399         :BasicElementBase( rLocalName, xAttributes, pParent, pImport )
400         ,m_xLib( rxLib )
401         ,m_aName( rName )
402     {
403     }
404 
405     // -----------------------------------------------------------------------------
406     // XElement
407     // -----------------------------------------------------------------------------
408 
409     Reference< xml::input::XElement > BasicModuleElement::startChildElement(
410             sal_Int32 nUid, const ::rtl::OUString& rLocalName,
411             const Reference< xml::input::XAttributes >& xAttributes )
412         throw (xml::sax::SAXException, RuntimeException)
413     {
414         // TODO: <byte-code>
415 
416         Reference< xml::input::XElement > xElement;
417 
418         if ( nUid != m_pImport->XMLNS_UID )
419         {
420             throw xml::sax::SAXException(
421                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "illegal namespace!" ) ),
422                 Reference< XInterface >(), Any() );
423         }
424         else if ( rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "source-code" ) ) )
425         {
426             // TODO: password protected libraries
427 
428             if ( xAttributes.is() )
429             {
430                 if ( m_xLib.is() && m_aName.getLength() )
431                     xElement.set( new BasicSourceCodeElement( rLocalName, xAttributes, this, m_pImport, m_xLib, m_aName ) );
432             }
433         }
434         else
435         {
436             throw xml::sax::SAXException(
437                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "expected source-code element!" ) ),
438                 Reference< XInterface >(), Any() );
439         }
440 
441         return xElement;
442     }
443 
444     // -----------------------------------------------------------------------------
445 
446     void BasicModuleElement::endElement()
447         throw (xml::sax::SAXException, RuntimeException)
448     {
449     }
450 
451 
452     // =============================================================================
453     // BasicSourceCodeElement
454     // =============================================================================
455 
456     BasicSourceCodeElement::BasicSourceCodeElement( const ::rtl::OUString& rLocalName,
457             const Reference< xml::input::XAttributes >& xAttributes,
458             BasicElementBase* pParent, BasicImport* pImport,
459             const Reference< container::XNameContainer >& rxLib, const ::rtl::OUString& rName )
460         :BasicElementBase( rLocalName, xAttributes, pParent, pImport )
461         ,m_xLib( rxLib )
462         ,m_aName( rName )
463     {
464     }
465 
466     // -----------------------------------------------------------------------------
467     // XElement
468     // -----------------------------------------------------------------------------
469 
470     void BasicSourceCodeElement::characters( const ::rtl::OUString& rChars )
471         throw (xml::sax::SAXException, RuntimeException)
472     {
473         m_aBuffer.append( rChars );
474     }
475 
476     // -----------------------------------------------------------------------------
477 
478     void BasicSourceCodeElement::endElement()
479         throw (xml::sax::SAXException, RuntimeException)
480     {
481         try
482         {
483             if ( m_xLib.is() && m_aName.getLength() )
484             {
485                 Any aElement;
486 		        aElement <<= m_aBuffer.makeStringAndClear();
487 		        m_xLib->insertByName( m_aName, aElement );
488             }
489         }
490         catch ( container::ElementExistException& e )
491         {
492             OSL_TRACE( "BasicSourceCodeElement::endElement: caught ElementExceptionExist reason %s",
493                 ::rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
494         }
495         catch ( lang::IllegalArgumentException& e )
496         {
497             OSL_TRACE( "BasicSourceCodeElement::endElement: caught IllegalArgumentException reason %s",
498                 ::rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
499         }
500         catch ( lang::WrappedTargetException& e )
501         {
502             OSL_TRACE( "BasicSourceCodeElement::endElement: caught WrappedTargetException reason %s",
503                 ::rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).pData->buffer );
504         }
505     }
506 
507 
508     // =============================================================================
509     // BasicImport
510     // =============================================================================
511 
512     BasicImport::BasicImport( const Reference< frame::XModel >& rxModel, sal_Bool bOasis )
513         :m_xModel( rxModel )
514         ,m_bOasis( bOasis )
515     {
516     }
517 
518     // -----------------------------------------------------------------------------
519 
520     BasicImport::~BasicImport()
521     {
522     }
523 
524     // -----------------------------------------------------------------------------
525     // XRoot
526     // -----------------------------------------------------------------------------
527 
528     void BasicImport::startDocument( const Reference< xml::input::XNamespaceMapping >& xNamespaceMapping )
529         throw (xml::sax::SAXException, RuntimeException)
530     {
531         if ( xNamespaceMapping.is() )
532         {
533             ::rtl::OUString aURI;
534             if ( m_bOasis )
535                 aURI = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( XMLNS_OOO_URI ) );
536             else
537                 aURI = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( XMLNS_SCRIPT_URI ) );
538             XMLNS_UID = xNamespaceMapping->getUidByUri( aURI );
539             XMLNS_XLINK_UID = xNamespaceMapping->getUidByUri( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( XMLNS_XLINK_URI ) ) );
540         }
541     }
542 
543     // -----------------------------------------------------------------------------
544 
545     void BasicImport::endDocument()
546         throw (xml::sax::SAXException, RuntimeException)
547     {
548     }
549 
550     // -----------------------------------------------------------------------------
551 
552 void BasicImport::processingInstruction( const ::rtl::OUString& /*rTarget*/, const ::rtl::OUString& /*rData*/ )
553         throw (xml::sax::SAXException, RuntimeException)
554     {
555     }
556 
557     // -----------------------------------------------------------------------------
558 
559 void BasicImport::setDocumentLocator( const Reference< xml::sax::XLocator >& /*xLocator*/ )
560         throw (xml::sax::SAXException, RuntimeException)
561     {
562     }
563 
564     // -----------------------------------------------------------------------------
565 
566     Reference< xml::input::XElement > BasicImport::startRootElement( sal_Int32 nUid, const ::rtl::OUString& rLocalName,
567             Reference< xml::input::XAttributes > const & xAttributes )
568         throw (xml::sax::SAXException, RuntimeException)
569     {
570         Reference< xml::input::XElement > xElement;
571 
572         if ( nUid != XMLNS_UID )
573         {
574             throw xml::sax::SAXException(
575                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "illegal namespace!" ) ),
576                 Reference< XInterface >(), Any() );
577         }
578         else if ( rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "libraries" ) ) )
579         {
580             Reference< script::XLibraryContainer2 > xLibContainer;
581 
582             // try the XEmbeddedScripts interface
583             Reference< document::XEmbeddedScripts > xDocumentScripts( m_xModel, UNO_QUERY );
584             if ( xDocumentScripts.is() )
585                 xLibContainer.set( xDocumentScripts->getBasicLibraries().get() );
586 
587             if ( !xLibContainer.is() )
588             {
589                 // try the "BasicLibraries" property (old-style, for compatibility)
590                 Reference< beans::XPropertySet > xPSet( m_xModel, UNO_QUERY );
591                 if ( xPSet.is() )
592 	                xPSet->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "BasicLibraries" ) ) ) >>= xLibContainer;
593             }
594 
595             OSL_ENSURE( xLibContainer.is(), "BasicImport::startRootElement: nowhere to import to!" );
596 
597             if ( xLibContainer.is() )
598             {
599                 xElement.set( new BasicLibrariesElement( rLocalName, xAttributes, 0, this, xLibContainer ) );
600             }
601         }
602         else
603         {
604             throw xml::sax::SAXException(
605                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "illegal root element (expected libraries) given: " ) ) +
606                 rLocalName, Reference< XInterface >(), Any() );
607         }
608 
609         return xElement;
610     }
611 
612 
613     // =============================================================================
614     // component operations
615     // =============================================================================
616 
617     ::rtl::OUString getImplementationName_XMLBasicImporter()
618     {
619         static ::rtl::OUString* pImplName = 0;
620 	    if ( !pImplName )
621 	    {
622             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
623             if ( !pImplName )
624 		    {
625                 static ::rtl::OUString aImplName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.xmlscript.XMLBasicImporter" ) );
626 			    pImplName = &aImplName;
627 		    }
628 	    }
629 	    return *pImplName;
630     }
631 
632     // -----------------------------------------------------------------------------
633 
634     Sequence< ::rtl::OUString > getSupportedServiceNames_XMLBasicImporter()
635     {
636         static Sequence< ::rtl::OUString >* pNames = 0;
637 	    if ( !pNames )
638 	    {
639             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
640 		    if ( !pNames )
641 		    {
642                 static Sequence< ::rtl::OUString > aNames(1);
643                 aNames.getArray()[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.XMLBasicImporter" ) );
644                 pNames = &aNames;
645 		    }
646 	    }
647 	    return *pNames;
648     }
649 
650     // -----------------------------------------------------------------------------
651 
652     ::rtl::OUString getImplementationName_XMLOasisBasicImporter()
653     {
654         static ::rtl::OUString* pImplName = 0;
655 	    if ( !pImplName )
656 	    {
657             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
658             if ( !pImplName )
659 		    {
660                 static ::rtl::OUString aImplName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.xmlscript.XMLOasisBasicImporter" ) );
661 			    pImplName = &aImplName;
662 		    }
663 	    }
664 	    return *pImplName;
665     }
666 
667     // -----------------------------------------------------------------------------
668 
669     Sequence< ::rtl::OUString > getSupportedServiceNames_XMLOasisBasicImporter()
670     {
671         static Sequence< ::rtl::OUString >* pNames = 0;
672 	    if ( !pNames )
673 	    {
674             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
675 		    if ( !pNames )
676 		    {
677                 static Sequence< ::rtl::OUString > aNames(1);
678                 aNames.getArray()[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.XMLOasisBasicImporter" ) );
679                 pNames = &aNames;
680 		    }
681 	    }
682 	    return *pNames;
683     }
684 
685 
686     // =============================================================================
687     // XMLBasicImporterBase
688     // =============================================================================
689 
690     XMLBasicImporterBase::XMLBasicImporterBase( const Reference< XComponentContext >& rxContext, sal_Bool bOasis )
691         :m_xContext( rxContext )
692         ,m_bOasis( bOasis )
693     {
694     }
695 
696     // -----------------------------------------------------------------------------
697 
698     XMLBasicImporterBase::~XMLBasicImporterBase()
699     {
700     }
701 
702     // -----------------------------------------------------------------------------
703     // XServiceInfo
704     // -----------------------------------------------------------------------------
705 
706     sal_Bool XMLBasicImporterBase::supportsService( const ::rtl::OUString& rServiceName ) throw (RuntimeException)
707     {
708 	    Sequence< ::rtl::OUString > aNames( getSupportedServiceNames() );
709 	    const ::rtl::OUString* pNames = aNames.getConstArray();
710 	    const ::rtl::OUString* pEnd = pNames + aNames.getLength();
711 	    for ( ; pNames != pEnd && !pNames->equals( rServiceName ); ++pNames )
712 		    ;
713 
714 	    return pNames != pEnd;
715     }
716 
717     // -----------------------------------------------------------------------------
718     // XImporter
719     // -----------------------------------------------------------------------------
720 
721     void XMLBasicImporterBase::setTargetDocument( const Reference< XComponent >& rxDoc )
722 	    throw (IllegalArgumentException, RuntimeException)
723     {
724         ::osl::MutexGuard aGuard( m_aMutex );
725 
726         m_xModel.set( rxDoc, UNO_QUERY );
727 
728         if ( !m_xModel.is() )
729         {
730             throw IllegalArgumentException(
731                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "XMLBasicExporter::setTargetDocument: no document model!" ) ),
732                 Reference< XInterface >(), 1 );
733         }
734 
735         if ( m_xContext.is() )
736         {
737             Reference< XMultiComponentFactory > xSMgr( m_xContext->getServiceManager() );
738             if ( xSMgr.is() )
739             {
740                 Reference < xml::input::XRoot > xRoot( new BasicImport( m_xModel, m_bOasis ) );
741 	            Sequence < Any > aArgs( 1 );
742 	            aArgs[0] <<= xRoot;
743                 m_xHandler.set( xSMgr->createInstanceWithArgumentsAndContext(
744                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.input.SaxDocumentHandler" ) ),
745                     aArgs, m_xContext ), UNO_QUERY );
746             }
747         }
748     }
749 
750     // -----------------------------------------------------------------------------
751     // XDocumentHandler
752     // -----------------------------------------------------------------------------
753 
754     void XMLBasicImporterBase::startDocument()
755         throw (xml::sax::SAXException, RuntimeException)
756     {
757         ::osl::MutexGuard aGuard( m_aMutex );
758 
759         if ( m_xHandler.is() )
760             m_xHandler->startDocument();
761     }
762 
763     // -----------------------------------------------------------------------------
764 
765     void XMLBasicImporterBase::endDocument()
766         throw (xml::sax::SAXException, RuntimeException)
767     {
768         ::osl::MutexGuard aGuard( m_aMutex );
769 
770         if ( m_xHandler.is() )
771             m_xHandler->endDocument();
772     }
773 
774     // -----------------------------------------------------------------------------
775 
776     void XMLBasicImporterBase::startElement( const ::rtl::OUString& aName,
777             const Reference< xml::sax::XAttributeList >& xAttribs )
778         throw (xml::sax::SAXException, RuntimeException)
779     {
780         ::osl::MutexGuard aGuard( m_aMutex );
781 
782         if ( m_xHandler.is() )
783             m_xHandler->startElement( aName, xAttribs );
784     }
785 
786     // -----------------------------------------------------------------------------
787 
788     void XMLBasicImporterBase::endElement( const ::rtl::OUString& aName )
789         throw (xml::sax::SAXException, RuntimeException)
790     {
791         ::osl::MutexGuard aGuard( m_aMutex );
792 
793         if ( m_xHandler.is() )
794             m_xHandler->endElement( aName );
795     }
796 
797     // -----------------------------------------------------------------------------
798 
799     void XMLBasicImporterBase::characters( const ::rtl::OUString& aChars )
800         throw (xml::sax::SAXException, RuntimeException)
801     {
802         ::osl::MutexGuard aGuard( m_aMutex );
803 
804         if ( m_xHandler.is() )
805             m_xHandler->characters( aChars );
806     }
807 
808     // -----------------------------------------------------------------------------
809 
810     void XMLBasicImporterBase::ignorableWhitespace( const ::rtl::OUString& aWhitespaces )
811         throw (xml::sax::SAXException, RuntimeException)
812     {
813         ::osl::MutexGuard aGuard( m_aMutex );
814 
815         if ( m_xHandler.is() )
816             m_xHandler->ignorableWhitespace( aWhitespaces );
817     }
818 
819     // -----------------------------------------------------------------------------
820 
821     void XMLBasicImporterBase::processingInstruction( const ::rtl::OUString& aTarget,
822             const ::rtl::OUString& aData )
823         throw (xml::sax::SAXException, RuntimeException)
824     {
825         ::osl::MutexGuard aGuard( m_aMutex );
826 
827         if ( m_xHandler.is() )
828             m_xHandler->processingInstruction( aTarget, aData );
829     }
830 
831     // -----------------------------------------------------------------------------
832 
833     void XMLBasicImporterBase::setDocumentLocator( const Reference< xml::sax::XLocator >& xLocator )
834         throw (xml::sax::SAXException, RuntimeException)
835     {
836         ::osl::MutexGuard aGuard( m_aMutex );
837 
838         if ( m_xHandler.is() )
839             m_xHandler->setDocumentLocator( xLocator );
840     }
841 
842 
843     // =============================================================================
844     // XMLBasicImporter
845     // =============================================================================
846 
847     XMLBasicImporter::XMLBasicImporter( const Reference< XComponentContext >& rxContext )
848         :XMLBasicImporterBase( rxContext, sal_False )
849     {
850     }
851 
852     // -----------------------------------------------------------------------------
853 
854     XMLBasicImporter::~XMLBasicImporter()
855     {
856     }
857 
858     // -----------------------------------------------------------------------------
859     // XServiceInfo
860     // -----------------------------------------------------------------------------
861 
862     ::rtl::OUString XMLBasicImporter::getImplementationName(  ) throw (RuntimeException)
863     {
864         return getImplementationName_XMLBasicImporter();
865     }
866 
867     // -----------------------------------------------------------------------------
868 
869     Sequence< ::rtl::OUString > XMLBasicImporter::getSupportedServiceNames(  ) throw (RuntimeException)
870     {
871         return getSupportedServiceNames_XMLBasicImporter();
872     }
873 
874 
875     // =============================================================================
876     // XMLOasisBasicImporter
877     // =============================================================================
878 
879     XMLOasisBasicImporter::XMLOasisBasicImporter( const Reference< XComponentContext >& rxContext )
880         :XMLBasicImporterBase( rxContext, sal_True )
881     {
882     }
883 
884     // -----------------------------------------------------------------------------
885 
886     XMLOasisBasicImporter::~XMLOasisBasicImporter()
887     {
888     }
889 
890     // -----------------------------------------------------------------------------
891     // XServiceInfo
892     // -----------------------------------------------------------------------------
893 
894     ::rtl::OUString XMLOasisBasicImporter::getImplementationName(  ) throw (RuntimeException)
895     {
896         return getImplementationName_XMLOasisBasicImporter();
897     }
898 
899     // -----------------------------------------------------------------------------
900 
901     Sequence< ::rtl::OUString > XMLOasisBasicImporter::getSupportedServiceNames(  ) throw (RuntimeException)
902     {
903         return getSupportedServiceNames_XMLOasisBasicImporter();
904     }
905 
906 
907     // =============================================================================
908     // component operations
909     // =============================================================================
910 
911     Reference< XInterface > SAL_CALL create_XMLBasicImporter(
912         Reference< XComponentContext > const & xContext )
913         SAL_THROW( () )
914     {
915         return static_cast< lang::XTypeProvider * >( new XMLBasicImporter( xContext ) );
916     }
917 
918     // -----------------------------------------------------------------------------
919 
920     Reference< XInterface > SAL_CALL create_XMLOasisBasicImporter(
921         Reference< XComponentContext > const & xContext )
922         SAL_THROW( () )
923     {
924         return static_cast< lang::XTypeProvider * >( new XMLOasisBasicImporter( xContext ) );
925     }
926 
927     // -----------------------------------------------------------------------------
928 
929 //.........................................................................
930 }	// namespace xmlscript
931 //.........................................................................
932