Exemple #1
0
 internal ValidationState(RdpPattern startState)
 {
     this.state = startState;
 }
Exemple #2
0
        public override bool Read()
        {
            PrepareState();

            // If the input XmlReader is already positioned on
            // the first node to validate, skip Read() here
            // (idea by Alex).
            bool ret;

            if (firstRead)
            {
                firstRead = false;
                if (reader.ReadState == ReadState.Initial)
                {
                    ret = reader.Read();
                }
                else
                {
                    ret = !((reader.ReadState == ReadState.Closed) || (reader.ReadState == ReadState.EndOfFile));
                }
            }
            else
            {
                ret = reader.Read();
            }

            // Process pending text node validation if required.
            if (cachedValue != null)
            {
                ValidateText(ret);
            }
            else if (cachedValue == null &&
                     reader.NodeType == XmlNodeType.EndElement &&
                     startElementDepth == reader.Depth)
            {
                ValidateWeakMatch3();
            }

            switch (reader.NodeType)
            {
            case XmlNodeType.Element:
                inContent = true;
                // StartTagOpenDeriv
                prevState = vState;
                vState    = memo.StartTagOpenDeriv(vState,
                                                   reader.LocalName, reader.NamespaceURI);
                if (vState.PatternType == RelaxngPatternType.NotAllowed)
                {
                    if (InvalidNodeFound != null)
                    {
                        vState = HandleError(String.Format("Invalid start tag found. LocalName = {0}, NS = {1}.", reader.LocalName, reader.NamespaceURI), true, prevState, RecoverFromInvalidStartTag);
                    }
                }

                // AttsDeriv equals to for each AttDeriv
                string elementNS = reader.NamespaceURI;
                if (reader.MoveToFirstAttribute())
                {
                    do
                    {
                        if (reader.NamespaceURI == "http://www.w3.org/2000/xmlns/")
                        {
                            continue;
                        }

                        RdpPattern savedState = vState;
                        prevState = vState;
                        string attrNS = reader.NamespaceURI;

                        vState = memo.StartAttDeriv(vState, reader.LocalName, attrNS);
                        if (vState == RdpNotAllowed.Instance)
                        {
                            vState = HandleError(String.Format("Invalid attribute occurence found. LocalName = {0}, NS = {1}.", reader.LocalName, reader.NamespaceURI), false, savedState, p => p);
                            continue;                             // the following steps are ignored.
                        }
                        prevState = vState;
                        vState    = memo.TextOnlyDeriv(vState);
                        vState    = TextDeriv(vState, reader.Value, reader);
                        if (Util.IsWhitespace(reader.Value))
                        {
                            vState = vState.Choice(prevState);
                        }
                        if (vState == RdpNotAllowed.Instance)
                        {
                            vState = HandleError(String.Format("Invalid attribute value is found. Value = '{0}'", reader.Value), false, prevState, RecoverFromInvalidText);
                        }
                        prevState = vState;
                        vState    = memo.EndAttDeriv(vState);
                        if (vState == RdpNotAllowed.Instance)
                        {
                            vState = HandleError(String.Format("Invalid attribute value is found. Value = '{0}'", reader.Value), false, prevState, RecoverFromInvalidEnd);
                        }
                    } while (reader.MoveToNextAttribute());
                    MoveToElement();
                }

                // StarTagCloseDeriv
                prevState = vState;
                vState    = memo.StartTagCloseDeriv(vState);
                if (vState.PatternType == RelaxngPatternType.NotAllowed)
                {
                    vState = HandleError(String.Format("Invalid start tag closing found. LocalName = {0}, NS = {1}.", reader.LocalName, reader.NamespaceURI), false, prevState, RecoverFromInvalidStartTagClose);
                }

                // if it is empty, then redirect to EndElement
                if (reader.IsEmptyElement)
                {
                    ValidateWeakMatch3();
                    goto case XmlNodeType.EndElement;
                }
                break;

            case XmlNodeType.EndElement:
                if (reader.Depth == 0)
                {
                    inContent = false;
                }
                // EndTagDeriv
                prevState = vState;
                vState    = memo.EndTagDeriv(vState);
                if (vState.PatternType == RelaxngPatternType.NotAllowed)
                {
                    vState = HandleError(String.Format("Invalid end tag found. LocalName = {0}, NS = {1}.", reader.LocalName, reader.NamespaceURI), true, prevState, RecoverFromInvalidEnd);
                }
                break;

            case XmlNodeType.Whitespace:
                if (inContent)
                {
                    goto case XmlNodeType.Text;
                }
                break;

            case XmlNodeType.CDATA:
            case XmlNodeType.Text:
            case XmlNodeType.SignificantWhitespace:
                // Whitespace cannot be skipped because data and
                // value types are required to validate whitespaces.
                cachedValue += Value;
                break;
            }

            if (reader.NodeType == XmlNodeType.Element && !reader.IsEmptyElement)
            {
                startElementDepth = reader.Depth;
            }
            else if (reader.NodeType == XmlNodeType.EndElement)
            {
                startElementDepth = -1;
            }

            return(ret);
        }
 // interleave :: Pattern -> Pattern -> Pattern
 internal static RdpPattern Interleave(RdpPattern p1, RdpPattern p2)
 {
     return(p1.Interleave(p2));
 }
