Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "xml_import.hxx"
21 :
22 : #include "cppuhelper/factory.hxx"
23 : #include "cppuhelper/implementationentry.hxx"
24 : #include "cppuhelper/implbase1.hxx"
25 : #include "cppuhelper/implbase3.hxx"
26 :
27 : #include "com/sun/star/xml/input/XAttributes.hpp"
28 : #include "com/sun/star/lang/XInitialization.hpp"
29 : #include "com/sun/star/uno/XComponentContext.hpp"
30 :
31 : #include <vector>
32 : #include <boost/unordered_map.hpp>
33 :
34 : #include <memory>
35 :
36 : using namespace ::rtl;
37 : using namespace ::osl;
38 : using namespace ::com::sun::star;
39 : using namespace ::com::sun::star::uno;
40 :
41 : namespace xmlscript
42 : {
43 :
44 : const sal_Int32 UID_UNKNOWN = -1;
45 :
46 0 : Sequence< OUString > getSupportedServiceNames_DocumentHandlerImpl()
47 : {
48 0 : OUString name( "com.sun.star.xml.input.SaxDocumentHandler" );
49 0 : return Sequence< OUString >( &name, 1 );
50 : }
51 :
52 0 : OUString getImplementationName_DocumentHandlerImpl()
53 : {
54 0 : return OUString( "com.sun.star.comp.xml.input.SaxDocumentHandler" );
55 : }
56 :
57 : typedef ::boost::unordered_map< OUString, sal_Int32, OUStringHash > t_OUString2LongMap;
58 :
59 0 : struct PrefixEntry
60 : {
61 : ::std::vector< sal_Int32 > m_Uids;
62 :
63 0 : inline PrefixEntry() SAL_THROW(())
64 0 : { m_Uids.reserve( 4 ); }
65 : };
66 :
67 : typedef ::boost::unordered_map<
68 : OUString, PrefixEntry *, OUStringHash > t_OUString2PrefixMap;
69 :
70 0 : struct ElementEntry
71 : {
72 : Reference< xml::input::XElement > m_xElement;
73 : ::std::vector< OUString > m_prefixes;
74 :
75 0 : inline ElementEntry()
76 0 : { m_prefixes.reserve( 2 ); }
77 : };
78 :
79 : typedef ::std::vector< ElementEntry * > t_ElementVector;
80 :
81 : class ExtendedAttributes;
82 :
83 : //==============================================================================
84 : struct MGuard
85 : {
86 : Mutex * m_pMutex;
87 0 : explicit MGuard( Mutex * pMutex )
88 0 : : m_pMutex( pMutex )
89 0 : { if (m_pMutex) m_pMutex->acquire(); }
90 0 : ~MGuard() throw ()
91 0 : { if (m_pMutex) m_pMutex->release(); }
92 : };
93 :
94 : //==============================================================================
95 : class DocumentHandlerImpl :
96 : public ::cppu::WeakImplHelper3< xml::sax::XDocumentHandler,
97 : xml::input::XNamespaceMapping,
98 : lang::XInitialization >
99 : {
100 : friend class ExtendedAttributes;
101 :
102 : Reference< xml::input::XRoot > m_xRoot;
103 :
104 : t_OUString2LongMap m_URI2Uid;
105 : sal_Int32 m_uid_count;
106 :
107 : OUString m_sXMLNS_PREFIX_UNKNOWN;
108 : OUString m_sXMLNS;
109 :
110 : sal_Int32 m_nLastURI_lookup;
111 : OUString m_aLastURI_lookup;
112 :
113 : t_OUString2PrefixMap m_prefixes;
114 : sal_Int32 m_nLastPrefix_lookup;
115 : OUString m_aLastPrefix_lookup;
116 :
117 : t_ElementVector m_elements;
118 : sal_Int32 m_nSkipElements;
119 :
120 : Mutex * m_pMutex;
121 :
122 : inline Reference< xml::input::XElement > getCurrentElement() const;
123 :
124 : inline sal_Int32 getUidByURI( OUString const & rURI );
125 : inline sal_Int32 getUidByPrefix( OUString const & rPrefix );
126 :
127 : inline void pushPrefix(
128 : OUString const & rPrefix, OUString const & rURI );
129 : inline void popPrefix( OUString const & rPrefix );
130 :
131 : inline void getElementName(
132 : OUString const & rQName, sal_Int32 * pUid, OUString * pLocalName );
133 :
134 : public:
135 : DocumentHandlerImpl(
136 : Reference< xml::input::XRoot > const & xRoot,
137 : bool bSingleThreadedUse );
138 : virtual ~DocumentHandlerImpl() throw ();
139 :
140 : // XServiceInfo
141 : virtual OUString SAL_CALL getImplementationName()
142 : throw (RuntimeException);
143 : virtual sal_Bool SAL_CALL supportsService(
144 : OUString const & servicename )
145 : throw (RuntimeException);
146 : virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
147 : throw (RuntimeException);
148 :
149 : // XInitialization
150 : virtual void SAL_CALL initialize(
151 : Sequence< Any > const & arguments )
152 : throw (Exception);
153 :
154 : // XDocumentHandler
155 : virtual void SAL_CALL startDocument()
156 : throw (xml::sax::SAXException, RuntimeException);
157 : virtual void SAL_CALL endDocument()
158 : throw (xml::sax::SAXException, RuntimeException);
159 : virtual void SAL_CALL startElement(
160 : OUString const & rQElementName,
161 : Reference< xml::sax::XAttributeList > const & xAttribs )
162 : throw (xml::sax::SAXException, RuntimeException);
163 : virtual void SAL_CALL endElement(
164 : OUString const & rQElementName )
165 : throw (xml::sax::SAXException, RuntimeException);
166 : virtual void SAL_CALL characters(
167 : OUString const & rChars )
168 : throw (xml::sax::SAXException, RuntimeException);
169 : virtual void SAL_CALL ignorableWhitespace(
170 : OUString const & rWhitespaces )
171 : throw (xml::sax::SAXException, RuntimeException);
172 : virtual void SAL_CALL processingInstruction(
173 : OUString const & rTarget, OUString const & rData )
174 : throw (xml::sax::SAXException, RuntimeException);
175 : virtual void SAL_CALL setDocumentLocator(
176 : Reference< xml::sax::XLocator > const & xLocator )
177 : throw (xml::sax::SAXException, RuntimeException);
178 :
179 : // XNamespaceMapping
180 : virtual sal_Int32 SAL_CALL getUidByUri( OUString const & Uri )
181 : throw (RuntimeException);
182 : virtual OUString SAL_CALL getUriByUid( sal_Int32 Uid )
183 : throw (container::NoSuchElementException, RuntimeException);
184 : };
185 :
186 : //______________________________________________________________________________
187 0 : DocumentHandlerImpl::DocumentHandlerImpl(
188 : Reference< xml::input::XRoot > const & xRoot,
189 : bool bSingleThreadedUse )
190 : : m_xRoot( xRoot ),
191 : m_uid_count( 0 ),
192 : m_sXMLNS_PREFIX_UNKNOWN( "<<< unknown prefix >>>" ),
193 : m_sXMLNS( "xmlns" ),
194 : m_nLastURI_lookup( UID_UNKNOWN ),
195 : m_aLastURI_lookup( "<<< unknown URI >>>" ),
196 : m_nLastPrefix_lookup( UID_UNKNOWN ),
197 : m_aLastPrefix_lookup( "<<< unknown URI >>>" ),
198 : m_nSkipElements( 0 ),
199 0 : m_pMutex( 0 )
200 : {
201 0 : m_elements.reserve( 10 );
202 :
203 0 : if (! bSingleThreadedUse)
204 0 : m_pMutex = new Mutex();
205 0 : }
206 :
207 : //______________________________________________________________________________
208 0 : DocumentHandlerImpl::~DocumentHandlerImpl() throw ()
209 : {
210 0 : if (m_pMutex != 0)
211 : {
212 0 : delete m_pMutex;
213 : #if OSL_DEBUG_LEVEL == 0
214 0 : m_pMutex = 0;
215 : #endif
216 : }
217 0 : }
218 :
219 : //______________________________________________________________________________
220 : inline Reference< xml::input::XElement >
221 0 : DocumentHandlerImpl::getCurrentElement() const
222 : {
223 0 : MGuard aGuard( m_pMutex );
224 0 : if (m_elements.empty())
225 0 : return Reference< xml::input::XElement >();
226 : else
227 0 : return m_elements.back()->m_xElement;
228 : }
229 :
230 : //______________________________________________________________________________
231 0 : inline sal_Int32 DocumentHandlerImpl::getUidByURI( OUString const & rURI )
232 : {
233 0 : MGuard guard( m_pMutex );
234 0 : if (m_nLastURI_lookup == UID_UNKNOWN || m_aLastURI_lookup != rURI)
235 : {
236 0 : t_OUString2LongMap::const_iterator iFind( m_URI2Uid.find( rURI ) );
237 0 : if (iFind != m_URI2Uid.end()) // id found
238 : {
239 0 : m_nLastURI_lookup = iFind->second;
240 0 : m_aLastURI_lookup = rURI;
241 : }
242 : else
243 : {
244 0 : m_nLastURI_lookup = m_uid_count;
245 0 : ++m_uid_count;
246 0 : m_URI2Uid[ rURI ] = m_nLastURI_lookup;
247 0 : m_aLastURI_lookup = rURI;
248 : }
249 : }
250 0 : return m_nLastURI_lookup;
251 : }
252 :
253 : //______________________________________________________________________________
254 0 : inline sal_Int32 DocumentHandlerImpl::getUidByPrefix(
255 : OUString const & rPrefix )
256 : {
257 : // commonly the last added prefix is used often for several tags...
258 : // good guess
259 0 : if (m_nLastPrefix_lookup == UID_UNKNOWN || m_aLastPrefix_lookup != rPrefix)
260 : {
261 : t_OUString2PrefixMap::const_iterator iFind(
262 0 : m_prefixes.find( rPrefix ) );
263 0 : if (iFind != m_prefixes.end())
264 : {
265 0 : const PrefixEntry & rPrefixEntry = *iFind->second;
266 : OSL_ASSERT( ! rPrefixEntry.m_Uids.empty() );
267 0 : m_nLastPrefix_lookup = rPrefixEntry.m_Uids.back();
268 0 : m_aLastPrefix_lookup = rPrefix;
269 : }
270 : else
271 : {
272 0 : m_nLastPrefix_lookup = UID_UNKNOWN;
273 0 : m_aLastPrefix_lookup = m_sXMLNS_PREFIX_UNKNOWN;
274 : }
275 : }
276 0 : return m_nLastPrefix_lookup;
277 : }
278 :
279 : //______________________________________________________________________________
280 0 : inline void DocumentHandlerImpl::pushPrefix(
281 : OUString const & rPrefix, OUString const & rURI )
282 : {
283 : // lookup id for URI
284 0 : sal_Int32 nUid = getUidByURI( rURI );
285 :
286 : // mark prefix with id
287 0 : t_OUString2PrefixMap::const_iterator iFind( m_prefixes.find( rPrefix ) );
288 0 : if (iFind == m_prefixes.end()) // unused prefix
289 : {
290 0 : PrefixEntry * pEntry = new PrefixEntry();
291 0 : pEntry->m_Uids.push_back( nUid ); // latest id for prefix
292 0 : m_prefixes[ rPrefix ] = pEntry;
293 : }
294 : else
295 : {
296 0 : PrefixEntry * pEntry = iFind->second;
297 : OSL_ASSERT( ! pEntry->m_Uids.empty() );
298 0 : pEntry->m_Uids.push_back( nUid );
299 : }
300 :
301 0 : m_aLastPrefix_lookup = rPrefix;
302 0 : m_nLastPrefix_lookup = nUid;
303 0 : }
304 :
305 : //______________________________________________________________________________
306 0 : inline void DocumentHandlerImpl::popPrefix(
307 : OUString const & rPrefix )
308 : {
309 0 : t_OUString2PrefixMap::iterator iFind( m_prefixes.find( rPrefix ) );
310 0 : if (iFind != m_prefixes.end()) // unused prefix
311 : {
312 0 : PrefixEntry * pEntry = iFind->second;
313 0 : pEntry->m_Uids.pop_back(); // pop last id for prefix
314 0 : if (pEntry->m_Uids.empty()) // erase prefix key
315 : {
316 0 : m_prefixes.erase( iFind );
317 0 : delete pEntry;
318 : }
319 : }
320 :
321 0 : m_nLastPrefix_lookup = UID_UNKNOWN;
322 0 : m_aLastPrefix_lookup = m_sXMLNS_PREFIX_UNKNOWN;
323 0 : }
324 :
325 : //______________________________________________________________________________
326 0 : inline void DocumentHandlerImpl::getElementName(
327 : OUString const & rQName, sal_Int32 * pUid, OUString * pLocalName )
328 : {
329 0 : sal_Int32 nColonPos = rQName.indexOf( (sal_Unicode)':' );
330 0 : *pLocalName = (nColonPos >= 0 ? rQName.copy( nColonPos +1 ) : rQName);
331 : *pUid = getUidByPrefix(
332 0 : nColonPos >= 0 ? rQName.copy( 0, nColonPos ) : OUString() );
333 0 : }
334 :
335 :
336 : //==============================================================================
337 : class ExtendedAttributes :
338 : public ::cppu::WeakImplHelper1< xml::input::XAttributes >
339 : {
340 : sal_Int32 m_nAttributes;
341 : sal_Int32 * m_pUids;
342 : OUString * m_pPrefixes;
343 : OUString * m_pLocalNames;
344 : OUString * m_pQNames;
345 : OUString * m_pValues;
346 :
347 : DocumentHandlerImpl * m_pHandler;
348 :
349 : public:
350 : inline ExtendedAttributes(
351 : sal_Int32 nAttributes,
352 : sal_Int32 * pUids, OUString * pPrefixes,
353 : OUString * pLocalNames, OUString * pQNames,
354 : Reference< xml::sax::XAttributeList > const & xAttributeList,
355 : DocumentHandlerImpl * pHandler );
356 : virtual ~ExtendedAttributes() throw ();
357 :
358 : // XAttributes
359 : virtual sal_Int32 SAL_CALL getLength()
360 : throw (RuntimeException);
361 : virtual sal_Int32 SAL_CALL getIndexByQName(
362 : OUString const & rQName )
363 : throw (RuntimeException);
364 : virtual sal_Int32 SAL_CALL getIndexByUidName(
365 : sal_Int32 nUid, OUString const & rLocalName )
366 : throw (RuntimeException);
367 : virtual OUString SAL_CALL getQNameByIndex(
368 : sal_Int32 nIndex )
369 : throw (RuntimeException);
370 : virtual sal_Int32 SAL_CALL getUidByIndex(
371 : sal_Int32 nIndex )
372 : throw (RuntimeException);
373 : virtual OUString SAL_CALL getLocalNameByIndex(
374 : sal_Int32 nIndex )
375 : throw (RuntimeException);
376 : virtual OUString SAL_CALL getValueByIndex(
377 : sal_Int32 nIndex )
378 : throw (RuntimeException);
379 : virtual OUString SAL_CALL getValueByUidName(
380 : sal_Int32 nUid, OUString const & rLocalName )
381 : throw (RuntimeException);
382 : virtual OUString SAL_CALL getTypeByIndex(
383 : sal_Int32 nIndex )
384 : throw (RuntimeException);
385 : };
386 :
387 : //______________________________________________________________________________
388 0 : inline ExtendedAttributes::ExtendedAttributes(
389 : sal_Int32 nAttributes,
390 : sal_Int32 * pUids, OUString * pPrefixes,
391 : OUString * pLocalNames, OUString * pQNames,
392 : Reference< xml::sax::XAttributeList > const & xAttributeList,
393 : DocumentHandlerImpl * pHandler )
394 : : m_nAttributes( nAttributes )
395 : , m_pUids( pUids )
396 : , m_pPrefixes( pPrefixes )
397 : , m_pLocalNames( pLocalNames )
398 : , m_pQNames( pQNames )
399 0 : , m_pValues( new OUString[ nAttributes ] )
400 0 : , m_pHandler( pHandler )
401 : {
402 0 : m_pHandler->acquire();
403 :
404 0 : for ( sal_Int16 nPos = 0; nPos < nAttributes; ++nPos )
405 : {
406 0 : m_pValues[ nPos ] = xAttributeList->getValueByIndex( nPos );
407 : }
408 0 : }
409 :
410 : //______________________________________________________________________________
411 0 : ExtendedAttributes::~ExtendedAttributes() throw ()
412 : {
413 0 : m_pHandler->release();
414 :
415 0 : delete [] m_pUids;
416 0 : delete [] m_pPrefixes;
417 0 : delete [] m_pLocalNames;
418 0 : delete [] m_pQNames;
419 0 : delete [] m_pValues;
420 0 : }
421 :
422 :
423 : //##############################################################################
424 :
425 : // XServiceInfo
426 :
427 : //______________________________________________________________________________
428 0 : OUString DocumentHandlerImpl::getImplementationName()
429 : throw (RuntimeException)
430 : {
431 0 : return getImplementationName_DocumentHandlerImpl();
432 : }
433 :
434 : //______________________________________________________________________________
435 0 : sal_Bool DocumentHandlerImpl::supportsService(
436 : OUString const & servicename )
437 : throw (RuntimeException)
438 : {
439 0 : Sequence< OUString > names( getSupportedServiceNames_DocumentHandlerImpl() );
440 0 : for ( sal_Int32 nPos = names.getLength(); nPos--; )
441 : {
442 0 : if (names[ nPos ].equals( servicename ))
443 0 : return sal_True;
444 : }
445 0 : return sal_False;
446 : }
447 :
448 : //______________________________________________________________________________
449 0 : Sequence< OUString > DocumentHandlerImpl::getSupportedServiceNames()
450 : throw (RuntimeException)
451 : {
452 0 : return getSupportedServiceNames_DocumentHandlerImpl();
453 : }
454 :
455 : // XInitialization
456 :
457 : //______________________________________________________________________________
458 0 : void DocumentHandlerImpl::initialize(
459 : Sequence< Any > const & arguments )
460 : throw (Exception)
461 : {
462 0 : MGuard guard( m_pMutex );
463 0 : Reference< xml::input::XRoot > xRoot;
464 0 : if (arguments.getLength() == 1 &&
465 0 : (arguments[ 0 ] >>= xRoot) &&
466 0 : xRoot.is())
467 : {
468 0 : m_xRoot = xRoot;
469 : }
470 : else
471 : {
472 0 : throw RuntimeException( "missing root instance!", Reference< XInterface >() );
473 0 : }
474 0 : }
475 :
476 :
477 : // XNamespaceMapping
478 :
479 : //______________________________________________________________________________
480 0 : sal_Int32 DocumentHandlerImpl::getUidByUri( OUString const & Uri )
481 : throw (RuntimeException)
482 : {
483 0 : sal_Int32 uid = getUidByURI( Uri );
484 : OSL_ASSERT( uid != UID_UNKNOWN );
485 0 : return uid;
486 : }
487 :
488 : //______________________________________________________________________________
489 0 : OUString DocumentHandlerImpl::getUriByUid( sal_Int32 Uid )
490 : throw (container::NoSuchElementException, RuntimeException)
491 : {
492 0 : MGuard guard( m_pMutex );
493 0 : t_OUString2LongMap::const_iterator iPos( m_URI2Uid.begin() );
494 0 : t_OUString2LongMap::const_iterator const iEnd( m_URI2Uid.end() );
495 0 : for ( ; iPos != iEnd; ++iPos )
496 : {
497 0 : if (iPos->second == Uid)
498 0 : return iPos->first;
499 : }
500 0 : throw container::NoSuchElementException( "no such xmlns uid!" , static_cast< OWeakObject * >(this) );
501 : }
502 :
503 :
504 : // XDocumentHandler
505 :
506 : //______________________________________________________________________________
507 0 : void DocumentHandlerImpl::startDocument()
508 : throw (xml::sax::SAXException, RuntimeException)
509 : {
510 0 : m_xRoot->startDocument( static_cast< xml::input::XNamespaceMapping * >( this ) );
511 0 : }
512 :
513 : //______________________________________________________________________________
514 0 : void DocumentHandlerImpl::endDocument()
515 : throw (xml::sax::SAXException, RuntimeException)
516 : {
517 0 : m_xRoot->endDocument();
518 0 : }
519 :
520 : //______________________________________________________________________________
521 0 : void DocumentHandlerImpl::startElement(
522 : OUString const & rQElementName,
523 : Reference< xml::sax::XAttributeList > const & xAttribs )
524 : throw (xml::sax::SAXException, RuntimeException)
525 : {
526 0 : Reference< xml::input::XElement > xCurrentElement;
527 0 : Reference< xml::input::XAttributes > xAttributes;
528 : sal_Int32 nUid;
529 0 : OUString aLocalName;
530 0 : ::std::auto_ptr< ElementEntry > elementEntry( new ElementEntry );
531 :
532 : { // guard start:
533 0 : MGuard aGuard( m_pMutex );
534 : // currently skipping elements and waiting for end tags?
535 0 : if (m_nSkipElements > 0)
536 : {
537 0 : ++m_nSkipElements; // wait for another end tag
538 : #if OSL_DEBUG_LEVEL > 1
539 : OString aQName(
540 : OUStringToOString( rQElementName, RTL_TEXTENCODING_ASCII_US ) );
541 : OSL_TRACE( "### no context given on createChildElement() "
542 : "=> ignoring element \"%s\" ...", aQName.getStr() );
543 : #endif
544 0 : return;
545 : }
546 :
547 0 : sal_Int16 nAttribs = xAttribs->getLength();
548 :
549 : // save all namespace ids
550 0 : sal_Int32 * pUids = new sal_Int32[ nAttribs ];
551 0 : OUString * pPrefixes = new OUString[ nAttribs ];
552 0 : OUString * pLocalNames = new OUString[ nAttribs ];
553 0 : OUString * pQNames = new OUString[ nAttribs ];
554 :
555 : // first recognize all xmlns attributes
556 : sal_Int16 nPos;
557 0 : for ( nPos = 0; nPos < nAttribs; ++nPos )
558 : {
559 : // mark attribute to be collected further
560 : // on with attribute's uid and current prefix
561 0 : pUids[ nPos ] = 0; // modified
562 :
563 0 : pQNames[ nPos ] = xAttribs->getNameByIndex( nPos );
564 0 : OUString const & rQAttributeName = pQNames[ nPos ];
565 :
566 0 : if (rQAttributeName.compareTo( m_sXMLNS, 5 ) == 0)
567 : {
568 0 : if (rQAttributeName.getLength() == 5) // set default namespace
569 : {
570 0 : OUString aDefNamespacePrefix;
571 : pushPrefix(
572 : aDefNamespacePrefix,
573 0 : xAttribs->getValueByIndex( nPos ) );
574 0 : elementEntry->m_prefixes.push_back( aDefNamespacePrefix );
575 0 : pUids[ nPos ] = UID_UNKNOWN;
576 0 : pPrefixes[ nPos ] = m_sXMLNS;
577 0 : pLocalNames[ nPos ] = aDefNamespacePrefix;
578 : }
579 0 : else if ((sal_Unicode)':' == rQAttributeName[ 5 ]) // set prefix
580 : {
581 0 : OUString aPrefix( rQAttributeName.copy( 6 ) );
582 0 : pushPrefix( aPrefix, xAttribs->getValueByIndex( nPos ) );
583 0 : elementEntry->m_prefixes.push_back( aPrefix );
584 0 : pUids[ nPos ] = UID_UNKNOWN;
585 0 : pPrefixes[ nPos ] = m_sXMLNS;
586 0 : pLocalNames[ nPos ] = aPrefix;
587 : }
588 : // else just a name starting with xmlns, but no prefix
589 : }
590 : }
591 :
592 : // now read out attribute prefixes (all namespace prefixes have been set)
593 0 : for ( nPos = 0; nPos < nAttribs; ++nPos )
594 : {
595 0 : if (pUids[ nPos ] >= 0) // no xmlns: attribute
596 : {
597 0 : OUString const & rQAttributeName = pQNames[ nPos ];
598 : OSL_ENSURE(
599 : !rQAttributeName.startsWith( "xmlns:" ),
600 : "### unexpected xmlns!" );
601 :
602 : // collect attribute's uid and current prefix
603 0 : sal_Int32 nColonPos = rQAttributeName.indexOf( (sal_Unicode) ':' );
604 0 : if (nColonPos >= 0)
605 : {
606 0 : pPrefixes[ nPos ] = rQAttributeName.copy( 0, nColonPos );
607 0 : pLocalNames[ nPos ] = rQAttributeName.copy( nColonPos +1 );
608 : }
609 : else
610 : {
611 0 : pPrefixes[ nPos ] = OUString();
612 0 : pLocalNames[ nPos ] = rQAttributeName;
613 : // leave local names unmodified
614 : }
615 0 : pUids[ nPos ] = getUidByPrefix( pPrefixes[ nPos ] );
616 : }
617 : }
618 : // ownership of arrays belongs to attribute list
619 : xAttributes = static_cast< xml::input::XAttributes * >(
620 : new ExtendedAttributes(
621 : nAttribs, pUids, pPrefixes, pLocalNames, pQNames,
622 0 : xAttribs, this ) );
623 :
624 0 : getElementName( rQElementName, &nUid, &aLocalName );
625 :
626 : // create new child context and append to list
627 0 : if (! m_elements.empty())
628 0 : xCurrentElement = m_elements.back()->m_xElement;
629 : } // :guard end
630 :
631 0 : if (xCurrentElement.is())
632 : {
633 0 : elementEntry->m_xElement =
634 0 : xCurrentElement->startChildElement( nUid, aLocalName, xAttributes );
635 : }
636 : else
637 : {
638 0 : elementEntry->m_xElement =
639 0 : m_xRoot->startRootElement( nUid, aLocalName, xAttributes );
640 : }
641 :
642 : {
643 0 : MGuard aGuard( m_pMutex );
644 0 : if (elementEntry->m_xElement.is())
645 : {
646 0 : m_elements.push_back( elementEntry.release() );
647 : }
648 : else
649 : {
650 0 : ++m_nSkipElements;
651 : #if OSL_DEBUG_LEVEL > 1
652 : OString aQName(
653 : OUStringToOString( rQElementName, RTL_TEXTENCODING_ASCII_US ) );
654 : OSL_TRACE(
655 : "### no context given on createChildElement() => "
656 : "ignoring element \"%s\" ...", aQName.getStr() );
657 : #endif
658 0 : }
659 0 : }
660 : }
661 :
662 : //______________________________________________________________________________
663 0 : void DocumentHandlerImpl::endElement(
664 : OUString const & rQElementName )
665 : throw (xml::sax::SAXException, RuntimeException)
666 : {
667 0 : Reference< xml::input::XElement > xCurrentElement;
668 : {
669 0 : MGuard aGuard( m_pMutex );
670 0 : if (m_nSkipElements)
671 : {
672 0 : --m_nSkipElements;
673 : #if OSL_DEBUG_LEVEL > 1
674 : OString aQName(
675 : OUStringToOString( rQElementName, RTL_TEXTENCODING_ASCII_US ) );
676 : OSL_TRACE( "### received endElement() for \"%s\".", aQName.getStr() );
677 : #endif
678 : static_cast<void>(rQElementName);
679 0 : return;
680 : }
681 :
682 : // popping context
683 : OSL_ASSERT( ! m_elements.empty() );
684 0 : ElementEntry * pEntry = m_elements.back();
685 0 : xCurrentElement = pEntry->m_xElement;
686 :
687 : #if OSL_DEBUG_LEVEL > 0
688 : sal_Int32 nUid;
689 : OUString aLocalName;
690 : getElementName( rQElementName, &nUid, &aLocalName );
691 : OSL_ASSERT( xCurrentElement->getLocalName() == aLocalName );
692 : OSL_ASSERT( xCurrentElement->getUid() == nUid );
693 : #endif
694 :
695 : // pop prefixes
696 0 : for ( sal_Int32 nPos = pEntry->m_prefixes.size(); nPos--; )
697 : {
698 0 : popPrefix( pEntry->m_prefixes[ nPos ] );
699 : }
700 0 : m_elements.pop_back();
701 0 : delete pEntry;
702 : }
703 0 : xCurrentElement->endElement();
704 : }
705 :
706 : //______________________________________________________________________________
707 0 : void DocumentHandlerImpl::characters( OUString const & rChars )
708 : throw (xml::sax::SAXException, RuntimeException)
709 : {
710 0 : Reference< xml::input::XElement > xCurrentElement( getCurrentElement() );
711 0 : if (xCurrentElement.is())
712 0 : xCurrentElement->characters( rChars );
713 0 : }
714 :
715 : //______________________________________________________________________________
716 0 : void DocumentHandlerImpl::ignorableWhitespace(
717 : OUString const & rWhitespaces )
718 : throw (xml::sax::SAXException, RuntimeException)
719 : {
720 0 : Reference< xml::input::XElement > xCurrentElement( getCurrentElement() );
721 0 : if (xCurrentElement.is())
722 0 : xCurrentElement->ignorableWhitespace( rWhitespaces );
723 0 : }
724 :
725 : //______________________________________________________________________________
726 0 : void DocumentHandlerImpl::processingInstruction(
727 : OUString const & rTarget, OUString const & rData )
728 : throw (xml::sax::SAXException, RuntimeException)
729 : {
730 0 : Reference< xml::input::XElement > xCurrentElement( getCurrentElement() );
731 0 : if (xCurrentElement.is())
732 0 : xCurrentElement->processingInstruction( rTarget, rData );
733 : else
734 0 : m_xRoot->processingInstruction( rTarget, rData );
735 0 : }
736 :
737 : //______________________________________________________________________________
738 0 : void DocumentHandlerImpl::setDocumentLocator(
739 : Reference< xml::sax::XLocator > const & xLocator )
740 : throw (xml::sax::SAXException, RuntimeException)
741 : {
742 0 : m_xRoot->setDocumentLocator( xLocator );
743 0 : }
744 :
745 : //##############################################################################
746 :
747 : // XAttributes
748 :
749 : //______________________________________________________________________________
750 0 : sal_Int32 ExtendedAttributes::getIndexByQName( OUString const & rQName )
751 : throw (RuntimeException)
752 : {
753 0 : for ( sal_Int32 nPos = m_nAttributes; nPos--; )
754 : {
755 0 : if (m_pQNames[ nPos ].equals( rQName ))
756 : {
757 0 : return nPos;
758 : }
759 : }
760 0 : return -1;
761 : }
762 :
763 : //______________________________________________________________________________
764 0 : sal_Int32 ExtendedAttributes::getLength()
765 : throw (RuntimeException)
766 : {
767 0 : return m_nAttributes;
768 : }
769 :
770 : //______________________________________________________________________________
771 0 : OUString ExtendedAttributes::getLocalNameByIndex( sal_Int32 nIndex )
772 : throw (RuntimeException)
773 : {
774 0 : if (nIndex < m_nAttributes)
775 0 : return m_pLocalNames[ nIndex ];
776 : else
777 0 : return OUString();
778 : }
779 :
780 : //______________________________________________________________________________
781 0 : OUString ExtendedAttributes::getQNameByIndex( sal_Int32 nIndex )
782 : throw (RuntimeException)
783 : {
784 0 : if (nIndex < m_nAttributes)
785 0 : return m_pQNames[ nIndex ];
786 : else
787 0 : return OUString();
788 : }
789 :
790 : //______________________________________________________________________________
791 0 : OUString ExtendedAttributes::getTypeByIndex( sal_Int32 nIndex )
792 : throw (RuntimeException)
793 : {
794 : static_cast<void>(nIndex);
795 : OSL_ASSERT( nIndex < m_nAttributes );
796 0 : return OUString(); // unsupported
797 : }
798 :
799 : //______________________________________________________________________________
800 0 : OUString ExtendedAttributes::getValueByIndex( sal_Int32 nIndex )
801 : throw (RuntimeException)
802 : {
803 0 : if (nIndex < m_nAttributes)
804 0 : return m_pValues[ nIndex ];
805 : else
806 0 : return OUString();
807 : }
808 :
809 : //______________________________________________________________________________
810 0 : sal_Int32 ExtendedAttributes::getIndexByUidName(
811 : sal_Int32 nUid, OUString const & rLocalName )
812 : throw (RuntimeException)
813 : {
814 0 : for ( sal_Int32 nPos = m_nAttributes; nPos--; )
815 : {
816 0 : if (m_pUids[ nPos ] == nUid && m_pLocalNames[ nPos ] == rLocalName)
817 : {
818 0 : return nPos;
819 : }
820 : }
821 0 : return -1;
822 : }
823 :
824 : //______________________________________________________________________________
825 0 : sal_Int32 ExtendedAttributes::getUidByIndex( sal_Int32 nIndex )
826 : throw (RuntimeException)
827 : {
828 0 : if (nIndex < m_nAttributes)
829 0 : return m_pUids[ nIndex ];
830 : else
831 0 : return -1;
832 : }
833 :
834 : //______________________________________________________________________________
835 0 : OUString ExtendedAttributes::getValueByUidName(
836 : sal_Int32 nUid, OUString const & rLocalName )
837 : throw (RuntimeException)
838 : {
839 0 : for ( sal_Int32 nPos = m_nAttributes; nPos--; )
840 : {
841 0 : if (m_pUids[ nPos ] == nUid && m_pLocalNames[ nPos ] == rLocalName)
842 : {
843 0 : return m_pValues[ nPos ];
844 : }
845 : }
846 0 : return OUString();
847 : }
848 :
849 :
850 : //##############################################################################
851 :
852 :
853 : //==============================================================================
854 0 : Reference< xml::sax::XDocumentHandler > SAL_CALL createDocumentHandler(
855 : Reference< xml::input::XRoot > const & xRoot,
856 : bool bSingleThreadedUse )
857 : SAL_THROW(())
858 : {
859 : OSL_ASSERT( xRoot.is() );
860 0 : if (xRoot.is())
861 : {
862 : return static_cast< xml::sax::XDocumentHandler * >(
863 0 : new DocumentHandlerImpl( xRoot, bSingleThreadedUse ) );
864 : }
865 0 : return Reference< xml::sax::XDocumentHandler >();
866 : }
867 :
868 : //------------------------------------------------------------------------------
869 0 : Reference< XInterface > SAL_CALL create_DocumentHandlerImpl(
870 : SAL_UNUSED_PARAMETER Reference< XComponentContext > const & )
871 : SAL_THROW( (Exception) )
872 : {
873 : return static_cast< ::cppu::OWeakObject * >(
874 : new DocumentHandlerImpl(
875 0 : Reference< xml::input::XRoot >(), false /* mt use */ ) );
876 : }
877 :
878 : }
879 :
880 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|