summaryrefslogtreecommitdiff
path: root/source/expatimpl.py
blob: 06d53750aae3bd9273cb8ff41387857ea733c233 (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
import xml.parsers.expat, sys
import globals, node

class ParseFailed(globals.Exception):
    def __init__ (self):
        globals.Exception.__init__(self, "parse failed")

class ParserBase:
    def __init__ (self, strm):
        self.strm = strm
        self.root = node.Root()
        self.nodestack = [self.root]
        self.char = None

    def startElement(self, name, attrs):
        for key in attrs.keys():
            s = attrs[key]
            s = s.replace('$[officename]', globals.productName)
            s = s.replace('%PRODUCTNAME', globals.productName)
            attrs[key] = s

        n = node.Element(name, attrs)
        self.nodestack[-1].appendChild(n)
        self.nodestack.append(n)

    def endElement(self, name):
        if name != self.nodestack[-1].name:
            raise ParseFailed()
        self.nodestack.pop()

    def character(self, data):
        if len(data) > 0 and data[-1] == "\n":
            data = data[:-1]

        s = repr(data)
        if len(s) >= 3 and s[0] == 'u' and s[1] == "'" and s[-1] == "'":
            # move the unicode quote (e.g. u'foo' -> foo) if exists.
            s = s[2:-1]

        s = s.replace('$[officename]', globals.productName)
        s = s.replace('%PRODUCTNAME', globals.productName)
        self.nodestack[-1].appendChild(node.Content(s))
        self.char = s # store current character.

    def parse (self):
        p = xml.parsers.expat.ParserCreate()
        p.StartElementHandler = self.startElement
        p.EndElementHandler = self.endElement
        p.CharacterDataHandler = self.character
        p.Parse(self.strm, 1)


class XHPParser(ParserBase):

    def __init__ (self, strm):
        ParserBase.__init__(self, strm)
        self.filename = None
        self.ids = {}

    def startElement(self, name, attrs):
        ParserBase.startElement(self, name, attrs)
        if attrs.has_key('id'):
            # associate this node with this ID.
            val = attrs['id']
            self.ids[val] = self.nodestack[-1]

    def character(self, data):
        ParserBase.character(self, data)
        if self.nodestack[-1].name == 'filename':
            # For now, I just assume that the filename element is always at the correct position.
            self.filename = self.char


class TreeParser(ParserBase):

    def __init__ (self, strm):
        ParserBase.__init__(self, strm)