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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
"""
# Copyright 1999-2005 Gentoo Foundation
# This source code is distributed under the terms of version 2 of the GNU
# General Public License as published by the Free Software Foundation, a copy
# of which can be found in the main directory of this project.
Gentoo Linux Installer
$Id: SimpleXMLParser.py,v 1.13 2006/01/22 23:42:29 agaffney Exp $
"""
import xml.sax, string
class SimpleXMLParser(xml.sax.ContentHandler):
##
# Brief description of function
# @param self Parameter description
# @param file=None Parameter description
def __init__(self, file=None):
self._xml_elements = []
self._xml_attrs = []
self._xml_current_data = ""
self._fntable = {}
self._path = file
self._prepass = False
##
# Brief description of function
# @param self Parameter description
# @param path Parameter description
# @param fn Parameter description
# @param call_on_null=False Parameter description
def addHandler(self, path, fn, call_on_null=False):
try:
self._fntable[path].append((fn,call_on_null))
except KeyError:
self._fntable[path] = [(fn, call_on_null)]
##
# Brief description of function
# @param self Parameter description
# @param path Parameter description
# @param fn Parameter description
def delHandler(self, path, fn):
try:
for function in self._fntable[path]:
if function[0] == fn:
self._fntable[path].remove(function)
return True
return False
except KeyError:
return False
def startElement(self, name, attr):
"""
XML SAX start element handler
Called when the SAX parser encounters an XML openning element.
"""
self._xml_elements.append(name)
self._xml_attrs.append(attr)
self._xml_current_data = ""
##
# Brief description of function
# @param self Parameter description
# @param name Parameter description
def endElement(self, name):
path = self._xml_element_path()
if self._prepass:
if self._xml_elements[-2] == "include":
if self._xml_elements[-1] == "file":
pass
elif self._xml_elements[-1] == "command":
pass
if path in self._fntable.keys():
for fn in self._fntable[path]:
if self._xml_current_data != "" or fn[1]:
# This is being disabled since this should only apply to certain values
# if self._xml_current_data == "True":
# self._xml_current_data = True
# if self._xml_current_data == "False":
# self._xml_current_data = False
# if self._xml_current_data == "None":
# self._xml_current_data = None
if self._xml_current_data.startswith("<![CDATA["):
self._xml_current_data = self._xml_current_data[9:-5]
else:
self._xml_current_data = self._xml_current_data.strip()
fn[0](path, self._xml_current_data, self._xml_attrs[-1])
# Keep the XML state
self._xml_current_data = ""
self._xml_attrs.pop()
self._xml_elements.pop()
##
# Brief description of function
# @param self Parameter description
# @param data Parameter description
def characters(self, data):
"""
XML SAX character data handler
Called when the SAX parser encounters character data.
"""
# This converts data to a string instead of being Unicode
# Maybe we should use Unicode strings instead of normal strings?
self._xml_current_data += str(data)
##
# Brief description of function
# @param self Parameter description
def _xml_element_path(self):
"""
Return path to current XML node
"""
return string.join(self._xml_elements, '/')
##
# Brief description of function
# @param self Parameter description
# @param path=None Parameter description
def parse(self, path=None):
"""
Parse serialized configuration file.
"""
# self._prepass = True
if path == None and self._path == None:
raise GLIException("NoFileGiven",'fatal', 'parse', "You must specify a file to parse!")
elif path == None:
xml.sax.parse(self._path, self)
else:
xml.sax.parse(path, self)
|