public void TestAttributeWithEnumeration() { CreateAndHandleElementWithoutValue("foo"); CreateAndHandleAttributeWithoutValue("bar"); XmlSchemaEnumerationFacet enumeration = new XmlSchemaEnumerationFacet(); enumeration.Value = "baz"; handler.Enumeration(enumeration); enumeration.Value = "oof"; handler.Enumeration(enumeration); handler.EndAttribute(); Assert.AreEqual("baz", GetResultsRoot().Item(0).Attributes.Item(0).Value, "Instance generator returned an incorrect element value"); }
protected internal override void Write(XmlSchemaObject obj) { var simpleType = (XmlSchemaSimpleType) obj; var restriction = new XmlSchemaSimpleTypeRestriction(); foreach (var item in Items) { var facet = new XmlSchemaEnumerationFacet(); item.Write(facet); restriction.Facets.Add(facet); } simpleType.Content = restriction; base.Write(obj); }
/// <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; }
private static XmlSchema GetBuildInSchema(XmlNameTable nt) { XmlSchema schema = new XmlSchema(); schema.TargetNamespace = XmlReservedNs.NsXml; XmlSchemaAttribute lang = new XmlSchemaAttribute(); lang.Name = "lang"; lang.SchemaTypeName = new XmlQualifiedName("language", nt.Add(XmlReservedNs.NsXsd)); schema.Items.Add(lang); XmlSchemaAttribute space = new XmlSchemaAttribute(); space.Name = "space"; XmlSchemaSimpleType type = new XmlSchemaSimpleType(); XmlSchemaSimpleTypeRestriction r = new XmlSchemaSimpleTypeRestriction(); r.BaseTypeName = new XmlQualifiedName("NCName", nt.Add(XmlReservedNs.NsXsd)); 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"; schema.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); schema.Items.Add(attributeGroup); return schema; }
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; }
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; }
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; } }
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; }
protected static XmlSchemaElement GetTimeSchema(string elementName) { XmlSchemaSimpleType simpleType = new XmlSchemaSimpleType(); XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction(); restriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); XmlSchemaEnumerationFacet enumeration = new XmlSchemaEnumerationFacet(); enumeration.Value = "minutes"; restriction.Facets.Add(enumeration); enumeration = new XmlSchemaEnumerationFacet(); enumeration.Value = "hours"; restriction.Facets.Add(enumeration); enumeration = new XmlSchemaEnumerationFacet(); enumeration.Value = "weeks"; restriction.Facets.Add(enumeration); enumeration = new XmlSchemaEnumerationFacet(); enumeration.Value = "days"; restriction.Facets.Add(enumeration); enumeration = new XmlSchemaEnumerationFacet(); enumeration.Value = "months"; restriction.Facets.Add(enumeration); enumeration = new XmlSchemaEnumerationFacet(); enumeration.Value = "years"; restriction.Facets.Add(enumeration); simpleType.Content = restriction; XmlSchemaSimpleType languageType = new XmlSchemaSimpleType(); XmlSchemaSimpleTypeRestriction languageEnum = new XmlSchemaSimpleTypeRestriction(); languageEnum.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); enumeration = new XmlSchemaEnumerationFacet(); enumeration.Value = "dicom"; languageEnum.Facets.Add(enumeration); languageType.Content = languageEnum; XmlSchemaComplexType type = new XmlSchemaComplexType(); XmlSchemaAttribute attrib = new XmlSchemaAttribute(); attrib.Name = "time"; attrib.Use = XmlSchemaUse.Required; attrib.SchemaTypeName = new XmlQualifiedName("double", "http://www.w3.org/2001/XMLSchema"); type.Attributes.Add(attrib); attrib = new XmlSchemaAttribute(); attrib.Name = "unit"; attrib.Use = XmlSchemaUse.Required; attrib.SchemaType = simpleType; type.Attributes.Add(attrib); attrib = new XmlSchemaAttribute(); attrib.Name = "refValue"; attrib.Use = XmlSchemaUse.Optional; attrib.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); type.Attributes.Add(attrib); attrib = new XmlSchemaAttribute(); attrib.Name = "expressionLanguage"; attrib.Use = XmlSchemaUse.Optional; attrib.SchemaType = languageType; type.Attributes.Add(attrib); XmlSchemaElement element = new XmlSchemaElement(); element.Name = elementName; element.SchemaType = type; return element; }
/// <summary> /// For a given enum type, generate a simple schema type. /// </summary> /// <param name="t">A reflected enum data type.</param> /// <param name="schema">A Schema object used to store the new simple type.</param> /// <param name="schemaNamespace">A string containing the Schemas targetnamespace.</param> /// <param name="serviceDesc"></param> void ProcessEnumMemberType(Type t, XmlSchema schema, string schemaNamespace, ServiceDescription serviceDesc) { // Create the type XmlSchemaSimpleType simpleType = new XmlSchemaSimpleType(); simpleType.Name = t.Name + "Type"; XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction(); restriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); simpleType.Content = restriction; // Create an element used to reference the type XmlSchemaElement element = new XmlSchemaElement(); element.Name = t.Name; element.SchemaTypeName = new XmlQualifiedName(simpleType.Name, schemaNamespace); // Iterate list of enum members and create schema elements and types MemberInfo[] members = GetDataMembers(t); foreach (MemberInfo member in members) { XmlSchemaEnumerationFacet enumFacet = new XmlSchemaEnumerationFacet(); enumFacet.Value = member.Name; restriction.Facets.Add(enumFacet); } AddSchemaItem(schema, element); AddSchemaItem(schema, simpleType); }
private void ExportEnumDataContract(EnumDataContract enumDataContract, XmlSchema schema) { XmlSchemaSimpleType item = new XmlSchemaSimpleType { Name = enumDataContract.StableName.Name }; XmlElement element = (enumDataContract.BaseContractName == DefaultEnumBaseTypeName) ? null : this.ExportActualType(enumDataContract.BaseContractName); item.Annotation = GetSchemaAnnotation(new System.Xml.XmlNode[] { element, this.ExportSurrogateData(enumDataContract) }); schema.Items.Add(item); XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction { BaseTypeName = StringQualifiedName }; SchemaHelper.AddSchemaImport(enumDataContract.BaseContractName.Namespace, schema); if (enumDataContract.Values != null) { for (int i = 0; i < enumDataContract.Values.Count; i++) { XmlSchemaEnumerationFacet facet = new XmlSchemaEnumerationFacet { Value = enumDataContract.Members[i].Name }; if (enumDataContract.Values[i] != GetDefaultEnumValue(enumDataContract.IsFlags, i)) { facet.Annotation = this.GetSchemaAnnotation(EnumerationValueAnnotationName, enumDataContract.GetStringFromEnumValue(enumDataContract.Values[i]), schema); } restriction.Facets.Add(facet); } } if (enumDataContract.IsFlags) { XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList(); XmlSchemaSimpleType type2 = new XmlSchemaSimpleType { Content = restriction }; list.ItemType = type2; item.Content = list; } else { item.Content = restriction; } }
private void Write26_XmlSchemaEnumerationFacet(string n, string ns, XmlSchemaEnumerationFacet o, bool isNullable, bool needType) { if (o == null) { if (isNullable) { base.WriteNullTagLiteral(n, ns); } } else { if (!needType && !(o.GetType() == typeof(XmlSchemaEnumerationFacet))) { throw base.CreateUnknownTypeException(o); } base.EscapeName = false; base.WriteStartElement(n, ns, o, false, o.Namespaces); if (needType) { base.WriteXsiType("XmlSchemaEnumerationFacet", "http://www.w3.org/2001/XMLSchema"); } base.WriteAttribute("id", "", o.Id); XmlAttribute[] unhandledAttributes = o.UnhandledAttributes; if (unhandledAttributes != null) { for (int i = 0; i < unhandledAttributes.Length; i++) { XmlAttribute node = unhandledAttributes[i]; base.WriteXmlAttribute(node, o); } } base.WriteAttribute("value", "", o.Value); if (o.IsFixed) { base.WriteAttribute("fixed", "", XmlConvert.ToString(o.IsFixed)); } this.Write11_XmlSchemaAnnotation("annotation", "http://www.w3.org/2001/XMLSchema", o.Annotation, false, false); base.WriteEndElement(o); } }
private XmlQualifiedName ExportEnumMapping(EnumMapping mapping, string ns) { if (((XmlSchemaSimpleType) this.types[mapping]) == null) { this.CheckForDuplicateType(mapping.TypeName, mapping.Namespace); XmlSchemaSimpleType type = new XmlSchemaSimpleType { Name = mapping.TypeName }; 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; } } else { this.AddSchemaImport(mapping.Namespace, ns); } return new XmlQualifiedName(mapping.TypeName, mapping.Namespace); }
//<enumeration // id = ID // value = anySimpleType // {any attributes with non-schema namespace . . .}> // Content: (annotation?) //</enumeration> internal static XmlSchemaEnumerationFacet Read(XmlSchemaReader reader, ValidationEventHandler h) { XmlSchemaEnumerationFacet enumeration = new XmlSchemaEnumerationFacet(); reader.MoveToElement(); if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname) { error(h,"Should not happen :1: XmlSchemaEnumerationFacet.Read, name="+reader.Name,null); reader.Skip(); return null; } enumeration.LineNumber = reader.LineNumber; enumeration.LinePosition = reader.LinePosition; enumeration.SourceUri = reader.BaseURI; while(reader.MoveToNextAttribute()) { if(reader.Name == "id") { enumeration.Id = reader.Value; } else if(reader.Name == "value") { enumeration.Value = reader.Value; } else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace) { error(h,reader.Name + " is not a valid attribute for "+xmlname,null); } else { XmlSchemaUtil.ReadUnhandledAttribute(reader,enumeration); } } reader.MoveToElement(); if(reader.IsEmptyElement) return enumeration; // Content: (annotation?) int level = 1; while(reader.ReadNextElement()) { if(reader.NodeType == XmlNodeType.EndElement) { if(reader.LocalName != xmlname) error(h,"Should not happen :2: XmlSchemaEnumerationFacet.Read, name="+reader.Name,null); break; } if(level <= 1 && reader.LocalName == "annotation") { level = 2; //Only one annotation XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h); if(annotation != null) enumeration.Annotation = annotation; continue; } reader.RaiseInvalidElementError(); } return enumeration; }
XmlSchemaSimpleTypeRestriction CreateEnumMembers (Type type, bool expectAttribute) { var r = new XmlSchemaSimpleTypeRestriction () { BaseTypeName = GetSchemaTypeName (typeof (string)) }; foreach (var mi in type.GetFields (BindingFlags.Public | BindingFlags.Static)) { var ema = expectAttribute ? mi.GetCustomAttribute<EnumMemberAttribute> (false) : null; if (expectAttribute && ema == null) continue; var xe = new XmlSchemaEnumerationFacet () { Value = ema != null && ema.Value != null ? ema.Value : mi.Name }; r.Facets.Add (xe); } return r; }
XmlQualifiedName ExportEnumMapping(EnumMapping mapping, string ns) { XmlSchemaSimpleType dataType = (XmlSchemaSimpleType)types[mapping]; if (dataType == null) { CheckForDuplicateType(mapping.TypeName, mapping.Namespace); dataType = new XmlSchemaSimpleType(); dataType.Name = mapping.TypeName; 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; } } else { AddSchemaImport(mapping.Namespace, ns); } return new XmlQualifiedName(mapping.TypeName, mapping.Namespace); }
/// <summary> /// Generate the VOC.xsd file /// </summary> private void GenerateVocabulary() { System.Diagnostics.Trace.WriteLine("Generating VOC.XSD...", "debug"); ClassRepository classRep = hostContext.Data["SourceCR"] as ClassRepository; // Enumerations var enumerations = from clsRep in classRep where clsRep.Value is Enumeration && (clsRep.Value as Enumeration).Literals.Count > 0 select clsRep.Value as Enumeration; XmlSchema schema = new XmlSchema(); //schema.TargetNamespace = targetNs; // Enumerations foreach (var eType in enumerations) { #region Check for members bool used = false; // Does this enumeration have any references? foreach (KeyValuePair<String, Feature> kv in eType.MemberOf) if (kv.Value is Class) foreach (ClassContent cc in (kv.Value as Class).Content) used |= cc is Property && (cc as Property).SupplierDomain == eType; #endregion Check for Members // Is this enumeration used? if (used) { // Generate the type XmlSchemaSimpleType enumerationType = new XmlSchemaSimpleType(); enumerationType.Name = eType.Name; // Populate documentation if (!Documentation.IsEmpty(eType.Documentation)) { XmlSchemaDocumentation documentation = new XmlSchemaDocumentation(); XmlDocument documentationMarkup = new XmlDocument(); documentationMarkup.LoadXml("<div xmlns=\"http://www.w3.org/1999/xhtml\">" + eType.Documentation.ToString() + "</div>"); documentation.Markup = new List<XmlNode>(documentationMarkup.ChildNodes.Cast<XmlNode>()).ToArray(); enumerationType.Annotation = new XmlSchemaAnnotation(); enumerationType.Annotation.Items.Add(documentation); } // Populate the simple type XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction() { BaseTypeName = new XmlQualifiedName("token", "http://www.w3.org/2001/XMLSchema") }; // Create the restriction facets foreach (var literal in eType.Literals) { XmlSchemaEnumerationFacet facet = new XmlSchemaEnumerationFacet() { Value = literal.Name }; // Populate documentation if (!Documentation.IsEmpty(literal.Documentation)) { try { XmlSchemaDocumentation facetDocumentation = new XmlSchemaDocumentation(); XmlDocument facetDocumentationMarkup = new XmlDocument(); facetDocumentationMarkup.LoadXml("<div xmlns=\"http://www.w3.org/1999/xhtml\">" + literal.Documentation.ToString() + "</div>"); facetDocumentation.Markup = new List<XmlNode>(facetDocumentationMarkup.ChildNodes.Cast<XmlNode>()).ToArray(); facet.Annotation = new XmlSchemaAnnotation(); facet.Annotation.Items.Add(facetDocumentation); } catch (Exception e) { if (hostContext.Mode == Pipeline.OperationModeType.Quirks) System.Diagnostics.Trace.WriteLine(String.Format("error {0} is being ignored", e.Message), "quirks"); else throw; } } restriction.Facets.Add(facet); } enumerationType.Content = restriction; // Now, add the type to the schema schema.Items.Add(enumerationType); } } TextWriter tw = null; try { tw = File.CreateText(Path.Combine(outputDir, "voc.xsd")); schema.Write(tw); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message, "error"); } finally { if (tw != null) tw.Close(); } }
protected static XmlSchemaElement GetBaseSchema(string elementName) { XmlSchemaSimpleType languageType = new XmlSchemaSimpleType(); XmlSchemaSimpleTypeRestriction languageEnum = new XmlSchemaSimpleTypeRestriction(); languageEnum.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); XmlSchemaEnumerationFacet enumeration = new XmlSchemaEnumerationFacet(); enumeration.Value = "dicom"; languageEnum.Facets.Add(enumeration); languageType.Content = languageEnum; XmlSchemaComplexType type = new XmlSchemaComplexType(); XmlSchemaAttribute attrib = new XmlSchemaAttribute(); attrib.Name = "refValue"; attrib.Use = XmlSchemaUse.Optional; attrib.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); type.Attributes.Add(attrib); attrib = new XmlSchemaAttribute(); attrib.Name = "expressionLanguage"; attrib.Use = XmlSchemaUse.Optional; attrib.SchemaType = languageType; type.Attributes.Add(attrib); XmlSchemaElement element = new XmlSchemaElement(); element.Name = elementName; element.SchemaType = type; return element; }
protected virtual void Visit(XmlSchemaEnumerationFacet facet) { }
void ExportEnumDataContract(EnumDataContract enumDataContract, XmlSchema schema) { XmlSchemaSimpleType type = new XmlSchemaSimpleType(); type.Name = enumDataContract.StableName.Name; XmlElement actualTypeElement = (enumDataContract.BaseContractName == DefaultEnumBaseTypeName) ? null : ExportActualType(enumDataContract.BaseContractName); type.Annotation = GetSchemaAnnotation(actualTypeElement, ExportSurrogateData(enumDataContract)); schema.Items.Add(type); XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction(); restriction.BaseTypeName = StringQualifiedName; SchemaHelper.AddSchemaImport(enumDataContract.BaseContractName.Namespace, schema); if (enumDataContract.Values != null) { for (int i = 0; i < enumDataContract.Values.Count; i++) { XmlSchemaEnumerationFacet facet = new XmlSchemaEnumerationFacet(); facet.Value = enumDataContract.Members[i].Name; if (enumDataContract.Values[i] != GetDefaultEnumValue(enumDataContract.IsFlags, i)) facet.Annotation = GetSchemaAnnotation(EnumerationValueAnnotationName, enumDataContract.GetStringFromEnumValue(enumDataContract.Values[i]), schema); restriction.Facets.Add(facet); } } if (enumDataContract.IsFlags) { XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList(); XmlSchemaSimpleType anonymousType = new XmlSchemaSimpleType(); anonymousType.Content = restriction; list.ItemType = anonymousType; type.Content = list; } else type.Content = restriction; }
public void TestEnumeratedAttributeWithUnmatchedDefaultValue() { // TODO: Decide whether this condition should actually fail fast / throw. CreateAndHandleElementWithoutValue("foo"); CreateAndHandleAttribute("bar", "rab", null); XmlSchemaEnumerationFacet enumeration = new XmlSchemaEnumerationFacet(); enumeration.Value = "baz"; handler.Enumeration(enumeration); enumeration.Value = "oof"; handler.Enumeration(enumeration); handler.EndAttribute(); handler.EndElement(); Assert.AreEqual("baz", GetResultsRoot().Item(0).Attributes.Item(0).Value, "Instance generator returned an incorrect attribute value"); }
protected override void Visit(XmlSchemaEnumerationFacet facet) { AddLeaf(SimpleTypeStructureNodeType.FacetEnumeration, facet); }
public void TestEnumeratedElementSiblings() { CreateAndHandleElementWithoutValue("root"); CreateAndHandleElementWithoutValue("foo"); XmlSchemaEnumerationFacet enumeration = new XmlSchemaEnumerationFacet(); enumeration.Value = "bar"; handler.Enumeration(enumeration); enumeration.Value = "baz"; handler.Enumeration(enumeration); handler.EndElement(); CreateAndHandleElementWithoutValue("oof"); enumeration = new XmlSchemaEnumerationFacet(); enumeration.Value = "rab"; handler.Enumeration(enumeration); enumeration.Value = "zab"; handler.Enumeration(enumeration); handler.EndElement(); Assert.AreEqual("bar", GetResultsRoot().Item(0).FirstChild.InnerXml, "Instance generator returned an incorrect element value"); Assert.AreEqual("rab", GetResultsRoot().Item(0).FirstChild.NextSibling.InnerXml, "Instance generator returned an incorrect element value"); }
/// <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; }
public void TestEnumeratedElementWithMatchedFixedValue() { CreateAndHandleElement("foo", null, "baz"); XmlSchemaEnumerationFacet enumeration = new XmlSchemaEnumerationFacet(); enumeration.Value = "bar"; handler.Enumeration(enumeration); enumeration.Value = "baz"; handler.Enumeration(enumeration); handler.EndElement(); Assert.AreEqual("baz", GetResultsRoot().Item(0).InnerXml, "Instance generator returned an incorrect element value"); }
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; }
public void TestEnumeratedElementWithUnmatchedFixedValue() { // TODO: Decide whether this condition should actually fail fast / throw. CreateAndHandleElement("foo", null, "oof"); XmlSchemaEnumerationFacet enumeration = new XmlSchemaEnumerationFacet(); enumeration.Value = "bar"; handler.Enumeration(enumeration); enumeration.Value = "baz"; handler.Enumeration(enumeration); handler.EndElement(); Assert.AreEqual("oof", GetResultsRoot().Item(0).InnerXml, "Instance generator returned an incorrect element value"); }
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; }
internal static XmlSchemaEnumerationFacet Read(XmlSchemaReader reader, ValidationEventHandler h) { XmlSchemaEnumerationFacet xmlSchemaEnumerationFacet = new XmlSchemaEnumerationFacet(); reader.MoveToElement(); if (reader.NamespaceURI != "http://www.w3.org/2001/XMLSchema" || reader.LocalName != "enumeration") { XmlSchemaObject.error(h, "Should not happen :1: XmlSchemaEnumerationFacet.Read, name=" + reader.Name, null); reader.Skip(); return(null); } xmlSchemaEnumerationFacet.LineNumber = reader.LineNumber; xmlSchemaEnumerationFacet.LinePosition = reader.LinePosition; xmlSchemaEnumerationFacet.SourceUri = reader.BaseURI; while (reader.MoveToNextAttribute()) { if (reader.Name == "id") { xmlSchemaEnumerationFacet.Id = reader.Value; } else if (reader.Name == "value") { xmlSchemaEnumerationFacet.Value = 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 enumeration", null); } else { XmlSchemaUtil.ReadUnhandledAttribute(reader, xmlSchemaEnumerationFacet); } } reader.MoveToElement(); if (reader.IsEmptyElement) { return(xmlSchemaEnumerationFacet); } int num = 1; while (reader.ReadNextElement()) { if (reader.NodeType == XmlNodeType.EndElement) { if (reader.LocalName != "enumeration") { XmlSchemaObject.error(h, "Should not happen :2: XmlSchemaEnumerationFacet.Read, name=" + reader.Name, null); } break; } if (num <= 1 && reader.LocalName == "annotation") { num = 2; XmlSchemaAnnotation xmlSchemaAnnotation = XmlSchemaAnnotation.Read(reader, h); if (xmlSchemaAnnotation != null) { xmlSchemaEnumerationFacet.Annotation = xmlSchemaAnnotation; } } else { reader.RaiseInvalidElementError(); } } return(xmlSchemaEnumerationFacet); }