Exemple #4
0
 private void AssertPattern(string s, RelaxngPatternType expected, RdpPattern p)
 {
     AssertEquals(s, expected, p.PatternType);
 }
        /*
         * // childDeriv :: Context -> Pattern -> ChildNode -> Pattern
         * internal static RdpPattern ChildDeriv (RdpContext ctx, RdpPattern p, RdpChildNode child)
         * {
         *  return p.ChildDeriv (child);
         * }
         */

        // textDeriv :: Context -> Pattern -> String -> Pattern
        internal static RdpPattern TextDeriv(XmlReader reader, RdpPattern p, string s)
        {
            return(p.TextDeriv(s, reader));
        }
 // choice :: Pattern -> Pattern -> Pattern
 internal static RdpPattern Choice(RdpPattern p1, RdpPattern p2)
 {
     return(p1.Choice(p2));
 }
Exemple #7
0
 // endTagDeriv :: Pattern -> Pattern
 // [implemented]
 internal static RdpPattern EndTagDeriv(RdpPattern p)
 {
     return(p.EndTagDeriv());
 }
Exemple #8
0
 RdpPattern RecoverFromInvalidStartTagClose(RdpPattern p)
 {
     return(ReplaceAttributesWithEmpty(p));
 }
Exemple #9
0
 // startTagCloseDeriv :: Pattern -> Pattern
 // [implemented]
 internal static RdpPattern StartTagCloseDeriv(RdpPattern p)
 {
     return(p.StartTagCloseDeriv());
 }
Exemple #10
0
 // oneOrMore :: Pattern -> Pattern
 // [implemented in RdpPattern]
 internal static RdpPattern OneOrMore(RdpPattern p)
 {
     return(p.OneOrMore());
 }
Exemple #11
0
 // valueMatch :: Context -> Pattern -> String -> Bool
 // [implemented in RdpPattern]
 internal static bool ValueMatch(RdpPattern p, string s, XmlReader reader)
 {
     return(p.ValueMatch(s, reader));
 }
Exemple #12
0
        /*
         * // attsDeriv :: Context -> Pattern -> [AttributeNode] -> Pattern
         * // [implemented in RdpPattern]
         * internal static RdpPattern AttsDeriv (RdpPattern p, RdpAttributes attributes)
         * {
         *      return p.AttsDeriv (attributes);
         * }
         */

        // attDeriv :: Context -> Pattern -> AttributeNode -> Pattern
        // [all implemented]
        internal static RdpPattern AttDeriv(XmlReader reader,
                                            RdpPattern p, string name, string ns, string value)
        {
            return(p.AttDeriv(name, ns, value, reader));
        }
Exemple #13
0
 // startTagOpenDeriv :: Pattern -> QName -> Pattern
 // TODO remains: Interleave, OneOrMore, Group, After
 internal static RdpPattern StartTagOpenDeriv(RdpPattern pattern, string name, string ns)
 {
     return(pattern.StartTagOpenDeriv(name, ns));
 }
Exemple #14
0
 RdpPattern RecoverFromInvalidText(RdpPattern p)
 {
     return(ReplaceAfterHeadWithEmpty(p));
 }
