public DekiScriptMap AddAt(string[] keys, DekiScriptLiteral value, bool ignoreOnError) { if (ArrayUtil.IsNullOrEmpty(keys)) { throw new ArgumentNullException("keys"); } if (value == null) { throw new ArgumentNullException("value"); } // loop over all keys, except last, to get to the last map string key; DekiScriptMap current = this; for (int i = 0; i < keys.Length - 1; ++i) { if (keys[i] == null) { if (ignoreOnError) { return(this); } throw new ArgumentException(string.Format("keys[{0}] is null", i)); } key = keys[i]; DekiScriptLiteral next = current[key]; if (next.ScriptType == DekiScriptType.NIL) { next = new DekiScriptMap(); current.Add(key, next); } else if (next.ScriptType != DekiScriptType.MAP) { if (ignoreOnError) { return(this); } throw new Exception(string.Format("entry at '{0}' is not a map", string.Join(".", keys, 0, i + 1))); } current = (DekiScriptMap)next; } // add new item using the final key current.Add(keys[keys.Length - 1], value); return(this); }
public static DekiScriptLiteral FromXml(XDoc doc) { // check if response is an HTML document if (doc.HasName("html")) { // TODO (steveb): this handling seems to be to specific to belong here. return(new DekiScriptList().Add(new DekiScriptXml(doc))); } // check if response is a DekiScript XML document if (!doc.HasName("value") || (doc["@type"].AsText == null)) { throw new ArgumentException("doc"); } switch (doc["@type"].AsText) { case "nil": return(DekiScriptNil.Value); case "bool": return(Constant(doc.AsBool ?? false)); case "num": return(Constant(doc.AsDouble ?? 0.0)); case "str": return(Constant(doc.AsText ?? string.Empty)); case "uri": { return(Constant(doc.AsUri)); } case "map": { DekiScriptMap result = new DekiScriptMap(); foreach (XDoc value in doc["value"]) { result.Add(value["@key"].AsText, FromXml(value)); } return(result); } case "list": { DekiScriptList result = new DekiScriptList(); foreach (XDoc value in doc["value"]) { result.Add(FromXml(value)); } return(result); } case "xml": if ((doc.AsXmlNode.ChildNodes.Count == 1) && (doc.AsXmlNode.ChildNodes[0].NodeType == XmlNodeType.Element)) { return(new DekiScriptXml(doc[doc.AsXmlNode.ChildNodes[0]])); } return(DekiScriptNil.Value); default: throw new ArgumentException("doc"); } }