Newer
Older
Khaled Noaman
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xerces" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache\@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation, and was
* originally based on software copyright (c) 2001, International
* Business Machines, Inc., http://www.ibm.com . For more information
* on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
/**
* This file contains code to build the DOM tree. It registers a document
* handler with the scanner. In these handler methods, appropriate DOM nodes
* are created and added to the DOM tree.
*
* $Id$
*
*/
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#include <xercesc/parsers/DOMBuilderImpl.hpp>
#include <xercesc/util/IOException.hpp>
#include <xercesc/dom/DOMEntityResolver.hpp>
#include <xercesc/dom/DOMErrorHandler.hpp>
#include <xercesc/dom/impl/DOMErrorImpl.hpp>
#include <xercesc/dom/impl/DOMLocatorImpl.hpp>
#include <xercesc/dom/DOMException.hpp>
#include <xercesc/sax/SAXParseException.hpp>
#include <xercesc/internal/XMLScanner.hpp>
#include <xercesc/util/DOMInputSourceWrapper.hpp>
// ---------------------------------------------------------------------------
// DOMBuilderImpl: Constructors and Destructor
// ---------------------------------------------------------------------------
DOMBuilderImpl::DOMBuilderImpl(XMLValidator* const valToAdopt) :
AbstractDOMParser(valToAdopt)
, fAutoValidation(false)
, fValidation(false)
, fErrorHandler(0)
, fEntityResolver(0)
, fFilter(0)
, fReuseGrammar(false)
, fCharsetOverridesXMLEncoding(true)
Khaled Noaman
committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
{
}
DOMBuilderImpl::~DOMBuilderImpl()
{
}
// ---------------------------------------------------------------------------
// DOMBuilderImpl: Setter methods
// ---------------------------------------------------------------------------
void DOMBuilderImpl::setErrorHandler(DOMErrorHandler* const handler)
{
fErrorHandler = handler;
if (fErrorHandler) {
getScanner()->setErrorReporter(this);
}
else {
getScanner()->setErrorReporter(0);
}
}
void DOMBuilderImpl::setEntityResolver(DOMEntityResolver* const handler)
{
fEntityResolver = handler;
if (fEntityResolver) {
getScanner()->setEntityHandler(this);
}
else {
getScanner()->setEntityHandler(0);
}
}
void DOMBuilderImpl::setFilter(DOMBuilderFilter* const filter)
{
throw DOMException(DOMException::NOT_SUPPORTED_ERR, 0);
}
// ---------------------------------------------------------------------------
// DOMBuilderImpl: Feature methods
// ---------------------------------------------------------------------------
void DOMBuilderImpl::setFeature(const XMLCh* const name, const bool state)
{
if (XMLString::compareIString(name, XMLUni::fgDOMEntities) == 0) {
Khaled Noaman
committed
setCreateEntityReferenceNodes(state);
}
else if (XMLString::compareIString(name, XMLUni::fgDOMComments) == 0) {
setCreateCommentNodes(state);
}
else if (XMLString::compareIString(name, XMLUni::fgDOMDatatypeNormalization) == 0) {
getScanner()->setNormalizeData(state);
}
else if (XMLString::compareIString(name, XMLUni::fgDOMNamespaces) == 0) {
Khaled Noaman
committed
setDoNamespaces(state);
}
else if (XMLString::compareIString(name, XMLUni::fgDOMWhitespaceInElementContent) == 0) {
Khaled Noaman
committed
setIncludeIgnorableWhitespace(state);
}
else if (XMLString::compareIString(name, XMLUni::fgDOMValidation) == 0) {
Khaled Noaman
committed
fValidation = state;
if (state) {
if (getValidationScheme() == AbstractDOMParser::Val_Never)
setValidationScheme(AbstractDOMParser::Val_Always);
}
else {
setValidationScheme(AbstractDOMParser::Val_Never);
}
}
else if (XMLString::compareIString(name, XMLUni::fgDOMValidateIfSchema) == 0) {
Khaled Noaman
committed
fAutoValidation = state;
if (state) {
setValidationScheme(AbstractDOMParser::Val_Auto);
}
else {
setValidationScheme(AbstractDOMParser::Val_Never);
}
}
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
else if (XMLString::compareIString(name, XMLUni::fgDOMCharsetOverridesXMLEncoding) == 0) {
// in fact, setting this has no effect to the parser
fCharsetOverridesXMLEncoding = state;
}
else if (XMLString::compareIString(name, XMLUni::fgDOMSupportedMediatypesOnly) == 0 ||
XMLString::compareIString(name, XMLUni::fgDOMInfoset) == 0 ||
XMLString::compareIString(name, XMLUni::fgDOMCanonicalForm) == 0 ) {
if (state)
throw DOMException(DOMException::NOT_SUPPORTED_ERR, 0);
}
else if (XMLString::compareIString(name, XMLUni::fgDOMNamespaceDeclarations) == 0 ||
XMLString::compareIString(name, XMLUni::fgDOMCDATASections) == 0 ) {
if (!state)
throw DOMException(DOMException::NOT_SUPPORTED_ERR, 0);
}
else if (XMLString::compareIString(name, XMLUni::fgXercesReuseGrammar) == 0)
{
fReuseGrammar = state;
}
else if (XMLString::compareIString(name, XMLUni::fgXercesSchema) == 0)
{
setDoSchema(state);
}
else if (XMLString::compareIString(name, XMLUni::fgXercesSchemaFullChecking) == 0)
{
setValidationSchemaFullChecking(state);
}
else if (XMLString::compareIString(name, XMLUni::fgXercesLoadExternalDTD) == 0)
{
setLoadExternalDTD(state);
}
else if (XMLString::compareIString(name, XMLUni::fgXercesContinueAfterFatalError) == 0)
{
setExitOnFirstFatalError(!state);
}
else if (XMLString::compareIString(name, XMLUni::fgXercesValidationErrorAsFatal) == 0)
{
setValidationConstraintFatal(state);
}
Khaled Noaman
committed
else {
throw DOMException(DOMException::NOT_FOUND_ERR, 0);
Khaled Noaman
committed
}
}
bool DOMBuilderImpl::getFeature(const XMLCh* const name)
{
if (XMLString::compareIString(name, XMLUni::fgDOMEntities) == 0) {
Khaled Noaman
committed
return getCreateEntityReferenceNodes();
}
else if (XMLString::compareIString(name, XMLUni::fgDOMComments) == 0) {
return getCreateCommentNodes();
}
else if (XMLString::compareIString(name, XMLUni::fgDOMDatatypeNormalization) == 0) {
return getScanner()->getNormalizeData();
}
else if (XMLString::compareIString(name, XMLUni::fgDOMNamespaces) == 0) {
Khaled Noaman
committed
return getDoNamespaces();
}
else if (XMLString::compareIString(name, XMLUni::fgDOMWhitespaceInElementContent) == 0) {
Khaled Noaman
committed
return getIncludeIgnorableWhitespace();
}
else if (XMLString::compareIString(name, XMLUni::fgDOMValidation) == 0) {
Khaled Noaman
committed
return fValidation;
}
else if (XMLString::compareIString(name, XMLUni::fgDOMValidateIfSchema) == 0) {
Khaled Noaman
committed
return fAutoValidation;
}
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
else if (XMLString::compareIString(name, XMLUni::fgDOMCharsetOverridesXMLEncoding) == 0) {
return fCharsetOverridesXMLEncoding;
}
else if (XMLString::compareIString(name, XMLUni::fgDOMSupportedMediatypesOnly) == 0 ||
XMLString::compareIString(name, XMLUni::fgDOMInfoset) == 0 ||
XMLString::compareIString(name, XMLUni::fgDOMCanonicalForm) == 0 ) {
return false;
}
else if (XMLString::compareIString(name, XMLUni::fgDOMNamespaceDeclarations) == 0 ||
XMLString::compareIString(name, XMLUni::fgDOMCDATASections) == 0 ) {
return true;
}
else if (XMLString::compareIString(name, XMLUni::fgXercesReuseGrammar) == 0)
{
return fReuseGrammar;
}
else if (XMLString::compareIString(name, XMLUni::fgXercesSchema) == 0)
{
return getDoSchema();
}
else if (XMLString::compareIString(name, XMLUni::fgXercesSchemaFullChecking) == 0)
{
return getValidationSchemaFullChecking();
}
else if (XMLString::compareIString(name, XMLUni::fgXercesLoadExternalDTD) == 0)
{
return getLoadExternalDTD();
}
else if (XMLString::compareIString(name, XMLUni::fgXercesContinueAfterFatalError) == 0)
{
return !getExitOnFirstFatalError();
}
else if (XMLString::compareIString(name, XMLUni::fgXercesValidationErrorAsFatal) == 0)
{
return getValidationConstraintFatal();
}
Khaled Noaman
committed
else {
throw DOMException(DOMException::NOT_FOUND_ERR, 0);
Khaled Noaman
committed
}
return false;
}
bool DOMBuilderImpl::canSetFeature(const XMLCh* const name, const bool state)
{
if ((XMLString::compareIString(name, XMLUni::fgDOMEntities) == 0) ||
(XMLString::compareIString(name, XMLUni::fgDOMComments) == 0) ||
(XMLString::compareIString(name, XMLUni::fgDOMDatatypeNormalization) == 0) ||
(XMLString::compareIString(name, XMLUni::fgDOMNamespaces) == 0) ||
(XMLString::compareIString(name, XMLUni::fgDOMValidation) == 0) ||
(XMLString::compareIString(name, XMLUni::fgDOMValidateIfSchema) == 0) ||
(XMLString::compareIString(name, XMLUni::fgDOMCharsetOverridesXMLEncoding) == 0) ||
(XMLString::compareIString(name, XMLUni::fgDOMWhitespaceInElementContent) == 0)) {
Khaled Noaman
committed
return true;
}
else if (XMLString::compareIString(name, XMLUni::fgDOMSupportedMediatypesOnly) == 0 ||
XMLString::compareIString(name, XMLUni::fgDOMInfoset) == 0 ||
XMLString::compareIString(name, XMLUni::fgDOMCanonicalForm) == 0 ) {
if (!state)
return true;
}
else if (XMLString::compareIString(name, XMLUni::fgDOMNamespaceDeclarations) == 0 ||
XMLString::compareIString(name, XMLUni::fgDOMCDATASections) == 0 ) {
if (state)
return true;
}
else if ((XMLString::compareIString(name, XMLUni::fgXercesReuseGrammar) == 0) ||
(XMLString::compareIString(name, XMLUni::fgXercesSchema) == 0) ||
(XMLString::compareIString(name, XMLUni::fgXercesSchemaFullChecking) == 0) ||
(XMLString::compareIString(name, XMLUni::fgXercesLoadExternalDTD) == 0) ||
(XMLString::compareIString(name, XMLUni::fgXercesContinueAfterFatalError) == 0) ||
(XMLString::compareIString(name, XMLUni::fgXercesValidationErrorAsFatal) == 0)) {
return true;
}
Khaled Noaman
committed
return false;
}
void DOMBuilderImpl::setProperty(const XMLCh* const name, void* value)
{
if (XMLString::compareIString(name, XMLUni::fgXercesSchemaExternalSchemaLocation) == 0)
{
setExternalSchemaLocation((XMLCh*)value);
}
else if (XMLString::compareIString(name, XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation) == 0)
{
setExternalNoNamespaceSchemaLocation((XMLCh*)value);
}
else
throw DOMException(DOMException::NOT_FOUND_ERR, 0);
}
void* DOMBuilderImpl::getProperty(const XMLCh* const name) const
{
if (XMLString::compareIString(name, XMLUni::fgXercesSchemaExternalSchemaLocation) == 0)
return (void*)getExternalSchemaLocation();
else if (XMLString::compareIString(name, XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation) == 0)
return (void*)getExternalNoNamespaceSchemaLocation();
else
throw DOMException(DOMException::NOT_FOUND_ERR, 0);
return 0;
}
Khaled Noaman
committed
// ---------------------------------------------------------------------------
// DOMBuilderImpl: Parsing methods
// ---------------------------------------------------------------------------
DOMDocument* DOMBuilderImpl::parse(const DOMInputSource& source)
Khaled Noaman
committed
{
DOMInputSourceWrapper isWrapper((DOMInputSource*) &source);
isWrapper.setAdoptInputSource(false);
AbstractDOMParser::parse(isWrapper, fReuseGrammar);
Khaled Noaman
committed
return getDocument();
}
DOMDocument* DOMBuilderImpl::parseURI(const XMLCh* const systemId)
Khaled Noaman
committed
{
AbstractDOMParser::parse(systemId, fReuseGrammar);
Khaled Noaman
committed
return getDocument();
}
DOMDocument* DOMBuilderImpl::parseURI(const char* const systemId)
Khaled Noaman
committed
{
AbstractDOMParser::parse(systemId, fReuseGrammar);
Khaled Noaman
committed
return getDocument();
}
void DOMBuilderImpl::parseWithContext(const DOMInputSource& source,
DOMNode* const contextNode,
const short action)
{
throw DOMException(DOMException::NOT_SUPPORTED_ERR, 0);
}
// ---------------------------------------------------------------------------
// DOMBuilderImpl: Implementation of the XMLErrorReporter interface
// ---------------------------------------------------------------------------
void DOMBuilderImpl::error( const unsigned int code
, const XMLCh* const msgDomain
, const XMLErrorReporter::ErrTypes errType
, const XMLCh* const errorText
, const XMLCh* const systemId
, const XMLCh* const publicId
, const XMLSSize_t lineNum
, const XMLSSize_t colNum)
{
if (fErrorHandler) {
short severity = DOMError::DOM_SEVERITY_ERROR;
Khaled Noaman
committed
if (errType == XMLErrorReporter::ErrType_Warning)
severity = DOMError::DOM_SEVERITY_WARNING;
Khaled Noaman
committed
else if (errType == XMLErrorReporter::ErrType_Fatal)
severity = DOMError::DOM_SEVERITY_FATAL_ERROR;
Khaled Noaman
committed
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
DOMLocatorImpl location((int)lineNum, (int) colNum, getCurrentNode(), systemId);
DOMErrorImpl domError(severity, errorText, &location);
fErrorHandler->handleError(domError);
}
}
void DOMBuilderImpl::resetErrors()
{
}
// ---------------------------------------------------------------------------
// DOMBuilderImpl: Implementation of XMLEntityHandler interface
// ---------------------------------------------------------------------------
InputSource*
DOMBuilderImpl::resolveEntity(const XMLCh* const publicId,
const XMLCh* const systemId,
const XMLCh* const baseURI)
{
//
// Just map it to the SAX entity resolver. If there is not one installed,
// return a null pointer to cause the default resolution.
//
if (fEntityResolver) {
DOMInputSource* is = fEntityResolver->resolveEntity(publicId, systemId, baseURI);
if (is)
return new DOMInputSourceWrapper(is);
}
return 0;
}