Ejemplo n.º 1
0
        private void SetToken(string[] tokenValues)
        {
            if (tokenValues == null || tokenValues.Length == 0)
            {
                throw new ArgumentException();
            }


            mValue = tokenValues[0];
            if (mValue.StartsWith("/"))
            {
                mValue         = mValue.Substring(1);
                mCheckChildren = true;
            }


            if (mValue != string.Empty)
            {
                bool isInQuote          = false;
                bool isInNodeName       = true;
                bool isInAttributeName  = false;
                bool isInAttributeValue = false;
                for (int i = 0; i < mValue.Length; i++)
                {
                    if (mValue[i] == '\"')
                    {
                        isInQuote = !isInQuote; continue;
                    }

                    if (!isInQuote)
                    {
                        switch (mValue[i])
                        {
                        case '[':
                            isInNodeName      = false;
                            isInAttributeName = true;
                            break;

                        case ']':
                            isInAttributeName  = false;
                            isInAttributeValue = false;
                            if (mExtensionType != TokenExtensionType.AttributeID)
                            {
                                if (int.TryParse(mAttributeTag, out mIndex))
                                {
                                    mExtensionType = TokenExtensionType.Index;
                                }
                                mAttributeTag = string.Empty;
                            }
                            break;

                        case '@':
                            mExtensionType = TokenExtensionType.AttributeID;
                            break;

                        case '=':
                            if (isInAttributeName)
                            {
                                isInAttributeName  = false;
                                isInAttributeValue = true;
                            }
                            break;

                        default:
                            if (isInNodeName)
                            {
                                mName += mValue[i];
                            }
                            else if (isInAttributeName)
                            {
                                mAttributeTag += mValue[i];
                            }
                            break;
                        }
                    }
                    else if (isInAttributeValue)
                    {
                        mAttributeValue += mValue[i];
                    }
                }
            }


            if (tokenValues.Length > 1)
            {
                string[] newVals = new string[tokenValues.Length - 1];
                for (int i = 1; i < tokenValues.Length; i++)
                {
                    newVals[i - 1] = tokenValues[i];
                }
                mChild = new XPath(newVals);
            }
        }
Ejemplo n.º 2
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());
        }