protected static NamespaceCacheElement AppendNamespaceElement(NamespaceCacheElement ncParent, string name) { if (ncParent == null) { throw new ArgumentException("Invalid argument: parent"); } if (String.IsNullOrWhiteSpace(name) || name.Contains(".")) { throw new ArgumentException("Invalid argument: name"); } NamespaceCacheElement ncChild = findDescendent(ncParent, name, false); if (ncChild == null) { ncChild = new NamespaceCacheElement(ncParent, name); if (ncParent._ncChildren == null) { ncParent._ncChildren = new Generic.Dictionary <String, NamespaceCacheElement>(StringComparer.OrdinalIgnoreCase); } ncParent._ncChildren[name] = ncChild; } return(ncChild); }
protected static NamespaceCacheElement findDescendent(NamespaceCacheElement ncElem, String ns, bool throwOnNamespaceNotFound) { if (String.IsNullOrWhiteSpace(ns)) { throw new ArgumentException("Invalid argument: namespace"); } String[] nsParts = ns.Split('.'); for (int i = 0; i < nsParts.Length; i++) { String nsPart = nsParts[i]; if (ncElem._ncChildren == null || !ncElem._ncChildren.ContainsKey(nsPart)) { ncElem = null; break; } ncElem = ncElem._ncChildren[nsPart]; } if (throwOnNamespaceNotFound && ncElem == null) { throw new System.Collections.Generic.KeyNotFoundException(); } return(ncElem); }
private static void populate(NamespaceCacheRoot ncRoot, String ns) { String[] nsParts = ns.Split('.'); NamespaceCacheElement ncElem = ncRoot; foreach (String nsPart in nsParts) { ncElem = AppendNamespaceElement(ncElem, nsPart); } }
protected static String assembleAbsoluleName(NamespaceCacheElement ncElem) { if (ncElem is NamespaceCacheRoot) { return("[Global]"); } NamespaceCacheElement[] ncAncestors = enumerateAncestors(ncElem, true); if (ncAncestors.First() is NamespaceCacheRoot) { return(String.Join(".", ncAncestors.Skip(1).Select(x => x.Name).ToArray())); } throw new Exception("Orphan namespace, cannot resolve full path"); }
protected static NamespaceCacheRoot traverseToRoot(NamespaceCacheElement ncElem) { if (ncElem is NamespaceCacheRoot) { return((NamespaceCacheRoot)ncElem); } NamespaceCacheElement ncOldestAncestor = enumerateAncestors(ncElem).DefaultIfEmpty(null).First(); if (ncOldestAncestor == null || !(ncOldestAncestor is NamespaceCacheRoot)) { return(null); } return((NamespaceCacheRoot)ncOldestAncestor); }
protected static NamespaceCacheElement[] enumerateAncestors(NamespaceCacheElement ncElem, bool includeSelf) { Generic.List <NamespaceCacheElement> ncAncestors = new Generic.List <NamespaceCacheElement>(); if (includeSelf) { ncAncestors.Insert(0, ncElem); } while (ncElem.Parent != null) { ncElem = ncElem.Parent; ncAncestors.Insert(0, ncElem); } return(ncAncestors.ToArray()); }
protected static NamespaceCacheElement[] enumerateAncestors(NamespaceCacheElement ncElem) { return(enumerateAncestors(ncElem, false)); }
// Methods public bool ContainsNamespace(NamespaceCacheElement ncElem) { return(_ncChildren != null && _ncChildren.ContainsKey(ncElem.Name)); }
// Gah, why doesn't C# support indexed properties yet >:( /*public NamespaceCacheElement Children[String ns] { * get { return _children[ns]; } * }*/ // Constructor (public construction not allowed) protected NamespaceCacheElement(NamespaceCacheElement parent, String name) { _parent = parent; _name = name; }
protected static NamespaceCacheElement findDescendent(NamespaceCacheElement ncElem, String ns) { return(findDescendent(ncElem, ns, false)); }