예제 #1
0
파일: XsdReader.cs 프로젝트: Bubesz/soal-cs
 private SoalType ImportPhase1SimpleType(XElement elem, string name, XsdTypeKind kind, XElement parentElem, bool register)
 {
     XAttribute nameAttr = elem.Attribute("name");
     XAttribute typeAttr = elem.Attribute("type");
     if (nameAttr != null)
     {
         name = nameAttr.Value;
     }
     if (name == null)
     {
         this.Importer.Diagnostics.AddError("The simpleType has no name.", this.Uri, this.GetTextSpan(elem));
         return null;
     }
     XElement restriction = elem.Element(xsd + "restriction");
     if (restriction != null)
     {
         XAttribute baseAttr = restriction.Attribute("base");
         if (baseAttr != null)
         {
             XName baseRef = this.GetXName(restriction, baseAttr.Value);
             if (baseRef == null)
             {
                 this.Importer.Diagnostics.AddError("Invalid base type: '" + baseAttr.Value + "'", this.Uri, this.GetTextSpan(baseAttr));
                 return null;
             }
             bool stringBased = baseRef.Namespace == xsd && baseRef.LocalName == "string";
             IEnumerable<XElement> enums = restriction.Elements(xsd + "enumeration");
             if (stringBased && enums.Any())
             {
                 Enum enm = SoalFactory.Instance.CreateEnum();
                 //name = this.GetUniqueName(name, element);
                 if (!register || (kind == XsdTypeKind.Type && this.Importer.XsdTypes.Register(this, tns + name, elem, enm) != null) ||
                     (kind == XsdTypeKind.Element && this.Importer.XsdElements.Register(this, tns + name, parentElem, enm) != null) ||
                     (kind == XsdTypeKind.Attribute && this.Importer.XsdAttributes.Register(this, tns + name, parentElem, enm) != null))
                 {
                     if (register)
                     {
                         if (kind == XsdTypeKind.Type)
                         {
                             this.definedElements.Add(elem);
                         }
                         else
                         {
                             this.definedElements.Add(parentElem);
                         }
                     }
                     enm.Name = name;
                     enm.Namespace = this.Namespace;
                     return enm;
                 }
                 else
                 {
                     ModelContext.Current.RemoveInstance((ModelObject)enm);
                 }
             }
         }
         else
         {
             this.Importer.Diagnostics.AddWarning("The restriction has no 'base' attribute.", this.Uri, this.GetTextSpan(restriction));
         }
     }
     else
     {
         this.Importer.Diagnostics.AddWarning("The importer of this element is not implemented.", this.Uri, this.GetTextSpan(elem));
     }
     PrimitiveType type = SoalFactory.Instance.CreatePrimitiveType();
     type.Name = name;
     type.Namespace = this.Namespace;
     if (register)
     {
         switch (kind)
         {
             case XsdTypeKind.Type:
                 return this.Importer.XsdTypes.Register(this, tns + name, elem, type);
             case XsdTypeKind.Element:
                 return this.Importer.XsdElements.Register(this, tns + name, parentElem, type);
             case XsdTypeKind.Attribute:
                 return this.Importer.XsdAttributes.Register(this, tns + name, parentElem, type);
             default:
                 break;
         }
     }
     return type;
 }
예제 #2
0
파일: XsdReader.cs 프로젝트: Bubesz/soal-cs
 private SoalType ImportPhase1ComplexType(XElement elem, string name, XsdTypeKind kind, XElement parentElem, bool register)
 {
     XAttribute nameAttr = elem.Attribute("name");
     XAttribute typeAttr = elem.Attribute("type");
     if (nameAttr != null)
     {
         name = nameAttr.Value;
     }
     if (name == null)
     {
         this.Importer.Diagnostics.AddError("The complexType has no name.", this.Uri, this.GetTextSpan(elem));
         return null;
     }
     Struct st = SoalFactory.Instance.CreateStruct();
     //name = this.GetUniqueName(name, element);
     if (!register || (kind == XsdTypeKind.Type && this.Importer.XsdTypes.Register(this, tns + name, elem, st) != null) ||
         (kind == XsdTypeKind.Element && this.Importer.XsdElements.Register(this, tns + name, parentElem, st) != null) ||
         (kind == XsdTypeKind.Attribute && this.Importer.XsdAttributes.Register(this, tns + name, parentElem, st) != null))
     {
         if (register)
         {
             if (kind == XsdTypeKind.Type)
             {
                 this.definedElements.Add(elem);
             }
             else
             {
                 this.definedElements.Add(parentElem);
             }
         }
         st.Name = name;
         st.Namespace = this.Namespace;
         XElement sequenceElem = elem.Element(xsd + "sequence");
         XElement choiceElem = elem.Element(xsd + "choice");
         XElement allElem = elem.Element(xsd + "all");
         XElement complexElem = null;
         if (sequenceElem != null)
         {
             complexElem = sequenceElem;
             List<XElement> children = sequenceElem.Elements(xsd + "element").ToList();
             if (children.Count == 1)
             {
                 XElement child = children[0];
                 XAttribute refAttr = child.Attribute("ref");
                 if (refAttr == null)
                 {
                     XAttribute nillableAttr = child.Attribute("nillable");
                     XAttribute minOccursAttr = child.Attribute("minOccurs");
                     XAttribute maxOccursAttr = child.Attribute("maxOccurs");
                     bool nillable = false;
                     int minOccurs = 1;
                     int maxOccurs = 1;
                     if (nillableAttr != null)
                     {
                         nillable = nillableAttr.Value == "1" || nillableAttr.Value.ToLower() == "true";
                     }
                     if (minOccursAttr != null)
                     {
                         if (!int.TryParse(minOccursAttr.Value, out minOccurs))
                         {
                             minOccurs = 1;
                         }
                     }
                     if (maxOccursAttr != null)
                     {
                         if (maxOccursAttr.Value.ToLower() == "unbounded")
                         {
                             maxOccurs = -1;
                         }
                         else if (!int.TryParse(maxOccursAttr.Value, out maxOccurs))
                         {
                             maxOccurs = 1;
                         }
                     }
                     if (maxOccurs < 0 || maxOccurs > 1)
                     {
                         ArrayType array = SoalFactory.Instance.CreateArrayType();
                         this.Importer.RegisterReplacementType(st, array);
                     }
                 }
             }
         }
         else if (choiceElem != null)
         {
             complexElem = choiceElem;
         }
         else if (allElem != null)
         {
             complexElem = allElem;
         }
         else
         {
             //this.Importer.Diagnostics.AddError("The complexType has invalid content.", this.Uri, this.GetTextSpan(elem));
             //return null;
         }
         return st;
     }
     else
     {
         ModelContext.Current.RemoveInstance((ModelObject)st);
     }
     return null;
 }