1 |
# Copyright (C) 2003 by Intevation GmbH |
2 |
# Authors: |
3 |
# Jonathan Coles <[email protected]> |
4 |
# |
5 |
# This program is free software under the GPL (>=v2) |
6 |
# Read the file COPYING coming with GRASS for details. |
7 |
|
8 |
""" |
9 |
Parser for thuban session files. |
10 |
""" |
11 |
|
12 |
__version__ = "$Revision$" |
13 |
|
14 |
import os |
15 |
import xml.sax |
16 |
import xml.sax.handler |
17 |
from xml.sax import make_parser, ErrorHandler, SAXNotRecognizedException |
18 |
|
19 |
class XMLReader(xml.sax.handler.ContentHandler): |
20 |
|
21 |
# Dictionary mapping element names (or (URI, element name) pairs for |
22 |
# documents using namespaces) to method names. The methods should |
23 |
# accept the same parameters as the startElement (or startElementNS) |
24 |
# methods. The start_dispatcher is used by the default startElement |
25 |
# and startElementNS methods to call a method for the open tag of an |
26 |
# element. |
27 |
start_dispatcher = {} |
28 |
|
29 |
# end_dispatcher works just like start_dispatcher but it's used by |
30 |
# endElement and endElementNS. The method whose names it maps to |
31 |
# should accept the same parameters as endElement and endElementNS. |
32 |
end_dispatcher = {} |
33 |
|
34 |
|
35 |
def __init__(self): |
36 |
self.chars = '' |
37 |
self.__directory = "" |
38 |
self.__dispatchers = {} |
39 |
|
40 |
def read(self, file_or_filename): |
41 |
|
42 |
if hasattr(file_or_filename, "read"): |
43 |
# it's a file object |
44 |
self.__directory = "" |
45 |
self.__file = file_or_filename |
46 |
else: |
47 |
filename = file_or_filename |
48 |
self.__directory = os.path.dirname(filename) |
49 |
self.__file = open(filename) |
50 |
|
51 |
parser = make_parser() |
52 |
parser.setContentHandler(self) |
53 |
parser.setErrorHandler(ErrorHandler()) |
54 |
parser.setFeature(xml.sax.handler.feature_namespaces, 1) |
55 |
|
56 |
# |
57 |
# Well, this isn't pretty, but it appears that if you |
58 |
# use Python 2.2 without the site-package _xmlplus then |
59 |
# the following will fail, and without them it will work. |
60 |
# However, if you do have the site-package and you don't |
61 |
# call these functions, the reader raises an exception |
62 |
# |
63 |
# The reason we set these to 0 in the first place is |
64 |
# because there is an unresolved issue with external |
65 |
# entities causing an exception in the reader |
66 |
# |
67 |
try: |
68 |
parser.setFeature(xml.sax.handler.feature_validation,0) |
69 |
parser.setFeature(xml.sax.handler.feature_external_ges,0) |
70 |
parser.setFeature(xml.sax.handler.feature_external_pes,0) |
71 |
except SAXNotRecognizedException: |
72 |
pass |
73 |
|
74 |
parser.parse(self.__file) |
75 |
|
76 |
self.close() |
77 |
|
78 |
def close(self): |
79 |
self.__file.close() |
80 |
|
81 |
def GetFilename(self): |
82 |
if hasattr(self.__file, "name"): |
83 |
return self.__file.name |
84 |
|
85 |
return "" |
86 |
|
87 |
def GetDirectory(self): |
88 |
return self.__directory |
89 |
|
90 |
|
91 |
def AddDispatchers(self, dict): |
92 |
"""Add the function names that should be used to process XML tags. |
93 |
|
94 |
dict -- a dictionary whose keys are XML tag strings and whose values |
95 |
are pairs of strings such that the first string is |
96 |
the name of the function that should be called when the |
97 |
XML tag opens and the second string is the name of the |
98 |
function that should be called when the XML tag closes. |
99 |
If a pair element is None, no function is called. |
100 |
""" |
101 |
|
102 |
self.__dispatchers.update(dict) |
103 |
|
104 |
def startElementNS(self, name, qname, attrs): |
105 |
"""Call the method given for name in self.start_dispatcher |
106 |
""" |
107 |
if name[0] is None: |
108 |
method_name = self.__dispatchers.get(name[1]) |
109 |
else: |
110 |
# Dispatch with namespace |
111 |
method_name = self.__dispatchers.get(name) |
112 |
if method_name is not None and method_name[0] is not None: |
113 |
getattr(self, method_name[0])(name, qname, attrs) |
114 |
|
115 |
def endElementNS(self, name, qname): |
116 |
"""Call the method given for name in self.end_dispatcher |
117 |
""" |
118 |
if name[0] is None: |
119 |
method_name = self.__dispatchers.get(name[1]) |
120 |
else: |
121 |
# Dispatch with namespace |
122 |
method_name = self.__dispatchers.get(name) |
123 |
if method_name is not None and method_name[1] is not None: |
124 |
getattr(self, method_name[1])(name, qname) |
125 |
|
126 |
def encode(self, str): |
127 |
"""Assume that str is in Unicode and encode it into Latin1. |
128 |
|
129 |
If str is None, return None |
130 |
""" |
131 |
|
132 |
if str is not None: |
133 |
return str.encode("latin1") |
134 |
else: |
135 |
return None |