Mercurial > p > roundup > code
comparison TAL/XMLParser.py @ 982:bfd348432420
Adding TAL to the dist
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Fri, 30 Aug 2002 08:23:53 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 979:f36ffa50374f | 982:bfd348432420 |
|---|---|
| 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 import zLOG | |
| 19 | |
| 20 class XMLParser: | |
| 21 | |
| 22 ordered_attributes = 0 | |
| 23 | |
| 24 handler_names = [ | |
| 25 "StartElementHandler", | |
| 26 "EndElementHandler", | |
| 27 "ProcessingInstructionHandler", | |
| 28 "CharacterDataHandler", | |
| 29 "UnparsedEntityDeclHandler", | |
| 30 "NotationDeclHandler", | |
| 31 "StartNamespaceDeclHandler", | |
| 32 "EndNamespaceDeclHandler", | |
| 33 "CommentHandler", | |
| 34 "StartCdataSectionHandler", | |
| 35 "EndCdataSectionHandler", | |
| 36 "DefaultHandler", | |
| 37 "DefaultHandlerExpand", | |
| 38 "NotStandaloneHandler", | |
| 39 "ExternalEntityRefHandler", | |
| 40 "XmlDeclHandler", | |
| 41 "StartDoctypeDeclHandler", | |
| 42 "EndDoctypeDeclHandler", | |
| 43 "ElementDeclHandler", | |
| 44 "AttlistDeclHandler" | |
| 45 ] | |
| 46 | |
| 47 def __init__(self, encoding=None): | |
| 48 self.parser = p = self.createParser() | |
| 49 if self.ordered_attributes: | |
| 50 try: | |
| 51 self.parser.ordered_attributes = self.ordered_attributes | |
| 52 except AttributeError: | |
| 53 zLOG.LOG("TAL.XMLParser", zLOG.INFO, | |
| 54 "Can't set ordered_attributes") | |
| 55 self.ordered_attributes = 0 | |
| 56 for name in self.handler_names: | |
| 57 method = getattr(self, name, None) | |
| 58 if method is not None: | |
| 59 try: | |
| 60 setattr(p, name, method) | |
| 61 except AttributeError: | |
| 62 zLOG.LOG("TAL.XMLParser", zLOG.PROBLEM, | |
| 63 "Can't set expat handler %s" % name) | |
| 64 | |
| 65 def createParser(self, encoding=None): | |
| 66 global XMLParseError | |
| 67 try: | |
| 68 from Products.ParsedXML.Expat import pyexpat | |
| 69 XMLParseError = pyexpat.ExpatError | |
| 70 return pyexpat.ParserCreate(encoding, ' ') | |
| 71 except ImportError: | |
| 72 from xml.parsers import expat | |
| 73 XMLParseError = expat.ExpatError | |
| 74 return expat.ParserCreate(encoding, ' ') | |
| 75 | |
| 76 def parseFile(self, filename): | |
| 77 self.parseStream(open(filename)) | |
| 78 | |
| 79 def parseString(self, s): | |
| 80 self.parser.Parse(s, 1) | |
| 81 | |
| 82 def parseURL(self, url): | |
| 83 import urllib | |
| 84 self.parseStream(urllib.urlopen(url)) | |
| 85 | |
| 86 def parseStream(self, stream): | |
| 87 self.parser.ParseFile(stream) | |
| 88 | |
| 89 def parseFragment(self, s, end=0): | |
| 90 self.parser.Parse(s, end) |
