static XMLResponseItem getResponseItem(XmlNode xnResponseItem) { XMLResponseItem responseItem = new XMLResponseItem(); switch (xnResponseItem.Attributes["type"].Value) { case "int": responseItem.Type = ResponseItemType.INT; break; case "string": responseItem.Type = ResponseItemType.STRING; break; case "list": responseItem.Type = ResponseItemType.LIST; break; case "dict": responseItem.Type = ResponseItemType.DICT; break; default: break; } responseItem.ItemName = xnResponseItem.SelectSingleNode("name").InnerText; responseItem.ItemRemark = xnResponseItem.SelectSingleNode("remark").InnerText; if (xnResponseItem.SelectSingleNode("list") == null) { responseItem.IsEntryList = false; XmlNodeList xnlSubItems = xnResponseItem.SelectNodes("item"); if (xnlSubItems.Count > 0) { foreach (XmlNode xnSubItem in xnlSubItems) { XMLResponseItem subResponseItem = getResponseItem(xnSubItem); responseItem.SubItems.Add(subResponseItem.ItemName, subResponseItem); } } } else { responseItem.IsEntryList = true; XmlNodeList xnlSubItems = xnResponseItem.SelectNodes("list/item"); if (xnlSubItems.Count > 0) { foreach (XmlNode xnSubItem in xnlSubItems) { XMLResponseItem subResponseItem = getResponseItem(xnSubItem); responseItem.SubItems.Add(subResponseItem.ItemName, subResponseItem); } } } return(responseItem); }
static JsonXMLItemMatcher matchJsonEntryWithXMLItem(JsonResponseEntry jsonEntry, XMLResponseItem xmlItem) { String itemKey = xmlItem.ItemName; JsonXMLItemMatcher itemMatcher = new JsonXMLItemMatcher(); switch (jsonEntry.type) { case JsonResponseEntry.ResponseEntryType.DICT: if (xmlItem.Type == ResponseItemType.DICT) { JsonXMLMatcher matcher = matchJsonWithXML(jsonEntry.subEntry, xmlItem.SubItems, false); switch (matcher.matchResult) { case JsonXMLMatcher.MatchResult.LIST_OR_DICT_NULL: itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.DICT_NULL; itemMatcher.strResult = itemKey + "->dict null\n " + matcher.ToString().Replace("\n", "\n "); break; case JsonXMLMatcher.MatchResult.MATCH: itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.DICT_MATCHED; break; case JsonXMLMatcher.MatchResult.MATCH_PARTLY: itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.LIST_OR_DICT_PARTLY_MATCHED; itemMatcher.strResult = itemKey + "-> dict partly match\n " + matcher.ToString().Replace("\n", "\n "); break; case JsonXMLMatcher.MatchResult.NOT_MATCH: itemMatcher.strResult = itemKey + "-> Not match\n " + matcher.ToString().Replace("\n", "\n "); itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH; break; } } else { itemMatcher.strResult = itemKey + "-> Type not match in xml is " + XMLResponseItemTypeToString(xmlItem.Type) + " but in json is DICT\n"; itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH; } break; case JsonResponseEntry.ResponseEntryType.DICT_NULL: if (xmlItem.Type == ResponseItemType.DICT) { itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.DICT_NULL; itemMatcher.strResult = itemKey + "-> Dict null\n"; } else { itemMatcher.strResult = itemKey + "-> Type not match in xml is" + XMLResponseItemTypeToString(xmlItem.Type) + "but in json is DICT_NULL\n"; itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH; } break; case JsonResponseEntry.ResponseEntryType.LIST_ENTRY: if (xmlItem.Type == ResponseItemType.LIST) { JsonXMLMatcher matcher = matchJsonWithXML(jsonEntry.listEntry[0], xmlItem.SubItems, false); switch (matcher.matchResult) { case JsonXMLMatcher.MatchResult.LIST_OR_DICT_NULL: itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.DICT_NULL; itemMatcher.strResult = itemKey + "->list null\n" + matcher.ToString().Replace("\n", "\n "); break; case JsonXMLMatcher.MatchResult.MATCH: itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.DICT_MATCHED; break; case JsonXMLMatcher.MatchResult.MATCH_PARTLY: itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.LIST_OR_DICT_PARTLY_MATCHED; itemMatcher.strResult = itemKey + "-> List partly match\n " + matcher.ToString().Replace("\n", "\n "); break; case JsonXMLMatcher.MatchResult.NOT_MATCH: itemMatcher.strResult = itemKey + "-> Not match\n " + matcher.ToString().Replace("\n", "\n "); itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH; break; } } else { itemMatcher.strResult = itemKey + "-> Type not match in xml is " + XMLResponseItemTypeToString(xmlItem.Type) + " but in json is LIST_ENTRY\n"; itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH; } break; case JsonResponseEntry.ResponseEntryType.LIST_STRING: if (xmlItem.Type == ResponseItemType.LIST) { itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.LIST_STRING_MATCHED; } else { itemMatcher.strResult = itemKey + "-> Type not match in xml is " + XMLResponseItemTypeToString(xmlItem.Type) + " but in json is LIST_STRING\n"; itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH; } break; case JsonResponseEntry.ResponseEntryType.LIST_NULL: if (xmlItem.Type == ResponseItemType.LIST) { itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.LIST_NULL; itemMatcher.strResult = itemKey + "-> List null\n"; } else { itemMatcher.strResult = itemKey + "-> Type not match in xml is " + XMLResponseItemTypeToString(xmlItem.Type) + " but in json is LIST_NULL\n"; itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH; } break; case JsonResponseEntry.ResponseEntryType.STRING: if (xmlItem.Type == ResponseItemType.STRING) { itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.STRING_MATCHED; } else { itemMatcher.strResult = itemKey + "-> Type not match in xml is " + XMLResponseItemTypeToString(xmlItem.Type) + " but in json is STRING\n"; itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH; } break; case JsonResponseEntry.ResponseEntryType.INT: if (xmlItem.Type == ResponseItemType.INT) { itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.INT_MATCHED; } else { itemMatcher.strResult = itemKey + "-> Type not match in xml is " + XMLResponseItemTypeToString(xmlItem.Type) + " but in json is INT\n"; itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH; } break; case JsonResponseEntry.ResponseEntryType.NULL: if (xmlItem.Type == ResponseItemType.INT || xmlItem.Type == ResponseItemType.STRING) { itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.NULL_MATCHED; } else { itemMatcher.strResult = itemKey + "-> Type not match in xml is " + XMLResponseItemTypeToString(xmlItem.Type) + " but in json is NULL\n"; itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH; } break; default: break; } if (itemMatcher.itemMatchResult > xmlItem.matchResult) { xmlItem.matchResult = itemMatcher.itemMatchResult; } return(itemMatcher); }
public static Dictionary <String, XMLApiItem> analyzeXML(FileInfo fi) { Dictionary <String, XMLApiItem> apiItems = new Dictionary <string, XMLApiItem>(); XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(fi.FullName); XmlNode root = xmldoc.SelectSingleNode("root"); XMLApiItem apiItem = new XMLApiItem(); foreach (XmlNode apiNode in root.SelectNodes("api")) { //获取product属性 apiItem.Product = root.Attributes["product"].Value; //解析API参数 apiItem.Name = apiNode.SelectSingleNode("name").InnerText; apiItem.Apiurl = apiNode.SelectSingleNode("apiulr").InnerText; apiItem.Function = apiNode.SelectSingleNode("function").InnerText; if (apiNode.SelectSingleNode("scene") != null) { apiItem.Scene = apiNode.SelectSingleNode("scene").InnerText; } apiItem.MenuFolder = apiNode.SelectSingleNode("menuFolder").InnerText; apiItem.MenuText = apiNode.SelectSingleNode("menuText").InnerText; //解析Request XmlNode xnRequestNode = apiNode.SelectSingleNode("request"); switch (xnRequestNode.Attributes["type"].Value) { case "GET": apiItem.Request.Type = RequestType.GET; break; case "POST": apiItem.Request.Type = RequestType.POST; break; default: break; } XmlNodeList xnlRequestParams = xnRequestNode.SelectNodes("param"); foreach (XmlNode xnRequestParam in xnlRequestParams) { XMLRequestParam requestParam = new XMLRequestParam(); switch (xnRequestParam.Attributes["type"].Value) { case "int": requestParam.Type = RequestParamType.INT; break; case "string": requestParam.Type = RequestParamType.STRING; break; default: break; } requestParam.Compulsory = xnRequestParam.Attributes["compulsory"].Value == "true" ? true : false; requestParam.ParaName = xnRequestParam.SelectSingleNode("paraname").InnerText; requestParam.ParaInstruction = xnRequestParam.SelectSingleNode("paraInstruction").InnerText; apiItem.Request.RequestParams.Add(requestParam); } //解析Response XmlNode xnResponseNode = apiNode.SelectSingleNode("response"); switch (xnResponseNode.Attributes["type"].Value) { case "json": apiItem.Response.Type = ResponseType.JSON; break; case "json_list": apiItem.Response.Type = ResponseType.JSON_LIST; break; case "raw": apiItem.Response.Type = ResponseType.RAW; break; default: break; } XmlNodeList xnlSubItems = xnResponseNode.SelectNodes("item"); if (xnlSubItems.Count > 0) { foreach (XmlNode xnSubItem in xnlSubItems) { XMLResponseItem responseItem = getResponseItem(xnSubItem); apiItem.Response.Items.Add(responseItem.ItemName, responseItem); } } apiItems.Add(apiItem.Apiurl, apiItem); apiItem = new XMLApiItem(); } return(apiItems); }
private TreeNode xmlResponseItemToTreeNode(XMLResponseItem item) { TreeNode node = new TreeNode(); node.Text = item.ItemName; node.ToolTipText = item.ItemRemark; switch (item.Type) { case ResponseItemType.INT: case ResponseItemType.STRING: break; case ResponseItemType.DICT: foreach (XMLResponseItem subitem in item.SubItems.Values) { node.Nodes.Add(xmlResponseItemToTreeNode(subitem)); } break; case ResponseItemType.LIST: if (item.IsEntryList) { foreach (XMLResponseItem subitem in item.SubItems.Values) { node.Nodes.Add(xmlResponseItemToTreeNode(subitem)); } } break; default: break; } switch (item.matchResult) { case DataStructure.Matcher.JsonXMLItemMatcher.ItemMatchResult.DICT_MATCHED: case DataStructure.Matcher.JsonXMLItemMatcher.ItemMatchResult.INT_MATCHED: case DataStructure.Matcher.JsonXMLItemMatcher.ItemMatchResult.LIST_ENTRY_MATCHED: case DataStructure.Matcher.JsonXMLItemMatcher.ItemMatchResult.LIST_STRING_MATCHED: case DataStructure.Matcher.JsonXMLItemMatcher.ItemMatchResult.STRING_MATCHED: node.BackColor = Color.LightGreen; break; case DataStructure.Matcher.JsonXMLItemMatcher.ItemMatchResult.DICT_NULL: case DataStructure.Matcher.JsonXMLItemMatcher.ItemMatchResult.LIST_NULL: case DataStructure.Matcher.JsonXMLItemMatcher.ItemMatchResult.NULL_MATCHED: case DataStructure.Matcher.JsonXMLItemMatcher.ItemMatchResult.LIST_OR_DICT_PARTLY_MATCHED: node.BackColor = Color.Yellow; break; case DataStructure.Matcher.JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH: node.BackColor = Color.White; break; default: node.BackColor = Color.Red; break; } switch (item.Type) { case ResponseItemType.STRING: node.ImageIndex = 0; node.SelectedImageIndex = 0; break; case ResponseItemType.INT: node.ImageIndex = 1; node.SelectedImageIndex = 1; break; case ResponseItemType.LIST: node.ImageIndex = 2; node.SelectedImageIndex = 2; break; case ResponseItemType.DICT: node.ImageIndex = 3; node.SelectedImageIndex = 3; break; default: break; } nodeMap.Add(node, item); node.ContextMenuStrip = cmsTreeNode; return(node); }