📄 biwsdl.py
字号:
"""class BiWsdlMessage: """ <summary> Constructor. </summary> <param name="aSrcElement">wsdl:message DOM element from which message object should be loaded </param> <param name="aWsdl">Parent BiWsdl of this object</param> """ def __init__(self, aSrcElement, aWsdl): self.__partNames = []; self.__parts = {}; # Load message parts for lPartTag in aSrcElement.getElementsByTagNameNS(BiWsdl.WSDL_NS, "part"): lPartName = lPartTag.getAttribute("name") self.__partNames.append(lPartName) self.__parts[lPartName] = BiWsdlMessagePart(self, lPartTag) """ <summary> Return sequence of message part names. </summary> """ def getPartNames(self): return self.__partNames """ <summary> Given a message part's name, return a BiWsdlMessagePart corresponding to it. </summary> <param name="aPartName">Name of message part</param> """ def getPart(self, aPartName): return self.__parts[aPartName]"""<summary>A BiWsdlDefinitionDictionary is a keyed collection of WSDL definitions. In addition to providing a lookup service for registered definitions, the dictionary can be connected to imported dictionaries -- and delegate lookup to them when a key is not found within the dictionary's local data. This facility is used to manage imported WSDLs.</summary>"""class BiWsdlDefinitionDictionary: """ <summary> Constructor </summary> <param name="aWsdl">Owner BiWsdl of this dictionary. </param> <param name="aDefTagType">Name of tags within wsdl:definitions designating definitions relevant to this dictionary </param> <param name="aDefFactory">Factory function to create objects representing the definitions for this dictionary. This factory function is of prototype: fn(aDefinitionDomElement, aOwnerBiWsdl). </param> """ def __init__(self, aWsdl, aDefTagType, aDefFactory): self.wsdl = aWsdl self.defTag = aDefTagType self.defFactory = aDefFactory self.imports = [] self.definitions = {} """ <summary> Add an imported definitions dictionary to this dictionary. When lookups are performed against self, if a matching key is not found among definitions contained in self, the lookup will be delgated to imported definitions dictionaries, in the order of registration using this function. </summary> <param name="aImport">Imported definitions dictionary to add </param> """ def addImport(self, aImport): self.imports.append(aImport) """ <summary> Load definitions from a wsdl:definitions DOM element into the dictionary, constructing object representations of defintions using factory function specified on construction. </summary> <param name="aDomDefs">wsdl:definitions DOM element the children of which should be loaded into definitions dictionary. </param> """ def loadDom(self, aDomDefs): # Get relevant tags under wsdl:definitions, lDefTags = aDomDefs.getElementsByTagNameNS(BiWsdl.WSDL_NS, self.defTag) for lDefTag in lDefTags: # Construct object representations and save in dictionary. self.definitions["[" + self.wsdl.getTargetNamespace() + "]" + lDefTag.getAttribute("name")] = self.defFactory(lDefTag, self.wsdl) """ <summary> Lookup a definition by its name ([ns]localname). If definition not found within definitions loaded into this dictionary, delegate lookup to all imported dictionaries (registered thru addImport). </summary> <param name="aName">Name of definition to lookup </param> """ def getDefinition(self, aName): # Definition found locally? if self.definitions.has_key(aName): # return it return self.definitions[aName] else: # Let imports locate definition for lImport in self.imports: lResultObj = lImport.getDefinition(aName) if lResultObj: return lResultObj # Definition not found. return None """ <summary> Return names of definitions </summary> """ def getNames(self): return itertools.chain( self.definitions.keys(), *itertools.imap(lambda lImport: lImport.getNames(), self.imports) );"""<summary>Represents a WSDL file and the definitions within it -- including definitions taken from imported WSDL files.</summary>"""class BiWsdl: WSDL_NS = "http://schemas.xmlsoap.org/wsdl/" """ <summary> Constructor </summary> <param name="aSrc">Source for WSDL file.</param> """ def __init__(self, aSrc): # Setup definition dictionaries and imports list self.__imports = [] self.__messages = BiWsdlDefinitionDictionary(self, "message", BiWsdlMessage) self.__portTypes = BiWsdlDefinitionDictionary(self, "portType", BiWsdlPortType) self.__bindings = BiWsdlDefinitionDictionary(self, "binding", BiWsdlBinding) self.__services = BiWsdlDefinitionDictionary(self, "service", BiWsdlService) # Parse XML document, make sure we have a single wsdl:definitions root, and record target namespace for definitions BiWsdlLogger.debug("Loading " + aSrc) self.__baseUrl = aSrc; self.__wsdlDocument = parse(urllib.urlopen(aSrc)) lDefNodes = self.__wsdlDocument.getElementsByTagNameNS(BiWsdl.WSDL_NS, "definitions") if lDefNodes.length<>1 or lDefNodes[0] is not self.__wsdlDocument.documentElement: raise "A single wsdl:definitions node expected." self.__tns = self.__wsdlDocument.documentElement.getAttribute("targetNamespace") # Process imported WSDLs -- load the WSDLs, and connect their dictionaries to ours. self.__processImports() # Load definitions from WSDL DOM into dictionaries. BiWsdlLogger.debug("Resolving definitions for " + aSrc) self.__messages.loadDom(self.__wsdlDocument) self.__portTypes.loadDom(self.__wsdlDocument) self.__bindings.loadDom(self.__wsdlDocument) self.__services.loadDom(self.__wsdlDocument) BiWsdlLogger.debug("WSDL " + aSrc + " loaded.") """ <summary> Construct and load BiWsdl objects for wsdl:import statements, and import them into this BiWsdl instance. </summary> """ def __processImports(self): # Iterate through wsdl:import nodes.... for lImport in self.__wsdlDocument.documentElement.getElementsByTagNameNS(BiWsdl.WSDL_NS, "import"): # Construct BiWsdl object and load imported WSDL into it lNs = lImport.getAttribute("namespace") lLocation = lImport.getAttribute("location") lImportedDoc = BiWsdl(urlparse.urljoin(self.__baseUrl, lLocation)) # Make sure imported WSDL's targetNamespace matches the namespace specified in the wsdl:import element. if lImportedDoc.getTargetNamespace()<>lNs: raise "Target namespace of WSDL differs from namespace attribute of wsdl:import tag." # Record imported WSDL object, self.__imports.append(lImportedDoc) # And import definition dictionaries into this instance's definition dictionaries self.__messages.addImport(lImportedDoc.__messages) self.__portTypes.addImport(lImportedDoc.__portTypes) self.__bindings.addImport(lImportedDoc.__bindings) self.__services.addImport(lImportedDoc.__services) """ <summary> Get target namespace for definitions within this WSDL </summary> """ def getTargetNamespace(self): return self.__tns """ <summary> Get message definitions dictionary (as a BiWsdlDefinitionDictionary object) </summary> """ def getMessages(self): return self.__messages """ <summary> Get binding definitions dictionary (as a BiWsdlDefinitionDictionary object) </summary> """ def getBindings(self): return self.__bindings """ <summary> Get port type definitions dictionary (as a BiWsdlDefinitionDictionary object) </summary> """ def getPortTypes(self): return self.__portTypes """ <summary> Get service definitions dictionary (as a BiWsdlDefinitionDictionary object) </summary> """ def getServices(self): return self.__servicesQNameRe = re.compile("(([^:]+):)?(.*)")"""<summary>Expand a QName (pfx:localname) to form [namespace-uri]localname, given a context node. The context node constitutes the namespace prefix definitions in effect for the translation.</summary><param name="aQName">QName to transform </param><param name="aContextNode">Node within the context of which name translation should occur</param>"""def expandQName(aQName, aContextNode): # Split QName to pfx, localname lMatchRes = QNameRe.match(aQName) lPf = lMatchRes.group(2) lLn = lMatchRes.group(3) # Devise namespace declaration to look for (xmlns= / xmlns:pfx=) lUri = "" lAttrSeek = "" if lPf: lAttrSeek="xmlns:"+lPf else: lAttrSeek="xmlns" # Look for namespace declaration in ancestors of context node until # found. while lUri=="" and aContextNode is not None: if isinstance(aContextNode, Element) and aContextNode.getAttribute(lAttrSeek): lUri = aContextNode.getAttribute(lAttrSeek) aContextNode = aContextNode.parentNode # Return expanded name return "[" + lUri + "]" + lLn"""<summary>Get the local part of a QName</summary><param name="aQName">QName to retrieve the local part of which</param>"""def getQNameLocalPart(aQName): return QNameRe.match(aQName).group(3)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -