private static XmlSchemaAnnotation GetSchemaAnnotation(params XmlNode[] nodes) { if (nodes == null || nodes.Length == 0) { return(null); } bool hasAnnotation = false; for (int i = 0; i < nodes.Length; i++) { if (nodes[i] != null) { hasAnnotation = true; break; } } if (!hasAnnotation) { return(null); } XmlSchemaAnnotation annotation = new XmlSchemaAnnotation(); XmlSchemaAppInfo appInfo = new XmlSchemaAppInfo(); annotation.Items.Add(appInfo); appInfo.Markup = nodes; return(annotation); }
private List <XmlNode> GetAnnotation(XmlSchemaAnnotated annotated) { List <XmlNode> nodes = new List <XmlNode>(); XmlSchemaAnnotation annotation = annotated.Annotation; if (annotation != null) { // find the first <xs:appinfo> element foreach (XmlSchemaObject schemaObj in annotation.Items) { XmlSchemaAppInfo appInfo = schemaObj as XmlSchemaAppInfo; if (appInfo != null) { // copy annotation, removing comments foreach (XmlNode node in appInfo.Markup) { if (node.NodeType != XmlNodeType.Comment) { nodes.Add(node); } } } } } return(nodes); }
public void IterateThrough(XmlSchemaAppInfo obj) { if (_functionalVisitor.StartProcessing(obj)) { } _functionalVisitor.EndProcessing(obj); }
private void setAnnotation(AbstractDef def, XmlSchemaAnnotation annotation) { if (annotation != null) { string desc = string.Empty; foreach (XmlSchemaObject o in annotation.Items) { XmlSchemaDocumentation description = o as XmlSchemaDocumentation; if (description != null) { foreach (XmlNode node in description.Markup) { desc += node.OuterXml; } } else { XmlSchemaAppInfo appinfo = o as XmlSchemaAppInfo; if (o != null) { def.SetFlags(appinfo.Markup[0].InnerText); } } } def.Desc = desc; } }
} //WriteXmlSchemaElement() //XmlSchemaAnnotation public static void WriteXmlSchemaAnnotation(XmlSchemaAnnotation annotation, XmlTextWriter myXmlTextWriter) { // Not a complete implementation myXmlTextWriter.WriteStartElement("annotation", XmlSchema.Namespace); foreach (object o in annotation.Items) { if (o is XmlSchemaDocumentation) { myXmlTextWriter.WriteStartElement("documentation", XmlSchema.Namespace); XmlSchemaDocumentation xmlsd = (XmlSchemaDocumentation)o; foreach (XmlNode n in xmlsd.Markup) { myXmlTextWriter.WriteStartElement("documentation values", XmlSchema.Namespace); myXmlTextWriter.WriteString(n.Value); myXmlTextWriter.WriteEndElement(); } //foreach myXmlTextWriter.WriteEndElement(); } //if else { if (o is XmlSchemaAppInfo) { XmlSchemaAppInfo xsai = (XmlSchemaAppInfo)o; foreach (XmlNode n in xsai.Markup) { myXmlTextWriter.WriteStartElement("appinfo values", XmlSchema.Namespace); myXmlTextWriter.WriteString(n.Value); myXmlTextWriter.WriteEndElement(); } //foreach } //if } //else } //foreach myXmlTextWriter.WriteEndElement(); } //WriteXmlSchemaAnnotation()
private void SetChoiceName(XmlSchemaChoice schemaChoice) { // See if the schema defines the name of the choice element if (schemaChoice.Annotation != null) { this.Name = GetChoiceAppInfoText(schemaChoice.Annotation); } else if (schemaChoice.Items.Count > 0 && ((XmlSchemaChoice)schemaChoice.Items[0].Parent).Annotation != null) { // Weird bug in System.Xml.Schema where annotations stored in the schema are reflected by the children's parent, // and are never directly on schemaChoice.Annotation this.Name = GetChoiceAppInfoText(((XmlSchemaChoice)schemaChoice.Items[0].Parent).Annotation); } if (!string.IsNullOrEmpty(this.Name)) { return; } this.Name = Shared.Helper.GetChoiceCommonName(schemaChoice, this.SimpleSchema.Schema.TargetNamespace); // Save the choice name to the schema XmlSchemaAppInfo newAppInfo = new XmlSchemaAppInfo(); newAppInfo.Source = SimpleSchema.SchemaChoiceAppInfoUri; newAppInfo.Markup = new XmlNode[] { new XmlDocument().CreateTextNode(this.Name) }; if (schemaChoice.Annotation == null) { schemaChoice.Annotation = new XmlSchemaAnnotation(); } schemaChoice.Annotation.Items.Add(newAppInfo); }
internal static void WriteAppInfo(this XmlSchemaAnnotated annotated, string nodeName, string attributeName, string value, string ns, string defaultValue) { if (value == defaultValue || string.IsNullOrEmpty(value) == true) { return; } if (annotated.Annotation == null) { annotated.Annotation = new XmlSchemaAnnotation(); } var annotation = annotated.Annotation; { XmlSchemaAppInfo appInfo = null; foreach (XmlSchemaObject item in annotation.Items) { if (item is XmlSchemaAppInfo == true) { appInfo = item as XmlSchemaAppInfo; break; } } if (appInfo == null) { appInfo = new XmlSchemaAppInfo(); annotation.Items.Add(appInfo); } var doc = new XmlDocument(); var root = doc.CreateElement("root", ns); doc.AppendChild(root); var element = doc.CreateElement(nodeName, ns); var valueAttr = doc.CreateAttribute(attributeName); valueAttr.Value = value as string; element.Attributes.Append(valueAttr); doc.DocumentElement.AppendChild(element); if (appInfo.Markup == null) { appInfo.Markup = new XmlNode[1] { element }; } else { var nodes = new XmlNode[appInfo.Markup.Length + 1]; appInfo.Markup.CopyTo(nodes, 0); nodes[appInfo.Markup.Length] = element; appInfo.Markup = nodes; } } }
internal void ExportDictionaryContractType(CollectionDataContractAttribute attr, SerializationMap map, Type dicType) { var type = map.RuntimeType; var qname = map.XmlName; var typeArgs = dicType.IsGenericType ? dicType.GetGenericArguments() : null; var keyType = typeArgs != null ? typeArgs [0] : typeof(object); var valueType = typeArgs != null ? typeArgs [1] : typeof(object); ExportCore(keyType, false); ExportCore(valueType, false); string keyName = "Key", valueName = "Value"; if (attr != null) { keyName = attr.KeyName ?? keyName; valueName = attr.ValueName ?? valueName; } string itemName = attr != null && attr.ItemName != null ? attr.ItemName : "KeyValueOf" + keyName + valueName; var ct = CreateComplexType(qname, type); var appInfo = new XmlSchemaAppInfo(); var node = new XmlDocument().CreateElement("IsDictionary", KnownTypeCollection.MSSimpleNamespace); node.InnerText = "true"; appInfo.Markup = new XmlNode [] { node }; ct.Annotation = new XmlSchemaAnnotation(); ct.Annotation.Items.Add(appInfo); var seq = new XmlSchemaSequence(); ct.Particle = seq; var el = new XmlSchemaElement() { Name = itemName, MinOccurs = 0, MaxOccursString = "unbounded" }; seq.Items.Add(el); var dictType = new XmlSchemaComplexType(); el.SchemaType = dictType; var dictSeq = new XmlSchemaSequence(); dictType.Particle = dictSeq; dictSeq.Items.Add(new XmlSchemaElement() { Name = keyName, SchemaTypeName = GetSchemaTypeName(keyType), IsNillable = true }); dictSeq.Items.Add(new XmlSchemaElement() { Name = valueName, SchemaTypeName = GetSchemaTypeName(valueType), IsNillable = true }); }
public static void AddAppinfo(this XmlSchemaElement element, XmlSchemaAppInfo ai) { var annotation = element.Annotation; if (annotation == null) { annotation = new XmlSchemaAnnotation(); element.Annotation = annotation; } annotation.Items.Insert(0, ai); }
private XmlSchemaAnnotation GetSchemaAnnotation(XmlQualifiedName annotationQualifiedName, string innerText, XmlSchema schema) { XmlSchemaAnnotation annotation = new XmlSchemaAnnotation(); XmlSchemaAppInfo appInfo = new XmlSchemaAppInfo(); XmlElement annotationElement = GetAnnotationMarkup(annotationQualifiedName, innerText, schema); appInfo.Markup = new XmlNode[1] { annotationElement }; annotation.Items.Add(appInfo); return(annotation); }
/// <summary> /// Extracts annotation section from passes XmlSchema object /// </summary> /// <param name="annotatedElement">Annotation element of XSD schema with documentation and appinfo tags</param> /// <returns>Extracted Documentation and AppInfo</returns> public static Dictionary <string, string> GetDocumentationForElement(XmlSchemaAnnotated annotatedElement) { Dictionary <string, string> annotationElements = new Dictionary <string, string>(); StringBuilder documatationHolder = new StringBuilder(); StringBuilder appInfoHolder = new StringBuilder(); XmlSchemaAnnotation annotation = annotatedElement.Annotation; // Look inside the Annotation element if (annotation != null) { XmlSchemaObjectCollection annotationItems = annotation.Items; if (annotationItems.Count > 0) { // Cannot use for..each for (int elementCount = 0; elementCount < annotationItems.Count; elementCount++) { XmlSchemaDocumentation xsdDocumentation = annotationItems[elementCount] as XmlSchemaDocumentation; if (xsdDocumentation != null) { XmlNode[] documentationMarkups = xsdDocumentation.Markup; if (documentationMarkups.Length > 0) { documatationHolder.Append(documentationMarkups[0].InnerText); documatationHolder.Append(" "); } } XmlSchemaAppInfo xsdAppInfo = annotationItems[elementCount] as XmlSchemaAppInfo; if (xsdAppInfo != null) { XmlNode[] appInfoMarkups = xsdAppInfo.Markup; if (appInfoMarkups.Length > 0) { appInfoHolder.Append(appInfoMarkups[0].InnerText); appInfoHolder.Append(" "); } } } if (documatationHolder.Length > 0) { annotationElements.Add("DOC", documatationHolder.ToString()); } if (appInfoHolder.Length > 0) { annotationElements.Add("APPINFO", appInfoHolder.ToString()); } } } return(annotationElements); }
private static void SetAppInfoAndDataType(IPluglet pluglet, XmlSchemaElement element) { X12BaseDataType dataType = pluglet.DataType; if (dataType != null && !string.IsNullOrEmpty(dataType.Name)) { XmlSchemaAppInfo appInfo = new XmlSchemaAppInfo(); XmlDocument doc = new XmlDocument(); XmlElement elem = doc.CreateElement("STD_Info"); elem.SetAttribute("DataType", dataType.Name); if (!string.IsNullOrEmpty(pluglet.DEStandard)) { elem.SetAttribute("Name", pluglet.DEStandard); } if (!string.IsNullOrEmpty(pluglet.DENumber)) { elem.SetAttribute("Number", pluglet.DENumber); } if (dataType.MaxLength > 0) { elem.SetAttribute("MaximumLength", dataType.MaxLength.ToString()); } XmlNode[] nodeArray = new XmlNode[1] { elem }; appInfo.Markup = nodeArray; if (element.Annotation != null) { element.Annotation.Items.Add(appInfo); } else { XmlSchemaAnnotation annotation = new XmlSchemaAnnotation(); annotation.Items.Add(appInfo); element.Annotation = annotation; } if (string.Equals(dataType.Name, X12DataTypeFactory.IDNew_DataTypeName, StringComparison.OrdinalIgnoreCase)) { PopulateEnumerationValues(pluglet.DataType as X12_IdDataType, element); } } }
void AddXmlnsAnnotation(XmlSchemaComplexType type, string xmlnsMemberName) { XmlSchemaAnnotation annotation = new XmlSchemaAnnotation(); XmlSchemaAppInfo appinfo = new XmlSchemaAppInfo(); XmlDocument d = new XmlDocument(); XmlElement e = d.CreateElement("keepNamespaceDeclarations"); if (xmlnsMemberName != null) { e.InsertBefore(d.CreateTextNode(xmlnsMemberName), null); } appinfo.Markup = new XmlNode[] { e }; annotation.Items.Add(appinfo); type.Annotation = annotation; }
//public static List<SchemaObject> GetComplexTypesFromSchema(int implementationGuideTypeId) //{ // using (IObjectRepository tdb = DBContext.Create()) // { // ImplementationGuideType igType = tdb.ImplementationGuideTypes.Single(y => y.Id == implementationGuideTypeId); // SimpleSchema schema = CreateSimpleSchema(Helper.GetIGSimplifiedSchemaLocation(igType)); // return schema.ComplexTypes; // } //} #region Private Methods private string GetChoiceAppInfoText(XmlSchemaAnnotation annotation) { XmlSchemaAppInfo appInfo = annotation.Items.OfType <XmlSchemaAppInfo>().SingleOrDefault(y => y.Source == SimpleSchema.SchemaChoiceAppInfoUri); if (appInfo != null) { XmlText nameText = appInfo.Markup.OfType <XmlText>().FirstOrDefault(); if (nameText != null && !string.IsNullOrEmpty(nameText.Value)) { return(nameText.Value); } } return(string.Empty); }
private static void ApplyAnnotation(XmlSchemaAnnotated annotatedType, DescriptionAttribute[] descriptionAtts, ConfigurationPropertyAttribute configProperty, string xmlDocumentation, string typeName, string fullName) { string standardDesc; if (configProperty != null) { standardDesc = configProperty.IsRequired ? "Required" : "Optional"; standardDesc += " " + fullName; standardDesc += " " + (configProperty.DefaultValue == null || configProperty.DefaultValue.ToString() == "System.Object" ? string.Empty : "[" + configProperty.DefaultValue + "]"); } else { standardDesc = string.Empty; } var documentation = new XmlSchemaDocumentation(); if (descriptionAtts.Length > 0) { documentation.Markup = TextToNodeArray(descriptionAtts[0].Description + " " + standardDesc); } else if (!String.IsNullOrEmpty(xmlDocumentation)) { // normalise line endings and remove trailing whitespace(s) xmlDocumentation = Regex.Replace(xmlDocumentation, @"\s*(\r\n|\n\r|\n|\r)", "\r\n"); documentation.Markup = TextToNodeArray(xmlDocumentation); } else { documentation.Markup = TextToNodeArray(standardDesc); } // machine documentation var appInfo = new XmlSchemaAppInfo { Markup = TextToNodeArray(typeName) }; // add the documentation to the object annotatedType.Annotation.Items.Add(documentation); annotatedType.Annotation.Items.Add(appInfo); }
public static void Main() { XmlSchema schema = new XmlSchema(); // <xs:element name="State"> XmlSchemaElement element = new XmlSchemaElement(); schema.Items.Add(element); element.Name = "State"; // <xs:annotation> XmlSchemaAnnotation annNorthwestStates = new XmlSchemaAnnotation(); element.Annotation = annNorthwestStates; // <xs:documentation>State Name</xs:documentation> XmlSchemaDocumentation docNorthwestStates = new XmlSchemaDocumentation(); annNorthwestStates.Items.Add(docNorthwestStates); docNorthwestStates.Markup = TextToNodeArray("State Name"); // <xs:appInfo>Application Information</xs:appInfo> XmlSchemaAppInfo appInfo = new XmlSchemaAppInfo(); annNorthwestStates.Items.Add(appInfo); appInfo.Markup = TextToNodeArray("Application Information"); XmlSchemaSet schemaSet = new XmlSchemaSet(); schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackOne); schemaSet.Add(schema); schemaSet.Compile(); XmlSchema compiledSchema = null; foreach (XmlSchema schema1 in schemaSet.Schemas()) { compiledSchema = schema1; } XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable()); nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema"); compiledSchema.Write(Console.Out, nsmgr); }
private void Write7_XmlSchemaAppInfo(XmlSchemaAppInfo o) { if (o != null) { this.WriteStartElement("appinfo"); this.WriteAttribute("source", "", o.Source); XmlNode[] markup = o.Markup; if (markup != null) { for (int i = 0; i < markup.Length; i++) { XmlNode node = markup[i]; this.WriteStartElement("node"); this.WriteAttribute("xml", "", node.OuterXml); } } this.WriteEndElement(); } }
/// <summary> /// The description of a class. /// </summary> /// <param name="presentationSchema">The PresentationSchema to which this class belongs.</param> /// <param name="xmlSchemaComplexType">The XML Schema description of the class.</param> public ClassSchema(PresentationSchema presentationSchema, XmlSchemaComplexType xmlSchemaComplexType) { // Initialize the object. this.name = xmlSchemaComplexType.QualifiedName.Name; this.presentationSchema = presentationSchema; this.propertyList = new List <PropertySchema>(); this.targetNamespace = xmlSchemaComplexType.QualifiedName.Namespace; this.type = xmlSchemaComplexType.QualifiedName.ToString(); // This will create the list of properties of this type. if (xmlSchemaComplexType.Particle is XmlSchemaSequence) { XmlSchemaSequence xmlSchemaSequence = xmlSchemaComplexType.Particle as XmlSchemaSequence; foreach (XmlSchemaObject item in xmlSchemaSequence.Items) { if (item is XmlSchemaElement) { this.propertyList.Add(new PropertySchema(this, item as XmlSchemaElement)); } } } // This will create and initialize a description of the constructor for the class from the XML Schema extensions. this.constructorSchema = new ConstructorSchema(this); if (xmlSchemaComplexType.Annotation != null) { foreach (XmlSchemaObject xmlSchemaObject in xmlSchemaComplexType.Annotation.Items) { if (xmlSchemaObject is XmlSchemaAppInfo) { XmlSchemaAppInfo xmlSchemaAppInfo = xmlSchemaObject as XmlSchemaAppInfo; foreach (XmlNode constructorNode in xmlSchemaAppInfo.Markup) { if (QualifiedName.Constructor == new XmlQualifiedName(constructorNode.LocalName, constructorNode.NamespaceURI)) { this.constructorSchema = new ConstructorSchema(this, constructorNode); } } } } } }
static XmlElement ImportAnnotation(XmlSchemaAnnotation annotation, XmlQualifiedName annotationQualifiedName) { if (annotation != null && annotation.Items != null && annotation.Items.Count > 0 && annotation.Items[0] is XmlSchemaAppInfo) { XmlSchemaAppInfo appInfo = (XmlSchemaAppInfo)annotation.Items[0]; XmlNode[] markup = appInfo.Markup; if (markup != null) { for (int i = 0; i < markup.Length; i++) { XmlElement annotationElement = markup[i] as XmlElement; if (annotationElement != null && annotationElement.LocalName == annotationQualifiedName.Name && annotationElement.NamespaceURI == annotationQualifiedName.Namespace) { return(annotationElement); } } } } return(null); }
void Write7_XmlSchemaAppInfo(XmlSchemaAppInfo o) { if ((object)o == null) { return; } WriteStartElement("appinfo"); WriteAttribute("source", "", o.Source); XmlNode[] a = (XmlNode[])o.@Markup; if (a != null) { for (int ia = 0; ia < a.Length; ia++) { XmlNode ai = (XmlNode)a[ia]; WriteStartElement("node"); WriteAttribute("xml", "", ai.OuterXml); } } WriteEndElement(); }
private static string ProcessAppInfo(XmlSchemaAppInfo schemaAppInfo) { StringBuilder result = new StringBuilder(); if (schemaAppInfo == null) { return(result.ToString()); } if (!string.IsNullOrEmpty(schemaAppInfo.Source)) { result.AppendLine(schemaAppInfo.Source.Trim()); } List <string> parentRefs = new List <string>(); // list of parents under which this type can be nested foreach (XmlNode node in schemaAppInfo.Markup) { switch (node.Name) { case "xse:parent": parentRefs.Add(node.Attributes["ref"].Value); break; default: result.AppendLine(string.Format("Unsupported appinfo node: {0}", node.Name)); break; } } if (parentRefs.Count > 0) { result.AppendLine(string.Format("May be nested under \\b {0}.", string.Join(", \\b ", parentRefs.ToArray()))); } return(result.ToString()); }
private XmlSchemaAnnotation CreateSchemaAnnotation(string schemaNamespace, Definition definition) { if (!definition.Documentation.Any()) { return(null); } var markup = definition.Documentation .Select(doc => CreateTitleElement(doc.Item1, doc.Item2, ns => addRequiredImport(schemaNamespace, ns, null))) .Cast <XmlNode>(); #if NETSTANDARD1_6_1 var appInfo = new XmlSchemaAppInfo(); appInfo.Markup.AddRange(markup); #else var appInfo = new XmlSchemaAppInfo { Markup = markup.ToArray() }; #endif return(new XmlSchemaAnnotation { Items = { appInfo } }); }
bool IsDeprecated(XmlSchemaAttribute attribute) { XmlSchemaAnnotation annotation = attribute.Annotation; if (annotation != null) { foreach (XmlSchemaObject schemaObject in annotation.Items) { XmlSchemaAppInfo appInfo = schemaObject as XmlSchemaAppInfo; if (appInfo != null) { foreach (XmlNode node in appInfo.Markup) { XmlElement element = node as XmlElement; if (element.LocalName == "deprecated") { return(true); } } } } } return(false); }
/// <summary> /// Gets the application specific markup extensions to the XML Schema. /// </summary> /// <param name="xmlSchema">The schema.</param> /// <returns>A set of nodes that describe the application specific extensions to the standard XML Schema.</returns> private XmlNode[] GetMarkup(XmlSchema xmlSchema) { // Search through all the items in the schema looking for annotations. The annotations contain extensions to the // standard XML Schema. In this case, such items as constructor parameters and CLR namespace import statements are // added to the schema to help produce the output assembly. foreach (XmlSchemaObject schemaItem in xmlSchema.Items) { if (schemaItem is XmlSchemaAnnotation) { XmlSchemaAnnotation xmlSchemaAnnotation = schemaItem as XmlSchemaAnnotation; foreach (XmlSchemaObject annotationItem in xmlSchemaAnnotation.Items) { if (annotationItem is XmlSchemaAppInfo) { XmlSchemaAppInfo xmlSchemaAppInfo = annotationItem as XmlSchemaAppInfo; return(xmlSchemaAppInfo.Markup); } } } } // At this point the schema has no application specific markup. return(new XmlNode[0]); }
/// <summary> /// Run the application with the given arguments. /// </summary> /// <param name="args">The commandline arguments.</param> /// <returns>The number of errors that were found.</returns> private int Run(string[] args) { XmlSchema mainSchema = null; XmlSchemaCollection schemas = new XmlSchemaCollection(); try { this.ParseCommandLine(args); if (this.showLogo) { Assembly thisAssembly = Assembly.GetExecutingAssembly(); Console.WriteLine("Microsoft (R) Windows Installer Xsd Stitch version {0}", thisAssembly.GetName().Version.ToString()); Console.WriteLine("Copyright (C) Microsoft Corporation 2006. All rights reserved."); Console.WriteLine(); } if (this.showHelp) { Console.WriteLine(" usage: xsdStitch.exe mainSchema.xsd stitched.xsd"); Console.WriteLine(); Console.WriteLine(" -ext extSchema.xsd adds an extension schema to the main schema"); Console.WriteLine(" -nologo suppress displaying the logo information"); Console.WriteLine(" -? this help information"); Console.WriteLine(); return(0); } XmlTextReader mainSchemaReader = null; // load the main schema try { mainSchemaReader = new XmlTextReader(this.mainSchemaFile); mainSchema = XmlSchema.Read(mainSchemaReader, null); schemas.Add(mainSchema); } finally { if (null != mainSchemaReader) { mainSchemaReader.Close(); } } StringCollection addedSchemas = new StringCollection(); // load the extension schemas foreach (string extensionSchemaFile in this.extensionSchemaFiles) { XmlTextReader reader = null; try { string schemaFilename = Path.GetFileNameWithoutExtension(extensionSchemaFile); if (addedSchemas.Contains(schemaFilename)) { int duplicateNameCounter = 2; while (addedSchemas.Contains(String.Concat(schemaFilename, duplicateNameCounter))) { duplicateNameCounter++; } schemaFilename = String.Concat(schemaFilename, duplicateNameCounter); } addedSchemas.Add(schemaFilename); reader = new XmlTextReader(extensionSchemaFile); XmlSchema extensionSchema = XmlSchema.Read(reader, null); mainSchema.Namespaces.Add(schemaFilename, extensionSchema.TargetNamespace); schemas.Add(extensionSchema); // add an import for the extension schema XmlSchemaImport import = new XmlSchemaImport(); import.Namespace = extensionSchema.TargetNamespace; import.SchemaLocation = Path.GetFileName(extensionSchemaFile); mainSchema.Includes.Add(import); } finally { if (null != reader) { reader.Close(); } } } foreach (XmlSchema schema in schemas) { if (schema != mainSchema) { foreach (XmlSchemaElement element in schema.Elements.Values) { // retrieve explicitly-specified parent elements if (element.Annotation != null) { foreach (XmlSchemaObject obj in element.Annotation.Items) { XmlSchemaAppInfo appInfo = obj as XmlSchemaAppInfo; if (appInfo != null) { foreach (XmlNode node in appInfo.Markup) { XmlElement markupElement = node as XmlElement; if (markupElement != null && markupElement.LocalName == "parent" && markupElement.NamespaceURI == XmlSchemaExtensionNamespace) { string parentNamespace = markupElement.GetAttribute("namespace"); string parentRef = markupElement.GetAttribute("ref"); if (parentNamespace.Length == 0) { throw new ApplicationException("The parent element is missing the namespace attribute."); } if (parentRef.Length == 0) { throw new ApplicationException("The parent element is missing the ref attribute."); } XmlQualifiedName parentQualifiedName = new XmlQualifiedName(parentRef, parentNamespace); XmlSchemaElement parentElement = (XmlSchemaElement)mainSchema.Elements[parentQualifiedName]; XmlSchemaComplexType complexType = (XmlSchemaComplexType)parentElement.ElementType; if (complexType.Particle != null) { XmlSchemaElement elementRef = new XmlSchemaElement(); elementRef.RefName = element.QualifiedName; this.InsertElement(complexType.Particle, elementRef); } } } } } } } foreach (XmlSchemaAttribute attribute in schema.Attributes.Values) { if (attribute.Annotation != null) { foreach (XmlSchemaObject obj in attribute.Annotation.Items) { XmlSchemaAppInfo appInfo = obj as XmlSchemaAppInfo; if (appInfo != null) { foreach (XmlNode node in appInfo.Markup) { XmlElement markupElement = node as XmlElement; if (markupElement != null && markupElement.LocalName == "parent" && markupElement.NamespaceURI == XmlSchemaExtensionNamespace) { string parentNamespace = markupElement.GetAttribute("namespace"); string parentRef = markupElement.GetAttribute("ref"); if (parentNamespace.Length == 0) { throw new ApplicationException("The parent element is missing the namespace attribute."); } if (parentRef.Length == 0) { throw new ApplicationException("The parent element is missing the ref attribute."); } XmlQualifiedName parentQualifiedName = new XmlQualifiedName(parentRef, parentNamespace); XmlSchemaElement parentElement = (XmlSchemaElement)mainSchema.Elements[parentQualifiedName]; XmlSchemaComplexType complexType = (XmlSchemaComplexType)parentElement.ElementType; if (complexType.Particle != null) { XmlSchemaAttribute attributeRef = new XmlSchemaAttribute(); attributeRef.RefName = attribute.QualifiedName; anyAttributeElements.Add(complexType); complexType.Attributes.Add(attribute); } } } } } } } } } // remove the any items foreach (DictionaryEntry entry in this.anys) { XmlSchemaAny any = (XmlSchemaAny)entry.Key; XmlSchemaParticle particle = (XmlSchemaParticle)entry.Value; if (particle is XmlSchemaChoice) { XmlSchemaChoice choice = (XmlSchemaChoice)particle; choice.Items.Remove(any); } else if (particle is XmlSchemaSequence) { XmlSchemaSequence sequence = (XmlSchemaSequence)particle; sequence.Items.Remove(any); } } // remove anyAttribute items foreach (XmlSchemaComplexType complexType in this.anyAttributeElements) { complexType.AnyAttribute = null; } XmlTextWriter writer = null; try { writer = new XmlTextWriter(this.outputFile, System.Text.Encoding.Default); writer.Indentation = 4; writer.IndentChar = ' '; writer.Formatting = Formatting.Indented; mainSchema.Write(writer); } finally { writer.Close(); } } catch (Exception e) { Console.WriteLine("xsdStitch.exe : fatal error XSDS0001 : {0}\r\n\n\nStack Trace:\r\n{1}", e.Message, e.StackTrace); return(1); } return(0); }
/// <summary> /// Constructs a schema from the contents of an XML specification. /// </summary> /// <param name="fileContents">The contents of a file that specifies the schema in XML.</param> public AppInfoSchema(DataModelSchema dataModelSchema, XmlSchemaAppInfo xmlSchemaAppInfo) : base(dataModelSchema, xmlSchemaAppInfo) { this.source = xmlSchemaAppInfo.Source; this.markup = xmlSchemaAppInfo.Markup; }
/// <summary> /// Create a table schema from the XML Schema specification. /// </summary> /// <param name="presentationSchema">The data model to which this table belongs.</param> /// <param name="xmlSchemaElement">The root of the XmlSchema element that describes the table.</param> public PropertySchema(ClassSchema classSchema, XmlSchemaElement xmlSchemaElement) { // Initialize the object. this.classSchema = classSchema; this.isSimpleType = true; this.maxOccurs = xmlSchemaElement.MaxOccurs; this.minOccurs = xmlSchemaElement.MinOccurs; this.name = xmlSchemaElement.Name; this.querySchemaList = new List <QuerySchema>(); this.snippetSchemaList = new List <SnippetSchema>(); this.type = String.Empty; // Initialize the data type from a complex type description. The complex type can describe either a non-standard XML // data type through the unhandled 'DataType' specification, or it can reference a data type that's declared in the // schema. if (xmlSchemaElement.ElementSchemaType is XmlSchemaComplexType) { XmlSchemaComplexType xmlSchemaComplexType = xmlSchemaElement.ElementSchemaType as XmlSchemaComplexType; if (xmlSchemaComplexType.QualifiedName == QualifiedName.AnyType) { foreach (XmlAttribute xmlAttribute in xmlSchemaElement.UnhandledAttributes) { if (QualifiedName.DataType == new XmlQualifiedName(xmlAttribute.LocalName, xmlAttribute.NamespaceURI)) { this.isSimpleType = true; this.type = xmlAttribute.Value; } } } else { this.type = xmlSchemaComplexType.QualifiedName.ToString(); this.isSimpleType = false; } } // Initialize the data type from a simple type description. if (xmlSchemaElement.ElementSchemaType is XmlSchemaSimpleType) { XmlSchemaSimpleType xmlSchemaSimpleType = xmlSchemaElement.ElementSchemaType as XmlSchemaSimpleType; this.isSimpleType = true; this.type = xmlSchemaSimpleType.Datatype.ValueType.FullName; } // This will run through each of the custom fields associated with the element and create fields that will sort, filter // and transform the data from the source list into the destination list. if (xmlSchemaElement.Annotation != null) { foreach (XmlSchemaObject xmlSchemaObject in xmlSchemaElement.Annotation.Items) { if (xmlSchemaObject is XmlSchemaAppInfo) { XmlSchemaAppInfo xmlSchemaAppInfo = xmlSchemaObject as XmlSchemaAppInfo; foreach (XmlNode xmlNode in xmlSchemaAppInfo.Markup) { XmlQualifiedName nodeName = new XmlQualifiedName(xmlNode.LocalName, xmlNode.NamespaceURI); if (QualifiedName.Where == nodeName) { this.querySchemaList.Add(new WhereSchema(this, xmlNode)); } if (QualifiedName.Select == nodeName) { this.querySchemaList.Add(new SelectSchema(this, xmlNode)); } if (QualifiedName.OrderBy == nodeName) { this.querySchemaList.Add(new OrderBySchema(this, xmlNode)); } if (QualifiedName.Snippet == nodeName) { this.snippetSchemaList.Add(new SnippetSchema(xmlNode)); } if (QualifiedName.Setter == nodeName) { this.snippetSchemaList.Add(new SetterSchema(xmlNode)); } } } } } }
private static void ConvertObjectAnnotation(XmlSchemaObject schemaObj) { XmlSchemaAnnotated annObj = schemaObj as XmlSchemaAnnotated; if (annObj != null && annObj.Annotation != null) { XmlSchemaDocumentation comment = null; foreach (XmlSchemaObject annotation in annObj.Annotation.Items) { XmlSchemaAppInfo appInfo = annotation as XmlSchemaAppInfo; if (appInfo != null) { for (int i = 0; i < appInfo.Markup.Length; i++) { XmlNode markup = appInfo.Markup[i]; if (markup != null) { XmlAttribute typeAttrib = markup.Attributes["type", "http://www.w3.org/2001/XMLSchema-instance"]; if (typeAttrib != null) { if (typeAttrib.Value.Contains(":Annotation")) { string ns = typeAttrib.Value.Split(':')[0]; typeAttrib = markup.Attributes[ns, "http://www.w3.org/2000/xmlns/"]; if (typeAttrib != null && typeAttrib.Value == Annotation.AnnotationNamespace) { //This is an annotation created by us. Convert it to ws:documentation element and break comment = CreateDocumentationItem(markup.InnerText); appInfo.Markup[i] = null; break; } } else if (typeAttrib.Value.Contains(":EnumAnnotation")) { string ns = typeAttrib.Value.Split(':')[0]; typeAttrib = markup.Attributes[ns, "http://www.w3.org/2000/xmlns/"]; if (typeAttrib != null && typeAttrib.Value == Annotation.AnnotationNamespace) { DataContractSerializer serializer = new DataContractSerializer(typeof(EnumAnnotation)); using (XmlReader reader = new XmlNodeReader(markup)) { EnumAnnotation enumAnn = (EnumAnnotation)serializer.ReadObject(reader, false); if (enumAnn.EnumText != null) { //This is an annotation created by us. Convert it to ws:documentation element and break comment = CreateDocumentationItem(enumAnn.EnumText); } if (enumAnn.Members.Count > 0) { foreach (XmlSchemaEnumerationFacet enumObj in GetEnumItems(schemaObj)) { string docText; if (enumAnn.Members.TryGetValue(enumObj.Value, out docText)) { if (enumObj.Annotation == null) { enumObj.Annotation = new XmlSchemaAnnotation(); } enumObj.Annotation.Items.Add(CreateDocumentationItem(docText)); } } } appInfo.Markup[i] = null; break; } } } } } } } } if (comment != null) { annObj.Annotation.Items.Add(comment); } } foreach (XmlSchemaObject subObj in GetSubItems(schemaObj)) { ConvertObjectAnnotation(subObj); } }
/// <summary> /// Creates the an xml schema for a complex type. This method automatically takes care of /// any base classes that must be added. /// <para /> /// This method already adds the <see cref="XmlSchemaComplexType" /> to the <see cref="XmlSchemaSet" />. /// </summary> /// <param name="type">The type to create the complex schema for.</param> /// <param name="schema">The schema.</param> /// <param name="schemaSet">The schema set.</param> /// <param name="serializationManager">The serialization manager.</param> /// <param name="generateFlatSchema">A value indicating whether the schema should be generated as flat schema.</param> /// <param name="exportedTypes">The exported types.</param> /// <returns>The complex schema for the specified type.</returns> private static XmlSchemaComplexType CreateSchemaComplexType(Type type, XmlSchema schema, XmlSchemaSet schemaSet, ISerializationManager serializationManager, bool generateFlatSchema, HashSet <string> exportedTypes) { // Determine name, which is complex in generic types string typeName = GetTypeNameForSchema(type); // First, add the type, otherwise we might get into a stackoverflow when using generic base types // <xs:complexType> var modelBaseType = new XmlSchemaComplexType(); modelBaseType.Name = typeName; modelBaseType.IsMixed = false; schema.Items.Add(modelBaseType); var propertiesSequence = GetPropertiesSequence(type, schema, schemaSet, serializationManager, exportedTypes); // If flat, don't generate base classes, just the type itself if (generateFlatSchema) { modelBaseType.Particle = propertiesSequence; return(modelBaseType); } if (type.IsGenericType) { var genericComplexType = new XmlSchemaComplexType(); genericComplexType.Name = typeName; // <xs:annotation> var annotation = new XmlSchemaAnnotation(); // <xs:appinfo> var appInfo = new XmlSchemaAppInfo(); //<GenericType xmlns="http://schemas.microsoft.com/2003/10/Serialization/" Name="DataContractBaseOf{0}{#}" Namespace="http://schemas.datacontract.org/2004/07/STC"> // <GenericParameter Name="ProfileDateRange2" Namespace="http://schemas.datacontract.org/2004/07/STC.Meter.ProfileData"/> //</GenericType> var genericTypeElement = new XElement("GenericType"); genericTypeElement.Add(new XAttribute("Name", string.Format("{0}Of{{0}}{{#}}", typeName))); genericTypeElement.Add(new XAttribute("Namespace", GetTypeNamespaceForSchema(type))); foreach (var genericArgument in type.GetGenericArgumentsEx()) { var genericArgumentQualifiedName = AddTypeToSchemaSet(genericArgument, schemaSet, serializationManager, exportedTypes); var genericArgumentElement = new XElement("GenericParameter"); genericArgumentElement.Add(new XAttribute("Name", genericArgumentQualifiedName.Name)); genericArgumentElement.Add(new XAttribute("Namespace", genericArgumentQualifiedName.Namespace)); genericTypeElement.Add(genericArgumentElement); } var conversionDoc = new XmlDocument(); appInfo.Markup = new XmlNode[] { conversionDoc.CreateTextNode(genericTypeElement.ToString()) }; annotation.Items.Add(appInfo); } var baseTypeQualifiedName = AddTypeToSchemaSet(type.BaseType, schemaSet, serializationManager, exportedTypes); if (baseTypeQualifiedName != null) { // <xs:extensions base="address"> var complexContentExtension = new XmlSchemaComplexContentExtension(); complexContentExtension.BaseTypeName = baseTypeQualifiedName; complexContentExtension.Particle = propertiesSequence; // <xs:complexContent> var complexContent = new XmlSchemaComplexContent(); complexContent.Content = complexContentExtension; modelBaseType.ContentModel = complexContent; } else { modelBaseType.Particle = propertiesSequence; } return(modelBaseType); }
private static void Equal(XmlSchemaAppInfo expected, XmlSchemaAppInfo actual) { Assert.Equal(expected.Source, actual.Source); XmlNodeIsEquivalentTo(expected.Markup, actual.Markup); }