示例#1
0
        public override bool Accepts(ListExp exp)
        {
            if (listItems == null)
            {
                // split the literal by whitespace.
                string[] tokens = StringTokenizer.Tokenize(literal);

//			Trace.WriteLine(string.Format("literal:'{0}' token size:{1}",literal,tokens.Length));
                listItems = new StringToken[tokens.Length];
                for (int i = 0; i < tokens.Length; i++)
                {
                    listItems[i] = new StringToken(tokens[i], builder, context);
                }
            }

            Expression body = exp.exp;

            for (int i = 0; i < listItems.Length; i++)
            {
                body = Residual.Calc(body, listItems[i], builder);
//			Trace.WriteLine("list residual: "+ ExpPrinter.printContentModel(body));
                if (body == Expression.NotAllowed)
                {
                    return(false);
                }
            }
            return(body.IsNullable);
        }
示例#2
0
 public override bool Accepts(DataExp exp)
 {
     return(exp.dt.IsValid(literal, context) &&
            (
                exp.except == null ||
                !Residual.Calc(exp.except, this, builder).IsNullable));
 }
        private Expression VerifyText(Expression exp, Expression[] atoms)
        {
            string literal;

            if (characters.Length == 0)
            {
                literal = String.Empty;
            }
            else
            {
                literal    = characters.ToString();
                characters = new StringBuilder();
            }
            bool ignorable = (literal.Trim().Length == 0);


            Trace.WriteLine("characters: " + literal.Trim());

            StringToken token = new StringToken(literal, builder, this);

            Expression r = Residual.Calc(exp, token, builder);

            if (ignorable)
            {
                exp = builder.CreateChoice(exp, r);
            }
            else
            {
                exp = r;
            }

            if (exp == Expression.NotAllowed)
            {
                // error: unexpected literal
                if (literal.Length > 20)
                {
                    literal = literal.Substring(0, 20) + " ...";
                }
                ReportError(ERR_INVALID_TEXT, literal);
            }

            if (atoms != null)
            {
                for (int i = atoms.Length - 1; i >= 0; i--)
                {
                    r = Residual.Calc(atoms[i], token, builder);
                    if (ignorable)
                    {
                        atoms[i] = builder.CreateChoice(atoms[i], r);
                    }
                    else
                    {
                        atoms[i] = r;
                    }
                }
            }

            return(exp);
        }
示例#4
0
        public override bool Accepts(AttributeExp exp)
        {
            if (!exp.Name.Contains(uri, localName))
            {
                return(false);
            }

            if (valueToken == null)
            {
                valueToken = new StringToken(value, builder, context);
            }

            if (ignorable && exp.exp.IsNullable)
            {
                return(true);
            }

            return(Residual.Calc(exp.exp, valueToken, builder).IsNullable);
        }
        private Expression OnElement(Expression contentModel, Expression[] atoms)
        {
            contentModel = VerifyText(contentModel, atoms);

            Trace.WriteLine("<" + document.Name + ">");
            Trace.Indent();
            Trace.WriteLine("expecting: " + ExpPrinter.printContentModel(contentModel));

            ElementExp[] match = ContentModelCollector.Collect(
                contentModel, new XmlName(document.NamespaceURI, document.LocalName));

            if (match[0] == null)
            {
                // error: unexpected element
                ReportError(ERR_UNEXPECTED_ELEMENT, document.Name);
            }

            Expression combinedChild = Expression.NotAllowed;
            int        i;

            for (i = 0; match[i] != null; i++)
            {
                combinedChild = builder.CreateChoice(combinedChild, match[i].exp);
            }
            int clen = i;

            Expression[] cp = null;
            if (clen > 1)
            {
                Trace.WriteLine(string.Format("{0} elements are matched", clen));
                cp = new Expression[clen];
                for (i = 0; i < clen; i++)
                {
                    cp[i] = match[i].exp;
                }
            }

            Trace.WriteLine("combined child: " +
                            ExpPrinter.printContentModel(combinedChild));

            if (document.MoveToFirstAttribute())
            {
                do
                {
                    combinedChild = OnAttribute(combinedChild, cp);
                } while(document.MoveToNextAttribute());
            }

            document.MoveToElement();

            combinedChild = attPruner.prune(combinedChild);
            if (combinedChild == Expression.NotAllowed)
            {
                // error: required attribute is missing
                // TODO: name of required attributes
                ReportError(ERR_MISSING_ATTRIBUTE);
            }

            if (!document.IsEmptyElement)
            {
                document.ReadStartElement();
                combinedChild = Verify(combinedChild, cp);
                combinedChild = VerifyText(combinedChild, cp);
                Trace.Unindent();
                Trace.WriteLine("</" + document.Name + ">");
            }
            else
            {
                // treat it as ""
                combinedChild = VerifyText(combinedChild, cp);
                Trace.Unindent();
                Trace.WriteLine("</" + document.Name + ">");
            }

            if (!combinedChild.IsNullable)
            {
                // error: unexpected end of element.
                ReportError(ERR_CONTENTMODEL_INCOMPLETE, document.Name);
            }

            document.Read();            // read the end tag.


            if (cp != null)
            {
                for (i = 0; i < clen; i++)
                {
                    if (!cp[i].IsNullable)
                    {
                        match[i] = null;
                    }
                }
            }

            ElementToken e = new ElementToken(match, clen);

            if (atoms != null)
            {
                for (i = 0; i < atoms.Length; i++)
                {
                    atoms[i] = Residual.Calc(atoms[i], e, builder);
                }
            }

            contentModel = Residual.Calc(contentModel, e, builder);
            Trace.WriteLine("residual: " + ExpPrinter.printContentModel(contentModel));
            return(contentModel);
        }