summaryrefslogtreecommitdiff
path: root/xmlparser.h
blob: 29745eb36ec584d9a22b078605035df3a4bed10c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
jmdict, a frontend to the JMdict file. http://mandrill.fuxx0r.net/jmdict.php
Copyright (C) 2004 Florian Bluemel (florian.bluemel@uni-dortmund.de)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#include <expat.h>
#include <istream>
#include <string>
#include <map>

namespace xml {

class Tag {
public:
    Tag(const std::string& name, const char** attrs) : m_name(name) {
        while (*attrs) {
            m_attributes[*attrs] = *(attrs + 1);
            attrs += 2;
        }
    }

    const std::string& name() const {
        return m_name;
    }

    std::string attribute(const std::string& name) const {
        std::map<std::string, std::string>::const_iterator val = m_attributes.find(name);
        if (val == m_attributes.end())
            return "";
        return val->second;
    }

    const std::string& text() const {
        return m_text;
    }

    void append(const std::string& t) {
        m_text += t;
    }

    void append(const char* t, int len) {
        m_text.append(t, len);
    }

private:
    std::string m_name;
    std::string m_text;
    std::map<std::string, std::string> m_attributes;
};

template<class Stack>
class Parser {
public:
    Parser(Stack& stack) : m_stack(stack) {
        m_parser = XML_ParserCreate(0);
        XML_SetUserData(m_parser, this);
        XML_SetElementHandler(m_parser, &Parser::start, &Parser::end);
        XML_SetCharacterDataHandler(m_parser, Parser::chardata);
    }

    ~Parser() {
        XML_ParserFree(m_parser);
    }

    void parse(std::istream& in) {
        const size_t BLOCK_SIZE = 1 << 15;
        while (in) {
            char* buffer = static_cast<char*>(XML_GetBuffer(m_parser, BLOCK_SIZE));
            in.read(buffer, BLOCK_SIZE);
            XML_ParseBuffer(m_parser, in.gcount(), in.eof());
        }
    }

private:
    static void start(void* data, const char* e, const char** a) {
        Parser& parser = *static_cast<Parser*>(data);
        parser.m_stack.push(Tag(e, a));
    }

    static void chardata(void* data, const XML_Char* text, int len) {
        Parser& parser = *static_cast<Parser*>(data);
        parser.m_stack.top().append(text, len);
    }
        
    static void end(void* data, const char*) {
        Parser& parser = *static_cast<Parser*>(data);
        parser.m_stack.pop();
    }
        
    XML_Parser m_parser;
    Stack& m_stack;
};

} // namespace xml