XmlSchemaAttribute MakeAttribute(string name, ParameterAttribute paramAttr) { name = paramAttr.name; var attr = new XmlSchemaAttribute(); attr.Name = name; attr.Annotate(paramAttr.description); if (paramAttr.type.IsEnum) { attr.SchemaType = GetEnumType(paramAttr.type); } else { attr.SchemaTypeName = GetSchemaType(paramAttr.type); } if (!paramAttr.required) { attr.Use = XmlSchemaUse.Optional; if (!string.IsNullOrEmpty(paramAttr.defaultValue)) { var valStr = paramAttr.defaultValue; var valType = paramAttr.type; if (valType == typeof(bool)) { valStr = XmlConvert.ToString(bool.Parse(valStr)); } attr.DefaultValue = valStr; } else if (paramAttr.type.IsEnum) { var content = (XmlSchemaSimpleTypeRestriction)attr.SchemaType.Content; var facet = (XmlSchemaEnumerationFacet)content.Facets[0]; attr.DefaultValue = facet.Value; } } else { attr.Use = XmlSchemaUse.Required; } return(attr); }
void PopulatePluginType(XmlSchemaComplexType complexType, PluginElementAttribute pluginAttr) { if (pluginAttr.Named) { var nameAttr = new XmlSchemaAttribute(); nameAttr.Name = "name"; nameAttr.Annotate("{0} name.".Fmt(pluginAttr.PluginType.Name)); nameAttr.Use = XmlSchemaUse.Optional; complexType.Attributes.Add(nameAttr); } var typeAttr = new XmlSchemaAttribute(); typeAttr.Name = pluginAttr.AttributeName; typeAttr.Use = XmlSchemaUse.Required; typeAttr.Annotate("Specify the class name of a Peach {0}. You can implement your own {1}s as needed.".Fmt( pluginAttr.PluginType.Name, pluginAttr.PluginType.Name.ToLower() )); var restrictEnum = new XmlSchemaSimpleTypeRestriction(); restrictEnum.BaseTypeName = new XmlQualifiedName("string", XmlSchema.Namespace); foreach (var item in ClassLoader.GetAllByAttribute <PluginAttribute>((t, a) => a.Type == pluginAttr.PluginType && a.IsDefault && !a.IsTest)) { restrictEnum.Facets.Add(MakePluginFacet(item.Key, item.Value)); } var enumType = new XmlSchemaSimpleType(); enumType.Content = restrictEnum; var restrictLen = new XmlSchemaSimpleTypeRestriction(); restrictLen.BaseTypeName = new XmlQualifiedName("string", XmlSchema.Namespace); restrictLen.Facets.Add(new XmlSchemaMaxLengthFacet() { Value = "1024" }); var userType = new XmlSchemaSimpleType(); userType.Content = restrictLen; var union = new XmlSchemaSimpleTypeUnion(); union.BaseTypes.Add(userType); union.BaseTypes.Add(enumType); var schemaType = new XmlSchemaSimpleType(); schemaType.Content = union; typeAttr.SchemaType = schemaType; complexType.Attributes.Add(typeAttr); if (!objElemCache.ContainsKey(typeof(PluginParam))) { AddElement("Param", typeof(PluginParam), null); } var schemaElem = new XmlSchemaElement(); schemaElem.RefName = new XmlQualifiedName("Param", schema.TargetNamespace); XmlSchemaGroupBase schemaParticle; if (pluginAttr.PluginType == typeof(Transformer)) { schemaParticle = new XmlSchemaChoice(); schemaParticle.MinOccursString = "0"; schemaParticle.MaxOccursString = "unbounded"; var transElem = new XmlSchemaElement(); transElem.RefName = new XmlQualifiedName("Transformer", schema.TargetNamespace); schemaParticle.Items.Add(transElem); } else { schemaParticle = new XmlSchemaSequence(); schemaElem.MinOccursString = "0"; schemaElem.MaxOccursString = "unbounded"; } schemaParticle.Items.Add(schemaElem); complexType.Particle = schemaParticle; }
void CombinePluginType(XmlSchemaComplexType complexType, PluginElementAttribute pluginAttr) { var addedAttrs = new Dictionary <string, XmlSchemaAttribute>(); var addedElems = new Dictionary <string, XmlSchemaElement>(); var schemaParticle = new XmlSchemaChoice(); schemaParticle.MinOccursString = "0"; schemaParticle.MaxOccursString = "unbounded"; var restrictEnum = new XmlSchemaSimpleTypeRestriction(); restrictEnum.BaseTypeName = new XmlQualifiedName("string", XmlSchema.Namespace); foreach (var item in ClassLoader.GetAllByAttribute <PluginAttribute>((t, a) => a.Type == pluginAttr.PluginType && a.IsDefault && !a.IsTest)) { restrictEnum.Facets.Add(MakePluginFacet(item.Key, item.Value)); foreach (var pi in item.Value.GetProperties()) { var attrAttr = pi.GetAttributes <XmlAttributeAttribute>().FirstOrDefault(); if (attrAttr != null) { var attr = MakeAttribute(attrAttr.AttributeName, pi); if (!addedAttrs.ContainsKey(attr.Name)) { complexType.Attributes.Add(attr); addedAttrs.Add(attr.Name, attr); } continue; } var elemAttr = pi.GetAttributes <XmlElementAttribute>().FirstOrDefault(); if (elemAttr != null) { var elems = MakeElement(elemAttr.ElementName, null, pi, null); foreach (var elem in elems) { var key = elem.Name ?? elem.RefName.Name; if (!addedElems.ContainsKey(key)) { elem.MinOccursString = null; elem.MaxOccursString = null; schemaParticle.Items.Add(elem); addedElems.Add(key, elem); } } } var anyAttr = pi.GetAttributes <XmlAnyAttributeAttribute>().FirstOrDefault(); if (anyAttr != null) { complexType.AnyAttribute = new XmlSchemaAnyAttribute() { ProcessContents = XmlSchemaContentProcessing.Skip }; continue; } } foreach (var prop in item.Value.GetAttributes <ParameterAttribute>()) { var attr = MakeAttribute(prop.name, prop); if (!addedAttrs.ContainsKey(attr.Name)) { complexType.Attributes.Add(attr); addedAttrs.Add(attr.Name, attr); } } } var enumType = new XmlSchemaSimpleType(); enumType.Content = restrictEnum; var typeAttr = new XmlSchemaAttribute(); typeAttr.Name = pluginAttr.AttributeName; typeAttr.Use = XmlSchemaUse.Required; typeAttr.SchemaType = enumType; typeAttr.Annotate("Specify the {0} of a Peach {1}.".Fmt( pluginAttr.AttributeName, pluginAttr.PluginType.Name.ToLower() )); complexType.Attributes.Add(typeAttr); if (schemaParticle.Items.Count > 0) { complexType.Particle = schemaParticle; } }
XmlSchemaAttribute MakeAttribute(string name, PropertyInfo pi) { if (string.IsNullOrEmpty(name)) { name = pi.Name; } var defaultValue = pi.GetAttributes <DefaultValueAttribute>().FirstOrDefault(); var attr = new XmlSchemaAttribute(); attr.Name = name; attr.Annotate(pi); if (pi.PropertyType.IsEnum) { attr.SchemaType = GetEnumType(pi.PropertyType); } else { attr.SchemaTypeName = GetSchemaType(pi.PropertyType); } if (defaultValue != null) { attr.Use = XmlSchemaUse.Optional; if (defaultValue.Value != null) { var valStr = defaultValue.Value.ToString(); var valType = defaultValue.Value.GetType(); if (valType == typeof(bool)) { valStr = XmlConvert.ToString((bool)defaultValue.Value); } else if (valType.IsEnum) { var enumType = valType.GetField(valStr); var enumAttr = enumType.GetAttributes <XmlEnumAttribute>().FirstOrDefault(); if (enumAttr != null) { valStr = enumAttr.Name; } } attr.DefaultValue = valStr; } else if (pi.PropertyType.IsEnum) { var content = (XmlSchemaSimpleTypeRestriction)attr.SchemaType.Content; var facet = (XmlSchemaEnumerationFacet)content.Facets[0]; attr.DefaultValue = facet.Value; } } else { attr.Use = XmlSchemaUse.Required; } return(attr); }