Inheritance: XmlSchemaSimpleTypeContent
        public XmlSchemaElement GetSchema()
        {
            var type = new XmlSchemaComplexType();

            type.Attributes.Add(new XmlSchemaAttribute
                                    {
                                        Name = "refValue",
                                        Use = XmlSchemaUse.Required,
                                        SchemaTypeName =
                                            new XmlQualifiedName("positiveInteger", "http://www.w3.org/2001/XMLSchema")
                                    });

            type.Attributes.Add(new XmlSchemaAttribute
                                    {
                                        Name = "test",
                                        Use = XmlSchemaUse.Optional,
                                        SchemaTypeName =
                                            new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
                                    });

            
            var restriction = new XmlSchemaSimpleTypeRestriction
                                  {
                                      BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
                                  };

            restriction.Facets.Add(new XmlSchemaEnumerationFacet {Value = "years"});
            restriction.Facets.Add(new XmlSchemaEnumerationFacet {Value = "weeks"});
            restriction.Facets.Add(new XmlSchemaEnumerationFacet {Value = "days"});

            var simpleType = new XmlSchemaSimpleType {Content = restriction};


            type.Attributes.Add(new XmlSchemaAttribute
                                    {
                                        Name = "units",
                                        Use = XmlSchemaUse.Required,
                                        SchemaType = simpleType
                                    });


            type.Attributes.Add(new XmlSchemaAttribute
                                    {
                                        Name = "expressionLanguage",
                                        Use = XmlSchemaUse.Optional,
                                        SchemaTypeName =
                                            new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
                                    });

            var element = new XmlSchemaElement
                              {
                                  Name = "dicom-age-less-than",
                                  SchemaType = type
                              };

            return element;
        }
    private void CreateSimpletypeLength(string length, string minLength, string maxLength, bool expected) {
      passed = true;

      XmlSchema schema = new XmlSchema();

      XmlSchemaSimpleType testType = new XmlSchemaSimpleType();
      testType.Name = "TestType";

      XmlSchemaSimpleTypeRestriction testTypeRestriction = new XmlSchemaSimpleTypeRestriction();
      testTypeRestriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");

      if (length != "-") {
        XmlSchemaLengthFacet _length = new XmlSchemaLengthFacet();
        _length.Value = length;
        testTypeRestriction.Facets.Add(_length);
      }
      if (minLength != "-") {
        XmlSchemaMinLengthFacet _minLength = new XmlSchemaMinLengthFacet();
        _minLength.Value = minLength;
        testTypeRestriction.Facets.Add(_minLength);
      }
      if (maxLength != "-") {
        XmlSchemaMaxLengthFacet _maxLength = new XmlSchemaMaxLengthFacet();
        _maxLength.Value = maxLength;
        testTypeRestriction.Facets.Add(_maxLength);
      }

      testType.Content = testTypeRestriction;
      schema.Items.Add(testType);
      schema.Compile(new ValidationEventHandler(ValidationCallbackOne));

      Assert.IsTrue (expected == passed, (passed ? "Test passed, should have failed" : "Test failed, should have passed") + ": " + length + " " + minLength + " " + maxLength);
      
    }
        protected override void Visit(XmlSchemaSimpleTypeRestriction restriction)
        {
            PushNode(SimpleTypeStructureNodeType.Restriction, restriction);

            var baseType = _schemaSet.ResolveType(restriction.BaseType, restriction.BaseTypeName);
            Traverse(baseType);
            Traverse(restriction.Facets);

            PopNode();
        }
Beispiel #4
0
        private XmlSchemaSimpleType CreateGuidType()
        {
            var type = new XmlSchemaSimpleType { Name = "Guid" };

            var restriction = new XmlSchemaSimpleTypeRestriction { BaseType = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.String) };
            restriction.Facets.Add(new XmlSchemaPatternFacet
            {
                Value =
                    @"[{(]?[0-9A-Fa-f]{8}\-?[0-9A-Fa-f]{4}\-?[0-9A-Fa-f]{4}\-?[0-9A-Fa-f]{4}\-?[0-9A-Fa-f]{12}[})]?|([!$])(\(var|\(loc|\(wix)\.[_A-Za-z][0-9A-Za-z_.]*\)"
            });
            type.Content = restriction;
            return type;
        }
        protected internal override void Write(XmlSchemaObject obj)
        {
            var simpleType = (XmlSchemaSimpleType) obj;
            var restriction = new XmlSchemaSimpleTypeRestriction();

            foreach (var facet in Facets)
            {
                restriction.Facets.Add(facet);
            }

            simpleType.Content = restriction;
            base.Write(obj);
        }
Beispiel #6
0
        private SimpleType MakeBaseTypeAsEmptiable(SimpleType st)
        {
            switch (st.QualifiedName.Namespace)
            {
            case XmlSchema.Namespace:
            case XdtNamespace:
                // If a primitive type
                return(XmlSchemaType.GetBuiltInSimpleType(
                           XmlTypeCode.String));
            }
            SimpleTypeRst str = st.Content as SimpleTypeRst;

            if (str != null)
            {
                ArrayList al = null;
                foreach (SchemaFacet f in str.Facets)
                {
                    if (f is LengthFacet ||
                        f is MinLengthFacet)
                    {
                        if (al == null)
                        {
                            al = new ArrayList();
                        }
                        al.Add(f);
                    }
                }
                foreach (SchemaFacet f in al)
                {
                    str.Facets.Remove(f);
                }
                if (str.BaseType != null)
                {
                    str.BaseType =
                        MakeBaseTypeAsEmptiable(st);
                }
                else
                {
                    // It might have a reference to an
                    // external simple type, but there is
                    // no assurance that any of those
                    // external types allow an empty
                    // string. So just set base type as
                    // xs:string.
                    str.BaseTypeName = QNameString;
                }
            }             // union/list can have empty string value.

            return(st);
        }
 private static void AddNonXsdPrimitive(Type type, string dataTypeName, string ns, string formatterName, XmlQualifiedName baseTypeName, XmlSchemaFacet[] facets, TypeFlags flags)
 {
     XmlSchemaSimpleType dataType = new XmlSchemaSimpleType {
         Name = dataTypeName
     };
     XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction {
         BaseTypeName = baseTypeName
     };
     foreach (XmlSchemaFacet facet in facets)
     {
         restriction.Facets.Add(facet);
     }
     dataType.Content = restriction;
     TypeDesc desc = new TypeDesc(type, false, dataType, formatterName, flags);
     if (primitiveTypes[type] == null)
     {
         primitiveTypes.Add(type, desc);
     }
     primitiveDataTypes.Add(dataType, desc);
     primitiveNames.Add(dataTypeName, ns, desc);
 }
