Code

moved
[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.
16 """
18 class XMLParser:
20     ordered_attributes = 0
22     handler_names = [
23         "StartElementHandler",
24         "EndElementHandler",
25         "ProcessingInstructionHandler",
26         "CharacterDataHandler",
27         "UnparsedEntityDeclHandler",
28         "NotationDeclHandler",
29         "StartNamespaceDeclHandler",
30         "EndNamespaceDeclHandler",
31         "CommentHandler",
32         "StartCdataSectionHandler",
33         "EndCdataSectionHandler",
34         "DefaultHandler",
35         "DefaultHandlerExpand",
36         "NotStandaloneHandler",
37         "ExternalEntityRefHandler",
38         "XmlDeclHandler",
39         "StartDoctypeDeclHandler",
40         "EndDoctypeDeclHandler",
41         "ElementDeclHandler",
42         "AttlistDeclHandler"
43         ]
45     def __init__(self, encoding=None):
46         self.parser = p = self.createParser()
47         if self.ordered_attributes:
48             try:
49                 self.parser.ordered_attributes = self.ordered_attributes
50             except AttributeError:
51                 #zLOG.LOG("TAL.XMLParser", zLOG.INFO, 
52                 #         "Can't set ordered_attributes")
53                 self.ordered_attributes = 0
54         for name in self.handler_names:
55             method = getattr(self, name, None)
56             if method is not None:
57                 try:
58                     setattr(p, name, method)
59                 except AttributeError:
60                     #zLOG.LOG("TAL.XMLParser", zLOG.PROBLEM,
61                     #         "Can't set expat handler %s" % name)
62                     pass
64     def createParser(self, encoding=None):
65         global XMLParseError
66         try:
67             from Products.ParsedXML.Expat import pyexpat
68             XMLParseError = pyexpat.ExpatError
69             return pyexpat.ParserCreate(encoding, ' ')
70         except ImportError:
71             from xml.parsers import expat
72             XMLParseError = expat.ExpatError
73             return expat.ParserCreate(encoding, ' ')
75     def parseFile(self, filename):
76         self.parseStream(open(filename))
78     def parseString(self, s):
79         self.parser.Parse(s, 1)
81     def parseURL(self, url):
82         import urllib
83         self.parseStream(urllib.urlopen(url))
85     def parseStream(self, stream):
86         self.parser.ParseFile(stream)
88     def parseFragment(self, s, end=0):
89         self.parser.Parse(s, end)