PageRenderTime 103ms CodeModel.GetById 60ms app.highlight 8ms RepoModel.GetById 32ms app.codeStats 0ms

/mordor/xml/parser.h

http://github.com/mozy/mordor
C Header | 87 lines | 72 code | 14 blank | 1 comment | 7 complexity | 4c635d3f4ee5670541a788afd1883d85 MD5 | raw file
 1#ifndef __MORDOR_XML_PARSER_H__
 2#define __MORDOR_XML_PARSER_H__
 3// Copyright (c) 2009 - Mozy, Inc.
 4
 5#include <boost/function.hpp>
 6
 7#include "mordor/ragel.h"
 8
 9namespace Mordor {
10
11class XMLParserEventHandler
12{
13public:
14    virtual ~XMLParserEventHandler() {}
15
16    virtual void onStartTag(const std::string &tag) {}
17    virtual void onEndTag(const std::string &tag) {}
18    virtual void onEmptyTag() {}
19    virtual void onAttributeName(const std::string &attribute) {}
20    virtual void onAttributeValue(const std::string &value) {}
21    virtual void onInnerText(const std::string &text) {}
22    virtual void onReference(const std::string &reference) {}
23};
24
25class CallbackXMLParserEventHandler : public XMLParserEventHandler
26{
27public:
28    CallbackXMLParserEventHandler(
29        boost::function<void (const std::string &)> startTag,
30        boost::function<void (const std::string &)> endTag = NULL,
31        boost::function<void ()> emptyTag = NULL,
32        boost::function<void (const std::string &)> attribName = NULL,
33        boost::function<void (const std::string &)> attribValue = NULL,
34        boost::function<void (const std::string &)> innerText = NULL,
35        boost::function<void (const std::string &)> reference = NULL)
36      : m_startTag(startTag),
37        m_endTag(endTag),
38        m_attribName(attribName),
39        m_attribValue(attribValue),
40        m_innerText(innerText),
41        m_reference(reference),
42        m_emptyTag(emptyTag)
43    {}
44
45    void onStartTag(const std::string &tag)
46    { if (m_startTag) m_startTag(tag); }
47    void onEndTag(const std::string &tag)
48    { if (m_endTag) m_endTag(tag); }
49    void onEmptyTag()
50    { if (m_emptyTag) m_emptyTag(); }
51    void onAttributeName(const std::string &attribute)
52    { if (m_attribName) m_attribName(attribute); }
53    void onAttributeValue(const std::string &value)
54    { if (m_attribValue) m_attribValue(value); }
55    void onInnerText(const std::string &text)
56    { if (m_innerText) m_innerText(text); }
57    void onReference(const std::string &reference)
58    { if (m_reference) m_reference(reference); }
59
60private:
61    boost::function<void (const std::string &)> m_startTag, m_endTag,
62        m_attribName, m_attribValue, m_innerText, m_reference;
63    boost::function<void ()> m_emptyTag;
64};
65
66class XMLParser : public RagelParserWithStack
67{
68public:
69    XMLParser(XMLParserEventHandler &handler)
70    : m_handler(handler)
71    {}
72
73    void init();
74    bool complete() const { return false; }
75    bool final() const;
76    bool error() const;
77
78protected:
79    void exec();
80
81private:
82    XMLParserEventHandler &m_handler;
83};
84
85}
86
87#endif