Beispiel #8
0
        void SetupSimpleTypeWrappers()
        {
            XmlSchema    schema = new XmlSchema();
            List <QName> qnames = new List <QName> ();

            foreach (string name in allTypes)
            {
                SimpleType st = new SimpleType();
                st.Name = "x-" + name;
                SimpleRest r = new SimpleRest();
                st.Content = r;
                QName qname = new QName(name, XmlSchema.Namespace);
                r.BaseTypeName = qname;
                qnames.Add(qname);
                schema.Items.Add(st);
            }
            XmlSchemaSet sset = new XmlSchemaSet();

            sset.Add(schema);
            sset.Compile();
            allWrappers = sset;
        }
        /// <summary>
        ///     Returns a SimpleType restricted by the datatype only
        /// </summary>
        public override XmlSchemaSimpleType GetSimpleType(string attributeDataType)
        {
            var retVal = new XmlSchemaSimpleType();

            // <xs:restriction base="<whatever xs:datatype is required>" />
            var dataTypeRestriction = new XmlSchemaSimpleTypeRestriction
            {
                BaseTypeName = new XmlQualifiedName(attributeDataType)
            };
            retVal.Content = dataTypeRestriction;

            //  if the property type is an enum then add an enumeration facet to the simple type
            if (Property.PropertyType.IsEnum)
            {
                // <xs:enumeration value="123" />
                foreach (var enumValue in Enum.GetNames(Property.PropertyType))
                {
                    var enumFacet = new XmlSchemaEnumerationFacet { Value = enumValue };
                    dataTypeRestriction.Facets.Add(enumFacet);
                }
            }

            return retVal;
        }
Beispiel #10
0
		void ExportDerivedSchema(XmlTypeMapping map) {
			if (IsMapExported (map)) return;
			SetMapExported (map);

			XmlSchema schema = GetSchema (map.XmlTypeNamespace);
			for (int i = 0; i < schema.Items.Count; i++) {
			        XmlSchemaSimpleType item = schema.Items [i] as XmlSchemaSimpleType;
			        if (item != null && item.Name == map.ElementName)
			                return;
			}
			XmlSchemaSimpleType stype = new XmlSchemaSimpleType ();
			stype.Name = map.ElementName;
			schema.Items.Add (stype);

			XmlSchemaSimpleTypeRestriction rest = new XmlSchemaSimpleTypeRestriction ();
			rest.BaseTypeName = new XmlQualifiedName (map.TypeData.MappedType.XmlType, XmlSchema.Namespace);
			XmlSchemaPatternFacet facet = map.TypeData.XmlSchemaPatternFacet;
			if (facet != null)
				rest.Facets.Add(facet);
			stype.Content = rest;
		}
 protected void ExportStreamBody(WsdlNS.Message message, string wrapperName, string wrapperNs, string partName, string partNs, bool isRpc, bool skipSchemaExport)
 {
     XmlSchemaSet schemas = this.exporter.GeneratedXmlSchemas;
     XmlSchema schema = SchemaHelper.GetSchema(DataContractSerializerMessageContractImporter.StreamBodyTypeName.Namespace, schemas);
     if (!schema.SchemaTypes.Contains(DataContractSerializerMessageContractImporter.StreamBodyTypeName))
     {
         XmlSchemaSimpleType streamBodyType = new XmlSchemaSimpleType();
         streamBodyType.Name = DataContractSerializerMessageContractImporter.StreamBodyTypeName.Name;
         XmlSchemaSimpleTypeRestriction contentRestriction = new XmlSchemaSimpleTypeRestriction();
         contentRestriction.BaseTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Base64Binary).QualifiedName;
         streamBodyType.Content = contentRestriction;
         SchemaHelper.AddTypeToSchema(streamBodyType, schema, schemas);
     }
     XmlSchemaSequence wrapperSequence = null;
     if (!isRpc && wrapperName != null)
         wrapperSequence = ExportWrappedPart(message, wrapperName, wrapperNs, schemas, skipSchemaExport);
     MessagePartDescription streamPart = new MessagePartDescription(partName, partNs);
     ExportMessagePart(message, streamPart, DataContractSerializerMessageContractImporter.StreamBodyTypeName, null/*xsdType*/, false/*isOptional*/, false/*isNillable*/, skipSchemaExport, !isRpc, wrapperNs, wrapperSequence, schemas);
 }
Beispiel #12
0
		void ImportEnum (CodeTypeDeclaration td, XmlSchemaSet schemas, XmlSchemaSimpleTypeRestriction r, XmlSchemaType type, XmlQualifiedName qname, bool isFlag)
		{
			if (isFlag && !r.BaseTypeName.Equals (new XmlQualifiedName ("string", XmlSchema.Namespace)))
				throw new InvalidDataContractException (String.Format ("For flags enumeration '{0}', the base type for the simple type restriction must be XML schema string", qname));

			td.IsEnum = true;
			AddTypeAttributes (td, type);
			if (isFlag)
				td.CustomAttributes.Add (new CodeAttributeDeclaration (new CodeTypeReference (typeof (FlagsAttribute))));

			foreach (var facet in r.Facets) {
				var e = facet as XmlSchemaEnumerationFacet;
				if (e == null)
					throw new InvalidDataContractException (String.Format ("Invalid simple type restriction (type {0}). Only enumeration is allowed.", qname));
				var em = new CodeMemberField () { Name = CodeIdentifier.MakeValid (e.Value) };
				var ea = new CodeAttributeDeclaration (enum_member_att_ref);
				if (e.Value != em.Name)
					ea.Arguments.Add (new CodeAttributeArgument ("Value", new CodePrimitiveExpression (e.Value)));
				em.CustomAttributes.Add (ea);
				td.Members.Add (em);
			}
		}
