merge the formfield patch from ooo-build
[ooovba.git] / toolkit / src2xml / source / globals.py
blob5e170b8c2a6edb343f150f13acc286e7831975dc
1 import sys
3 class ParseError (Exception):
4 pass
6 def error (msg, exit=0):
7 sys.stderr.write (msg)
8 if exit:
9 sys.exit (exit)
11 def progress (msg):
12 sys.stderr.write(msg)
15 def 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
23 class Macro(object):
24 def __init__ (self, name):
25 self.name = name
26 self.tokens = []
27 self.vars = {}
30 class Node(object):
31 def __init__ (self):
32 self.children = []
34 def appendChild (self, childnode):
35 self.children.append(childnode)
36 childnode.parent = self
38 def getChildren (self):
39 return self.children
42 class RootNode(Node):
43 def __init__ (self):
44 Node.__init__(self)
46 def dump (self):
47 chars = ''
48 for child in self.getChildren():
49 chars += child.dump()
50 return chars
53 class Element(Node):
55 INDENT = " "
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 ...
63 # The following attributes are copied when 'clone'ed.
64 self.rid = rid
65 self.attrs = {}
67 def dump (self, level = 0):
68 chars = ''
69 chars += "\n" + Element.INDENT*level
70 chars += '<%s'%self.name
72 if self.rid != None:
73 self.setAttr("rid", self.rid)
75 chars += self.__dumpAttrs()
77 if len(self.children) == 0:
78 chars += '/>'
79 else:
80 chars += '>'
81 for child in self.getChildren():
82 chars += child.dump(level+1)
84 chars += "\n"+Element.INDENT*level
85 chars += "</%s>"%self.name
87 return chars
89 def hasAttr (self, name):
90 return self.attrs.has_key(name)
92 def getAttr (self, name):
93 return self.attrs[name]
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
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
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