Refactor SignatureValidator and reduce interface headers
[platform/core/security/cert-svc.git] / 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 <vcore/SaxReader.h>
29 #include <vcore/exception.h>
30
31 namespace ValidationCore {
32 namespace ParserSchemaException {
33     VCORE_DECLARE_EXCEPTION_TYPE(ValidationCore::Exception, Base);
34     VCORE_DECLARE_EXCEPTION_TYPE(Base, XmlReaderError);
35     VCORE_DECLARE_EXCEPTION_TYPE(Base, CertificateLoaderError);
36     VCORE_DECLARE_EXCEPTION_TYPE(Base, UnsupportedAlgorithm);
37     VCORE_DECLARE_EXCEPTION_TYPE(Base, UnsupportedValue);
38 }
39
40 template<typename ParserType, typename DataType>
41 class ParserSchema {
42 public:
43
44     struct TagDescription {
45         TagDescription(const std::string &tag,
46                 const std::string & xmlNamespace) :
47             tagName(tag),
48             namespaceUri(xmlNamespace)
49         {
50         }
51
52         std::string tagName;
53         std::string namespaceUri;
54
55         bool operator<(const TagDescription &second) const
56         {
57             if (tagName < second.tagName) {
58                 return true;
59             }
60             if (tagName > second.tagName) {
61                 return false;
62             }
63             if (namespaceUri < second.namespaceUri) {
64                 return true;
65             }
66             return false;
67         }
68     };
69
70
71     ParserSchema(ParserType *parser)
72       : m_functions(parser) {}
73
74     virtual ~ParserSchema() {}
75
76     void initialize(
77             const std::string &filename,
78             bool defaultArgs,
79             SaxReader::ValidationType valType,
80             const std::string &xmlschema)
81     {
82         VcoreTry
83         {
84             m_reader.initialize(filename, defaultArgs, valType, xmlschema);
85         }
86         VcoreCatch (SaxReader::Exception::Base)
87         {
88             VcoreReThrowMsg(ParserSchemaException::XmlReaderError, "XmlReaderError");
89         }
90     }
91
92     void deinitialize()
93     {
94         m_reader.deinitialize();
95     }
96
97     void read(DataType &dataContainer)
98     {
99         VcoreTry
100         {
101             while (m_reader.next()) {
102                 switch (m_reader.type()) {
103                 case SaxReader::NODE_BEGIN:
104                     beginNode(dataContainer);
105                     break;
106                 case SaxReader::NODE_END:
107                     endNode(dataContainer);
108                     break;
109                 case SaxReader::NODE_TEXT:
110                     textNode(dataContainer);
111                     break;
112                 default:
113                     break;
114                 }
115             }
116         }
117         VcoreCatch (SaxReader::Exception::Base)
118         {
119             VcoreReThrowMsg(ParserSchemaException::XmlReaderError, "XmlReaderError");
120         }
121     }
122
123     typedef void (ParserType::*FunctionPtr)(DataType &data);
124     typedef std::map<TagDescription, FunctionPtr> FunctionMap;
125
126     void addBeginTagCallback(
127             const std::string &tag,
128             const std::string &namespaceUri,
129             FunctionPtr function)
130     {
131         TagDescription desc(tag, namespaceUri);
132         m_beginFunctionMap[desc] = function;
133     }
134
135     void addEndTagCallback(
136             const std::string &tag,
137             const std::string &namespaceUri,
138             FunctionPtr function)
139     {
140         TagDescription desc(tag, namespaceUri);
141         m_endFunctionMap[desc] = function;
142     }
143
144     SaxReader& getReader()
145     {
146         return m_reader;
147     }
148
149     std::string& getText()
150     {
151         return m_textNode;
152     }
153
154 protected:
155     void beginNode(DataType &dataContainer)
156     {
157         TagDescription desc(m_reader.name(), m_reader.namespaceURI());
158         FunctionPtr fun = m_beginFunctionMap[desc];
159
160         if (fun == 0) {
161             return;
162         }
163
164         (m_functions->*fun)(dataContainer);
165     }
166
167     void endNode(DataType &dataContainer)
168     {
169         TagDescription desc(m_reader.name(), m_reader.namespaceURI());
170         FunctionPtr fun = m_endFunctionMap[desc];
171
172         if (fun == 0) {
173             return;
174         }
175
176         (m_functions->*fun)(dataContainer);
177     }
178
179     void textNode(DataType &dataContainer)
180     {
181         (void)dataContainer;
182         m_textNode = m_reader.value();
183     }
184
185     ParserType *m_functions;
186     SaxReader m_reader;
187     FunctionMap m_beginFunctionMap;
188     FunctionMap m_endFunctionMap;
189
190     // temporary values require due parsing textNode
191     std::string m_textNode;
192 };
193
194 } // namespace ValidationCore
195 #endif