Example #1
0
        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);
        }
Example #2
0
        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);
        }
Example #3
0
        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());
        }