Go to the documentation of this file.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
00042
00043 __author__ = "Roland Leuthe <roland@leuthe-net.de>"
00044 __date__ = "28 July 2008"
00045 __version__ = "0.9"
00046
00047 from xml.dom import XML_NAMESPACE, XMLNS_NAMESPACE
00048 from xmlifUtils import NsNameTupleFactory, convertToAbsUrl
00049
00050
00051
00052
00053
00054
00055
00056
00057 class XmlIfBuilderExtensionBase:
00058 """XmlIf builder extension base class.
00059
00060 This class provides additional data (e.g. line numbers or caches)
00061 for an element node which are stored in the element node object during parsing.
00062 """
00063
00064 def __init__ (self, filePath, absUrl, treeWrapper, elementWrapperClass):
00065 """Constructor for this class
00066
00067 Input parameter:
00068 filePath: contains the file path of the corresponding XML file
00069 absUrl: contains the absolute URL of the corresponding XML file
00070 """
00071 self.filePath = filePath
00072 self.absUrl = absUrl
00073 self.baseUrlStack = [absUrl, ]
00074 self.treeWrapper = treeWrapper
00075 self.elementWrapperClass = elementWrapperClass
00076
00077
00078 def startElementHandler (self, curNode, startLineNumber, curNs, attributes=[]):
00079 """Called by the XML parser at creation of an element node.
00080
00081 Input parameter:
00082 curNode: current element node
00083 startLineNumber: first line number of the element tag in XML file
00084 curNs: namespaces visible for this element node
00085 attributes: list of attributes and their values for this element node
00086 (same sequence as int he XML file)
00087 """
00088
00089 elementWrapper = self.elementWrapperClass(curNode, self.treeWrapper, curNs, initAttrSeq=0)
00090
00091 elementWrapper.baseUrl = self.__getBaseUrl(elementWrapper)
00092 elementWrapper.absUrl = self.absUrl
00093 elementWrapper.filePath = self.filePath
00094 elementWrapper.startLineNumber = startLineNumber
00095 elementWrapper.curNs.extend ([("xml", XML_NAMESPACE), ("xmlns", XMLNS_NAMESPACE)])
00096
00097 if attributes != []:
00098 for i in range (0, len(attributes), 2):
00099 elementWrapper.attributeSequence.append(attributes[i])
00100 else:
00101 attrList = elementWrapper.getAttributeDict().keys()
00102 attrList.sort()
00103 elementWrapper.attributeSequence.extend (attrList)
00104
00105 self.baseUrlStack.insert (0, elementWrapper.baseUrl)
00106
00107
00108 def endElementHandler (self, curNode, endLineNumber):
00109 """Called by the XML parser after creation of an element node.
00110
00111 Input parameter:
00112 curNode: current element node
00113 endLineNumber: last line number of the element tag in XML file
00114 """
00115 curNode.xmlIfExtElementWrapper.endLineNumber = endLineNumber
00116 self.baseUrlStack.pop (0)
00117
00118
00119 def __getBaseUrl (self, elementWrapper):
00120 """Retrieve base URL for the given element node.
00121
00122 Input parameter:
00123 elementWrapper: wrapper of current element node
00124 """
00125 nsNameBaseAttr = NsNameTupleFactory ((XML_NAMESPACE, "base"))
00126 if elementWrapper.hasAttribute(nsNameBaseAttr):
00127 return convertToAbsUrl (elementWrapper.getAttribute(nsNameBaseAttr), self.baseUrlStack[0])
00128 else:
00129 return self.baseUrlStack[0]
00130