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