private void ProcessRawDocument(CPS_Connection connection, XmlElement xmld, bool saveDocs) { if (xmld.Name == connection.getDocumentRootXpath()) { string id = ""; if (this.p_command == "show-history") { try { XmlElement xmlid = xmld["revision_id", "www.clusterpoint.com"]; id = xmlid.InnerXml; } catch (Exception) { } } else { List<string> idPath = connection.getDocumentIdXpath(); XmlElement xmlid = xmld; int i = 0; for (i = 1; i < idPath.Count; i++) { if (xmlid[idPath[i]] != null) xmlid = xmlid[idPath[i]]; else break; if (i + 1 == idPath.Count) id = xmlid.InnerXml; } } if (id.Length > 0) { if (saveDocs) this.p_documents[id] = xmld; else this.p_documents[id] = null; } } }
/// <summary> /// Creates new instance of CPS Response class. /// </summary> /// <param name="connection">CPS_Connection class instance.</param> /// <param name="request">CPS_Request class instance.</param> /// <param name="responseXml">Raw response XML as string.</param> public CPS_Response(CPS_Connection connection, CPS_Request request, string responseXml) { this.p_connection = connection; this.p_transactionId = null; this.p_simpleXml = new XmlDocument(); try { this.p_simpleXml.LoadXml(responseXml); } catch (Exception) { throw new CPS_Exception("Received invalid XML response").SetCode(CPS_Exception.ERROR_CODE.INVALID_RESPONSE); } XmlElement content = null; this.p_errors = new List<CPS_SimpleXML>(); foreach (XmlElement xmle in this.p_simpleXml.DocumentElement.ChildNodes) { if (xmle.Name == "cps:storage") this.p_storageName = xmle.InnerXml; else if (xmle.Name == "cps:command") this.p_command = xmle.InnerXml; else if (xmle.Name == "cps:seconds") { try { this.p_seconds = float.Parse(xmle.InnerXml, System.Globalization.CultureInfo.InvariantCulture.NumberFormat); } catch (Exception) { this.p_seconds = 0; } } else if (xmle.Name == "cps:error") { this.p_errors.Add(new CPS_SimpleXML(xmle)); } else if (xmle.Name == "cps:content") content = xmle; } foreach (CPS_SimpleXML sxer in this.p_errors) { string elvl = sxer["level"]; if (elvl == "REJECTED" || elvl == "FAILED" || elvl == "ERROR" || elvl == "FATAL") throw new CPS_Exception(sxer["message"]).SetCode(sxer["code"]); } this.p_textParams = new Dictionary<string, string>(); this.p_contentArray = null; this.p_documents = new Dictionary<string, XmlElement>(); this.p_facets = new Dictionary<string, Dictionary<string, int>>(); bool saveDocs = true; this.p_words = null; this.p_wordCounts = new Dictionary<string, int>(); this.p_aggregate = new Dictionary<string, List<XmlElement>>(); if (content != null) { switch (this.p_command) { case "update": case "delete": case "insert": case "partial-replace": case "partial-xreplace": case "replace": case "lookup": if (this.p_command != "lookup") saveDocs = false; break; case "search": case "retrieve": case "retrieve-last": case "retrieve-first": case "list-last": case "list-first": case "similar": case "show-history": case "cursor-next-batch": // In PHP here comes document extraction. In C# document extraction is later, so nothing to do here break; case "alternatives": Dictionary<string, Dictionary<string, CPS_AlternativesResponse.WordInfo>> alt_words = new Dictionary<string, Dictionary<string, CPS_AlternativesResponse.WordInfo>>(); foreach (XmlElement xaltlist in content.ChildNodes) foreach (XmlElement xalt in xaltlist.ChildNodes) { if (xalt.Name == "alternatives") { Dictionary<string, CPS_AlternativesResponse.WordInfo> winfo = new Dictionary<string, CPS_AlternativesResponse.WordInfo>(); XmlElement xto = xalt["to"]; if (xto == null) continue; XmlElement xto_count = xalt["count"]; if (xto_count == null) continue; int ito_count = 0; try { ito_count = int.Parse(xto_count.InnerXml); } catch (Exception) { continue; } foreach (XmlElement xword in xalt.ChildNodes) { if (xword.Name == "word") { XmlAttribute xcount = xword.Attributes["count"]; XmlAttribute xcr = xword.Attributes["cr"]; XmlAttribute xidif = xword.Attributes["idif"]; XmlAttribute xh = xword.Attributes["h"]; if (xcount == null || xcr == null || xidif == null || xh == null) continue; CPS_AlternativesResponse.WordInfo info = new CPS_AlternativesResponse.WordInfo(); info.word = xword.InnerXml; try { info.count = int.Parse(xcount.Value); info.cr = float.Parse(xcr.Value, System.Globalization.CultureInfo.InvariantCulture.NumberFormat); info.idif = float.Parse(xidif.Value, System.Globalization.CultureInfo.InvariantCulture.NumberFormat); info.h = float.Parse(xh.Value, System.Globalization.CultureInfo.InvariantCulture.NumberFormat); } catch (Exception) { continue; } winfo[info.word] = info; } } alt_words[xto.InnerXml] = winfo; this.p_wordCounts[xto.InnerXml] = ito_count; } } this.p_words = alt_words; break; case "list-words": Dictionary<string, Dictionary<string, CPS_ListWordsResponse.WordInfo>> list_words = new Dictionary<string, Dictionary<string, CPS_ListWordsResponse.WordInfo>>(); foreach (XmlElement xlist in content.ChildNodes) { if (xlist.Name == "list") { Dictionary<string, CPS_ListWordsResponse.WordInfo> winfo = new Dictionary<string, CPS_ListWordsResponse.WordInfo>(); XmlAttribute xto = xlist.Attributes["to"]; if (xto == null) continue; foreach (XmlElement xword in xlist.ChildNodes) { if (xword.Name == "word") { XmlAttribute xcount = xword.Attributes["count"]; if (xcount == null) continue; CPS_ListWordsResponse.WordInfo info = new CPS_ListWordsResponse.WordInfo(); info.word = xword.InnerXml; try { info.count = int.Parse(xcount.Value); } catch (Exception) { continue; } winfo[info.word] = info; } } list_words[xto.Value] = winfo; } } this.p_words = list_words; break; case "status": case "list-paths": case "list-databases": case "create-database": case "edit-database": case "rename-database": case "drop-database": case "describe-database": case "list-nodes": case "list-hubs": case "list-hosts": case "set-offline": case "set-online": case "list-alerts": this.p_contentArray = content; break; case "begin-transaction": try { this.p_transactionId = content["transaction_id"].InnerXml; } catch (Exception) { this.p_transactionId = null; } break; case "commit-transaction": case "rollback-transaction": this.p_transactionId = null; break; default: // nothing special break; } if (this.p_command == "search" || this.p_command == "list-facets") { foreach (XmlElement xmle in content.ChildNodes) { if (xmle.Name == "facet") { XmlAttribute xattr = xmle.Attributes["path"]; if (xattr == null || xattr.Value.Length <= 0) continue; string path = xattr.Value; this.p_facets[path] = new Dictionary<string, int>(); foreach (XmlElement xmlt in xmle.ChildNodes) { if (xmlt.Name == "term") { this.p_facets[path][xmlt.InnerXml] = 0; if (this.p_command == "search") { xattr = xmlt.Attributes["hits"]; if (xattr != null && xattr.Value.Length > 0) { try { this.p_facets[path][xmlt.InnerXml] = int.Parse(xattr.Value); } catch (Exception) { } } } } } } if (xmle.Name == "aggregate") { string query = ""; try { XmlElement qel = xmle["query"]; if (qel != null) query = qel.InnerXml; } catch (Exception) { } if (query.Length > 0) { this.p_aggregate[query] = new List<XmlElement>(); foreach (XmlElement xmld in xmle.ChildNodes) if (xmld.Name == "data") this.p_aggregate[query].Add(xmld); } } } } foreach (XmlElement xmle in content.ChildNodes) { if (Array.Exists<string>(CPS_Response.p_textParamNames, pn => pn == xmle.Name)) this.p_textParams[xmle.Name] = xmle.InnerXml; else if (xmle.Name == "results") { foreach (XmlElement xmld in xmle.ChildNodes) this.ProcessRawDocument(connection, xmld, saveDocs); } else if (xmle.Name == connection.getDocumentRootXpath()) this.ProcessRawDocument(connection, xmle, saveDocs); } } }