Ejemplo n.º 1
0
        private void InferTextContent(RelaxngElement ct, bool isNew)
        {
            string         value = source.ReadString();
            RelaxngPattern p     = GetElementContent(ct);

            if (p == null)
            {
                ct.Patterns.Add(CreateSimplePattern(
                                    InferSimpleType(value)));
                return;
            }
            RelaxngPatternList pl = null;

            switch (p.PatternType)
            {
            case RelaxngPatternType.Text:
            case RelaxngPatternType.Data:
                return;                 // no way to narrow it to data.

            case RelaxngPatternType.Empty:
                ct.Patterns.Remove(p);
                ct.Patterns.Add(new RelaxngText());
                return;

            case RelaxngPatternType.Group:
                pl = ((RelaxngBinaryContentPattern)p).Patterns;
                break;

            case RelaxngPatternType.Optional:
            case RelaxngPatternType.ZeroOrMore:
            case RelaxngPatternType.OneOrMore:
                pl = ((RelaxngSingleContentPattern)p).Patterns;
                break;

            default:
                throw Error(p, "Unexpected pattern");
            }
            if (pl.Count > 0 && pl [0] is RelaxngMixed)
            {
                return;
            }
            RelaxngMixed m = new RelaxngMixed();

            while (pl.Count > 0)
            {
                RelaxngPattern child = pl [0];
                m.Patterns.Add(child);
                pl.Remove(child);
            }
            pl.Add(m);
        }
Ejemplo n.º 2
0
        private void ProcessSequence(RelaxngElement ct, RelaxngGroup s,
                                     ref int position, ref bool consumed,
                                     bool isNew)
        {
            RelaxngMixed       m  = s.Patterns.Count > 0 ? s.Patterns [0] as RelaxngMixed : null;
            RelaxngPatternList pl = m != null ?
                                    m.Patterns : s.Patterns;

            for (int i = 0; i < position; i++)
            {
                RelaxngPattern p   = pl [i];
                RelaxngRef     iel = p as RelaxngRef;
                if (iel == null)
                {
                    RelaxngOneOrMore oom =
                        p as RelaxngOneOrMore;
                    iel = (RelaxngRef)oom.Patterns [0];
                }
                if (ElementMatches(iel))
                {
                    // Sequence element type violation
                    // might happen (might not, but we
                    // cannot backtrack here). So switch
                    // to sequence of choice* here.
                    ProcessLax(ToSequenceOfChoice(ct, s));
                    return;
                }
            }

            if (pl.Count <= position)
            {
                QName name = new QName(source.LocalName,
                                       source.NamespaceURI);
                RelaxngDefine nel = GetGlobalElement(name);
                if (nel != null)
                {
                    InferElement(nel, false);
                }
                else
                {
                    nel = CreateGlobalElement(name);                      // used to be CreateElement().
                    InferElement(nel, true);
                }
                RelaxngRef re = new RelaxngRef();
                re.Name = nel.Name;
                pl.Add(re);
                consumed = true;
                return;
            }
            RelaxngPattern c  = pl [position];
            RelaxngRef     el = c as RelaxngRef;

            if (el == null)
            {
                RelaxngOneOrMore oom = c as RelaxngOneOrMore;
                el = (RelaxngRef)oom.Patterns [0];
            }
            if (el == null)
            {
                throw Error(s, String.Format("Target complex type content sequence has an unacceptable type of particle {0}", s.Patterns [position]));
            }
            bool matches = ElementMatches(el);

            if (matches)
            {
                if (consumed && c is RelaxngRef)
                {
                    RelaxngOneOrMore oom = new RelaxngOneOrMore();
                    oom.Patterns.Add(el);
                    pl [position] = oom;
                }
                InferElement(el, false);
                source.MoveToContent();
                switch (source.NodeType)
                {
                case XmlNodeType.None:
                    if (source.NodeType ==
                        XmlNodeType.Element)
                    {
                        goto case XmlNodeType.Element;
                    }
                    else if (source.NodeType ==
                             XmlNodeType.EndElement)
                    {
                        goto case XmlNodeType.EndElement;
                    }
                    break;

                case XmlNodeType.Element:
                    ProcessSequence(ct, s, ref position,
                                    ref consumed, isNew);
                    break;

                case XmlNodeType.Text:
                case XmlNodeType.CDATA:
                case XmlNodeType.SignificantWhitespace:
                    MarkAsMixed(ct);
                    source.ReadString();
                    goto case XmlNodeType.None;

                case XmlNodeType.Whitespace:
                    source.ReadString();
                    goto case XmlNodeType.None;

                case XmlNodeType.EndElement:
                    return;

                default:
                    source.Read();
                    break;
                }
            }
            else
            {
                if (consumed)
                {
                    position++;
                    consumed = false;
                    ProcessSequence(ct, s,
                                    ref position, ref consumed,
                                    isNew);
                }
                else
                {
                    ProcessLax(ToSequenceOfChoice(ct, s));
                }
            }
        }