Tizen 2.1 base
[framework/web/wrt-commons.git] / modules / vcore / src / vcore / ParserSchema.h
1 /*
2  * Copyright (c) 2011 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        ParserSchema.h
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief
21  */
22 #ifndef _PARSERSCHEMA_H_
23 #define _PARSERSCHEMA_H_
24
25 #include <map>
26 #include <string>
27
28 #include <dpl/log/log.h>
29
30 #include "SaxReader.h"
31
32 namespace ValidationCore {
33 namespace ParserSchemaException {
34 DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
35 DECLARE_EXCEPTION_TYPE(Base, XmlReaderError)
36 DECLARE_EXCEPTION_TYPE(Base, CertificateLoaderError)
37 DECLARE_EXCEPTION_TYPE(Base, UnsupportedAlgorithm)
38 DECLARE_EXCEPTION_TYPE(Base, UnsupportedValue)
39 }
40
41 template<typename ParserType, typename DataType>
42 class ParserSchema
43 {
44   public:
45     struct TagDescription
46     {
47         TagDescription(const std::string &tag,
48                 const std::string & xmlNamespace) :
49             tagName(tag),
50             namespaceUri(xmlNamespace)
51         {
52         }
53
54         std::string tagName;
55         std::string namespaceUri;
56
57         bool operator<(const TagDescription &second) const
58         {
59             if (tagName < second.tagName) {
60                 return true;
61             }
62             if (tagName > second.tagName) {
63                 return false;
64             }
65             if (namespaceUri < second.namespaceUri) {
66                 return true;
67             }
68             return false;
69         }
70     };
71
72     ParserSchema(ParserType * parser) :
73         m_functions(parser)
74     {
75     }
76
77     virtual ~ParserSchema()
78     {
79     }
80
81     void initialize(const std::string &filename,
82             bool defaultArgs,
83             SaxReader::ValidationType valType,
84             const std::string &xmlschema)
85     {
86         Try
87         {
88             m_reader.initialize(filename, defaultArgs, valType, xmlschema);
89         }
90         Catch(SaxReader::Exception::Base)
91         {
92             ReThrowMsg(ParserSchemaException::XmlReaderError, "XmlReaderError");
93         }
94     }
95
96     void deinitialize()
97     {
98         m_reader.deinitialize();
99     }
100
101     void read(DataType &dataContainer)
102     {
103         Try {
104             while (m_reader.next()) {
105                 switch (m_reader.type()) {
106                 case SaxReader::NODE_BEGIN:
107                     beginNode(dataContainer);
108                     break;
109                 case SaxReader::NODE_END:
110                     endNode(dataContainer);
111                     break;
112                 case SaxReader::NODE_TEXT:
113                     textNode(dataContainer);
114                     break;
115                 default:
116                     //              LogInfo("Unknown Type Node");
117                     break;
118                 }
119             }
120         }
121         Catch(SaxReader::Exception::Base)
122         {
123             ReThrowMsg(ParserSchemaException::XmlReaderError, "XmlReaderError");
124         }
125     }
126
127     typedef void (ParserType::*FunctionPtr)(DataType &data);
128     typedef std::map<TagDescription, FunctionPtr> FunctionMap;
129
130     void addBeginTagCallback(const std::string &tag,
131             const std::string &namespaceUri,
132             FunctionPtr function)
133     {
134         TagDescription desc(tag, namespaceUri);
135         m_beginFunctionMap[desc] = function;
136     }
137
138     void addEndTagCallback(const std::string &tag,
139             const std::string &namespaceUri,
140             FunctionPtr function)
141     {
142         TagDescription desc(tag, namespaceUri);
143         m_endFunctionMap[desc] = function;
144     }
145
146     SaxReader& getReader(void)
147     {
148         return m_reader;
149     }
150
151     std::string& getText(void)
152     {
153         return m_textNode;
154     }
155
156   protected:
157     void beginNode(DataType &dataContainer)
158     {
159         TagDescription desc(m_reader.name(), m_reader.namespaceURI());
160         FunctionPtr fun = m_beginFunctionMap[desc];
161
162         if (fun == 0) {
163             LogDebug("No function found for xml tag: " << m_reader.name());
164             return;
165         }
166
167         (m_functions->*fun)(dataContainer);
168     }
169
170     void endNode(DataType &dataContainer)
171     {
172         TagDescription desc(m_reader.name(), m_reader.namespaceURI());
173         FunctionPtr fun = m_endFunctionMap[desc];
174
175         if (fun == 0) {
176             LogDebug("No function found for xml tag: " << m_reader.name());
177             return;
178         }
179
180         (m_functions->*fun)(dataContainer);
181     }
182
183     void textNode(DataType &dataContainer)
184     {
185         (void)dataContainer;
186         m_textNode = m_reader.value();
187     }
188
189     ParserType *m_functions;
190
191     SaxReader m_reader;
192     FunctionMap m_beginFunctionMap;
193     FunctionMap m_endFunctionMap;
194
195     // temporary values require due parsing textNode
196     std::string m_textNode;
197 };
198 } // namespace ValidationCore
199 #endif