public static XmlnsSchemaPointerPart ParseSchemaData(XPointerLexer lexer)
        {
            //[1]       XmlnsSchemeData	   ::=       NCName S? '=' S? EscapedNamespaceName
            //[2]       EscapedNamespaceName	   ::=      EscapedData*
            //Read prefix as NCName
            lexer.NextLexeme();
            if (lexer.Kind != XPointerLexer.LexKind.NcName)
            {
                Debug.WriteLine(Resources.InvalidTokenInXmlnsSchemeWhileNCNameExpected);
                return(null);
            }
            var prefix = lexer.NcName;

            lexer.SkipWhiteSpace();
            lexer.NextLexeme();
            if (lexer.Kind != XPointerLexer.LexKind.Eq)
            {
                Debug.WriteLine(Resources.InvalidTokenInXmlnsSchemeWhileEqualsSignExpected);
                return(null);
            }
            lexer.SkipWhiteSpace();
            string nsUri;

            try
            {
                nsUri = lexer.ParseEscapedData();
            }
            catch (Exception e)
            {
                throw new XPointerSyntaxException(String.Format(CultureInfo.CurrentCulture, Resources.SyntaxErrorInXmlnsSchemeData, e.Message));
            }
            return(new XmlnsSchemaPointerPart(prefix, nsUri));
        }
        public static ElementSchemaPointerPart ParseSchemaData(XPointerLexer lexer)
        {
            //Productions:
            //[1]       ElementSchemeData	   ::=      (NCName ChildSequence?) | ChildSequence
            //[2]       ChildSequence	   ::=      ('/' [1-9] [0-9]*)+
            var xpathBuilder = new StringBuilder();
            var part         = new ElementSchemaPointerPart();

            lexer.NextLexeme();
            if (lexer.Kind == XPointerLexer.LexKind.NcName)
            {
                xpathBuilder.Append("id('");
                xpathBuilder.Append(lexer.NcName);
                xpathBuilder.Append("')");
                lexer.NextLexeme();
            }
            var childSequenceLen = 0;

            while (lexer.Kind == XPointerLexer.LexKind.Slash)
            {
                lexer.NextLexeme();
                if (lexer.Kind != XPointerLexer.LexKind.Number)
                {
                    Debug.WriteLine(Resources.InvalidTokenInElementSchemeWhileNumberExpected);
                    return(null);
                }
                if (lexer.Number == 0)
                {
                    Debug.WriteLine(Resources.ZeroIndexInElementSchemechildSequence);
                    return(null);
                }
                childSequenceLen++;
                xpathBuilder.Append("/*[");
                xpathBuilder.Append(lexer.Number);
                xpathBuilder.Append("]");
                lexer.NextLexeme();
            }
            if (lexer.Kind != XPointerLexer.LexKind.RrBracket)
            {
                throw new XPointerSyntaxException(Resources.InvalidTokenInElementSchemeWhileClosingRoundBracketExpected);
            }
            if (xpathBuilder.Length == 0 && childSequenceLen == 0)
            {
                Debug.WriteLine(Resources.EmptyElementSchemeXPointer);
                return(null);
            }
            part.XPath = xpathBuilder.ToString();
            return(part);
        }
Exemple #3
0
        public static Pointer ParseXPointer(string xpointer)
        {
            var lexer = new XPointerLexer(xpointer);

            lexer.NextLexeme();
            if (lexer.Kind == XPointerLexer.LexKind.NcName && !lexer.CanBeSchemaName)
            {
                //Shorthand pointer
                Pointer ptr = new ShorthandPointer(lexer.NcName);
                lexer.NextLexeme();
                if (lexer.Kind != XPointerLexer.LexKind.Eof)
                {
                    throw new XPointerSyntaxException(Resources.InvalidTokenAfterShorthandPointer);
                }
                return(ptr);
            }
            //SchemaBased pointer
            IList <PointerPart> parts = new List <PointerPart>();

            while (lexer.Kind != XPointerLexer.LexKind.Eof)
            {
                if ((lexer.Kind == XPointerLexer.LexKind.NcName ||
                     lexer.Kind == XPointerLexer.LexKind.QName) &&
                    lexer.CanBeSchemaName)
                {
                    var schemaType = GetSchema(lexer, parts);
                    //Move to '('
                    lexer.NextLexeme();
                    switch (schemaType)
                    {
                    case XPointerSchema.SchemaType.Element:
                        var elemPart = ElementSchemaPointerPart.ParseSchemaData(lexer);
                        if (elemPart != null)
                        {
                            parts.Add(elemPart);
                        }
                        break;

                    case XPointerSchema.SchemaType.Xmlns:
                        var xmlnsPart = XmlnsSchemaPointerPart.ParseSchemaData(lexer);
                        if (xmlnsPart != null)
                        {
                            parts.Add(xmlnsPart);
                        }
                        break;

                    case XPointerSchema.SchemaType.XPath1:
                        var xpath1Part = XPath1SchemaPointerPart.ParseSchemaData(lexer);
                        if (xpath1Part != null)
                        {
                            parts.Add(xpath1Part);
                        }
                        break;

                    case XPointerSchema.SchemaType.XPointer:
                        var xpointerPart = XPointerSchemaPointerPart.ParseSchemaData(lexer);
                        if (xpointerPart != null)
                        {
                            parts.Add(xpointerPart);
                        }
                        break;

                    default:
                        //Unknown scheme
                        lexer.ParseEscapedData();
                        break;
                    }
                    //Skip ')'
                    lexer.NextLexeme();
                    //Skip possible whitespace
                    if (lexer.Kind == XPointerLexer.LexKind.Space)
                    {
                        lexer.NextLexeme();
                    }
                }
                else
                {
                    throw new XPointerSyntaxException(Resources.InvalidToken);
                }
            }
            return(new SchemaBasedPointer(parts, xpointer));
        }