Beispiel #13
0
        private static void createEnum(PropertyInfo pi, Dictionary<Type, XmlSchemaType> xmlTypes, XmlSchema xmlSchema)
        {
            // Create enum
            var res = new XmlSchemaSimpleTypeRestriction();
            res.BaseTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.NmToken).QualifiedName;

            foreach (var o in Enum.GetNames(pi.PropertyType))
            {
                res.Facets.Add(new XmlSchemaEnumerationFacet
                    {
                        Value = (o.Substring(0, 1).ToLower() + o.Substring(1))
                });
            }

            XmlSchemaSimpleType st = new XmlSchemaSimpleType
            {
                Content = res
            };

            // For flags must create a union of the values & string
            if (CustomAttributeHelper.Has<System.FlagsAttribute>(pi.PropertyType))
            {
                XmlSchemaSimpleType st2 = new XmlSchemaSimpleType();
                st2.Name = getXmlTypeName(pi.PropertyType);

                var union = new XmlSchemaSimpleTypeUnion();

                XmlSchemaSimpleType st3 = new XmlSchemaSimpleType();
                var res3 = new XmlSchemaSimpleTypeRestriction();
                res3.BaseTypeName = XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.String).QualifiedName;
                st3.Content = res3;

                union.BaseTypes.Add(st);
                union.BaseTypes.Add(st3);

                st2.Content = union;
                xmlSchema.Items.Add(st2);
                xmlTypes[pi.PropertyType] = st2;
            }
            else
            {
                st.Name = getXmlTypeName(pi.PropertyType);
                xmlSchema.Items.Add(st);
                xmlTypes[pi.PropertyType] = st;
            }
        }
        XmlSchemaType ExportEnumMapping(EnumMapping mapping, string ns) {
            if (!mapping.IncludeInSchema) throw new InvalidOperationException(Res.GetString(Res.XmlCannotIncludeInSchema, mapping.TypeDesc.Name));
            XmlSchemaSimpleType dataType = (XmlSchemaSimpleType)types[mapping];
            if (dataType == null) {
                CheckForDuplicateType(mapping, mapping.Namespace);
                dataType = new XmlSchemaSimpleType();
                dataType.Name = mapping.TypeName;
                if (!mapping.IsAnonymousType) {
                    types.Add(mapping, dataType);
                    AddSchemaItem(dataType, mapping.Namespace, ns);
                }
                XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction();
                restriction.BaseTypeName = new XmlQualifiedName("string", XmlSchema.Namespace);

                for (int i = 0; i < mapping.Constants.Length; i++) {
                    ConstantMapping constant = mapping.Constants[i];
                    XmlSchemaEnumerationFacet enumeration = new XmlSchemaEnumerationFacet();
                    enumeration.Value = constant.XmlName;
                    restriction.Facets.Add(enumeration);
                }
                if (!mapping.IsFlags) {
                    dataType.Content = restriction;
                }
                else {
                    XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList();
                    XmlSchemaSimpleType enumType = new XmlSchemaSimpleType();
                    enumType.Content =  restriction;
                    list.ItemType = enumType;
                    dataType.Content =  list;
                }
            }
            if (!mapping.IsAnonymousType) {
                AddSchemaImport(mapping.Namespace, ns);
            }
            return dataType;
        }
Beispiel #15
0
 private static void AddSimpleType(XmlSchema schema, string typeName, string baseXsdType) {
     XmlSchemaSimpleType type = new XmlSchemaSimpleType();
     type.Name = typeName;
     XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction();
     restriction.BaseTypeName = new XmlQualifiedName(baseXsdType, XmlSchema.Namespace);
     type.Content = restriction;
     schema.Items.Add(type);
 }
 internal static XmlSchema GetBuildInSchema()
 {
     if (builtInSchemaForXmlNS == null)
     {
         XmlSchema schema = new XmlSchema {
             TargetNamespace = "http://www.w3.org/XML/1998/namespace"
         };
         schema.Namespaces.Add("xml", "http://www.w3.org/XML/1998/namespace");
         XmlSchemaAttribute item = new XmlSchemaAttribute {
             Name = "lang",
             SchemaTypeName = new XmlQualifiedName("language", "http://www.w3.org/2001/XMLSchema")
         };
         schema.Items.Add(item);
         XmlSchemaAttribute attribute2 = new XmlSchemaAttribute {
             Name = "base",
             SchemaTypeName = new XmlQualifiedName("anyURI", "http://www.w3.org/2001/XMLSchema")
         };
         schema.Items.Add(attribute2);
         XmlSchemaAttribute attribute3 = new XmlSchemaAttribute {
             Name = "space"
         };
         XmlSchemaSimpleType type = new XmlSchemaSimpleType();
         XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction {
             BaseTypeName = new XmlQualifiedName("NCName", "http://www.w3.org/2001/XMLSchema")
         };
         XmlSchemaEnumerationFacet facet = new XmlSchemaEnumerationFacet {
             Value = "default"
         };
         restriction.Facets.Add(facet);
         XmlSchemaEnumerationFacet facet2 = new XmlSchemaEnumerationFacet {
             Value = "preserve"
         };
         restriction.Facets.Add(facet2);
         type.Content = restriction;
         attribute3.SchemaType = type;
         attribute3.DefaultValue = "preserve";
         schema.Items.Add(attribute3);
         XmlSchemaAttributeGroup group = new XmlSchemaAttributeGroup {
             Name = "specialAttrs"
         };
         XmlSchemaAttribute attribute4 = new XmlSchemaAttribute {
             RefName = new XmlQualifiedName("lang", "http://www.w3.org/XML/1998/namespace")
         };
         group.Attributes.Add(attribute4);
         XmlSchemaAttribute attribute5 = new XmlSchemaAttribute {
             RefName = new XmlQualifiedName("space", "http://www.w3.org/XML/1998/namespace")
         };
         group.Attributes.Add(attribute5);
         XmlSchemaAttribute attribute6 = new XmlSchemaAttribute {
             RefName = new XmlQualifiedName("base", "http://www.w3.org/XML/1998/namespace")
         };
         group.Attributes.Add(attribute6);
         schema.Items.Add(group);
         schema.IsPreprocessed = true;
         schema.CompileSchemaInSet(new NameTable(), null, null);
         Interlocked.CompareExchange<XmlSchema>(ref builtInSchemaForXmlNS, schema, null);
     }
     return builtInSchemaForXmlNS;
 }
