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