/// <summary> /// Locates all the child elements of the given parent <see cref="XmlElement"/> /// and returns them in a (possibly empty) <see cref="XmlNodeList"/>. /// </summary> /// <param name="parent">The parent <see cref="XmlElement"/>.</param> /// <returns>A possibly empty <see cref="XmlNodeList"/> of child elements.</returns> public static XmlNodeList GetChildElements(XmlElement parent) { MutableNodeList list = new MutableNodeList (); if (parent != null) { foreach (XmlNode node in parent.ChildNodes) if (node.NodeType == XmlNodeType.Element) list.Add (node); } return (list); }
/// <summary> /// Evaluates a simple multiple valued path access from the given context /// node to the named grandchild elements. /// </summary> /// <param name="contexts">The context <see cref="XmlElement"/>.</param> /// <param name="name1">The name of the required child.</param> /// <param name="name2">The name of the required grandchild.</param> /// <returns>A possibly empty <see cref="XmlNodeList"/> of matching /// child elements.</returns> public static XmlNodeList Paths(XmlNodeList contexts, string name1, string name2) { MutableNodeList list = new MutableNodeList(); foreach (XmlElement context in contexts) { list.AddAll(Paths(Paths(context, name1), name2)); } return(list); }
/// <summary> /// Creates a (possibly empty) <see cref="XmlNodeList"/> containing all the /// element nodes of a given type (or a derived subtype). /// </summary> /// <param name="ns">The required namespace URI.</param> /// <param name="type">The required type.</param> /// <returns>A <see cref="XmlNodeList"/> of corresponding nodes.</returns> public XmlNodeList GetElementsByType(string ns, string type) { List <XmlSchemaType> matches; if (!compatibleTypes.ContainsKey(type)) { compatibleTypes.Add(type, matches = new List <XmlSchemaType> ()); // System.err.println ("%% Looking for " + ns + ":" + type); foreach (string key in typesByName.Keys) { List <XmlSchemaType> types = typesByName [key]; foreach (XmlSchemaType info in types) { if (type.Equals(info.Name) || IsDerived(new XmlQualifiedName(type, ns), info)) { matches.Add(info); // System.err.println ("%% Found: " + info.getTypeName ()); } } } } else { matches = compatibleTypes [type]; } MutableNodeList result = new MutableNodeList(); foreach (XmlSchemaType info in matches) { // System.err.println ("-- Matching elements of type: " + info.getTypeName ()); if (elementsByType.ContainsKey(info.Name)) { foreach (XmlElement element in elementsByType[info.Name]) { XmlSchemaType typeInfo = element.SchemaInfo.SchemaType; if (typeInfo.QualifiedName.Equals(info.QualifiedName)) { result.Add(element); // System.err.println ("-- Matched: " + element.getLocalName ()); } } } } return(result); }
/// <summary> /// Creates a (possibly empty) <see cref="XmlNodeList"/> containing all the /// element nodes identified by the given name strings. /// </summary> /// <param name="names">The name of the required elements.</param> /// <returns>A <see cref="XmlNodeList"/> of corresponding nodes.</returns> public XmlNodeList GetElementsByName(string [] names) { MutableNodeList list = new MutableNodeList(); foreach (string name in names) { if (elementsByName.ContainsKey(name)) { list.AddAll(elementsByName [name]); } } return(list); }
/// <summary> /// Locates all the child <see cref="XmlElement"/> instances of the indicated /// parent that match the given local name string, ignoring prefixes /// and namespaces. /// </summary> /// <param name="parent">The parent <see cref="XmlElement"/>.</param> /// <param name="localName">The required element local name string.</param> /// <returns>A possibly empty <see cref="XmlNodeList"/> of matching /// <see cref="XmlElement"/> instances.</returns> public static XmlNodeList GetElementsByLocalName(XmlElement parent, string localName) { MutableNodeList list = new MutableNodeList(); foreach (XmlNode node in parent.ChildNodes) { if ((node.NodeType == XmlNodeType.Element) && node.LocalName.Equals(localName)) { list.Add(node); } } return(list); }
/// <summary> /// Locates all the child elements of the given parent <see cref="XmlElement"/> /// and returns them in a (possibly empty) <see cref="XmlNodeList"/>. /// </summary> /// <param name="parent">The parent <see cref="XmlElement"/>.</param> /// <returns>A possibly empty <see cref="XmlNodeList"/> of child elements.</returns> public static XmlNodeList GetChildElements(XmlElement parent) { MutableNodeList list = new MutableNodeList(); if (parent != null) { foreach (XmlNode node in parent.ChildNodes) { if (node.NodeType == XmlNodeType.Element) { list.Add(node); } } } return(list); }
//--------------------------------------------------------------------------- /// <summary> /// Evaluates a simple multiple valued path access from the given context /// node to the named child elements. The '*', '.' and '..' specifiers are /// supported. /// </summary> /// <param name="context">The context <see cref="XmlElement"/>.</param> /// <param name="name">The name of the required child.</param> /// <returns>A possibly empty <see cref="XmlNodeList"/> of matching /// child elements.</returns> public static XmlNodeList Paths(XmlElement context, string name) { if (context != null) { if (name.Equals("*")) { return((context != null) ? DOM.GetChildElements(context) : MutableNodeList.EMPTY); } else if (name.Equals(".")) { if (context != null) { MutableNodeList list = new MutableNodeList(); list.Add(context); return(list); } else { return(MutableNodeList.EMPTY); } } else if (name.Equals("..")) { if ((context != null) && (context.ParentNode != null)) { MutableNodeList list = new MutableNodeList(); list.Add(context.ParentNode); return(list); } else { return(MutableNodeList.EMPTY); } } return(DOM.GetElementsByLocalName(context, name)); } return(MutableNodeList.EMPTY); }
/// <summary> /// Recursively walks a DOM tree creating an index of the elements by /// thier local name. /// </summary> /// <param name="node">The next <see cref="XmlNode"/> to be indexed.</param> private void IndexNodes(XmlNode node) { switch (node.NodeType) { case XmlNodeType.Document: IndexNodes((node as XmlDocument).DocumentElement); break; case XmlNodeType.Element: { string name = (node as XmlElement).LocalName; MutableNodeList list; if (!elementsByName.ContainsKey(name)) { elementsByName.Add(name, list = new MutableNodeList()); } else { list = elementsByName [name]; } list.Add(node); XmlSchemaType typeInfo = (node as XmlElement).SchemaInfo.SchemaType; if ((typeInfo != null) && ((name = typeInfo.Name) != null)) { List <XmlSchemaType> types; int index; if (!typesByName.ContainsKey(name)) { typesByName.Add(name, types = new List <XmlSchemaType> ()); } else { types = typesByName [name]; } for (index = 0; index < types.Count; ++index) { XmlSchemaType info = types [index]; if (typeInfo.QualifiedName.Equals(info.QualifiedName)) { break; } } if (index == types.Count) { types.Add(typeInfo); } if (!elementsByType.ContainsKey(name)) { elementsByType.Add(name, list = new MutableNodeList()); } else { list = elementsByType [name]; } list.Add(node); } XmlAttribute id = (node as XmlElement).GetAttributeNode("id"); if (id != null) { elementsById [id.Value] = node as XmlElement; } foreach (XmlAttribute attr in (node as XmlElement).Attributes) { if (!attributesByName.ContainsKey(attr.Name)) { attributesByName.Add(attr.Name, list = new MutableNodeList()); } else { list = attributesByName [attr.Name]; } list.Add(attr); } foreach (XmlNode child in (node as XmlElement).ChildNodes) { if (child.NodeType == XmlNodeType.Element) { IndexNodes(child); } } break; } } }
/// <summary> /// Validates all the elements registered at construction using the /// <see cref="NodeIndex"/> to locate them as quickly as possible. /// </summary> /// <param name="nodeIndex">The <see cref="NodeIndex"/> of the test document.</param> /// <param name="errorHandler">The <see cref="ValidationErrorHandler"/> used to report issues.</param> /// <returns><c>true</c> if the code values pass the checks, <c>false</c> /// otherwise.</returns> protected override bool Validate(NodeIndex nodeIndex, ValidationErrorHandler errorHandler) { bool result = true; for (int index = 0; index < elementNames.Length; ++index) { XmlNodeList list = nodeIndex.GetElementsByName (elementNames [index]); if (parentNames == null) result &= Validate (list, errorHandler); else { MutableNodeList targets = new MutableNodeList (); foreach (XmlElement context in list) { XmlNode parent = context.ParentNode; if ((parent.NodeType == XmlNodeType.Element) && parent.LocalName.Equals (parentNames [index])) targets.Add (context); } result &= Validate (targets, errorHandler); } } return (result); }
/// <summary> /// Locates all the child <see cref="XmlElement"/> instances of the indicated /// parent that match the given local name string, ignoring prefixes /// and namespaces. /// </summary> /// <param name="parent">The parent <see cref="XmlElement"/>.</param> /// <param name="localName">The required element local name string.</param> /// <returns>A possibly empty <see cref="XmlNodeList"/> of matching /// <see cref="XmlElement"/> instances.</returns> public static XmlNodeList GetElementsByLocalName(XmlElement parent, string localName) { MutableNodeList list = new MutableNodeList (); foreach (XmlNode node in parent.ChildNodes) { if ((node.NodeType == XmlNodeType.Element) && node.LocalName.Equals (localName)) list.Add (node); } return (list); }
/// <summary> /// Recursively walks a DOM tree creating an index of the elements by /// thier local name. /// </summary> /// <param name="node">The next <see cref="XmlNode"/> to be indexed.</param> private void IndexNodes(XmlNode node) { switch (node.NodeType) { case XmlNodeType.Document: IndexNodes ((node as XmlDocument).DocumentElement); break; case XmlNodeType.Element: { string name = (node as XmlElement).LocalName; MutableNodeList list; if (!elementsByName.ContainsKey (name)) elementsByName.Add (name, list = new MutableNodeList ()); else list = elementsByName [name]; list.Add (node); XmlSchemaType typeInfo = (node as XmlElement).SchemaInfo.SchemaType; if ((typeInfo != null) && ((name = typeInfo.Name) != null)) { List<XmlSchemaType> types; int index; if (!typesByName.ContainsKey (name)) typesByName.Add (name, types = new List<XmlSchemaType> ()); else types = typesByName [name]; for (index = 0; index < types.Count; ++index) { XmlSchemaType info = types [index]; if (typeInfo.QualifiedName.Equals (info.QualifiedName)) break; } if (index == types.Count) types.Add (typeInfo); if (!elementsByType.ContainsKey (name)) elementsByType.Add (name, list = new MutableNodeList ()); else list = elementsByType [name]; list.Add (node); } XmlAttribute id = (node as XmlElement).GetAttributeNode ("id"); if (id != null) elementsById [id.Value] = node as XmlElement; foreach (XmlAttribute attr in (node as XmlElement).Attributes) { if (!attributesByName.ContainsKey (attr.Name)) attributesByName.Add (attr.Name, list = new MutableNodeList ()); else list = attributesByName [attr.Name]; list.Add (attr); } foreach (XmlNode child in (node as XmlElement).ChildNodes) if (child.NodeType == XmlNodeType.Element) IndexNodes (child); break; } } }
/// <summary> /// Creates a (possibly empty) <see cref="XmlNodeList"/> containing all the /// element nodes of a given type (or a derived subtype). /// </summary> /// <param name="ns">The required namespace URI.</param> /// <param name="type">The required type.</param> /// <returns>A <see cref="XmlNodeList"/> of corresponding nodes.</returns> public XmlNodeList GetElementsByType(string ns, string type) { List<XmlSchemaType> matches; if (!compatibleTypes.ContainsKey (type)) { compatibleTypes.Add (type, matches = new List<XmlSchemaType> ()); // System.err.println ("%% Looking for " + ns + ":" + type); foreach (string key in typesByName.Keys) { List<XmlSchemaType> types = typesByName [key]; foreach (XmlSchemaType info in types) { if (type.Equals (info.Name) || IsDerived (new XmlQualifiedName (type, ns), info)) { matches.Add (info); // System.err.println ("%% Found: " + info.getTypeName ()); } } } } else matches = compatibleTypes [type]; MutableNodeList result = new MutableNodeList (); foreach (XmlSchemaType info in matches) { // System.err.println ("-- Matching elements of type: " + info.getTypeName ()); if (elementsByType.ContainsKey (info.Name)) { foreach (XmlElement element in elementsByType [info.Name]) { XmlSchemaType typeInfo = element.SchemaInfo.SchemaType; if (typeInfo.QualifiedName.Equals (info.QualifiedName)) { result.Add (element); // System.err.println ("-- Matched: " + element.getLocalName ()); } } } } return (result); }
/// <summary> /// Creates a (possibly empty) <see cref="XmlNodeList"/> containing all the /// element nodes identified by the given name strings. /// </summary> /// <param name="names">The name of the required elements.</param> /// <returns>A <see cref="XmlNodeList"/> of corresponding nodes.</returns> public XmlNodeList GetElementsByName(string [] names) { MutableNodeList list = new MutableNodeList (); foreach (string name in names) { if (elementsByName.ContainsKey (name)) list.AddAll (elementsByName [name]); } return (list); }
/// <summary> /// Evaluates a simple multiple valued path access from the given context /// node to the named grandchild elements. /// </summary> /// <param name="contexts">The context <see cref="XmlElement"/>.</param> /// <param name="name1">The name of the required child.</param> /// <param name="name2">The name of the required grandchild.</param> /// <returns>A possibly empty <see cref="XmlNodeList"/> of matching /// child elements.</returns> public static XmlNodeList Paths(XmlNodeList contexts, string name1, string name2) { MutableNodeList list = new MutableNodeList (); foreach (XmlElement context in contexts) list.AddAll (Paths (Paths (context, name1), name2)); return (list); }
//--------------------------------------------------------------------------- /// <summary> /// Evaluates a simple multiple valued path access from the given context /// node to the named child elements. The '*', '.' and '..' specifiers are /// supported. /// </summary> /// <param name="context">The context <see cref="XmlElement"/>.</param> /// <param name="name">The name of the required child.</param> /// <returns>A possibly empty <see cref="XmlNodeList"/> of matching /// child elements.</returns> public static XmlNodeList Paths(XmlElement context, string name) { if (context != null) { if (name.Equals ("*")) return ((context != null) ? DOM.GetChildElements (context) : MutableNodeList.EMPTY); else if (name.Equals (".")) { if (context != null) { MutableNodeList list = new MutableNodeList (); list.Add (context); return (list); } else return (MutableNodeList.EMPTY); } else if (name.Equals ("..")) { if ((context != null) && (context.ParentNode != null)) { MutableNodeList list = new MutableNodeList (); list.Add (context.ParentNode); return (list); } else return (MutableNodeList.EMPTY); } return (DOM.GetElementsByLocalName (context, name)); } return (MutableNodeList.EMPTY); }