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 <dpl/log/log.h>
30 #include "SaxReader.h"
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)
41 template<typename ParserType, typename DataType>
47 TagDescription(const std::string &tag,
48 const std::string & xmlNamespace) :
50 namespaceUri(xmlNamespace)
55 std::string namespaceUri;
57 bool operator<(const TagDescription &second) const
59 if (tagName < second.tagName) {
62 if (tagName > second.tagName) {
65 if (namespaceUri < second.namespaceUri) {
72 ParserSchema(ParserType * parser) :
77 virtual ~ParserSchema()
81 void initialize(const std::string &filename,
83 SaxReader::ValidationType valType,
84 const std::string &xmlschema)
88 m_reader.initialize(filename, defaultArgs, valType, xmlschema);
90 Catch(SaxReader::Exception::Base)
92 ReThrowMsg(ParserSchemaException::XmlReaderError, "XmlReaderError");
98 m_reader.deinitialize();
101 void read(DataType &dataContainer)
104 while (m_reader.next()) {
105 switch (m_reader.type()) {
106 case SaxReader::NODE_BEGIN:
107 beginNode(dataContainer);
109 case SaxReader::NODE_END:
110 endNode(dataContainer);
112 case SaxReader::NODE_TEXT:
113 textNode(dataContainer);
116 // LogInfo("Unknown Type Node");
121 Catch(SaxReader::Exception::Base)
123 ReThrowMsg(ParserSchemaException::XmlReaderError, "XmlReaderError");
127 typedef void (ParserType::*FunctionPtr)(DataType &data);
128 typedef std::map<TagDescription, FunctionPtr> FunctionMap;
130 void addBeginTagCallback(const std::string &tag,
131 const std::string &namespaceUri,
132 FunctionPtr function)
134 TagDescription desc(tag, namespaceUri);
135 m_beginFunctionMap[desc] = function;
138 void addEndTagCallback(const std::string &tag,
139 const std::string &namespaceUri,
140 FunctionPtr function)
142 TagDescription desc(tag, namespaceUri);
143 m_endFunctionMap[desc] = function;
146 SaxReader& getReader(void)
151 std::string& getText(void)
157 void beginNode(DataType &dataContainer)
159 TagDescription desc(m_reader.name(), m_reader.namespaceURI());
160 FunctionPtr fun = m_beginFunctionMap[desc];
163 LogDebug("No function found for xml tag: " << m_reader.name());
167 (m_functions->*fun)(dataContainer);
170 void endNode(DataType &dataContainer)
172 TagDescription desc(m_reader.name(), m_reader.namespaceURI());
173 FunctionPtr fun = m_endFunctionMap[desc];
176 LogDebug("No function found for xml tag: " << m_reader.name());
180 (m_functions->*fun)(dataContainer);
183 void textNode(DataType &dataContainer)
186 m_textNode = m_reader.value();
189 ParserType *m_functions;
192 FunctionMap m_beginFunctionMap;
193 FunctionMap m_endFunctionMap;
195 // temporary values require due parsing textNode
196 std::string m_textNode;
198 } // namespace ValidationCore