def passing_XMLtree(node, oldlayers_Server, newlayers_Server, cur, conn, name_xmlfile): # Will store the number of Server and URL where store .DTD file global Nwms, URL_dtd # If the node not the text (is not between opening and closing tags) if node.nodeType != node.TEXT_NODE: if(node.nodeName == "Service"): # Handler is called subtree tag <Service> and defined number of Server Nwms = 0 Nwms = parse_subtreeService(node, cur, conn, name_xmlfile) # Get all the layers of this server already exists in the database # SELECT Nlayer, Name From KnownLayers WHERE Nwms = Nwms; condition = {} condition["Nwms"] = Nwms oldlayers_Server = BD.ifselect_table(cur, "KnownLayers", "Nlayer", condition, "Name") # access to the following brother of the node that not to handle the # node some times node = node.nextSibling if(node.nodeName == "Layer"): # Handler is called subtree tag <Service> and defined number of Server parse_subtreeLayer(node, newlayers_Server, cur, conn, -1) # access to the following brother of the node node = node.nextSibling if(node.nodeName == "Capability"): # Update table WMSresources. Find record with primary key = Nwms # SELECT Nwms, Name FROM WMSresources WHERE Nwms = Nwms; condition = {} condition["Nwms"] = Nwms res = BD.ifselect_table(cur, "WMSresources", "Nwms", condition, "Name") if res: # If this Server already exists in the database # UPDATE WMSresources SET # Capabilites = 'node.toxml('utf-8').replace("\", "\'\'")' # WHERE Nwms = Nwms; BD.updatebd_xmlfield(cur, conn, "WMSresources", "Capabilites", node,\ "Nwms", Nwms) if(node.nodeType != node.TEXT_NODE): if node.nodeType == node.DOCUMENT_TYPE_NODE: # If the node is tag <!DOCTYPE> global URL_dtd # URL where store .DTD file URL_dtd = node.systemId # Recursively goes through all subnodes for child in node.childNodes: oldlayers_Server = passing_XMLtree(child, oldlayers_Server, newlayers_Server,\ cur, conn, name_xmlfile) # Returns a list of server layers, that were already in the database return oldlayers_Server
def parse_subtreeLayer(node, newlayer, cur, conn, parent_id): # Defined number of Server global Nwms # Will store the number of Layer Nlayers = 0 # BD.create_KnownLayers(cur, conn) # Value_table saved list values for table KnownLayers as (key, value) value_table = {} # Saved key Nwms which has this layer value_table["Nwms"] = Nwms value_table["Nl_group"] = parent_id value_table["access_mode"] = "" # view all nested tags in <Layer> </Layer> for child in node.childNodes: if child.nodeName in ["Name", "Title", "Abstract"]: value_table[child.nodeName] = get_TextfromTag(child.nodeName, child.childNodes) if child.nodeName in ["LatLonBoundingBox", "BoundingBox"]: # connect all attributes (k - keys and v - value) in one string "att_string" atts = child.attributes or {} att_string = " ".join(["%s=%s " % (k, v) for k, v in atts.items()]) if att_string : value_table[child.nodeName] = att_string if child.nodeName == "KeywordList": # Formed from all the key words one string keyword = " " for child1 in child.childNodes: # Get list keyword via space if child1.nodeName in ["Keyword"]: keyword = keyword + " %s " % get_TextfromTag(child1.nodeName,\ child1.childNodes) value_table[child.nodeName] = keyword if child.nodeName == "Layer": # If it have nested layers if not Nlayers: # If this layer has not yet been added to the database # Create values in table KnownLayers and get primary key. Find records # in table with field "Nwms" lstfind = {} lstfind["Nwms"] = Nwms if "Name" in value_table: lstfind["Name"] = "\'%s\'"%value_table["Name"] else: lstfind["Title"] = "\'%s\'"%value_table["Title"] # SELECT Nlayer FROM KnownLayers # WHERE Nwms = lstfind[Nwms] and # Name = lstfind[Name] and # Title = lstfind[Title]; # IF this records not exists # INSERT INTO KnownLayers (Nlayer, Nwms, Nl_group, access_mode, # Name, Title, Abstract, LatLonBoundingBox, BoundingBox, Keyword) # VALUES (Nlayer, value_table[Nwms], value_table[Nl_group], # value_table[access_mode], value_table[Name], value_table[Title], # value_table[Abstract], value_table[LatLonBoundingBox], # value_table[BoundingBox], value_table[Keyword]); Nlayers = BD.crtable(cur, conn, "KnownLayers", "Nlayer",\ value_table, lstfind) # UPDATE KnownLayers SET # LayerCapabilites = 'node.toxml('utf-8').replace("\", "\'\'")' # WHERE Nlayer = Nlayers; BD.updatebd_xmlfield(cur, conn, "KnownLayers", "LayerCapabilites", node,\ "Nlayer", Nlayers) # Recurses inner layer with the specified parent as Nlayers parse_subtreeLayer(child, newlayer, cur, conn, Nlayers) # Create values in table KnownLayers and get primary key. Find records # in table with field "Nwms" if not Nlayers: # If this layer has not yet been added to the database lstfind = {} lstfind["Nwms"] = Nwms if "Name" in value_table: lstfind["Name"] = "\'%s\'"%value_table["Name"] else: lstfind["Title"] = "\'%s\'"%value_table["Title"] # Entry is added as described above Nlayers = BD.crtable(cur, conn, "KnownLayers", "Nlayer",\ value_table, lstfind) BD.updatebd_xmlfield(cur, conn, "KnownLayers", "LayerCapabilites", node,\ "Nlayer", Nlayers) # Adds a layer to the list created by layers of servers newlayer.append(Nlayers)