xref: /aoo41x/main/toolkit/src2xml/source/globals.py (revision cdf0e10c)
1import sys
2
3class ParseError (Exception):
4    pass
5
6def error (msg, exit=0):
7    sys.stderr.write (msg)
8    if exit:
9        sys.exit (exit)
10
11def progress (msg):
12    sys.stderr.write(msg)
13
14
15def removeQuote (text):
16    """Remove quotes from a literal.
17"""
18    if len(text) >= 2 and text[0] == text[len(text)-1] == '"':
19        text = text[1:-1]
20    return text
21
22
23class Macro(object):
24    def __init__ (self, name):
25        self.name = name
26        self.tokens = []
27        self.vars = {}
28
29
30class Node(object):
31    def __init__ (self):
32        self.children = []
33
34    def appendChild (self, childnode):
35        self.children.append(childnode)
36        childnode.parent = self
37
38    def getChildren (self):
39        return self.children
40
41
42class RootNode(Node):
43    def __init__ (self):
44        Node.__init__(self)
45
46    def dump (self):
47        chars = ''
48        for child in self.getChildren():
49            chars += child.dump()
50        return chars
51
52
53class Element(Node):
54
55    INDENT = "    "
56
57    def __init__ (self, name, rid = None):
58        Node.__init__(self)
59        self.name = name
60        self.parent = None
61#        print "name: " + self.name - stats ...
62
63        # The following attributes are copied when 'clone'ed.
64        self.rid = rid
65        self.attrs = {}
66
67    def dump (self, level = 0):
68        chars = ''
69        chars += "\n" + Element.INDENT*level
70        chars += '<%s'%self.name
71
72        if self.rid != None:
73            self.setAttr("rid", self.rid)
74
75        chars += self.__dumpAttrs()
76
77        if len(self.children) == 0:
78            chars += '/>'
79        else:
80            chars += '>'
81            for child in self.getChildren():
82                chars += child.dump(level+1)
83
84            chars += "\n"+Element.INDENT*level
85            chars += "</%s>"%self.name
86
87        return chars
88
89    def hasAttr (self, name):
90        return self.attrs.has_key(name)
91
92    def getAttr (self, name):
93        return self.attrs[name]
94
95    def setAttr (self, name, value):
96        if type(value) == type(0):
97            value = "%d"%value
98        self.attrs[name] = removeQuote(value)
99#        print "attr: " + self.name + "." + name - stats ...
100        return
101
102    def clone (self, elem):
103        keys = elem.attrs.keys()
104        for key in keys:
105            self.attrs[key] = elem.attrs[key]
106        self.rid = elem.rid
107
108    def __dumpAttrs (self):
109        text = ''
110        keys = self.attrs.keys()
111        keys.sort()
112        for key in keys:
113            value = self.attrs[key]
114            text += ' %s="%s"'%(key, value)
115        return text
116