예제 #1
0
        // It returns RelaxngAttribute for local attribute, and
        // RelaxngRef for global attribute.
        private RelaxngPattern InferNewAttribute(
            QName attrName, bool isNewTypeDefinition)
        {
            RelaxngPattern p = null;
            bool           mergedRequired = false;

            if (attrName.Namespace.Length > 0)
            {
                // global attribute; might be already defined.
                // (Actually RELAX NG has no concept of "global
                // attributes" but it is still useful to
                // represent attributes in global scope.
                RelaxngDefine attr = GetGlobalAttribute(
                    attrName);
                if (attr == null)
                {
                    attr = CreateGlobalAttribute(attrName);
                    attr.Patterns.Add(CreateSimplePattern(
                                          InferSimpleType(source.Value)));
                }
                else
                {
                    RelaxngAttribute a = attr.Patterns [0] as RelaxngAttribute;
                    if (a != null)
                    {
                        mergedRequired = true;
                    }
                    else
                    {
                        RelaxngOptional opt =
                            (RelaxngOptional)attr.Patterns [0];
                        a = (RelaxngAttribute)opt.Patterns [0];
                    }
                    InferMergedAttribute(a);
                }
                RelaxngRef r = new RelaxngRef();
                r.Name = attr.Name;
                p      = r;
            }
            else
            {
                // local attribute
                RelaxngAttribute a = new RelaxngAttribute();
                a.NameClass = new RelaxngName(
                    attrName.Name, attrName.Namespace);
                a.Pattern = CreateSimplePattern(
                    InferSimpleType(source.Value));
                p = a;
            }
            // optional
            if (laxOccurence ||
                (!isNewTypeDefinition && !mergedRequired))
            {
                RelaxngOptional opt = new RelaxngOptional();
                opt.Patterns.Add(p);
                p = opt;
            }

            return(p);
        }
예제 #2
0
        RelaxngPattern CreatePatternFromParticle(XmlSchemaParticle xsdp)
        {
            RelaxngSingleContentPattern rngp = null;

            if (xsdp.MinOccurs == 0 && xsdp.MaxOccursString == "unbounded")
            {
                rngp = new RelaxngZeroOrMore();
            }
            else if (xsdp.MinOccurs == 1 && xsdp.MaxOccursString == "unbounded")
            {
                rngp = new RelaxngOneOrMore();
            }
            else if (xsdp.MinOccurs == 0)
            {
                rngp = new RelaxngOptional();
            }

            RelaxngPattern child = CreatePatternFromParticleCore(xsdp);

            if (rngp == null)
            {
                return(child);
            }
            rngp.Patterns.Add(child);
            return(rngp);
        }
예제 #3
0
        RelaxngPattern CreateAttribute(XmlSchemaAttribute attr)
        {
            RelaxngAttribute ra   = new RelaxngAttribute();
            RelaxngName      name = new RelaxngName();

            name.LocalName = attr.Name;
            ra.NameClass   = name;
            ra.Pattern     = attr.SchemaType != null?
                             CreatePatternFromType(attr.SchemaType) :
                                 CreatePatternFromTypeName(attr.SchemaTypeName);

            RelaxngPattern ret = ra;

            if (attr.Use == XmlSchemaUse.Optional)
            {
                RelaxngOptional opt = new RelaxngOptional();
                opt.Patterns.Add(ra);
                ret = opt;
            }
            return(ret);
        }
예제 #4
0
        private void InferAsEmptyElement(RelaxngElement ct, bool isNew)
        {
            RelaxngPattern content = GetElementContent(ct);

            if (content == null)
            {
                ct.Patterns.Add(new RelaxngEmpty());
                return;
            }

            RelaxngGroup g = content as RelaxngGroup;

            if (g == null)
            {
                return;
            }
            RelaxngOptional opt = new RelaxngOptional();

            opt.Patterns.Add(g);
            ct.Patterns.Remove(content);
            ct.Patterns.Add(opt);
        }
예제 #5
0
        private void InferAttributes(RelaxngElement ct, bool isNew)
        {
            RelaxngInterleave attList = null;
            Hashtable         table   = null;

            do
            {
                if (source.NamespaceURI == NamespaceXmlns)
                {
                    continue;
                }

                if (table == null)
                {
                    attList = GetAttributes(ct);
                    table   = CollectAttrTable(attList);
                }
                QName attrName = new QName(
                    source.LocalName, source.NamespaceURI);
                RelaxngPattern attr = table [attrName]
                                      as RelaxngPattern;
                if (attr == null)
                {
                    if (attList == null)
                    {
                        attList = new RelaxngInterleave();
                        ct.Patterns.Insert(0, attList);
                    }
                    attList.Patterns.Add(
                        InferNewAttribute(
                            attrName, isNew));
                }
                else
                {
                    table.Remove(attrName);
                    if (attrName.Namespace.Length > 0)
                    {
                        RelaxngDefine ga = GetGlobalAttribute(attrName);
                        InferMergedAttribute(
                            ga.Patterns [0]);
                    }
                    else
                    {
                        InferMergedAttribute(attr);
                    }
                }
            } while (source.MoveToNextAttribute());

            // mark all attr definitions that did not appear
            // as optional.
            if (table != null)
            {
                foreach (RelaxngPattern attr in table.Values)
                {
                    if (attr is RelaxngOptional)
                    {
                        continue;
                    }
                    attList.Patterns.Remove(attr);
                    RelaxngOptional opt = new RelaxngOptional();
                    opt.Patterns.Add(attr);
                    attList.Patterns.Add(opt);
                }
            }
        }
예제 #6
0
 public void WriteOptional(RelaxngOptional p)
 {
     WritePatterns(p.Patterns, true);
     w.Write('?');
 }