2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
20 * @brief Simple c++ interface for libxml2.
22 #include <dpl/assert.h>
23 #include <dpl/log/log.h>
25 #include <vcore/SaxReader.h>
27 namespace ValidationCore {
30 SaxReader::SaxReader() :
35 SaxReader::~SaxReader()
42 void SaxReader::initialize(
43 const std::string &filename,
45 ValidationType validate,
46 const std::string &schema)
48 Assert(m_reader == 0 && "Double initialization of SaxReader");
50 LogDebug("SaxReader opening file: " << filename);
52 m_reader = xmlNewTextReaderFilename(filename.c_str());
55 VcoreThrowMsg(SaxReader::Exception::FileOpeningError,
56 "opening file " << filename << " error");
59 if (validate == VALIDATION_XMLSCHEME &&
60 xmlTextReaderSchemaValidate(m_reader, schema.c_str())) {
62 * unable to turn on schema validation
64 VcoreThrowMsg(SaxReader::Exception::ParserInternalError,
65 "Turn on Schema validation failed");
68 // Path to DTD schema is taken from xml file.
69 if (validate == VALIDATION_DTD &&
70 xmlTextReaderSetParserProp(m_reader, XML_PARSER_VALIDATE, 1)) {
72 * unable to turn on DTD validation
74 VcoreThrowMsg(SaxReader::Exception::ParserInternalError,
75 "Turn on DTD validation failed!");
79 xmlTextReaderSetParserProp(m_reader, XML_PARSER_DEFAULTATTRS, 1)) {
81 * unable to turn on default arguments
83 VcoreThrowMsg(SaxReader::Exception::ParserInternalError,
84 "Turn on default arguments failed");
88 void SaxReader::deinitialize()
90 xmlFreeTextReader(m_reader);
94 bool SaxReader::next()
96 int res = xmlTextReaderRead(m_reader);
99 VcoreThrowMsg(SaxReader::Exception::ParserInternalError,
100 "xmlTextReaderRead error");
102 if (!xmlTextReaderIsValid(m_reader))
103 VcoreThrowMsg(SaxReader::Exception::FileNotValid,
104 "xmlTextReader is invalid");
106 return res ? true : false;
109 void SaxReader::next(const std::string &token)
111 int res = xmlTextReaderRead(m_reader);
114 VcoreThrowMsg(SaxReader::Exception::ParserInternalError,
115 "xmlTextReaderRead error");
117 if (!xmlTextReaderIsValid(m_reader))
118 VcoreThrowMsg(SaxReader::Exception::FileNotValid,
119 "xmlTextReader is invalid");
121 xmlChar *name = xmlTextReaderName(m_reader);
124 VcoreThrowMsg(SaxReader::Exception::ParserInternalError,
125 "xmlTextReaderName returns NULL");
127 xmlChar *xmlToken = xmlCharStrdup(token.c_str());
129 if (xmlStrcmp(name, xmlToken)) {
133 VcoreThrowMsg(SaxReader::Exception::WrongToken, "Wrong Token");
140 bool SaxReader::isEmpty(void)
142 int ret = xmlTextReaderIsEmptyElement(m_reader);
144 VcoreThrowMsg(SaxReader::Exception::ParserInternalError,
145 "xmlTextReaderIsEmptyElement error");
147 return ret ? true : false;
150 std::string SaxReader::attribute(const std::string &token, ThrowType throwStatus)
152 xmlChar *attr = xmlTextReaderGetAttribute(m_reader, BAD_CAST(token.c_str()));
154 if (throwStatus == THROW_DISABLE) {
155 return std::string();
158 VcoreThrowMsg(SaxReader::Exception::ParserInternalError,
159 "xmlTextReaderGetAttribute error");
163 std::string value = reinterpret_cast<const char *>(attr);
169 std::string SaxReader::name()
171 xmlChar *name = xmlTextReaderName(m_reader);
173 VcoreThrowMsg(SaxReader::Exception::ReadingNameError,
174 "reading name error");
176 std::string value = reinterpret_cast<const char *>(name);
178 size_t pos = value.find_last_of(":");
179 if (pos != std::string::npos) {
180 value.erase(0, pos + 1);
186 std::string SaxReader::namespaceURI()
188 xmlChar *name = xmlTextReaderNamespaceUri(m_reader);
190 return std::string();
193 std::string value = reinterpret_cast<const char *>(name);
199 std::string SaxReader::value()
201 xmlChar *text = xmlTextReaderValue(m_reader);
203 VcoreThrowMsg(SaxReader::Exception::ReadingValueError,
204 "reading value error");
206 std::string value = reinterpret_cast<const char*>(text);
212 SaxReader::NodeType SaxReader::type()
214 xmlReaderTypes type =
215 static_cast<xmlReaderTypes>(xmlTextReaderNodeType(m_reader));
217 case XML_READER_TYPE_ELEMENT:
219 case XML_READER_TYPE_END_ELEMENT:
221 case XML_READER_TYPE_TEXT:
223 case XML_READER_TYPE_NONE:
224 case XML_READER_TYPE_ATTRIBUTE:
225 case XML_READER_TYPE_CDATA:
226 case XML_READER_TYPE_ENTITY_REFERENCE:
227 case XML_READER_TYPE_ENTITY:
228 case XML_READER_TYPE_PROCESSING_INSTRUCTION:
229 case XML_READER_TYPE_COMMENT:
230 case XML_READER_TYPE_DOCUMENT:
231 case XML_READER_TYPE_DOCUMENT_TYPE:
232 case XML_READER_TYPE_DOCUMENT_FRAGMENT:
233 case XML_READER_TYPE_NOTATION:
234 case XML_READER_TYPE_WHITESPACE:
235 case XML_READER_TYPE_SIGNIFICANT_WHITESPACE:
236 case XML_READER_TYPE_END_ENTITY:
237 case XML_READER_TYPE_XML_DECLARATION:
239 return NODE_UNSUPPORTED;
243 void SaxReader::dumpNode(std::string &buffer)
245 xmlBufferPtr buff = xmlBufferCreate();
247 xmlNodePtr node = xmlTextReaderExpand(m_reader);
250 VcoreThrowMsg(SaxReader::Exception::ParserInternalError,
251 "xmlTextReaderExpand error");
254 int size = xmlNodeDump(buff, node->doc, node, 0, 0);
256 buffer.insert(0, reinterpret_cast<char*>(buff->content), size);
261 } // namespace ValidationCore