2 * Copyright (c) 2000 - 2015 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 Maciej Karpiuk (m.karpiuk2@samsung.com)
20 * @brief XML parser class.
32 #include <libxml/parser.h>
33 #include <libxml/tree.h>
43 ERROR_UNKNOWN = -1000,
44 ERROR_XML_VALIDATION_FAILED = -1001,
45 ERROR_XSD_PARSE_FAILED = -1002,
46 ERROR_XML_PARSE_FAILED = -1003,
47 ERROR_INVALID_ARGUMENT = -1004,
48 ERROR_CALLBACK_PRESENT = -1005,
49 ERROR_INVALID_VERSION = -1006,
50 ERROR_INTERNAL = -1007,
51 ERROR_NO_MEMORY = -1008
54 explicit Parser(const std::string &XML_filename);
57 int Validate(const std::string &XSD_schema);
65 typedef std::function<void (const ErrorType, const std::string &)> ErrorCb;
66 int RegisterErrorCb(const ErrorCb newCb);
68 typedef std::map<std::string, std::string> Attributes;
72 virtual ~ElementHandler() {}
74 // methods below may throw std::exception to invalidate the parsing process
75 // and remove all element listeners.
76 // In this case, parsing error code returned to the user after std::exception.
77 virtual void Start(const Attributes &) = 0;
78 virtual void Characters(const std::string & data) = 0;
79 virtual void End() = 0;
81 typedef std::shared_ptr<ElementHandler> ElementHandlerPtr;
83 typedef std::function<ElementHandlerPtr ()> StartCb;
84 typedef std::function<void (const ElementHandlerPtr &)> EndCb;
85 int RegisterElementCb(const char * elementName,
86 const StartCb startCb,
90 void StartElement(const xmlChar *name,
91 const xmlChar **attrs);
92 void EndElement(const xmlChar *name);
93 void Characters(const xmlChar *ch, size_t chLen);
94 void Error(const ErrorType errorType, const char *msg, va_list &);
97 static void StartElement(void *userData,
99 const xmlChar **attrs);
100 static void EndElement(void *userData,
101 const xmlChar *name);
102 static void Characters(void *userData,
105 static void ErrorValidate(void *userData,
108 static void Error(void *userData,
111 static void Warning(void *userData,
116 xmlSAXHandler m_saxHandler;
117 std::string m_XMLfile;
120 struct ElementListener
125 std::map<std::string, ElementListener> m_elementListenerMap;
126 std::stack<ElementHandlerPtr> m_elementHandlerStack;
128 void CallbackHelper(std::function<void (void)> func);
133 #endif /* XML_PARSER_H_ */