17 #ifndef HCEXML_PARSER_HPP
18 #define HCEXML_PARSER_HPP
20 #include <Poco/JSON/Object.h>
21 #include <Poco/SAX/SAXParser.h>
22 #include <Poco/SAX/ContentHandler.h>
23 #include <Poco/SAX/LexicalHandler.h>
24 #include <Poco/SAX/Attributes.h>
25 #include <Poco/SAX/Locator.h>
26 #include <Poco/Exception.h>
32 using Poco::XML::SAXParser;
33 using Poco::XML::XMLReader;
34 using Poco::XML::XMLString;
35 using Poco::XML::XMLChar;
36 using Poco::XML::ContentHandler;
37 using Poco::XML::LexicalHandler;
38 using Poco::XML::Attributes;
39 using Poco::XML::Locator;
41 using namespace HCE::exception;
62 void addAttrTag(std::string tagName)
64 attrTagNames.push_back(tagName);
68 void setDocumentLocator(
const Locator* loc)
81 void startElement(
const XMLString& uri,
const XMLString& localName,
const XMLString& qname,
const Attributes& attributes)
83 objStack.push(
new Poco::JSON::Object());
84 attrNamesStack.push(localName);
86 if(attributes.getLength() > 0)
88 for(
unsigned int i = 0; i < attrTagNames.size(); i++)
90 if(!attributes.getValue(attrTagNames[i]).empty())
92 attrNamesStack.top() = attributes.getValue(attrTagNames[i]);
97 Poco::JSON::Object::Ptr localObjPtr = Poco::JSON::Object::Ptr(
new Poco::JSON::Object());
98 Poco::JSON::Object::Ptr localObjPtrParent = objStack.top();
99 for(
unsigned int i = 0; i < static_cast<unsigned int>(attributes.getLength()) ; i++)
102 for(
unsigned int j = 0; j < attrTagNames.size(); j++)
104 if(attributes.getLocalName(i).compare(attrTagNames[j]) == 0)
112 tagVal = attributes.getValue(i);
113 insertAtEndElement(attributes.getLocalName(i), localObjPtr, localObjPtrParent);
120 void insertAtEndElement(
const XMLString& localName, Poco::JSON::Object::Ptr localObjPtr, Poco::JSON::Object::Ptr localObjPtrParent)
122 const XMLString &tagAttrName = ((attrNamesStack.empty() || attrNamesStack.top().empty()) ? localName : attrNamesStack.top());
123 if(tagAttrName.empty())
127 else if(tagAttrName != localName)
131 Poco::Dynamic::Var localVar;
132 if(localObjPtr->size() > 0)
134 localVar = localObjPtr;
140 if(!localObjPtrParent->has(tagAttrName))
142 localObjPtrParent->set(tagAttrName, localVar);
144 else if(localObjPtrParent->isArray(tagAttrName))
146 Poco::JSON::Array::Ptr localArray = localObjPtrParent->getArray(tagAttrName);
147 if(!localArray.isNull())
149 localArray->add(localVar);
154 Poco::JSON::Array::Ptr localArray = Poco::JSON::Array::Ptr(
new Poco::JSON::Array());
155 localArray->add(localObjPtrParent->get(tagAttrName));
156 localArray->add(localVar);
157 localObjPtrParent->remove(tagAttrName);
158 localObjPtrParent->set(tagAttrName, localArray);
162 void endElement(
const XMLString& uri,
const XMLString& localName,
const XMLString& qname)
164 Poco::JSON::Object::Ptr localObjPtr;
165 if(!objStack.empty())
167 localObjPtr = objStack.top();
169 if(objStack.size() > 0)
171 Poco::JSON::Object::Ptr localObjPtrParent = objStack.top();
172 if(!localObjPtr.isNull() && !localObjPtrParent.isNull())
174 insertAtEndElement(localName, localObjPtr, localObjPtrParent);
179 rootObject = localObjPtr;
181 attrNamesStack.pop();
186 void characters(
const XMLChar ch[],
int start,
int length)
188 tagVal.append(ch + start, length);
191 void ignorableWhitespace(
const XMLChar ch[],
int start,
int length)
195 void processingInstruction(
const XMLString& target,
const XMLString&
data)
199 void startPrefixMapping(
const XMLString& prefix,
const XMLString& uri)
203 void endPrefixMapping(
const XMLString& prefix)
207 void skippedEntity(
const XMLString&
name)
212 void startDTD(
const XMLString&
name,
const XMLString& publicId,
const XMLString& systemId)
220 void startEntity(
const XMLString&
name)
224 void endEntity(
const XMLString&
name)
236 void comment(
const XMLChar ch[],
int start,
int length)
240 Poco::JSON::Object::Ptr getObject()
246 const Locator* _pLocator;