Beispiel #17
0
                public XmlSchemaType WriteEnumType (Type type)
                {
                        if (type.IsEnum == false)
                                throw new Exception (String.Format ("{0} is not an enumeration.", type.Name));

                        if (generatedSchemaTypes.Contains (type.FullName)) // Caching
                                return null;

                        XmlSchemaSimpleType simpleType = new XmlSchemaSimpleType ();
                        simpleType.Name = type.Name;
                        FieldInfo [] fields = type.GetFields ();

                        XmlSchemaSimpleTypeRestriction simpleRestriction = new XmlSchemaSimpleTypeRestriction ();
                        simpleType.Content = simpleRestriction;
                        simpleRestriction.BaseTypeName = new XmlQualifiedName ("string", xs);

                        foreach (FieldInfo field in fields) {
                                if (field.IsSpecialName)
                                        continue;

                                XmlSchemaEnumerationFacet e = new XmlSchemaEnumerationFacet ();
                                e.Value = field.Name;
                                simpleRestriction.Facets.Add (e);
                        }

                        generatedSchemaTypes.Add (type.FullName, simpleType);
                        return simpleType;
                }
        /// <summary>
        /// Finish constructing built-in types by setting up derivation and list links.
        /// </summary>
        internal static void FinishBuiltinType(XmlSchemaSimpleType derivedType, XmlSchemaSimpleType baseType) {
            Debug.Assert(derivedType != null && baseType != null);

            // Create link from the derived type to the base type
            derivedType.SetBaseSchemaType(baseType);
            derivedType.SetDerivedBy(XmlSchemaDerivationMethod.Restriction);
            if (derivedType.Datatype.Variety == XmlSchemaDatatypeVariety.Atomic) { //Content is restriction
                XmlSchemaSimpleTypeRestriction restContent = new XmlSchemaSimpleTypeRestriction();
                restContent.BaseTypeName = baseType.QualifiedName;
                derivedType.Content = restContent;
            }

            // Create link from a list type to its member type
            if (derivedType.Datatype.Variety == XmlSchemaDatatypeVariety.List) {
                XmlSchemaSimpleTypeList listContent = new XmlSchemaSimpleTypeList();
                derivedType.SetDerivedBy(XmlSchemaDerivationMethod.List);
                switch (derivedType.Datatype.TypeCode) {
                    case XmlTypeCode.NmToken:
                        listContent.ItemType = listContent.BaseItemType = enumToTypeCode[(int) XmlTypeCode.NmToken];
                        break;

                    case XmlTypeCode.Entity:
                        listContent.ItemType = listContent.BaseItemType = enumToTypeCode[(int) XmlTypeCode.Entity];
                        break;

                    case XmlTypeCode.Idref:
                        listContent.ItemType = listContent.BaseItemType = enumToTypeCode[(int) XmlTypeCode.Idref];
                        break;
                }
                derivedType.Content = listContent;
            }
        }
        internal static XmlSchema GetBuildInSchema() {
            if (builtInSchemaForXmlNS == null) {
                XmlSchema tempSchema = new XmlSchema();
                tempSchema.TargetNamespace = XmlReservedNs.NsXml;
                tempSchema.Namespaces.Add("xml", XmlReservedNs.NsXml);
                
                XmlSchemaAttribute lang = new XmlSchemaAttribute();
                lang.Name = "lang";
                lang.SchemaTypeName = new XmlQualifiedName("language", XmlReservedNs.NsXs);
                tempSchema.Items.Add(lang);

                XmlSchemaAttribute xmlbase = new XmlSchemaAttribute();
                xmlbase.Name = "base";
                xmlbase.SchemaTypeName = new XmlQualifiedName("anyURI", XmlReservedNs.NsXs);
                tempSchema.Items.Add(xmlbase);

                XmlSchemaAttribute space = new XmlSchemaAttribute();
                space.Name = "space";
                    XmlSchemaSimpleType type = new XmlSchemaSimpleType();
                    XmlSchemaSimpleTypeRestriction r = new XmlSchemaSimpleTypeRestriction();
                    r.BaseTypeName = new XmlQualifiedName("NCName", XmlReservedNs.NsXs);
                    XmlSchemaEnumerationFacet space_default = new XmlSchemaEnumerationFacet();
                    space_default.Value = "default";
                    r.Facets.Add(space_default);
                    XmlSchemaEnumerationFacet space_preserve = new XmlSchemaEnumerationFacet();
                    space_preserve.Value = "preserve";
                    r.Facets.Add(space_preserve);
                    type.Content = r;
                    space.SchemaType = type;
                space.DefaultValue = "preserve";
                tempSchema.Items.Add(space);

                XmlSchemaAttributeGroup attributeGroup = new XmlSchemaAttributeGroup();
                attributeGroup.Name = "specialAttrs";
                XmlSchemaAttribute langRef = new XmlSchemaAttribute();
                langRef.RefName = new XmlQualifiedName("lang", XmlReservedNs.NsXml);
                attributeGroup.Attributes.Add(langRef);
                XmlSchemaAttribute spaceRef = new XmlSchemaAttribute();
                spaceRef.RefName = new XmlQualifiedName("space", XmlReservedNs.NsXml);
                attributeGroup.Attributes.Add(spaceRef);
                XmlSchemaAttribute baseRef = new XmlSchemaAttribute();
                baseRef.RefName = new XmlQualifiedName("base", XmlReservedNs.NsXml);
                attributeGroup.Attributes.Add(baseRef);
                tempSchema.Items.Add(attributeGroup);
                tempSchema.IsPreprocessed = true;
                tempSchema.CompileSchemaInSet(new NameTable(), null, null); //compile built-in schema

                Interlocked.CompareExchange<XmlSchema>(ref builtInSchemaForXmlNS, tempSchema, null);
            }
            return builtInSchemaForXmlNS; 
        }
		static XmlCompletionItemCollection GetAttributeValueCompletion(XmlSchemaSimpleTypeRestriction simpleTypeRestriction)
		{
			XmlCompletionItemCollection completionItems = new XmlCompletionItemCollection();
			
			foreach (XmlSchemaObject schemaObject in simpleTypeRestriction.Facets) {
				XmlSchemaEnumerationFacet enumFacet = schemaObject as XmlSchemaEnumerationFacet;
				if (enumFacet != null) {
					AddAttributeValue(completionItems, enumFacet.Value, enumFacet.Annotation);
				}
			}

			return completionItems;
		}
		XmlCompletionDataCollection GetAttributeValueCompletionData(XmlSchemaSimpleTypeRestriction simpleTypeRestriction)
		{
			XmlCompletionDataCollection data = new XmlCompletionDataCollection();
			
			foreach (XmlSchemaObject schemaObject in simpleTypeRestriction.Facets) {
				XmlSchemaEnumerationFacet enumFacet = schemaObject as XmlSchemaEnumerationFacet;
				if (enumFacet != null) {
					AddAttributeValue(data, enumFacet.Value, enumFacet.Annotation);
				}
			}

			return data;
		}
