Exemplo n.º 1
0
        /// <summary>
        /// Tokenizes the simple.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="kind">The kind.</param>
        /// <param name="nextMode">The next mode.</param>
        /// <returns></returns>
        private XmlToken TokenizeSimple(string text, XmlTokenKind kind, XmlTokenizerMode nextMode)
        {
            XmlToken token = new XmlToken(kind, text.Length);

            position += text.Length;
            mode      = nextMode;
            return(token);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Tokenizes the entity.
        /// </summary>
        /// <returns></returns>
        private XmlToken TokenizeEntity()
        {
            Debug.Assert(mode == XmlTokenizerMode.OutsideElement);
            Debug.Assert(input[position] == '&');
            XmlToken token = new XmlToken(XmlTokenKind.Entity, input.IndexOf(';', position) - position);

            position += token.Length;
            return(token);
        }
Exemplo n.º 3
0
        private XmlToken TokenizeAttributeValue()
        {
            Debug.Assert(mode == XmlTokenizerMode.AfterAttributeEquals);
            int      closePosition = input.IndexOf(input[position], position + 1);
            XmlToken token         = new XmlToken(XmlTokenKind.AttributeValue, closePosition + 1 - position);

            position = closePosition + 1;
            mode     = XmlTokenizerMode.InsideElement;
            return(token);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Nexts the token.
        /// </summary>
        /// <returns></returns>
        private XmlToken NextToken()
        {
            if (position >= input.Length)
            {
                return(new XmlToken(XmlTokenKind.EOF, 0));
            }

            XmlToken token;

            switch (mode)
            {
            case XmlTokenizerMode.AfterAttributeEquals:
                token = TokenizeAttributeValue();
                break;

            case XmlTokenizerMode.AfterAttributeName:
                token = TokenizeSimple("=", XmlTokenKind.Equals, XmlTokenizerMode.AfterAttributeEquals);
                break;

            case XmlTokenizerMode.AfterOpen:
                token = TokenizeName(XmlTokenKind.ElementName, XmlTokenizerMode.InsideElement);
                break;

            case XmlTokenizerMode.InsideCData:
                token = TokenizeInsideCData();
                break;

            case XmlTokenizerMode.InsideComment:
                token = TokenizeInsideComment();
                break;

            case XmlTokenizerMode.InsideElement:
                token = TokenizeInsideElement();
                break;

            case XmlTokenizerMode.InsideProcessingInstruction:
                token = TokenizeInsideProcessingInstruction();
                break;

            case XmlTokenizerMode.OutsideElement:
                token = TokenizeOutsideElement();
                break;

            default:
                token = new XmlToken(XmlTokenKind.EOF, 0);
                // Debug.Fail("missing case");
                break;
            }
            return(token);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Tokenizes the element whitespace.
        /// </summary>
        /// <returns></returns>
        private XmlToken TokenizeElementWhitespace()
        {
            int i;

            for (i = position; i < input.Length; i++)
            {
                if (!char.IsWhiteSpace(input[i]))
                {
                    break;
                }
            }
            XmlToken token = new XmlToken(XmlTokenKind.ElementWhitespace, i - position);

            position = i;
            return(token);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Tokenizes the inside comment.
        /// </summary>
        /// <returns></returns>
        private XmlToken TokenizeInsideComment()
        {
            Debug.Assert(mode == XmlTokenizerMode.InsideComment);
            int tokenend = input.IndexOf("-->", position);

            if (position == tokenend)
            {
                position += "-->".Length;
                mode      = XmlTokenizerMode.OutsideElement;
                return(new XmlToken(XmlTokenKind.CommentEnd, "-->".Length));
            }
            else
            {
                XmlToken token = new XmlToken(XmlTokenKind.CommentText, tokenend - position);
                position = tokenend;
                return(token);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Tokenizes the inside C data.
        /// </summary>
        /// <returns></returns>
        private XmlToken TokenizeInsideCData()
        {
            Debug.Assert(mode == XmlTokenizerMode.InsideCData);
            int tokenend = input.IndexOf("]]>", position);

            if (position == tokenend)
            {
                position += "]]>".Length;
                mode      = XmlTokenizerMode.OutsideElement;
                return(new XmlToken(XmlTokenKind.CDataEnd, "]]>".Length));
            }
            else
            {
                XmlToken token = new XmlToken(XmlTokenKind.TextContent, tokenend - position);
                position = tokenend;
                return(token);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Tokenizes the inside processing instruction.
        /// </summary>
        /// <returns></returns>
        private XmlToken TokenizeInsideProcessingInstruction()
        {
            Debug.Assert(mode == XmlTokenizerMode.InsideProcessingInstruction);
            int tokenend = input.IndexOf("?>", position);

            if (position == tokenend)
            {
                position += "?>".Length;
                mode      = XmlTokenizerMode.OutsideElement;
                return(new XmlToken(XmlTokenKind.CloseProcessingInstruction, "?>".Length));
            }
            else
            {
                XmlToken token = new XmlToken(XmlTokenKind.TextContent, tokenend - position);
                position = tokenend;
                return(token);
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Tokenizes the name.
        /// </summary>
        /// <param name="kind">The kind.</param>
        /// <param name="nextMode">The next mode.</param>
        /// <returns></returns>
        private XmlToken TokenizeName(XmlTokenKind kind, XmlTokenizerMode nextMode)
        {
            Debug.Assert(mode == XmlTokenizerMode.AfterOpen || mode == XmlTokenizerMode.InsideElement);
            int i;

            for (i = position; i < input.Length; i++)
            {
                if (!IsNameCharacter(input[i]))
                {
                    break;
                }
            }
            XmlToken token = new XmlToken(kind, i - position);

            mode     = nextMode;
            position = i;
            return(token);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Colors for token.
        /// </summary>
        /// <param name="token">The token.</param>
        /// <param name="tokenText">The token text.</param>
        /// <returns></returns>
        public static Color ColorForToken(XmlToken token, string tokenText)
        {
            Color color = Color.FromArgb((byte)255, 0, 0, 0);

            switch (token.Kind)
            {
            case XmlTokenKind.Open:
            case XmlTokenKind.OpenClose:
            case XmlTokenKind.Close:
            case XmlTokenKind.SelfClose:
            case XmlTokenKind.CommentBegin:
            case XmlTokenKind.CommentEnd:
            case XmlTokenKind.CDataBegin:
            case XmlTokenKind.CDataEnd:
            case XmlTokenKind.Equals:
            case XmlTokenKind.OpenProcessingInstruction:
            case XmlTokenKind.CloseProcessingInstruction:
            case XmlTokenKind.AttributeValue:
                color = Color.FromArgb((byte)255, 0, 0, 255);
                break;

            case XmlTokenKind.ElementName:
                color = Color.FromArgb((byte)255, 163, 21, 21);
                break;

            case XmlTokenKind.TextContent:
                color = Color.FromArgb((byte)255, 0, 0, 0);
                break;

            case XmlTokenKind.AttributeName:
            case XmlTokenKind.Entity:
                color = Color.FromArgb((byte)255, 255, 0, 0);
                break;

            case XmlTokenKind.CommentText:
                color = Color.FromArgb((byte)255, 0, 128, 0);
                break;
            }
            if (token.Kind == XmlTokenKind.ElementWhitespace ||
                (token.Kind == XmlTokenKind.TextContent && tokenText.Trim() == ""))
            {
            }
            return(color);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Tokenizes the text.
        /// </summary>
        /// <returns></returns>
        private XmlToken TokenizeText()
        {
            Debug.Assert(input[position] != '<');
            Debug.Assert(input[position] != '&');
            Debug.Assert(mode == XmlTokenizerMode.OutsideElement);
            int i;

            for (i = position; i < input.Length; i++)
            {
                if (input[i] == '<' || input[i] == '&')
                {
                    break;
                }
            }
            XmlToken token = new XmlToken(XmlTokenKind.TextContent, i - position);

            position = i;
            return(token);
        }