Code

removed debugging
[roundup.git] / roundup / cgi / TAL / XMLParser.py
1 ##############################################################################
2 #
3 # Copyright (c) 2001, 2002 Zope Corporation and Contributors.
4 # All Rights Reserved.
5
6 # This software is subject to the provisions of the Zope Public License,
7 # Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
8 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11 # FOR A PARTICULAR PURPOSE
12
13 ##############################################################################
14 """
15 Generic expat-based XML parser base class.
17 Modified for Roundup 0.5 release:
19 - removed dependency on zLOG
21 """
23 class XMLParser:
25     ordered_attributes = 0
27     handler_names = [
28         "StartElementHandler",
29         "EndElementHandler",
30         "ProcessingInstructionHandler",
31         "CharacterDataHandler",
32         "UnparsedEntityDeclHandler",
33         "NotationDeclHandler",
34         "StartNamespaceDeclHandler",
35         "EndNamespaceDeclHandler",
36         "CommentHandler",
37         "StartCdataSectionHandler",
38         "EndCdataSectionHandler",
39         "DefaultHandler",
40         "DefaultHandlerExpand",
41         "NotStandaloneHandler",
42         "ExternalEntityRefHandler",
43         "XmlDeclHandler",
44         "StartDoctypeDeclHandler",
45         "EndDoctypeDeclHandler",
46         "ElementDeclHandler",
47         "AttlistDeclHandler"
48         ]
50     def __init__(self, encoding=None):
51         self.parser = p = self.createParser()
52         if self.ordered_attributes:
53             try:
54                 self.parser.ordered_attributes = self.ordered_attributes
55             except AttributeError:
56                 #zLOG.LOG("TAL.XMLParser", zLOG.INFO, 
57                 #         "Can't set ordered_attributes")
58                 self.ordered_attributes = 0
59         for name in self.handler_names:
60             method = getattr(self, name, None)
61             if method is not None:
62                 try:
63                     setattr(p, name, method)
64                 except AttributeError:
65                     #zLOG.LOG("TAL.XMLParser", zLOG.PROBLEM,
66                     #         "Can't set expat handler %s" % name)
67                     pass
69     def createParser(self, encoding=None):
70         global XMLParseError
71         try:
72             from Products.ParsedXML.Expat import pyexpat
73             XMLParseError = pyexpat.ExpatError
74             return pyexpat.ParserCreate(encoding, ' ')
75         except ImportError:
76             from xml.parsers import expat
77             XMLParseError = expat.ExpatError
78             return expat.ParserCreate(encoding, ' ')
80     def parseFile(self, filename):
81         self.parseStream(open(filename))
83     def parseString(self, s):
84         self.parser.Parse(s, 1)
86     def parseURL(self, url):
87         import urllib
88         self.parseStream(urllib.urlopen(url))
90     def parseStream(self, stream):
91         self.parser.ParseFile(stream)
93     def parseFragment(self, s, end=0):
94         self.parser.Parse(s, end)