Beispiel #22
0
 private bool HasEnumFacets(XmlSchemaSimpleTypeRestriction restriction)
 {
     foreach (XmlSchemaFacet facet in restriction.Facets)
     {
         if (facet is XmlSchemaEnumerationFacet)
             return true;
     }
     return false;
 }
Beispiel #23
0
        private void CreateEnumeration(XmlSchemaSimpleTypeRestriction restriction, CodeTypeDeclaration codeType)
        {
            Logger.WriteLine("\tRestriction base type: " + restriction.BaseTypeName, LogLevel.Verbose);

            // Process facets
            foreach (XmlSchemaFacet facet in restriction.Facets)
            {
                int enumValue = -1;
                bool IsEnum = false;

                Logger.WriteLine("\t\tFacet Type: " + facet.GetType().ToString(), LogLevel.Verbose);
                Logger.WriteLine("\t\tFacet Value: " + facet.Value, LogLevel.Verbose);

                // If this is an Enum facet
                if (facet is XmlSchemaEnumerationFacet)
                {
                    IsEnum = true;
                    if (facet.Annotation != null)
                    {
                        foreach (XmlSchemaObject item in facet.Annotation.Items)
                        {
                            if (item is XmlSchemaAppInfo)
                            {
                                enumValue = Convert.ToInt32(((XmlSchemaAppInfo)item).Markup[0].InnerText);
                                break;
                            }
                            enumValue = -1;
                        }
                    }

                    // Validate the enum string. If the first char is numeric or any character is not Alpha Numeric throw
                    string enumTestValue = facet.Value.ToLower();
                    string enumFacet = "";
                    if (enumTestValue.Length < 64 && (enumTestValue[0] >= 'a' && facet.Value[0] <= 'z' || enumTestValue[0] == '_'))
                    {
                        for (int i = 0; i < enumTestValue.Length; ++i)
                        {
                            if ((enumTestValue[i] < '0' || (enumTestValue[i] > '9' && enumTestValue[i] < 'a') || enumTestValue[i] > 'z') && enumTestValue[i] != '_')
                                continue;
                            enumFacet += facet.Value[i];
                        }
                    }
                    else
                        throw new XmlException("Invalid enumeration value. " + facet.Value);
                    
                    // Add enum member to data contract
                    CodeMemberField codeMember = new CodeMemberField(enumFacet, enumFacet);

                    // Since valid XMLSchema enum values may contain values that cannot be used as real C# enum values a mechanism
                    // is required to map XmlSchema value names to real enum value names. If an XmlSchema enum value name cannot be
                    // used for a real enum value, a UserData dictionary entry is created containing the real facet value
                    // (the XmlSchema value name) that is used by serializers to map between the real enum and the XmlSchema enum.
                    if (enumFacet != facet.Value)
                    {
                        codeMember.UserData.Add(typeof(XmlSchemaEnumerationFacet), facet);
                    }
                    if (Convert.ToInt32(enumValue) >= 0)
                        codeMember.InitExpression = new CodePrimitiveExpression(enumValue);
                    CodeAttributeDeclaration fieldAttrDecl = new CodeAttributeDeclaration("EnumMember");
                    codeMember.CustomAttributes.Add(fieldAttrDecl);
                    codeMember.Attributes = MemberAttributes.Public;
                    codeType.Members.Add(codeMember);
                }
                else
                {
                    // Per WCF DataContract processing rules, length, minlength, maxlength,
                    // whitespace and pattern are are forbidden elements in a simple type enumeration
                    // If this is not an enumeration only restriction and simple type are supported
                    // all others are ignored.
                    Type facetType = facet.GetType();
                    if (IsEnum && (facetType == typeof(XmlSchemaLengthFacet) ||
                        facetType == typeof(XmlSchemaMinLengthFacet) ||
                        facetType == typeof(XmlSchemaMaxLengthFacet) ||
                        facetType == typeof(XmlSchemaWhiteSpaceFacet) ||
                        facetType == typeof(XmlSchemaPatternFacet)))
                    {
                        Logger.WriteLine("SimpleType facet " + facetType.ToString() +
                            " was ingnored in the enumeration.", LogLevel.Normal);
                        continue;
                    }
                    else
                    {
                        continue;
                    }
                }
            }
            return;
        }
