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