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.
17 * @file ParserSchema.h
18 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
22 #ifndef _PARSERSCHEMA_H_
23 #define _PARSERSCHEMA_H_
28 #include <vcore/SaxReader.h>
29 #include <vcore/exception.h>
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);
40 template<typename ParserType, typename DataType>
44 struct TagDescription {
45 TagDescription(const std::string &tag,
46 const std::string & xmlNamespace) :
48 namespaceUri(xmlNamespace)
53 std::string namespaceUri;
55 bool operator<(const TagDescription &second) const
57 if (tagName < second.tagName) {
60 if (tagName > second.tagName) {
63 if (namespaceUri < second.namespaceUri) {
71 ParserSchema(ParserType *parser)
72 : m_functions(parser) {}
74 virtual ~ParserSchema() {}
77 const std::string &filename,
79 SaxReader::ValidationType valType,
80 const std::string &xmlschema)
84 m_reader.initialize(filename, defaultArgs, valType, xmlschema);
86 VcoreCatch (SaxReader::Exception::Base)
88 VcoreReThrowMsg(ParserSchemaException::XmlReaderError, "XmlReaderError");
94 m_reader.deinitialize();
97 void read(DataType &dataContainer)
101 while (m_reader.next()) {
102 switch (m_reader.type()) {
103 case SaxReader::NODE_BEGIN:
104 beginNode(dataContainer);
106 case SaxReader::NODE_END:
107 endNode(dataContainer);
109 case SaxReader::NODE_TEXT:
110 textNode(dataContainer);
117 VcoreCatch (SaxReader::Exception::Base)
119 VcoreReThrowMsg(ParserSchemaException::XmlReaderError, "XmlReaderError");
123 typedef void (ParserType::*FunctionPtr)(DataType &data);
124 typedef std::map<TagDescription, FunctionPtr> FunctionMap;
126 void addBeginTagCallback(
127 const std::string &tag,
128 const std::string &namespaceUri,
129 FunctionPtr function)
131 TagDescription desc(tag, namespaceUri);
132 m_beginFunctionMap[desc] = function;
135 void addEndTagCallback(
136 const std::string &tag,
137 const std::string &namespaceUri,
138 FunctionPtr function)
140 TagDescription desc(tag, namespaceUri);
141 m_endFunctionMap[desc] = function;
144 SaxReader& getReader()
149 std::string& getText()
155 void beginNode(DataType &dataContainer)
157 TagDescription desc(m_reader.name(), m_reader.namespaceURI());
158 FunctionPtr fun = m_beginFunctionMap[desc];
164 (m_functions->*fun)(dataContainer);
167 void endNode(DataType &dataContainer)
169 TagDescription desc(m_reader.name(), m_reader.namespaceURI());
170 FunctionPtr fun = m_endFunctionMap[desc];
176 (m_functions->*fun)(dataContainer);
179 void textNode(DataType &dataContainer)
182 m_textNode = m_reader.value();
185 ParserType *m_functions;
187 FunctionMap m_beginFunctionMap;
188 FunctionMap m_endFunctionMap;
190 // temporary values require due parsing textNode
191 std::string m_textNode;
194 } // namespace ValidationCore