1 | // |
2 | // DOMParser.cpp |
3 | // |
4 | // Library: XML |
5 | // Package: DOM |
6 | // Module: DOMParser |
7 | // |
8 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/DOM/DOMParser.h" |
16 | #include "Poco/DOM/DOMBuilder.h" |
17 | #include "Poco/SAX/WhitespaceFilter.h" |
18 | #include "Poco/SAX/InputSource.h" |
19 | #include "Poco/XML/NamePool.h" |
20 | #include <sstream> |
21 | |
22 | |
23 | namespace Poco { |
24 | namespace XML { |
25 | |
26 | |
27 | const XMLString DOMParser::FEATURE_FILTER_WHITESPACE = toXMLString("http://www.appinf.com/features/no-whitespace-in-element-content" ); |
28 | |
29 | |
30 | DOMParser::DOMParser(NamePool* pNamePool): |
31 | _pNamePool(pNamePool), |
32 | _filterWhitespace(false) |
33 | { |
34 | if (_pNamePool) _pNamePool->duplicate(); |
35 | _saxParser.setFeature(XMLReader::FEATURE_NAMESPACES, true); |
36 | _saxParser.setFeature(XMLReader::FEATURE_NAMESPACE_PREFIXES, true); |
37 | } |
38 | |
39 | |
40 | DOMParser::DOMParser(unsigned long namePoolSize): |
41 | _pNamePool(new NamePool(namePoolSize)), |
42 | _filterWhitespace(false) |
43 | { |
44 | _saxParser.setFeature(XMLReader::FEATURE_NAMESPACES, true); |
45 | _saxParser.setFeature(XMLReader::FEATURE_NAMESPACE_PREFIXES, true); |
46 | } |
47 | |
48 | |
49 | DOMParser::~DOMParser() |
50 | { |
51 | if (_pNamePool) _pNamePool->release(); |
52 | } |
53 | |
54 | |
55 | void DOMParser::setEncoding(const XMLString& encoding) |
56 | { |
57 | _saxParser.setEncoding(encoding); |
58 | } |
59 | |
60 | |
61 | const XMLString& DOMParser::getEncoding() const |
62 | { |
63 | return _saxParser.getEncoding(); |
64 | } |
65 | |
66 | |
67 | void DOMParser::addEncoding(const XMLString& name, Poco::TextEncoding* pEncoding) |
68 | { |
69 | _saxParser.addEncoding(name, pEncoding); |
70 | } |
71 | |
72 | |
73 | void DOMParser::setFeature(const XMLString& name, bool state) |
74 | { |
75 | if (name == FEATURE_FILTER_WHITESPACE) |
76 | _filterWhitespace = state; |
77 | else |
78 | _saxParser.setFeature(name, state); |
79 | } |
80 | |
81 | |
82 | bool DOMParser::getFeature(const XMLString& name) const |
83 | { |
84 | if (name == FEATURE_FILTER_WHITESPACE) |
85 | return _filterWhitespace; |
86 | else |
87 | return _saxParser.getFeature(name); |
88 | } |
89 | |
90 | |
91 | Document* DOMParser::parse(const XMLString& uri) |
92 | { |
93 | if (_filterWhitespace) |
94 | { |
95 | WhitespaceFilter filter(&_saxParser); |
96 | DOMBuilder builder(filter, _pNamePool); |
97 | return builder.parse(uri); |
98 | } |
99 | else |
100 | { |
101 | DOMBuilder builder(_saxParser, _pNamePool); |
102 | return builder.parse(uri); |
103 | } |
104 | } |
105 | |
106 | |
107 | Document* DOMParser::parse(InputSource* pInputSource) |
108 | { |
109 | if (_filterWhitespace) |
110 | { |
111 | WhitespaceFilter filter(&_saxParser); |
112 | DOMBuilder builder(filter, _pNamePool); |
113 | return builder.parse(pInputSource); |
114 | } |
115 | else |
116 | { |
117 | DOMBuilder builder(_saxParser, _pNamePool); |
118 | return builder.parse(pInputSource); |
119 | } |
120 | } |
121 | |
122 | |
123 | Document* DOMParser::parseString(const std::string& xml) |
124 | { |
125 | return parseMemory(xml.data(), xml.size()); |
126 | } |
127 | |
128 | |
129 | Document* DOMParser::parseMemory(const char* xml, std::size_t size) |
130 | { |
131 | if (_filterWhitespace) |
132 | { |
133 | WhitespaceFilter filter(&_saxParser); |
134 | DOMBuilder builder(filter, _pNamePool); |
135 | return builder.parseMemoryNP(xml, size); |
136 | } |
137 | else |
138 | { |
139 | DOMBuilder builder(_saxParser, _pNamePool); |
140 | return builder.parseMemoryNP(xml, size); |
141 | } |
142 | } |
143 | |
144 | |
145 | EntityResolver* DOMParser::getEntityResolver() const |
146 | { |
147 | return _saxParser.getEntityResolver(); |
148 | } |
149 | |
150 | |
151 | void DOMParser::setEntityResolver(EntityResolver* pEntityResolver) |
152 | { |
153 | _saxParser.setEntityResolver(pEntityResolver); |
154 | } |
155 | |
156 | |
157 | } } // namespace Poco::XML |
158 | |