LCOV - code coverage report
Current view: top level - unoxml/source/dom - saxbuilder.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 181 0.0 %
Date: 2014-04-14 Functions: 0 21 0.0 %
Legend: Lines: hit not hit

          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             : #ifdef _MSC_VER
      20             : #pragma warning(disable : 4701)
      21             : #endif
      22             : 
      23             : #include "saxbuilder.hxx"
      24             : 
      25             : #include <com/sun/star/xml/dom/DocumentBuilder.hpp>
      26             : #include <comphelper/processfactory.hxx>
      27             : #include <cppuhelper/supportsservice.hxx>
      28             : 
      29             : namespace DOM
      30             : {
      31           0 :     Reference< XInterface > CSAXDocumentBuilder::_getInstance(const Reference< XMultiServiceFactory >& rSMgr)
      32             :     {
      33           0 :         return static_cast< XSAXDocumentBuilder* >(new CSAXDocumentBuilder(rSMgr));
      34             :     }
      35             : 
      36             :     const char* CSAXDocumentBuilder::aImplementationName = "com.sun.star.comp.xml.dom.SAXDocumentBuilder";
      37             :     const char* CSAXDocumentBuilder::aSupportedServiceNames[] = {
      38             :         "com.sun.star.xml.dom.SAXDocumentBuilder",
      39             :         NULL
      40             :     };
      41             : 
      42           0 :     CSAXDocumentBuilder::CSAXDocumentBuilder(const Reference< XMultiServiceFactory >& mgr)
      43             :         : m_aServiceManager(mgr)
      44           0 :         , m_aState( SAXDocumentBuilderState_READY)
      45           0 :     {}
      46             : 
      47           0 :     OUString CSAXDocumentBuilder::_getImplementationName()
      48             :     {
      49           0 :         return OUString::createFromAscii(aImplementationName);
      50             :     }
      51           0 :     Sequence<OUString> CSAXDocumentBuilder::_getSupportedServiceNames()
      52             :     {
      53           0 :         Sequence<OUString> aSequence;
      54           0 :         for (int i=0; aSupportedServiceNames[i]!=NULL; i++) {
      55           0 :             aSequence.realloc(i+1);
      56           0 :             aSequence[i]=(OUString::createFromAscii(aSupportedServiceNames[i]));
      57             :         }
      58           0 :         return aSequence;
      59             :     }
      60             : 
      61           0 :     Sequence< OUString > SAL_CALL CSAXDocumentBuilder::getSupportedServiceNames()
      62             :         throw (RuntimeException, std::exception)
      63             :     {
      64           0 :         return CSAXDocumentBuilder::_getSupportedServiceNames();
      65             :     }
      66             : 
      67           0 :     OUString SAL_CALL CSAXDocumentBuilder::getImplementationName()
      68             :         throw (RuntimeException, std::exception)
      69             :     {
      70           0 :         return CSAXDocumentBuilder::_getImplementationName();
      71             :     }
      72             : 
      73           0 :     sal_Bool SAL_CALL CSAXDocumentBuilder::supportsService(const OUString& aServiceName)
      74             :         throw (RuntimeException, std::exception)
      75             :     {
      76           0 :         return cppu::supportsService(this, aServiceName);
      77             :     }
      78             : 
      79           0 :     SAXDocumentBuilderState SAL_CALL CSAXDocumentBuilder::getState()
      80             :         throw (RuntimeException, std::exception)
      81             :     {
      82           0 :         ::osl::MutexGuard g(m_Mutex);
      83             : 
      84           0 :         return m_aState;
      85             :     }
      86             : 
      87           0 :     void SAL_CALL CSAXDocumentBuilder::reset()
      88             :         throw (RuntimeException, std::exception)
      89             :     {
      90           0 :         ::osl::MutexGuard g(m_Mutex);
      91             : 
      92           0 :         m_aDocument.clear();
      93           0 :         m_aFragment.clear();
      94           0 :         while (!m_aNodeStack.empty()) m_aNodeStack.pop();
      95           0 :         while (!m_aNSStack.empty()) m_aNSStack.pop();
      96           0 :         m_aState = SAXDocumentBuilderState_READY;
      97           0 :     }
      98             : 
      99           0 :     Reference< XDocument > SAL_CALL CSAXDocumentBuilder::getDocument()
     100             :         throw (RuntimeException, std::exception)
     101             :     {
     102           0 :         ::osl::MutexGuard g(m_Mutex);
     103             : 
     104           0 :         if (m_aState != SAXDocumentBuilderState_DOCUMENT_FINISHED)
     105           0 :             throw RuntimeException();
     106             : 
     107           0 :         return m_aDocument;
     108             :     }
     109             : 
     110           0 :     Reference< XDocumentFragment > SAL_CALL CSAXDocumentBuilder::getDocumentFragment()
     111             :          throw (RuntimeException, std::exception)
     112             :     {
     113           0 :         ::osl::MutexGuard g(m_Mutex);
     114             : 
     115           0 :         if (m_aState != SAXDocumentBuilderState_FRAGMENT_FINISHED)
     116           0 :             throw RuntimeException();
     117           0 :         return m_aFragment;
     118             :     }
     119             : 
     120           0 :     void SAL_CALL CSAXDocumentBuilder::startDocumentFragment(const Reference< XDocument >& ownerDoc)
     121             :         throw (RuntimeException, std::exception)
     122             :     {
     123           0 :         ::osl::MutexGuard g(m_Mutex);
     124             : 
     125             :         // start a new document fragment and push it onto the stack
     126             :         // we have to be in a clean state to do this
     127           0 :         if (m_aState != SAXDocumentBuilderState_READY)
     128           0 :             throw RuntimeException();
     129             : 
     130           0 :         m_aDocument = ownerDoc;
     131           0 :         Reference< XDocumentFragment > aFragment = m_aDocument->createDocumentFragment();
     132           0 :         m_aNodeStack.push(aFragment);
     133           0 :         m_aFragment = aFragment;
     134           0 :         m_aState = SAXDocumentBuilderState_BUILDING_FRAGMENT;
     135           0 :     }
     136             : 
     137           0 :     void SAL_CALL CSAXDocumentBuilder::endDocumentFragment()
     138             :         throw (RuntimeException, std::exception)
     139             :     {
     140           0 :         ::osl::MutexGuard g(m_Mutex);
     141             : 
     142             :         // there should only be the document left on the node stack
     143           0 :         if (m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT)
     144           0 :             throw RuntimeException();
     145             : 
     146           0 :         Reference< XNode > aNode = m_aNodeStack.top();
     147           0 :         if ( aNode->getNodeType() != NodeType_DOCUMENT_FRAGMENT_NODE)
     148           0 :             throw RuntimeException();
     149           0 :         m_aNodeStack.pop();
     150           0 :         m_aState = SAXDocumentBuilderState_FRAGMENT_FINISHED;
     151           0 :     }
     152             : 
     153             :     // document handler
     154             : 
     155           0 :     void SAL_CALL  CSAXDocumentBuilder::startDocument() throw (RuntimeException, SAXException, std::exception)
     156             :     {
     157           0 :         ::osl::MutexGuard g(m_Mutex);
     158             : 
     159             :         // start a new document and push it onto the stack
     160             :         // we have to be in a clean state to do this
     161           0 :         if (m_aState != SAXDocumentBuilderState_READY)
     162           0 :             throw SAXException();
     163             : 
     164           0 :         Reference< XDocumentBuilder > aBuilder(DocumentBuilder::create(comphelper::getComponentContext(m_aServiceManager)));
     165           0 :         Reference< XDocument > aDocument = aBuilder->newDocument();
     166           0 :         m_aNodeStack.push(aDocument);
     167           0 :         m_aDocument = aDocument;
     168           0 :         m_aState = SAXDocumentBuilderState_BUILDING_DOCUMENT;
     169           0 :     }
     170             : 
     171           0 :     void SAL_CALL CSAXDocumentBuilder::endDocument() throw (RuntimeException, SAXException, std::exception)
     172             :     {
     173           0 :         ::osl::MutexGuard g(m_Mutex);
     174             : 
     175             :         // there should only be the document left on the node stack
     176           0 :         if (m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT)
     177           0 :             throw SAXException();
     178             : 
     179           0 :         Reference< XNode > aNode = m_aNodeStack.top();
     180           0 :         if ( aNode->getNodeType() != NodeType_DOCUMENT_NODE)
     181           0 :             throw SAXException();
     182           0 :         m_aNodeStack.pop();
     183           0 :         m_aState = SAXDocumentBuilderState_DOCUMENT_FINISHED;
     184           0 :     }
     185             : 
     186           0 :     void SAL_CALL CSAXDocumentBuilder::startElement(const OUString& aName, const Reference< XAttributeList>& attribs)
     187             :         throw (RuntimeException, SAXException, std::exception)
     188             :     {
     189           0 :         ::osl::MutexGuard g(m_Mutex);
     190             : 
     191           0 :         if ( m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT &&
     192           0 :              m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT)
     193             :         {
     194           0 :             throw SAXException();
     195             :         }
     196             : 
     197             :         // start with mappings in effect for last level
     198           0 :         NSMap aNSMap;
     199           0 :         if (!m_aNSStack.empty())
     200           0 :             aNSMap = NSMap(m_aNSStack.top());
     201             : 
     202             :         // handle xmlns: attributes and add to mappings
     203           0 :         OUString attr_qname;
     204           0 :         OUString attr_value;
     205           0 :         OUString newprefix;
     206           0 :         AttrMap aAttrMap;
     207           0 :         sal_Int32 idx=-1;
     208           0 :         sal_Int16 nAttributes = attribs->getLength();
     209           0 :         for (sal_Int16 i=0; i<nAttributes; i++)
     210             :         {
     211           0 :             attr_qname = attribs->getNameByIndex(i);
     212           0 :             attr_value = attribs->getValueByIndex(i);
     213             :             // new prefix mapping
     214           0 :             if (attr_qname.startsWith("xmlns:"))
     215             :             {
     216           0 :                 newprefix = attr_qname.copy(attr_qname.indexOf(':')+1);
     217           0 :                 aNSMap.insert(NSMap::value_type(newprefix, attr_value));
     218             :             }
     219           0 :             else if ( attr_qname == "xmlns" )
     220             :             {
     221             :                 // new default prefix
     222           0 :                 aNSMap.insert(NSMap::value_type(OUString(), attr_value));
     223             :             }
     224             :             else
     225             :             {
     226           0 :                 aAttrMap.insert(AttrMap::value_type(attr_qname, attr_value));
     227             :             }
     228             :         }
     229             : 
     230             :         // does the element have a prefix?
     231           0 :         OUString aPrefix;
     232           0 :         OUString aURI;
     233           0 :         Reference< XElement > aElement;
     234           0 :         idx = aName.indexOf(':');
     235           0 :         if (idx != -1)
     236             :         {
     237           0 :             aPrefix = aName.copy(0, idx);
     238             :         }
     239             :         else
     240           0 :             aPrefix = OUString();
     241             : 
     242           0 :         NSMap::const_iterator result = aNSMap.find(aPrefix);
     243           0 :         if ( result != aNSMap.end())
     244             :         {
     245             :             // found a URI for prefix
     246             :             // qualified name
     247           0 :             aElement = m_aDocument->createElementNS( result->second, aName);
     248             :         }
     249             :         else
     250             :         {
     251             :             // no URI for prefix
     252           0 :             aElement = m_aDocument->createElement(aName);
     253             :         }
     254           0 :         aElement = Reference< XElement > (
     255           0 :             m_aNodeStack.top()->appendChild(aElement),
     256           0 :             UNO_QUERY);
     257           0 :         m_aNodeStack.push(aElement);
     258             : 
     259             :         // set non xmlns attributes
     260           0 :         aPrefix = OUString();
     261           0 :         aURI = OUString();
     262           0 :         AttrMap::const_iterator a = aAttrMap.begin();
     263           0 :         while (a != aAttrMap.end())
     264             :         {
     265           0 :             attr_qname = a->first;
     266           0 :             attr_value = a->second;
     267           0 :             idx = attr_qname.indexOf(':');
     268           0 :             if (idx != -1)
     269           0 :                 aPrefix = attr_qname.copy(0, idx);
     270             :             else
     271           0 :                 aPrefix = OUString();
     272             : 
     273           0 :             result = aNSMap.find(aPrefix);
     274           0 :             if (result != aNSMap.end())
     275             :             {
     276             :                 // set attribute with namespace
     277           0 :                 aElement->setAttributeNS(result->second, attr_qname, attr_value);
     278             :             }
     279             :             else
     280             :             {
     281             :                 // set attribute without namespace
     282           0 :                 aElement->setAttribute(attr_qname, attr_value);
     283             :             }
     284           0 :             ++a;
     285             :         }
     286           0 :         m_aNSStack.push(aNSMap);
     287           0 :     }
     288             : 
     289           0 :     void SAL_CALL CSAXDocumentBuilder::endElement(const OUString& aName)
     290             :         throw (RuntimeException, SAXException, std::exception)
     291             :     {
     292           0 :         ::osl::MutexGuard g(m_Mutex);
     293             : 
     294             :         // pop the current element from the stack
     295           0 :         if ( m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT &&
     296           0 :              m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT)
     297           0 :             throw SAXException();
     298             : 
     299           0 :         Reference< XNode > aNode(m_aNodeStack.top());
     300           0 :         if (aNode->getNodeType() != NodeType_ELEMENT_NODE)
     301           0 :             throw SAXException();
     302             : 
     303           0 :         Reference< XElement > aElement(aNode, UNO_QUERY);
     304           0 :         OUString aRefName;
     305           0 :         OUString aPrefix = aElement->getPrefix();
     306           0 :         if (!aPrefix.isEmpty())
     307           0 :             aRefName = aPrefix + ":" + aElement->getTagName();
     308             :         else
     309           0 :             aRefName = aElement->getTagName();
     310           0 :         if (aRefName != aName) // consistency check
     311           0 :             throw SAXException();
     312             : 
     313             :         // pop it
     314           0 :         m_aNodeStack.pop();
     315           0 :         m_aNSStack.pop();
     316           0 :     }
     317             : 
     318           0 :     void SAL_CALL CSAXDocumentBuilder::characters(const OUString& aChars)
     319             :         throw (RuntimeException, SAXException, std::exception)
     320             :     {
     321           0 :         ::osl::MutexGuard g(m_Mutex);
     322             : 
     323             :         //  append text node to the current top element
     324           0 :          if (m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT &&
     325           0 :              m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT)
     326           0 :             throw SAXException();
     327             : 
     328           0 :          Reference< XText > aText = m_aDocument->createTextNode(aChars);
     329           0 :          m_aNodeStack.top()->appendChild(aText);
     330           0 :     }
     331             : 
     332           0 :     void SAL_CALL CSAXDocumentBuilder::ignorableWhitespace(const OUString& )
     333             :         throw (RuntimeException, SAXException, std::exception)
     334             :     {
     335           0 :         ::osl::MutexGuard g(m_Mutex);
     336             : 
     337             :         //  ignore ignorable whitespace
     338           0 :         if ( m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT &&
     339           0 :              m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT)
     340           0 :             throw SAXException();
     341           0 :     }
     342             : 
     343           0 :     void SAL_CALL CSAXDocumentBuilder::processingInstruction(const OUString& aTarget, const OUString& aData)
     344             :         throw (RuntimeException, SAXException, std::exception)
     345             :     {
     346           0 :         ::osl::MutexGuard g(m_Mutex);
     347             : 
     348             :         //  append PI node to the current top
     349           0 :         if ( m_aState != SAXDocumentBuilderState_BUILDING_DOCUMENT &&
     350           0 :              m_aState != SAXDocumentBuilderState_BUILDING_FRAGMENT)
     351           0 :             throw SAXException();
     352             : 
     353           0 :         Reference< XProcessingInstruction > aInstruction = m_aDocument->createProcessingInstruction(
     354           0 :                 aTarget, aData);
     355           0 :         m_aNodeStack.top()->appendChild(aInstruction);
     356           0 :     }
     357             : 
     358           0 :     void SAL_CALL CSAXDocumentBuilder::setDocumentLocator(const Reference< XLocator >& aLocator)
     359             :         throw (RuntimeException, SAXException, std::exception)
     360             :     {
     361           0 :         ::osl::MutexGuard g(m_Mutex);
     362             : 
     363             :         // set the document locator...
     364           0 :         m_aLocator = aLocator;
     365           0 :     }
     366             : }
     367             : 
     368             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10