Mercurial > p > roundup > code
comparison roundup/cgi/TAL/XMLParser.py @ 1049:b9988e118055
moved
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Thu, 05 Sep 2002 00:37:09 +0000 |
| parents | |
| children | c08b3820edd1 |
comparison
equal
deleted
inserted
replaced
| 1048:1250251f2793 | 1049:b9988e118055 |
|---|---|
| 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 """ | |
| 17 | |
| 18 class XMLParser: | |
| 19 | |
| 20 ordered_attributes = 0 | |
| 21 | |
| 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 ] | |
| 44 | |
| 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 | |
| 63 | |
| 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, ' ') | |
| 74 | |
| 75 def parseFile(self, filename): | |
| 76 self.parseStream(open(filename)) | |
| 77 | |
| 78 def parseString(self, s): | |
| 79 self.parser.Parse(s, 1) | |
| 80 | |
| 81 def parseURL(self, url): | |
| 82 import urllib | |
| 83 self.parseStream(urllib.urlopen(url)) | |
| 84 | |
| 85 def parseStream(self, stream): | |
| 86 self.parser.ParseFile(stream) | |
| 87 | |
| 88 def parseFragment(self, s, end=0): | |
| 89 self.parser.Parse(s, end) |