Beispiel #24
0
		void ExportEnumSchema (XmlTypeMapping map)
		{
			if (IsMapExported (map)) return;
			SetMapExported (map);

			XmlSchema schema = GetSchema (map.XmlTypeNamespace);
			XmlSchemaSimpleType stype = new XmlSchemaSimpleType ();
			stype.Name = map.ElementName;
			schema.Items.Add (stype);

			XmlSchemaSimpleTypeRestriction rest = new XmlSchemaSimpleTypeRestriction ();
			rest.BaseTypeName = new XmlQualifiedName ("string",XmlSchema.Namespace);
			EnumMap emap = (EnumMap) map.ObjectMap;

			foreach (EnumMap.EnumMapMember emem in emap.Members)
			{
				XmlSchemaEnumerationFacet ef = new XmlSchemaEnumerationFacet ();
				ef.Value = emem.XmlName;
				rest.Facets.Add (ef);
			}

			if (emap.IsFlags) {
				XmlSchemaSimpleTypeList slist = new XmlSchemaSimpleTypeList ();
				XmlSchemaSimpleType restrictionType = new XmlSchemaSimpleType ();
				restrictionType.Content = rest;
				slist.ItemType = restrictionType;
				stype.Content = slist;
			} else {
				stype.Content = rest;
			}
		}
		void GetAttributeValueCompletionData (XmlCompletionDataList data, XmlSchemaSimpleTypeRestriction simpleTypeRestriction)
		{
			foreach (XmlSchemaObject schemaObject in simpleTypeRestriction.Facets) {
				var enumFacet = schemaObject as XmlSchemaEnumerationFacet;
				if (enumFacet != null)
					data.AddAttributeValue (enumFacet.Value, enumFacet.Annotation);
			}
		}
 private XmlSchemaType ExportEnumMapping(EnumMapping mapping, string ns)
 {
     if (!mapping.IncludeInSchema)
     {
         throw new InvalidOperationException(Res.GetString("XmlCannotIncludeInSchema", new object[] { mapping.TypeDesc.Name }));
     }
     XmlSchemaSimpleType type = (XmlSchemaSimpleType) this.types[mapping];
     if (type == null)
     {
         this.CheckForDuplicateType(mapping, mapping.Namespace);
         type = new XmlSchemaSimpleType {
             Name = mapping.TypeName
         };
         if (!mapping.IsAnonymousType)
         {
             this.types.Add(mapping, type);
             this.AddSchemaItem(type, mapping.Namespace, ns);
         }
         XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction {
             BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
         };
         for (int i = 0; i < mapping.Constants.Length; i++)
         {
             ConstantMapping mapping2 = mapping.Constants[i];
             XmlSchemaEnumerationFacet item = new XmlSchemaEnumerationFacet {
                 Value = mapping2.XmlName
             };
             restriction.Facets.Add(item);
         }
         if (!mapping.IsFlags)
         {
             type.Content = restriction;
         }
         else
         {
             XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList();
             XmlSchemaSimpleType type2 = new XmlSchemaSimpleType {
                 Content = restriction
             };
             list.ItemType = type2;
             type.Content = list;
         }
     }
     if (!mapping.IsAnonymousType)
     {
         this.AddSchemaImport(mapping.Namespace, ns);
     }
     return type;
 }
        private List<AttributeRule> GetRules(XmlSchemaSimpleTypeRestriction restriction)
        {
            List<AttributeRule> rules = new List<AttributeRule>();

            List<string> enumValues = null;
            foreach (XmlSchemaFacet facet in restriction.Facets)
            {
                XmlSchemaEnumerationFacet enumFacet = facet as XmlSchemaEnumerationFacet;
                if (enumFacet != null)
                {
                    if (enumValues == null)
                        enumValues = new List<string>();
                    enumValues.Add(enumFacet.Value);
                    continue;
                }

                XmlSchemaMinExclusiveFacet minExclusiveFacet = facet as XmlSchemaMinExclusiveFacet;
                if (minExclusiveFacet != null)
                {
                    double minExclusive;
                    if (Double.TryParse(minExclusiveFacet.Value, out minExclusive))
                        rules.Add(new NumericMinRule(minExclusive, false));
                    continue;
                }

                XmlSchemaMinInclusiveFacet minInclusiveFacet = facet as XmlSchemaMinInclusiveFacet;
                if (minInclusiveFacet != null)
                {
                    double minInclusive;
                    if (Double.TryParse(minInclusiveFacet.Value, out minInclusive))
                        rules.Add(new NumericMinRule(minInclusive, true));
                    continue;
                }

                XmlSchemaMaxExclusiveFacet maxExclusiveFacet = facet as XmlSchemaMaxExclusiveFacet;
                if (maxExclusiveFacet != null)
                {
                    double maxExclusive;
                    if (Double.TryParse(maxExclusiveFacet.Value, out maxExclusive))
                        rules.Add(new NumericMaxRule(maxExclusive, false));
                    continue;
                }

                XmlSchemaMaxInclusiveFacet maxInclusiveFacet = facet as XmlSchemaMaxInclusiveFacet;
                if (maxInclusiveFacet != null)
                {
                    double maxInclusive;
                    if (Double.TryParse(maxInclusiveFacet.Value, out maxInclusive))
                        rules.Add(new NumericMaxRule(maxInclusive, true));
                    continue;
                }
            }

            if (enumValues != null && enumValues.Count > 0)
                rules.Add(new StringEnumRule(enumValues.ToArray()));

            return rules;
        }
