/// <summary> /// Get a Single Location Node /// </summary> /// <param name="node">The parent node of the location</param> /// <param name="location">The location to get</param> /// <param name="index">Output Index of the location in the parent node</param> /// <returns>The location's node</returns> public Node GetSingleLocation(Node node, string location, out int?index, string[] subPaths) { index = null; location = NormalizeLocation(node, subPaths != null ? subPaths[0] : location, out index); if (location == null || location.ToLower().Equals("node")) { return(node); } Node root = (CurrentProgram ?? node.GetProgramNode()); root = root ?? node.Root; var result = root.Get(subPaths != null ? subPaths[0] : location); if (result != null) { if (subPaths != null) {//Check if a subpath matches for (int i = 1; i < subPaths.Length; i++) { int nbParent = subPaths[i].LastIndexOf('/') + 1; Node parent = result; int matchIndex = 0; for (int j = 0; j < nbParent; j++) { matchIndex = parent.Parent.IndexOf(parent); parent = parent.Parent; } int?index2 = null; var subResult = GetSingleSubLocation(parent, subPaths[i].Substring(nbParent), out index2, matchIndex + 1); if (subResult != null) { index = index2; if (subResult.CodeElement == null) { if (subResult.Parent == parent) { index = null; } else { result = subResult; } } else { result = subResult; } break; } } } return(result); } result = Create(root, subPaths != null ? subPaths[0] : location); if (result != null) { return(result); } throw new System.ArgumentException("Undefined URI: " + location); }
/// <summary> /// Get a Sub Location node /// </summary> /// <param name="parent">The parent of the the sub location</param> /// <param name="location">The sub location</param> /// <param name="index">Output Index of the location in the parent node</param> /// <param name="subIndex">Index of the starting sublocation in the parent</param> /// <returns></returns> public Node GetSingleSubLocation(Node parent, string location, out int?index, int subIndex) { index = null; location = NormalizeLocation(parent, location, out index); if (location == null || location.ToLower().Equals("node")) { return(parent); } var result = parent.Get(location, subIndex); return(result); }
/// <summary> /// Determines if the given location exists /// </summary> /// <param name="node">The parent node of the location</param> /// <param name="location">The loctaion to test.</param> /// <returns>true if the location exists, fals eotherwise</returns> public bool IsLocationExists(Node node, string location, out int?index) { index = null; location = NormalizeLocation(node, location, out index); if (location == null || location.ToLower().Equals("node")) { return(true); } Node root = CurrentProgram ?? node.GetProgramNode(); root = root ?? node.Root; var result = root.Get(location); return(result != null); }
/// <summary> /// Create a node at the given location. /// </summary> /// <param name="parent">The parent root node</param> /// <param name="location">The location as access path.</param> /// <returns>The create node</returns> public Node Create(Node parent, string location) { var factory = new Codegen.Nodes.Factory(); var parts = location.Split(new char[] { '.' }); var path = new System.Text.StringBuilder(); Node result = null; foreach (var part in parts) { path.Append(part); var current = parent.Get(path.ToString()); if (current == null) { string nextsibling; List <string> previoussibling; current = factory.Create(part, out nextsibling, out previoussibling); if (current == null) { return(null); } //All these nodes have been generated by a factory. //Mark them so that the generator can associate them to the first //parent having a location in the source file. current.SetFlag(Node.Flag.FactoryGeneratedNode, true, true); //Keep The insertion sequence ?? current.SetFlag(Node.Flag.FactoryGeneratedNodeKeepInsertionIndex, true, false); int index = -1; if (nextsibling != null) { var sibling = result.Get(nextsibling); if (sibling != null) { index = sibling.Parent.IndexOf(sibling); } } if (index >= 0) { result.Add(current, index); } else { index = 0; //Look for a next if (previoussibling != null) { foreach (string p in previoussibling) { var previous = result.Get(p); if (previous != null) { int pindex = previous.Parent.IndexOf(previous); if (pindex >= 0) { index = pindex + 1; break; } } } } result.Add(current, index); } } result = current; path.Append('.'); } return(result); }
private Node Create(Node node, string location) { var factory = new Codegen.Nodes.Factory(); var parts = location.Split(new char[] { '.' }); var path = new System.Text.StringBuilder(); Node result = null; foreach(var part in parts) { path.Append(part); var current = node.Get(path.ToString()); if (current == null) { string nextsibling; current = factory.Create(part, out nextsibling); if (current == null) return null; int index = 0; if (nextsibling != null) { var sibling = result.Get(nextsibling); index = sibling.Parent.IndexOf(sibling); } result.Add(current, index); } result = current; path.Append('.'); } return result; }