Logo Search packages:      
Sourcecode: poco version File versions  Download package

DOMBuilder.cpp

//
// DOMBuilder.cpp
//
// $Id: //poco/1.2/XML/src/DOMBuilder.cpp#1 $
//
// Library: XML
// Package: DOM
// Module:  DOMBuilder
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
// 
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//


#include "Poco/DOM/DOMBuilder.h"
#include "Poco/DOM/Document.h"
#include "Poco/DOM/DocumentType.h"
#include "Poco/DOM/CharacterData.h"
#include "Poco/DOM/Text.h"
#include "Poco/DOM/Comment.h"
#include "Poco/DOM/CDATASection.h"
#include "Poco/DOM/Element.h"
#include "Poco/DOM/Attr.h"
#include "Poco/DOM/Entity.h"
#include "Poco/DOM/EntityReference.h"
#include "Poco/DOM/Notation.h"
#include "Poco/DOM/ProcessingInstruction.h"
#include "Poco/DOM/AutoPtr.h"
#include "Poco/SAX/XMLReader.h"
#include "Poco/SAX/AttributesImpl.h"


namespace Poco {
namespace XML {


const XMLString DOMBuilder::EMPTY_STRING;


DOMBuilder::DOMBuilder(XMLReader& xmlReader, NamePool* pNamePool):
      _xmlReader(xmlReader),
      _pNamePool(pNamePool),
      _pDocument(0),
      _pParent(0),
      _pPrevious(0),
      _inCDATA(false),
      _namespaces(true)
{
      _xmlReader.setContentHandler(this);
      _xmlReader.setDTDHandler(this);
      _xmlReader.setProperty(XMLReader::PROPERTY_LEXICAL_HANDLER, static_cast<LexicalHandler*>(this));

      if (_pNamePool) _pNamePool->duplicate();
}


00079 DOMBuilder::~DOMBuilder()
{
      if (_pNamePool) _pNamePool->release();
}


00085 Document* DOMBuilder::parse(const XMLString& uri)
{
      setupParse();
      _pDocument->suspendEvents();
      try
      {
            _xmlReader.parse(uri);
      }
      catch (...)
      {
            _pDocument->release();
            _pDocument = 0;
            _pParent   = 0;
            _pPrevious = 0;
            throw;
      }
      _pDocument->resumeEvents();
      _pDocument->collectGarbage();
      return _pDocument;
}


00107 Document* DOMBuilder::parse(InputSource* pInputSource)
{
      setupParse();
      _pDocument->suspendEvents();
      try
      {
            _xmlReader.parse(pInputSource);
      }
      catch (...)
      {
            _pDocument->release();
            _pDocument = 0;
            _pParent   = 0;
            _pPrevious = 0;
            throw;
      }
      _pDocument->resumeEvents();
      _pDocument->collectGarbage();
      return _pDocument;
}


void DOMBuilder::setupParse()
{
      _pDocument  = new Document(_pNamePool);
      _pParent    = _pDocument;
      _pPrevious  = 0;
      _inCDATA    = false;
      _namespaces = _xmlReader.getFeature(XMLReader::FEATURE_NAMESPACES);
}


inline void DOMBuilder::appendNode(AbstractNode* pNode)
{
      if (_pPrevious && _pPrevious != _pParent)
      {
            _pPrevious->_pNext = pNode;
            pNode->_pParent = _pParent;
            pNode->duplicate();
      }
      else _pParent->appendChild(pNode);
      _pPrevious = pNode;
}


00152 void DOMBuilder::notationDecl(const XMLString& name, const XMLString* publicId, const XMLString* systemId)
{
      DocumentType* pDoctype = _pDocument->getDoctype();
      if (pDoctype)
      {
            AutoPtr<Notation> pNotation = _pDocument->createNotation(name, (publicId ? *publicId : EMPTY_STRING), (systemId ? *systemId : EMPTY_STRING));
            pDoctype->appendChild(pNotation);
      }
}


00163 void DOMBuilder::unparsedEntityDecl(const XMLString& name, const XMLString* publicId, const XMLString& systemId, const XMLString& notationName)
{
      DocumentType* pDoctype = _pDocument->getDoctype();
      if (pDoctype)
      {
            AutoPtr<Entity> pEntity = _pDocument->createEntity(name, publicId ? *publicId : EMPTY_STRING, systemId, notationName);
            pDoctype->appendChild(pEntity);
      }
}


void DOMBuilder::setDocumentLocator(const Locator* loc)
{
}


00179 void DOMBuilder::startDocument()
{
}


00184 void DOMBuilder::endDocument()
{
}


00189 void DOMBuilder::startElement(const XMLString& uri, const XMLString& localName, const XMLString& qname, const Attributes& attributes)
{
      AutoPtr<Element> pElem = _namespaces ? _pDocument->createElementNS(uri, qname.empty() ? localName : qname) : _pDocument->createElement(qname);

      const AttributesImpl& attrs = dynamic_cast<const AttributesImpl&>(attributes);
      for (AttributesImpl::iterator it = attrs.begin(); it != attrs.end(); ++it)
      {
            AutoPtr<Attr> pAttr = new Attr(_pDocument, 0, it->namespaceURI, it->localName, it->qname, it->value, it->specified);
            if (_namespaces) 
                  pElem->setAttributeNodeNS(pAttr);
            else
                  pElem->setAttributeNode(pAttr);
      }
      appendNode(pElem);
      _pParent = pElem;
}


00207 void DOMBuilder::endElement(const XMLString& uri, const XMLString& localName, const XMLString& qname)
{
      _pPrevious = _pParent;
      _pParent   = static_cast<AbstractContainerNode*>(_pParent->parentNode());
}


00214 void DOMBuilder::characters(const XMLChar ch[], int start, int length)
{
      if (_inCDATA)
      {
            if (_pPrevious && _pPrevious->nodeType() == Node::CDATA_SECTION_NODE)
            {
                  static_cast<CDATASection*>(_pPrevious)->appendData(XMLString(ch + start, length));
            }
            else
            {
                  AutoPtr<CDATASection> pCDATA = _pDocument->createCDATASection(XMLString(ch + start, length));
                  appendNode(pCDATA);
            }
      }
      else
      {
            if (_pPrevious && _pPrevious->nodeType() == Node::TEXT_NODE)
            {
                  static_cast<Text*>(_pPrevious)->appendData(XMLString(ch + start, length));
            }
            else
            {
                  AutoPtr<Text> pText = _pDocument->createTextNode(XMLString(ch + start, length));
                  appendNode(pText);
            }
      }
}


00243 void DOMBuilder::ignorableWhitespace(const XMLChar ch[], int start, int length)
{
      characters(ch, start, length);
}


00249 void DOMBuilder::processingInstruction(const XMLString& target, const XMLString& data)
{
      AutoPtr<ProcessingInstruction> pPI = _pDocument->createProcessingInstruction(target, data);
      appendNode(pPI);
}


00256 void DOMBuilder::startPrefixMapping(const XMLString& prefix, const XMLString& uri)
{
}


00261 void DOMBuilder::endPrefixMapping(const XMLString& prefix)
{
}


00266 void DOMBuilder::skippedEntity(const XMLString& name)
{
      AutoPtr<EntityReference> pER = _pDocument->createEntityReference(name);
      appendNode(pER);
}


void DOMBuilder::startDTD(const XMLString& name, const XMLString& publicId, const XMLString& systemId)
{
      AutoPtr<DocumentType> pDoctype = new DocumentType(_pDocument, name, publicId, systemId);
      _pDocument->setDoctype(pDoctype);
}


00280 void DOMBuilder::endDTD()
{
}


00285 void DOMBuilder::startEntity(const XMLString& name)
{
}


00290 void DOMBuilder::endEntity(const XMLString& name)
{
}


00295 void DOMBuilder::startCDATA()
{
      _inCDATA = true;
}


00301 void DOMBuilder::endCDATA()
{
      _inCDATA = false;
}


00307 void DOMBuilder::comment(const XMLChar ch[], int start, int length)
{
      AutoPtr<Comment> pComment = _pDocument->createComment(XMLString(ch + start, length));
      appendNode(pComment);
}


} } // namespace Poco::XML

Generated by  Doxygen 1.6.0   Back to index