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