internal IEnumOfXElement(System.ComponentModel.BindingList <object> refList, XParseName name) { this.ReferenceList = refList; this.Name = name; this.UpdateRefList(); this.ReferenceList.ListChanged += this.ReferenceList_Changed; }
public XParseAttribute Attribute(XParseName name) { foreach (object obj in mElements) { if (obj is XParseAttribute && XmlParser.CompareXName(((XParseAttribute)obj).Name, name)) { return((XParseAttribute)obj); } } return(null); }
public XParseAttribute[] Attributes(XParseName name) { List <XParseAttribute> lst = new List <XParseAttribute>(); foreach (object obj in mElements) { if (obj is XParseAttribute && XmlParser.CompareXName(((XParseAttribute)obj).Name, name)) { lst.Add((XParseAttribute)obj); } } return(lst.ToArray()); }
private XParseAttribute ParseAttribute(StringCounter sc) { sc.TrimStart(); XParseAttribute att = null; XParseName name = null; if (IsValidTagChar(sc.StartValue)) { name = ParseName(sc); } if (name != null && sc.StartValue == '=') { sc.Index++; sc.TrimStart(); string val = string.Empty; Nullable <char> quotesSign = (sc.StartValue == '\"' || sc.StartValue == '\'' ? (Nullable <char>)sc.StartValue : null); for (int i = (sc.Index + (quotesSign.HasValue ? 1 : 0)); i < sc.Value.Length; i++) { if (quotesSign.HasValue) { if (sc.Value[i] == quotesSign.Value) { sc.Index = i + 1; break; } else { val += sc.Value[i]; } } else { if (sc.Value[i] == ' ' || sc.Value[i] == '>') { sc.Index = i; break; } else { val += sc.Value[i]; } } } att = new XParseAttribute(name, XmlParser.DecodeXml(val)); } sc.TrimStart(); return(att); }
private XParseName ParseName(StringCounter sc) { sc.TrimStart(); XParseName name = null; string n = string.Empty; string ns = string.Empty; for (int i = sc.Index; i < sc.Value.Length; i++) { if (IsValidTagChar(sc.Value[i])) { n += sc.Value[i]; } else if (n == string.Empty && sc.Value[i] == ' ') { } else if (i > 0) { if (sc.Value[i] == ':' && n != string.Empty && ns == string.Empty) { ns = n; n = string.Empty; } else if (sc.Value[i] == ' ' || sc.Value[i] == '=' || sc.Value[i] == '>' || sc.Value[i] == '/') { sc.Index = i; break; } else { n = string.Empty; sc.Index = i; break; } } else { throw new Exception("Invalid Name."); } } if (n != string.Empty) { name = XParseName.Get(n.Replace("h1", "hx").Replace("h2", "hx"), ns); } sc.TrimStart(); return(name); }
private XObject FindParent(XObject child, XParseName parentName) { if (child.Parent != null) { if (CompareXName(child.Parent.Name, parentName)) { return(child.Parent); } else { return(FindParent(child.Parent, parentName)); } } else { return(null); } }
public XParseDocument ParseDocument(string text) { //System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew(); XParseDocument doc = new XParseDocument(); StringCounter sc; try { sc = new StringCounter(text); sc.TrimStart(); if (sc.StartsWith("<")) { while (!sc.IsAtEnd) { XContainer childNode = this.ParseNode(sc, doc); } XParseElement[] enm = MyHelper.EnumToArray(doc.Elements()); if (enm.Length > 1) { XParseElement root = new XParseElement(XParseName.Get("root")); foreach (XParseElement elem in enm) { root.Add(elem); } doc.Elements().Remove(); doc.Add(root); } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } //sw.Stop(); //System.Diagnostics.Debug.WriteLine(sw.Elapsed.TotalMilliseconds.ToString("N0")); return(doc); }
internal static bool CompareXName(XParseName x1, XParseName x2) { return(x1.ToString() == x2.ToString()); }
private XContainer ParseNode(StringCounter sc, XContainer parent) { sc.TrimStart(); if (sc.StartValue == '<') { sc.Index = sc.Index + 1; XContainer node = null; bool isComment = false; bool isDeclaration = false; int breakOff = 0; XParseName name = null; if (sc.StartsWith("?xml")) { //Declaration isDeclaration = true; sc.Index += 4; } else if (sc.StartValue == '!') { //Comment isComment = true; if (sc.Value[sc.Index + 1] == '-') { breakOff = 1; } if (sc.Value[sc.Index + 2] == '-') { breakOff = 2; } sc.Index += breakOff + 1; sc.TrimStart(); } else if (IsValidTagChar(sc.StartValue)) { //Name name = ParseName(sc); if (name != null) { node = new XParseElement(name); } else { throw new Exception("Invalid Node Name."); } } else { throw new Exception("Invalid Node Name."); } if (node != null || isComment || isDeclaration) { //Attributes bool elementAtEnd = name != null ? (name.LocalName == "br") : false; string comment = string.Empty; string declVer = string.Empty; string declEnc = string.Empty; string declSta = string.Empty; for (int i = sc.Index; i < sc.Value.Length; i++) { if (!isComment && !isDeclaration) { //Node Attributes if (sc.Value[i] != ' ') { if (sc.Value[i] == '>') { sc.Index = i + 1; break; } else if (sc.Value[i] == '/' && sc.Value[i + 1] == '>') { elementAtEnd = true; sc.Index += 2; break; } else if (IsValidTagChar(sc.Value[i])) { XParseAttribute att = ParseAttribute(sc.NewIndex(i)); i = sc.Index - 1; if (att != null) { node.Add(att); } } } } else { if (isComment) { //Comment if ((breakOff == 2 && sc.Value[i] == '-' && sc.Value[i + 1] == '-' && sc.Value[i + 2] == '>') || (breakOff == 0 && sc.Value[i] == '>') || (breakOff == 1 && sc.Value[i] == '-' && sc.Value[i + 1] == '>')) { if (parent != null) { parent.Add(new XComment(comment)); } sc.Index = i + breakOff + 1; break; } else { comment += sc.Value[i]; } } else if (isDeclaration) { //Declaration if (sc.Value[i] == '?' && sc.Value[i + 1] == '>') { if (parent != null && parent is XParseDocument) { ((XParseDocument)parent).Declaration = new XDeclaration(declVer, declEnc, declSta); } sc.Index = i + 2; break; } else if (IsValidTagChar(sc.Value[i])) { XParseAttribute att = ParseAttribute(sc.NewIndex(i)); i = sc.Index - 1; if (att != null) { if (att.Name.LocalName.ToLower() == "version") { declVer = att.Value; } else if (att.Name.LocalName.ToLower() == "encoding") { declEnc = att.Value; } else if (att.Name.LocalName.ToLower() == "standalone") { declSta = att.Value; } } } } } } if (node != null) { parent.Add(node); } //Content if (node != null && !elementAtEnd) { string innerText = string.Empty; string fullName = name.ToString(); for (int i = sc.Index; i < sc.Value.Length; i++) { if (sc.Value[i] == '<') { if (innerText != string.Empty) { node.Add(XmlParser.DecodeXml(innerText)); innerText = string.Empty; } if (sc.Value[i + 1] == '/') { //End Tag XParseName endName = ParseName(sc.NewIndex(i + 2)); if (endName != null) { if (CompareXName(endName, name)) { //Actual End Name sc.Index = i + 3 + fullName.Length; break; } else { if (name.LocalName != "script") { //Other End Name XContainer par = ((parent is XParseElement && XmlParser.CompareXName(((XParseElement)parent).Name, endName)) ? parent : (XContainer)this.FindParent(parent, endName)); if (par != null) { if (name.LocalName != "form") { XParseElement[] enm = MyHelper.EnumToArray(node.Elements()); if (enm.Length > 0) { foreach (XParseElement elem in enm) { parent.Add(elem); } } node.Elements().Remove(); sc.Index = i; break; } else { sc.Index = i + endName.ToString().Length + 3; i = sc.Index - 1; } } else { sc.Index = i + endName.ToString().Length + 2; i = sc.Index - 1; } } } } else if (fullName == "script") { //Script Text //innerText += text[i]; } else { throw new Exception("Invalid End Name."); } } else if (fullName == "script") { //Script Text //innerText += text[i]; } else { //Start Tag XContainer childNode = this.ParseNode(sc.NewIndex(i), node); i = sc.Index - 1; } } else if (!(sc.Value[i] == ' ' && innerText == string.Empty)) { //Inner Text if (fullName != "script") { innerText += sc.Value[i]; } } } } } sc.TrimStart(); return(node); } else { throw new Exception("Invalid Start Tag. A node has to start with [<]."); } }
public XParseElement(XParseName name) { mName = name; }
public IEnumOfXElement Elements(XParseName name) { return(new IEnumOfXElement(mElements, name)); }
public XParseAttribute(XParseName name, string value) { mName = name; this.Value = value; }
private XParseElement[] GetElements(XContainer container, bool returnFirstResult, bool isRootSeek = false) { List <XParseElement> matchNodes = new List <XParseElement>(); if (this.IsRootPath) { if (container is XParseElement && ((XParseElement)container).Name.LocalName == mChild.mName) { if (mChild.mExtensionType == TokenExtensionType.None) { matchNodes.Add((XParseElement)container); } else if (mChild.mExtensionType == TokenExtensionType.AttributeID) { XParseAttribute att = ((XParseElement)container).Attribute(XParseName.Get(mChild.mAttributeTag)); if (att != null && this.StringValuesEquals(mChild.mAttributeValue, att.Value)) { matchNodes.Add((XParseElement)container); } } } if (!(returnFirstResult && matchNodes.Count > 0)) { foreach (XParseElement elem in container.Elements()) { matchNodes.AddRange(this.GetElements(elem, returnFirstResult, true)); if (returnFirstResult && matchNodes.Count > 0) { break; } } } } else { int cnt = 0; foreach (XParseElement elem in container.Elements()) { if (elem.Name.LocalName == mName) { cnt++; switch (mExtensionType) { case TokenExtensionType.None: matchNodes.Add(elem); break; case TokenExtensionType.Index: if (cnt == mIndex) { matchNodes.Add(elem); } break; case TokenExtensionType.AttributeID: XParseAttribute att = elem.Attribute(XParseName.Get(mAttributeTag)); if (att != null && this.StringValuesEquals(mAttributeValue, att.Value)) { matchNodes.Add(elem); } break; } if (returnFirstResult && matchNodes.Count > 0) { break; } } } } List <XParseElement> results = new List <XParseElement>(); XPath child = mChild; if (this.IsRootPath) { child = child.Child; } if (!isRootSeek && child != null) { foreach (XParseElement match in matchNodes) { results.AddRange(child.GetElements(match, returnFirstResult)); if (returnFirstResult && results.Count > 0) { break; } } } else { results.AddRange(matchNodes); } return(results.ToArray()); }