Beispiel #28
0
 private void SetContainer(State state, object container) {
     switch (state) {
         case State.Root:
             break;
         case State.Schema:
             break;
         case State.Annotation:
             this.annotation = (XmlSchemaAnnotation)container;
             break;
         case State.Include:
             this.include = (XmlSchemaInclude)container;
             break;
         case State.Import:
             this.import = (XmlSchemaImport)container;
             break;
         case State.Element:
             this.element = (XmlSchemaElement)container;
             break;
         case State.Attribute:
             this.attribute = (XmlSchemaAttribute)container;
             break;
         case State.AttributeGroup:
             this.attributeGroup = (XmlSchemaAttributeGroup)container;
             break;
         case State.AttributeGroupRef:
             this.attributeGroupRef = (XmlSchemaAttributeGroupRef)container;
             break;
         case State.AnyAttribute:
             this.anyAttribute = (XmlSchemaAnyAttribute)container;
             break;
         case State.Group:
             this.group = (XmlSchemaGroup)container;
             break;
         case State.GroupRef:
             this.groupRef = (XmlSchemaGroupRef)container;
             break;
         case State.All:
             this.all = (XmlSchemaAll)container;
             break;
         case State.Choice:
             this.choice = (XmlSchemaChoice)container;
             break;
         case State.Sequence:
             this.sequence = (XmlSchemaSequence)container;
             break;
         case State.Any:
             this.anyElement = (XmlSchemaAny)container;
             break;
         case State.Notation:
             this.notation = (XmlSchemaNotation)container;
             break;
         case State.SimpleType:
             this.simpleType = (XmlSchemaSimpleType)container;
             break;
         case State.ComplexType:
             this.complexType = (XmlSchemaComplexType)container;
             break;
         case State.ComplexContent:
             this.complexContent = (XmlSchemaComplexContent)container;
             break;
         case State.ComplexContentExtension:
             this.complexContentExtension = (XmlSchemaComplexContentExtension)container;
             break;
         case State.ComplexContentRestriction:
             this.complexContentRestriction = (XmlSchemaComplexContentRestriction)container;
             break;
         case State.SimpleContent:
             this.simpleContent = (XmlSchemaSimpleContent)container;
             break;
         case State.SimpleContentExtension:
             this.simpleContentExtension = (XmlSchemaSimpleContentExtension)container;
             break;
         case State.SimpleContentRestriction:
             this.simpleContentRestriction = (XmlSchemaSimpleContentRestriction)container;
             break;
         case State.SimpleTypeUnion:
             this.simpleTypeUnion = (XmlSchemaSimpleTypeUnion)container;
             break;
         case State.SimpleTypeList:
             this.simpleTypeList = (XmlSchemaSimpleTypeList)container;
             break;
         case State.SimpleTypeRestriction:
             this.simpleTypeRestriction = (XmlSchemaSimpleTypeRestriction)container;
             break;
         case State.Unique:
         case State.Key:
         case State.KeyRef:
             this.identityConstraint = (XmlSchemaIdentityConstraint)container;
             break;
         case State.Selector:
         case State.Field:
             this.xpath = (XmlSchemaXPath)container;
             break;
         case State.MinExclusive:
         case State.MinInclusive:
         case State.MaxExclusive:
         case State.MaxInclusive:
         case State.TotalDigits:
         case State.FractionDigits:
         case State.Length:
         case State.MinLength:
         case State.MaxLength:
         case State.Enumeration:
         case State.Pattern:
         case State.WhiteSpace:
             this.facet = (XmlSchemaFacet)container;
             break;
         case State.AppInfo:
             this.appInfo = (XmlSchemaAppInfo)container;
             break;
         case State.Documentation:
             this.documentation = (XmlSchemaDocumentation)container;
             break;
         case State.Redefine:
             this.redefine = (XmlSchemaRedefine)container;
             break;
         default:
             Debug.Assert(false, "State is " + state);
             break;
     }
 }
Beispiel #29
0
		/// <summary>
		/// Creates a simple type that matches a regex
		/// </summary>
		/// <param name="Name">Name of the new type</param>
		/// <param name="Pattern">Regex pattern to match</param>
		/// <returns>A simple type which will match the given pattern</returns>
		static XmlSchemaSimpleType CreateSimpleTypeFromRegex(string Name, string Pattern)
		{
			XmlSchemaPatternFacet PatternFacet = new XmlSchemaPatternFacet();
			PatternFacet.Value = Pattern;

			XmlSchemaSimpleTypeRestriction Restriction = new XmlSchemaSimpleTypeRestriction();
			Restriction.BaseTypeName = StringTypeName;
			Restriction.Facets.Add(PatternFacet);

			XmlSchemaSimpleType SimpleType = new XmlSchemaSimpleType();
			SimpleType.Name = Name;
			SimpleType.Content = Restriction;
			return SimpleType;
		}
 void Write15_XmlSchemaSimpleTypeRestriction(XmlSchemaSimpleTypeRestriction o) {
     if ((object)o == null) return;
     WriteStartElement("restriction");
     WriteAttribute(@"id", @"", ((System.String)o.@Id));
     WriteAttributes((XmlAttribute[])o.@UnhandledAttributes, o);
     if ([email protected]){
         WriteAttribute(@"base", @"", o.@BaseTypeName);
     }
     Write5_XmlSchemaAnnotation((XmlSchemaAnnotation)o.@Annotation);
     Write9_XmlSchemaSimpleType((XmlSchemaSimpleType)o.@BaseType);
     WriteFacets(o.Facets);
     WriteEndElement();
 }
