Add initial values support - values to feed the shared database on first startup.
[platform/core/security/key-manager.git] / src / manager / initial-values / parser.h
1 /*
2  *  Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
15  *
16  *
17  * @file        parser.h
18  * @author      Maciej Karpiuk (m.karpiuk2@samsung.com)
19  * @version     1.0
20  * @brief       XML parser class.
21  */
22
23 #ifndef XML_PARSER_H_
24 #define XML_PARSER_H_
25
26 #include <map>
27 #include <vector>
28 #include <string>
29 #include <stack>
30 #include <functional>
31 #include <memory>
32 #include <libxml/parser.h>
33 #include <libxml/tree.h>
34
35 namespace CKM {
36 namespace XML {
37
38 class Parser
39 {
40 public:
41     enum ErrorCode {
42         PARSE_SUCCESS                  =   0,
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
52     };
53
54     explicit Parser(const std::string &XML_filename);
55     virtual ~Parser();
56
57     int Validate(const std::string &XSD_schema);
58     int Parse();
59
60     enum ErrorType {
61         VALIDATION_ERROR,
62         PARSE_ERROR,
63         PARSE_WARNING
64     };
65     typedef std::function<void (const ErrorType, const std::string &)> ErrorCb;
66     int RegisterErrorCb(const ErrorCb newCb);
67
68     typedef std::map<std::string, std::string> Attributes;
69     class ElementHandler
70     {
71         public:
72             virtual ~ElementHandler() {}
73
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;
80     };
81     typedef std::shared_ptr<ElementHandler> ElementHandlerPtr;
82
83     typedef std::function<ElementHandlerPtr ()> StartCb;
84     typedef std::function<void (const ElementHandlerPtr &)> EndCb;
85     int RegisterElementCb(const char * elementName,
86                           const StartCb startCb,
87                           const EndCb endCb);
88
89 protected:
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 &);
95
96 private:
97     static void StartElement(void *userData,
98                              const xmlChar *name,
99                              const xmlChar **attrs);
100     static void EndElement(void *userData,
101                            const xmlChar *name);
102     static void Characters(void *userData,
103                            const xmlChar *ch,
104                            int len);
105     static void ErrorValidate(void *userData,
106                               const char *msg,
107                               ...);
108     static void Error(void *userData,
109                       const char *msg,
110                       ...);
111     static void Warning(void *userData,
112                         const char *msg,
113                         ...);
114
115 private:
116     xmlSAXHandler           m_saxHandler;
117     std::string             m_XMLfile;
118     ErrorCb                 m_errorCb;
119
120     struct ElementListener
121     {
122         StartCb     startCb;
123         EndCb       endCb;
124     };
125     std::map<std::string, ElementListener> m_elementListenerMap;
126     std::stack<ElementHandlerPtr> m_elementHandlerStack;
127
128     void CallbackHelper(std::function<void (void)> func);
129 };
130
131 }
132 }
133 #endif /* XML_PARSER_H_ */