xmlif4Dom.py
Go to the documentation of this file.
00001 #
00002 # genxmlif, Release 0.9.0
00003 # file: xmlif4Dom.py
00004 #
00005 # XML interface class to the 4DOM library
00006 #
00007 # history:
00008 # 2005-04-25 rl   created
00009 # 2008-07-01 rl   Limited support of XInclude added
00010 #
00011 # Copyright (c) 2005-2008 by Roland Leuthe.  All rights reserved.
00012 #
00013 # --------------------------------------------------------------------
00014 # The generix XML interface is
00015 #
00016 # Copyright (c) 2005-2008 by Roland Leuthe
00017 #
00018 # By obtaining, using, and/or copying this software and/or its
00019 # associated documentation, you agree that you have read, understood,
00020 # and will comply with the following terms and conditions:
00021 #
00022 # Permission to use, copy, modify, and distribute this software and
00023 # its associated documentation for any purpose and without fee is
00024 # hereby granted, provided that the above copyright notice appears in
00025 # all copies, and that both that copyright notice and this permission
00026 # notice appear in supporting documentation, and that the name of
00027 # the author not be used in advertising or publicity
00028 # pertaining to distribution of the software without specific, written
00029 # prior permission.
00030 #
00031 # THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
00032 # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
00033 # ABILITY AND FITNESS.  IN NO EVENT SHALL THE AUTHOR
00034 # BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
00035 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
00036 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00037 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
00038 # OF THIS SOFTWARE.
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     # for description of the interface methods see xmlifbase.py
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         # XInclude support
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 # Extended DOM generator class derived from XmlDomGenerator
00102 # extended to store related line numbers, file/URL names and 
00103 # defined namespaces in the node object
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)


mavlink
Author(s): Lorenz Meier
autogenerated on Sun May 22 2016 04:05:43