Exemple #15
0
 public RdpFlip(RdpBinaryFunction func, RdpPattern p)
 {
     this.func = func;
     this.arg  = p;
 }
Exemple #16
0
 RdpPattern RecoverFromInvalidEnd(RdpPattern p)
 {
     return(CollectAfterTailAsChoice(p));
 }
Exemple #17
0
 public RdpPattern Apply(RdpPattern p)
 {
     return(func(p, arg));
 }
 // nullable :: Pattern -> Bool
 internal static bool Nullable(RdpPattern p)
 {
     return(p.Nullable);
 }
Exemple #19
0
        internal static string DebugRdpPattern(RdpPattern p, Hashtable visitedPattern)
        {
            if (p is RdpText)
            {
                return("<text/>\n");
            }
            if (p is RdpEmpty)
            {
                return("<empty/>\n");
            }
            if (p is RdpNotAllowed)
            {
                return("<notAllowed/>\n");
            }

            if (visitedPattern.Contains(p))
            {
                return("<" + p.PatternType + " ref='" + p.GetHashCode() + "'/>");
            }
            visitedPattern.Add(p, p);
            string intl = "(id=" + p.GetHashCode() + ") ";

            RdpAbstractSingleContent s = p as RdpAbstractSingleContent;

            if (s != null)
            {
                intl = DebugRdpPattern(s.Child, visitedPattern);
            }
            RdpAbstractBinary b = p as RdpAbstractBinary;

            if (b != null)
            {
                intl = DebugRdpPattern(b.LValue, visitedPattern) +
                       DebugRdpPattern(b.RValue, visitedPattern);
            }

            RdpData data = p as RdpData;

            if (data != null)
            {
                intl = String.Format("name={0},ns={1},type={2} {3}",
                                     data.Datatype.LocalName,
                                     data.Datatype.NamespaceURI,
                                     data.Datatype.GetType(),
                                     data is RdpDataExcept ? DebugRdpPattern(((RdpDataExcept)data).Except, visitedPattern) : String.Empty);
            }

            RdpValue value = p as RdpValue;

            if (value != null)
            {
                intl = String.Format("name={0},ns={1},value={2} type={3}",
                                     value.Datatype.LocalName,
                                     value.Datatype.NamespaceURI,
                                     value.Value,
                                     value.Datatype.GetType());
            }

            RdpElement el = p as RdpElement;

            if (el != null)
            {
                intl = DebugNameClass(el.NameClass) +
                       DebugRdpPattern(el.Children, visitedPattern);
            }

            RdpAttribute at = p as RdpAttribute;

            if (at != null)
            {
                intl = DebugNameClass(at.NameClass) +
                       DebugRdpPattern(at.Children, visitedPattern);
            }

            string str = String.Format("<{0} id='id{1}'>\n{2}\n</{0}>",
                                       p.PatternType.ToString(),
                                       p.GetHashCode(),
                                       intl);

            return(str);
        }
 // listDeriv :: Context -> Pattern -> [String] -> Pattern
 internal static RdpPattern ListDeriv(XmlReader reader, RdpPattern p, string [] list)
 {
     return(p.ListDeriv(list, 0, reader));
 }
Exemple #21
0
 public void SetUp()
 {
     pattern1 = new RdpElement(new RdpName("foo", "urn:foo"), RdpEmpty.Instance);
 }
 // group :: Pattern -> Pattern -> Pattern
 internal static RdpPattern Group(RdpPattern p1, RdpPattern p2)
 {
     return(p1.Group(p2));
 }
Exemple #23
0
 private void AssertPattern(string s, RelaxngPatternType expected, RdpPattern p)
 {
     Assert.AreEqual(expected, p.PatternType, s);
 }
 // after :: Pattern -> Pattern -> Pattern
 internal static RdpPattern After(RdpPattern p1, RdpPattern p2)
 {
     return(p1.After(p2));
 }
Exemple #25
0
        internal RelaxngException(string message, RdpPattern invalidatedPattern)
            : base(message)
        {
//			debugXml = RdpUtil.DebugRdpPattern (invalidatedPattern, new Hashtable ());
        }