Beispiel #31
0
		/// <summary>
		/// Create a schema type for the given enum.
		/// </summary>
		/// <param name="Name">Name for the new type</param>
		/// <param name="Type">CLR type information to create a schema type for</param>
		static XmlSchemaType CreateEnumType(string Name, Type Type)
		{
			XmlSchemaSimpleTypeRestriction Restriction = new XmlSchemaSimpleTypeRestriction();
			Restriction.BaseTypeName = StringTypeName;

			foreach(string EnumName in Enum.GetNames(Type))
			{
				XmlSchemaEnumerationFacet Facet = new XmlSchemaEnumerationFacet();
				Facet.Value = EnumName;
				Restriction.Facets.Add(Facet);
			}

			XmlSchemaSimpleType SchemaType = new XmlSchemaSimpleType();
			SchemaType.Name = Name;
			SchemaType.Content = Restriction;
			return SchemaType;
		}
Beispiel #32
0
        /// <summary>
        /// Gets the enum schema.
        /// </summary>
        /// <param name="t">The t.</param>
        /// <returns>schema info</returns>
        public static SchemaInfo GetEnumSchema(this Type t)
        {
            if (!t.IsEnum)
            {
                return null;
            }

            var schemaInfo = new SchemaInfo();
            var classType = new XmlSchemaSimpleType
                                {
                                    Name = t.Name
                                };

            var attribData = t.GetCustomAttributeDataOfType(typeof(DataContractAttribute));
            if (attribData.NamedArguments != null && attribData.NamedArguments.Count > 0)
            {
                foreach (var p1 in attribData.NamedArguments)
                {
                    switch (p1.MemberInfo.Name)
                    {
                        case "Namespace":
                            schemaInfo.Schema.TargetNamespace = p1.TypedValue.Value as string;
                            break;
                    }
                }
            }

            var content = new XmlSchemaSimpleTypeRestriction();
            classType.Content = content;

            content.BaseTypeName = typeof(string).XmlName();
            foreach (var e in t.GetEnumNames())
            {
                content.Facets.Add(new XmlSchemaEnumerationFacet
                                       {
                                           Value = e
                                       });
            }

            schemaInfo.Schema.Items.Add(classType);
            return schemaInfo;
        }
        internal static XmlSchemaSimpleType Read(XmlSchemaReader reader, ValidationEventHandler h)
        {
            XmlSchemaSimpleType xmlSchemaSimpleType = new XmlSchemaSimpleType();

            reader.MoveToElement();
            if (reader.NamespaceURI != "http://www.w3.org/2001/XMLSchema" || reader.LocalName != "simpleType")
            {
                XmlSchemaObject.error(h, "Should not happen :1: XmlSchemaGroup.Read, name=" + reader.Name, null);
                reader.Skip();
                return(null);
            }
            xmlSchemaSimpleType.LineNumber   = reader.LineNumber;
            xmlSchemaSimpleType.LinePosition = reader.LinePosition;
            xmlSchemaSimpleType.SourceUri    = reader.BaseURI;
            while (reader.MoveToNextAttribute())
            {
                if (reader.Name == "final")
                {
                    Exception ex;
                    xmlSchemaSimpleType.Final = XmlSchemaUtil.ReadDerivationAttribute(reader, out ex, "final", XmlSchemaUtil.FinalAllowed);
                    if (ex != null)
                    {
                        XmlSchemaObject.error(h, "some invalid values not a valid value for final", ex);
                    }
                }
                else if (reader.Name == "id")
                {
                    xmlSchemaSimpleType.Id = reader.Value;
                }
                else if (reader.Name == "name")
                {
                    xmlSchemaSimpleType.Name = reader.Value;
                }
                else if ((reader.NamespaceURI == string.Empty && reader.Name != "xmlns") || reader.NamespaceURI == "http://www.w3.org/2001/XMLSchema")
                {
                    XmlSchemaObject.error(h, reader.Name + " is not a valid attribute for simpleType", null);
                }
                else
                {
                    XmlSchemaUtil.ReadUnhandledAttribute(reader, xmlSchemaSimpleType);
                }
            }
            reader.MoveToElement();
            if (reader.IsEmptyElement)
            {
                return(xmlSchemaSimpleType);
            }
            int num = 1;

            while (reader.ReadNextElement())
            {
                if (reader.NodeType == XmlNodeType.EndElement)
                {
                    if (reader.LocalName != "simpleType")
                    {
                        XmlSchemaObject.error(h, "Should not happen :2: XmlSchemaSimpleType.Read, name=" + reader.Name, null);
                    }
                    break;
                }
                if (num <= 1 && reader.LocalName == "annotation")
                {
                    num = 2;
                    XmlSchemaAnnotation xmlSchemaAnnotation = XmlSchemaAnnotation.Read(reader, h);
                    if (xmlSchemaAnnotation != null)
                    {
                        xmlSchemaSimpleType.Annotation = xmlSchemaAnnotation;
                    }
                }
                else
                {
                    if (num <= 2)
                    {
                        if (reader.LocalName == "restriction")
                        {
                            num = 3;
                            XmlSchemaSimpleTypeRestriction xmlSchemaSimpleTypeRestriction = XmlSchemaSimpleTypeRestriction.Read(reader, h);
                            if (xmlSchemaSimpleTypeRestriction != null)
                            {
                                xmlSchemaSimpleType.content = xmlSchemaSimpleTypeRestriction;
                            }
                            continue;
                        }
                        if (reader.LocalName == "list")
                        {
                            num = 3;
                            XmlSchemaSimpleTypeList xmlSchemaSimpleTypeList = XmlSchemaSimpleTypeList.Read(reader, h);
                            if (xmlSchemaSimpleTypeList != null)
                            {
                                xmlSchemaSimpleType.content = xmlSchemaSimpleTypeList;
                            }
                            continue;
                        }
                        if (reader.LocalName == "union")
                        {
                            num = 3;
                            XmlSchemaSimpleTypeUnion xmlSchemaSimpleTypeUnion = XmlSchemaSimpleTypeUnion.Read(reader, h);
                            if (xmlSchemaSimpleTypeUnion != null)
                            {
                                xmlSchemaSimpleType.content = xmlSchemaSimpleTypeUnion;
                            }
                            continue;
                        }
                    }
                    reader.RaiseInvalidElementError();
                }
            }
            return(xmlSchemaSimpleType);
        }