private void setLeafValue(String path, ENAMLStem subtree, String val) { int dotpos = path.IndexOf('.'); if (dotpos != -1) //path is name.subpath { String name = path.Substring(0, dotpos); String subpath = path.Substring(dotpos + 1); if (!subtree.children.ContainsKey(name)) { subtree.children[name] = new ENAMLStem(subtree.fullpath + '.' + name); } setLeafValue(subpath, (ENAMLStem)subtree.children[name], val); } else { if (!subtree.children.ContainsKey(path)) { subtree.children[path] = new ENAMLLeaf(subtree.fullpath + '.' + path, val); } else { ENAMLNode pathend = subtree.children[path]; //last node in a path has to be a leaf node if (pathend is ENAMLLeaf) { ((ENAMLLeaf)pathend).value = val; //to (re)store val in } else { throw new ENAMLPathException("path [" + pathend.fullpath + "is not an endpoint"); } } } }
//gets the string from a leaf node at end of a path private String findLeafValue(String path) { String result = null; String leafname = ""; ENAMLStem subpath = null; int leafpos = path.LastIndexOf('.'); if (leafpos != -1) { leafname = path.Substring(leafpos + 1); //split the leaf node name from the end of the path path = path.Substring(0, leafpos); subpath = getSubPath(path); //get stem node that is parent to this leaf node } else { leafname = path; subpath = root; } if ((subpath != null) && (subpath.children.ContainsKey(leafname))) { ENAMLNode leaf = subpath.children[leafname]; //then get the leaf node child if (leaf != null && leaf is ENAMLLeaf) //these should both be true, check anyway { result = ((ENAMLLeaf)leaf).value; //and return it's value } } return(result); }
private void storeNode(List <string> lines, ENAMLNode node, String indent, String name) { String line = indent + name + ":"; if (node is ENAMLLeaf) { lines.Add(line + " " + ((ENAMLLeaf)node).value); } else { lines.Add(line); storeSubTree(lines, (ENAMLStem)node, indent + indentStr); } }
//gets stem node at end of a subpath (a path with the ending leaf node removed) private ENAMLStem getSubPath(String path) { string[] parts = path.Split(sep); //split xxx.yyy.zzz into (xxx, yyy, zzz) ENAMLNode subtree = root; for (int i = 0; i < parts.Length; i++) { string name = parts[i]; if ((subtree is ENAMLStem) && (((ENAMLStem)subtree).children.ContainsKey(name))) { subtree = ((ENAMLStem)subtree).children[name]; } else { return(null); } } if (subtree is ENAMLStem) //last node in subpath has to be a stem node { return((ENAMLStem)subtree); } return(null); }