File: xmlreader.py

package info (click to toggle)
thuban 1.2.2-14
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,176 kB
  • sloc: python: 30,410; ansic: 6,181; xml: 4,234; cpp: 1,595; makefile: 145
file content (134 lines) | stat: -rw-r--r-- 4,612 bytes parent folder | download | duplicates (6)
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
# Copyright (C) 2003, 2005 by Intevation GmbH
# Authors:
# Jonathan Coles <jonathan@intevation.de>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with GRASS for details.

"""
Parser for thuban session files.
"""

__version__ = "$Revision: 2642 $"

import os
import xml.sax
import xml.sax.handler
from xml.sax import make_parser, ErrorHandler, SAXNotRecognizedException
from Thuban import internal_from_unicode

class XMLReader(xml.sax.handler.ContentHandler):

    # Dictionary mapping element names (or (URI, element name) pairs for
    # documents using namespaces) to method names. The methods should
    # accept the same parameters as the startElement (or startElementNS)
    # methods. The start_dispatcher is used by the default startElement
    # and startElementNS methods to call a method for the open tag of an
    # element.
    start_dispatcher = {}

    # end_dispatcher works just like start_dispatcher but it's used by
    # endElement and endElementNS. The method whose names it maps to
    # should accept the same parameters as endElement and endElementNS.
    end_dispatcher = {}


    def __init__(self):
        self.chars = ''
        self.__directory = ""
        self.__dispatchers = {}

    def read(self, file_or_filename):

        if hasattr(file_or_filename, "read"):
            # it's a file object
            self.__directory = ""
            self.__file = file_or_filename
        else:
            filename = file_or_filename
            self.__directory = os.path.dirname(filename)
            self.__file = open(filename)

        parser = make_parser()
        parser.setContentHandler(self)
        parser.setErrorHandler(ErrorHandler())
        parser.setFeature(xml.sax.handler.feature_namespaces, 1)

        #
        # Well, this isn't pretty, but it appears that if you
        # use Python 2.2 without the site-package _xmlplus then
        # the following will fail, and without them it will work.
        # However, if you do have the site-package and you don't
        # call these functions, the reader raises an exception
        #
        # The reason we set these to 0 in the first place is 
        # because there is an unresolved issue with external
        # entities causing an exception in the reader
        #
        try:
            parser.setFeature(xml.sax.handler.feature_validation,0)
            parser.setFeature(xml.sax.handler.feature_external_ges,0)
            parser.setFeature(xml.sax.handler.feature_external_pes,0)
        except SAXNotRecognizedException:
            pass

        parser.parse(self.__file)

        self.close()

    def close(self):
        self.__file.close()
        
    def GetFilename(self):
        if hasattr(self.__file, "name"):
            return self.__file.name

        return ""

    def GetDirectory(self):
        return self.__directory


    def AddDispatchers(self, dict):
        """Add the function names that should be used to process XML tags.

        dict -- a dictionary whose keys are XML tag strings and whose values
                are pairs of strings such that the first string is
                the name of the function that should be called when the
                XML tag opens and the second string is the name of the
                function that should be called when the XML tag closes.
                If a pair element is None, no function is called.
        """

        self.__dispatchers.update(dict)

    def startElementNS(self, name, qname, attrs):
        """Call the method given for name in self.start_dispatcher
        """
        if name[0] is None:
            method_name = self.__dispatchers.get(name[1])
        else:
            # Dispatch with namespace
            method_name = self.__dispatchers.get(name)
        if method_name is not None and method_name[0] is not None:
            getattr(self, method_name[0])(name, qname, attrs)

    def endElementNS(self, name, qname):
        """Call the method given for name in self.end_dispatcher
        """
        if name[0] is None:
            method_name = self.__dispatchers.get(name[1])
        else:
            # Dispatch with namespace
            method_name = self.__dispatchers.get(name)
        if method_name is not None and method_name[1] is not None:
            getattr(self, method_name[1])(name, qname)

    def encode(self, str):
        """Return the unicode object str in Thuban's internal representation

        If str is None, return None
        """
        if str is None:
            return None
        return internal_from_unicode(str)