00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 import urllib
00042 from xml.dom.ext.reader.Sax2 import Reader, XmlDomGenerator
00043 from xml.sax._exceptions import SAXParseException
00044 from ..genxmlif import XMLIF_4DOM, GenXmlIfError
00045 from xmlifUtils import convertToAbsUrl
00046 from xmlifDom import XmlInterfaceDom, XmlIfBuilderExtensionDom, InternalDomTreeWrapper, InternalDomElementWrapper
00047
00048
00049 class XmlInterface4Dom (XmlInterfaceDom):
00050
00051
00052
00053
00054 def __init__ (self, verbose, useCaching, processXInclude):
00055 XmlInterfaceDom.__init__ (self, verbose, useCaching, processXInclude)
00056 self.xmlIfType = XMLIF_4DOM
00057 if self.verbose:
00058 print "Using 4Dom interface module..."
00059
00060
00061 def parse (self, file, baseUrl="", internalOwnerDoc=None):
00062 absUrl = convertToAbsUrl (file, baseUrl)
00063 fp = urllib.urlopen (absUrl)
00064 return self._parseStream (fp, file, absUrl, internalOwnerDoc)
00065
00066
00067 def parseString (self, text, baseUrl="", internalOwnerDoc=None):
00068 import cStringIO
00069 fp = cStringIO.StringIO(text)
00070 absUrl = convertToAbsUrl ("", baseUrl)
00071 return self._parseStream (fp, "", absUrl, internalOwnerDoc)
00072
00073
00074 def _parseStream (self, fp, file, absUrl, internalOwnerDoc):
00075 reader = Reader(validate=0, keepAllWs=0, catName=None,
00076 saxHandlerClass=ExtXmlDomGenerator, parser=None)
00077 reader.handler.extinit(file, absUrl, reader.parser, self)
00078 if internalOwnerDoc != None:
00079 ownerDoc = internalOwnerDoc.document
00080 else:
00081 ownerDoc = None
00082 try:
00083 tree = reader.fromStream(fp, ownerDoc)
00084 fp.close()
00085 except SAXParseException, errInst:
00086 fp.close()
00087 raise GenXmlIfError, "%s: SAXParseException: %s" %(file, str(errInst))
00088
00089 treeWrapper = reader.handler.treeWrapper
00090
00091
00092 if self.processXInclude:
00093 if internalOwnerDoc == None:
00094 internalOwnerDoc = treeWrapper.getTree()
00095 self.xInclude (treeWrapper.getRootNode(), absUrl, internalOwnerDoc)
00096
00097 return treeWrapper
00098
00099
00100
00101
00102
00103
00104
00105 class ExtXmlDomGenerator(XmlDomGenerator, XmlIfBuilderExtensionDom):
00106 def __init__(self, keepAllWs=0):
00107 XmlDomGenerator.__init__(self, keepAllWs)
00108 self.treeWrapper = None
00109
00110
00111 def extinit (self, filePath, absUrl, parser, xmlIf):
00112 self.filePath = filePath
00113 self.absUrl = absUrl
00114 self.parser = parser
00115 self.xmlIf = xmlIf
00116
00117
00118 def startElement(self, name, attribs):
00119 XmlDomGenerator.startElement(self, name, attribs)
00120
00121 if not self.treeWrapper:
00122 self.treeWrapper = self.xmlIf.treeWrapperClass(self, InternalDomTreeWrapper(self._rootNode), self.xmlIf.useCaching)
00123 XmlIfBuilderExtensionDom.__init__(self, self.filePath, self.absUrl, self.treeWrapper, self.xmlIf.elementWrapperClass)
00124
00125 curNode = self._nodeStack[-1]
00126 internal4DomElementWrapper = InternalDomElementWrapper(curNode, self.treeWrapper.getTree())
00127 curNs = self._namespaces.items()
00128 try:
00129 curNs.remove( (None,None) )
00130 except:
00131 pass
00132
00133 XmlIfBuilderExtensionDom.startElementHandler (self, internal4DomElementWrapper, self.parser.getLineNumber(), curNs)
00134
00135
00136 def endElement(self, name):
00137 curNode = self._nodeStack[-1]
00138 XmlIfBuilderExtensionDom.endElementHandler (self, curNode.xmlIfExtInternalWrapper, self.parser.getLineNumber())
00139 XmlDomGenerator.endElement(self, name)