Ejemplo n.º 1
0
        private bool ReadValue(XmlItem item)
        {
            FastString builder;

            if (Config.IsStringOptimization)
            {
                builder = new FastStringWithPool(stringPool);
            }
            else
            {
                builder = new FastString();
            }
            ReadState state          = ReadState.FindLeft;
            string    lastName       = "</" + this.lastName + ">";
            int       lastNameLength = lastName.Length;

            do
            {
                int c = reader.Read();
                if (c == -1)
                {
                    RaiseException();
                }

                builder.Append((char)c);
                if (state == ReadState.FindLeft)
                {
                    if (c == '<')
                    {
                        symbolInBuffer = '<';
                        return(false);
                    }
                    else if (c != ' ' && c != '\r' && c != '\n' && c != '\t')
                    {
                        state = ReadState.FindCloseItem;
                    }
                }
                else if (state == ReadState.FindCloseItem)
                {
                    if (builder.Length >= lastNameLength)
                    {
                        bool match = true;
                        for (int j = 0; j < lastNameLength; j++)
                        {
                            if (builder[builder.Length - lastNameLength + j] != lastName[j])
                            {
                                match = false;
                                break;
                            }
                        }

                        if (match)
                        {
                            builder.Length -= lastNameLength;
                            item.Value      = Converter.FromXml(builder.ToString());
                            return(true);
                        }
                    }
                }
            }while (true);
        }
Ejemplo n.º 2
0
        private ItemState ReadItem(XmlItem item)
        {
            FastString builder;

            if (Config.IsStringOptimization)
            {
                builder = new FastStringWithPool(stringPool);
            }
            else
            {
                builder = new FastString();
            }
            ReadState state   = ReadState.FindLeft;
            int       comment = 0;
            int       i       = 0;
            //string tempAttrName = null;
            //int lc = -1;
            int c = -1;

            // find <
            c = readNextSymbol();
            while (c != -1)
            {
                if (c == '<')
                {
                    break;
                }
                c = readNextSymbol();
            }

            //while not end
            while (state != ReadState.Done && c != -1)
            {
                // find name or comment;
                c = readNextSymbol();
                i = 0;
                while (c != -1)
                {
                    if (i <= comment)
                    {
                        switch (comment)
                        {
                        case 0: if (c == '!')
                            {
                                comment++;
                            }
                            break;

                        case 1: if (c == '-')
                            {
                                comment++;
                            }
                            break;

                        case 2: if (c == '-')
                            {
                                state = ReadState.FindComment;
                            }
                            break;

                        default: comment = -1; break;
                        }
                        if (state == ReadState.FindComment)
                        {
                            break;
                        }
                    }
                    i++;
                    switch (c)
                    {
                    case '>': state = ReadState.Done; break;      //Found name

                    case ' ': state = ReadState.FindRight; break; //Found name

                    case '<': RaiseException(); break;

                    default: builder.Append((char)c); break;
                    }
                    if (state != ReadState.FindLeft)
                    {
                        break;
                    }
                    c = readNextSymbol();
                }
                switch (state)
                {
                case ReadState.FindComment:
                    comment = 0;
                    while (c != -1)
                    {
                        c = readNextSymbol();
                        if (comment > 1)
                        {
                            if (c == '>')
                            {
                                state = ReadState.FindLeft;
                                break;
                            }
                        }
                        else
                        {
                            if (c == '-')
                            {
                                comment++;
                            }
                            else
                            {
                                comment = 0;
                            }
                        }
                    }
                    comment        = 0;
                    builder.Length = 0;
                    while (c != -1)
                    {
                        if (c == '<')
                        {
                            break;
                        }
                        c = readNextSymbol();
                    }
                    break;

                case ReadState.Done:
                    string result = builder.ToString();
                    if (result[0] == '/')
                    {
                        item.Name = result.Substring(1);
                        return(ItemState.End);
                    }
                    if (result[result.Length - 1] == '/')
                    {
                        item.Name = result.Substring(0, result.Length - 1);
                        return(ItemState.Complete);
                    }
                    item.Name = result;
                    return(ItemState.Begin);

                case ReadState.FindRight:
                    if (builder[0] == '/')
                    {
                        builder.Remove(0, 1);
                        item.Name = builder.ToString();
                        return(ItemState.End);
                    }
                    item.Name      = builder.ToString();
                    builder.Length = 0;
                    while (c != -1 && c != '>')
                    {
                        c = readNextSymbol();
                        while (c != -1)
                        {
                            if (c == ' ')
                            {
                                builder.Length = 0;
                                c = readNextSymbol();
                                continue;
                            }
                            if (c == '=' || c == '>')
                            {
                                break;
                            }
                            builder.Append((char)c);
                            c = readNextSymbol();
                        }
                        if (c == '>')
                        {
                            if (builder.Length > 0 && builder[builder.Length - 1] == '/')
                            {
                                return(ItemState.Complete);
                            }
                            return(ItemState.Begin);
                        }
                        c = readNextSymbol();
                        if (c != '"')
                        {
                            continue;
                        }
                        string attrName = builder.ToString();
                        builder.Length = 0;
                        while (c != -1)
                        {
                            c = readNextSymbol();
                            if (c == '"')
                            {
                                break;
                            }
                            builder.Append((char)c);
                        }
                        item.SetProp(attrName, Converter.FromXml(builder.ToString()));
                        builder.Length = 0;
                    }
                    break;
                }
            }


            //just for errors
            return(ItemState.Begin);
        }