4
// $Id: //poco/1.2/XML/src/DOMBuilder.cpp#1 $
10
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
13
// Permission is hereby granted, free of charge, to any person or organization
14
// obtaining a copy of the software and accompanying documentation covered by
15
// this license (the "Software") to use, reproduce, display, distribute,
16
// execute, and transmit the Software, and to prepare derivative works of the
17
// Software, and to permit third-parties to whom the Software is furnished to
18
// do so, all subject to the following:
20
// The copyright notices in the Software and this entire statement, including
21
// the above license grant, this restriction and the following disclaimer,
22
// must be included in all copies of the Software, in whole or in part, and
23
// all derivative works of the Software, unless such copies or derivative
24
// works are solely in the form of machine-executable object code generated by
25
// a source language processor.
27
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
30
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
31
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
32
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33
// DEALINGS IN THE SOFTWARE.
37
#include "Poco/DOM/DOMBuilder.h"
38
#include "Poco/DOM/Document.h"
39
#include "Poco/DOM/DocumentType.h"
40
#include "Poco/DOM/CharacterData.h"
41
#include "Poco/DOM/Text.h"
42
#include "Poco/DOM/Comment.h"
43
#include "Poco/DOM/CDATASection.h"
44
#include "Poco/DOM/Element.h"
45
#include "Poco/DOM/Attr.h"
46
#include "Poco/DOM/Entity.h"
47
#include "Poco/DOM/EntityReference.h"
48
#include "Poco/DOM/Notation.h"
49
#include "Poco/DOM/ProcessingInstruction.h"
50
#include "Poco/DOM/AutoPtr.h"
51
#include "Poco/SAX/XMLReader.h"
52
#include "Poco/SAX/AttributesImpl.h"
59
const XMLString DOMBuilder::EMPTY_STRING;
62
DOMBuilder::DOMBuilder(XMLReader& xmlReader, NamePool* pNamePool):
63
_xmlReader(xmlReader),
64
_pNamePool(pNamePool),
71
_xmlReader.setContentHandler(this);
72
_xmlReader.setDTDHandler(this);
73
_xmlReader.setProperty(XMLReader::PROPERTY_LEXICAL_HANDLER, static_cast<LexicalHandler*>(this));
75
if (_pNamePool) _pNamePool->duplicate();
79
DOMBuilder::~DOMBuilder()
81
if (_pNamePool) _pNamePool->release();
85
Document* DOMBuilder::parse(const XMLString& uri)
88
_pDocument->suspendEvents();
91
_xmlReader.parse(uri);
95
_pDocument->release();
101
_pDocument->resumeEvents();
102
_pDocument->collectGarbage();
107
Document* DOMBuilder::parse(InputSource* pInputSource)
110
_pDocument->suspendEvents();
113
_xmlReader.parse(pInputSource);
117
_pDocument->release();
123
_pDocument->resumeEvents();
124
_pDocument->collectGarbage();
129
void DOMBuilder::setupParse()
131
_pDocument = new Document(_pNamePool);
132
_pParent = _pDocument;
135
_namespaces = _xmlReader.getFeature(XMLReader::FEATURE_NAMESPACES);
139
inline void DOMBuilder::appendNode(AbstractNode* pNode)
141
if (_pPrevious && _pPrevious != _pParent)
143
_pPrevious->_pNext = pNode;
144
pNode->_pParent = _pParent;
147
else _pParent->appendChild(pNode);
152
void DOMBuilder::notationDecl(const XMLString& name, const XMLString* publicId, const XMLString* systemId)
154
DocumentType* pDoctype = _pDocument->getDoctype();
157
AutoPtr<Notation> pNotation = _pDocument->createNotation(name, (publicId ? *publicId : EMPTY_STRING), (systemId ? *systemId : EMPTY_STRING));
158
pDoctype->appendChild(pNotation);
163
void DOMBuilder::unparsedEntityDecl(const XMLString& name, const XMLString* publicId, const XMLString& systemId, const XMLString& notationName)
165
DocumentType* pDoctype = _pDocument->getDoctype();
168
AutoPtr<Entity> pEntity = _pDocument->createEntity(name, publicId ? *publicId : EMPTY_STRING, systemId, notationName);
169
pDoctype->appendChild(pEntity);
174
void DOMBuilder::setDocumentLocator(const Locator* loc)
179
void DOMBuilder::startDocument()
184
void DOMBuilder::endDocument()
189
void DOMBuilder::startElement(const XMLString& uri, const XMLString& localName, const XMLString& qname, const Attributes& attributes)
191
AutoPtr<Element> pElem = _namespaces ? _pDocument->createElementNS(uri, qname.empty() ? localName : qname) : _pDocument->createElement(qname);
193
const AttributesImpl& attrs = dynamic_cast<const AttributesImpl&>(attributes);
194
for (AttributesImpl::iterator it = attrs.begin(); it != attrs.end(); ++it)
196
AutoPtr<Attr> pAttr = new Attr(_pDocument, 0, it->namespaceURI, it->localName, it->qname, it->value, it->specified);
198
pElem->setAttributeNodeNS(pAttr);
200
pElem->setAttributeNode(pAttr);
207
void DOMBuilder::endElement(const XMLString& uri, const XMLString& localName, const XMLString& qname)
209
_pPrevious = _pParent;
210
_pParent = static_cast<AbstractContainerNode*>(_pParent->parentNode());
214
void DOMBuilder::characters(const XMLChar ch[], int start, int length)
218
if (_pPrevious && _pPrevious->nodeType() == Node::CDATA_SECTION_NODE)
220
static_cast<CDATASection*>(_pPrevious)->appendData(XMLString(ch + start, length));
224
AutoPtr<CDATASection> pCDATA = _pDocument->createCDATASection(XMLString(ch + start, length));
230
if (_pPrevious && _pPrevious->nodeType() == Node::TEXT_NODE)
232
static_cast<Text*>(_pPrevious)->appendData(XMLString(ch + start, length));
236
AutoPtr<Text> pText = _pDocument->createTextNode(XMLString(ch + start, length));
243
void DOMBuilder::ignorableWhitespace(const XMLChar ch[], int start, int length)
245
characters(ch, start, length);
249
void DOMBuilder::processingInstruction(const XMLString& target, const XMLString& data)
251
AutoPtr<ProcessingInstruction> pPI = _pDocument->createProcessingInstruction(target, data);
256
void DOMBuilder::startPrefixMapping(const XMLString& prefix, const XMLString& uri)
261
void DOMBuilder::endPrefixMapping(const XMLString& prefix)
266
void DOMBuilder::skippedEntity(const XMLString& name)
268
AutoPtr<EntityReference> pER = _pDocument->createEntityReference(name);
273
void DOMBuilder::startDTD(const XMLString& name, const XMLString& publicId, const XMLString& systemId)
275
AutoPtr<DocumentType> pDoctype = new DocumentType(_pDocument, name, publicId, systemId);
276
_pDocument->setDoctype(pDoctype);
280
void DOMBuilder::endDTD()
285
void DOMBuilder::startEntity(const XMLString& name)
290
void DOMBuilder::endEntity(const XMLString& name)
295
void DOMBuilder::startCDATA()
301
void DOMBuilder::endCDATA()
307
void DOMBuilder::comment(const XMLChar ch[], int start, int length)
309
AutoPtr<Comment> pComment = _pDocument->createComment(XMLString(ch + start, length));
310
appendNode(pComment);
314
} } // namespace Poco::XML