public virtual CodeGenerationResults Generate(IArtifactLink link) { Guard.ArgumentNotNull(link, "link"); CodeGenerationResults content = new CodeGenerationResults(); try { XmlSchemaTypeGenerator generator = new XmlSchemaTypeGenerator(UseXmlSerializer(link), GetNamespace(link)); string xsdMoniker = Utility.GetData<string>(link, ElementDataKey); string xmlSchemaSource = XmlSchemaUtility.GetXmlSchemaSource(xsdMoniker, link); if (!string.IsNullOrEmpty(xmlSchemaSource)) { CodeCompileUnit unit = generator.GenerateCodeCompileUnit(xmlSchemaSource); string element = new XmlSchemaElementMoniker(xsdMoniker).ElementName; UpdateUnit(unit, element, link); ThrowOnNoTypes(unit.Namespaces, element); this.assemblyReferences = GetAssemblyReferences(link, unit.ReferencedAssemblies); CodeDomProvider provider = GetCodeDomProvider(link); GenerateCode(unit, provider, content, link.ItemPath); } } catch (Exception exception) { LogErrors(exception); } return content; }
/// <summary> /// Gets the type of the base types from referenced. /// </summary> /// <param name="xsdMoniker">The XSD moniker.</param> /// <param name="link">The link.</param> /// <returns></returns> public static IList<string> GetBaseTypesFromReferencedType(string xsdMoniker, IArtifactLink link) { Guard.ArgumentNotNullOrEmptyString(xsdMoniker, "xsdMoniker"); Guard.ArgumentNotNull(link, "link"); IList<string> types = new List<string>(); string xmlSchemaSource = GetXmlSchemaSource(xsdMoniker, link); string element = new XmlSchemaElementMoniker(xsdMoniker).ElementName; // try first with DC serializer XmlSchemaTypeGenerator generator = new XmlSchemaTypeGenerator(false); CodeCompileUnit unit; try { unit = generator.GenerateCodeCompileUnit(xmlSchemaSource); } catch (InvalidSerializerException) { // now try with Xml serializer generator = new XmlSchemaTypeGenerator(true); unit = generator.GenerateCodeCompileUnit(xmlSchemaSource); } foreach (CodeNamespace ns in unit.Namespaces) { foreach (CodeTypeDeclaration codeType in ns.Types) { if (codeType.Name.Equals(element, StringComparison.OrdinalIgnoreCase)) { CollectNestedTypes(codeType, types, unit, ns.Types, link); return types; } } } return types; }
private void AddNodesFromTypes(XmlSchemaTypeGenerator generator, TreeNode node, string itemPath) { foreach (System.CodeDom.CodeNamespace ns in generator.GenerateCodeCompileUnit(itemPath).Namespaces) { foreach (System.CodeDom.CodeTypeDeclaration codeType in ns.Types) { if ((codeType.IsClass || codeType.IsEnum || codeType.IsStruct) && !node.Nodes.ContainsKey(codeType.Name)) { XsdElementNode elementNode = new XsdElementNode(codeType.Name, codeType); node.Nodes.Add(elementNode); } } } // add empty node if no elem added if (node.Nodes.Count == 0) { node.Nodes.Add(new XsdEmptyNode()); } }
private void LoadXsdElementsHierarchy(TreeNode node) { if (node.Nodes.Count > 0) { // use already loaded nodes return; } DTE vs = (DTE)this.serviceProvider.GetService(typeof(DTE)); ProjectItem projectItemNode = node.Tag as ProjectItem; string itemPath = null; if(projectItemNode.Properties != null && projectItemNode.Properties.Item("FullPath").Value != null) { itemPath = projectItemNode.Properties.Item("FullPath").Value.ToString(); } else { itemPath = projectItemNode.get_FileNames(1); } if(!String.IsNullOrEmpty(itemPath)) { // try first with DC serializer XmlSchemaTypeGenerator generator = new XmlSchemaTypeGenerator(false); try { AddNodesFromTypes(generator, node, itemPath); } catch (InvalidSerializerException) { // now try with Xml serializer generator = new XmlSchemaTypeGenerator(true); AddNodesFromTypes(generator, node, itemPath); } catch (Exception ex) { IUIService iUIservice = this.serviceProvider.GetService(typeof(IUIService)) as IUIService; iUIservice.ShowError(ex, LogEntry.ErrorMessageToString(ex)); } } }
public void ShouldGenerateWithDefaultSerializer() { XmlSchemaTypeGenerator generator = new XmlSchemaTypeGenerator(false); CodeTypeDeclarationCollection types = generator.GenerateTypes(ConfigurationLoader.GetConfigurationFilePath(@"SampleData\DescriptionModel\Company.xsd")); Assert.AreEqual<int>(4, types.Count); }
public void TestInitialize() { generator = new XmlSchemaTypeGenerator(); }
public void ShouldGeneratedWithIncludesAndXmlSerializer() { XmlSchemaTypeGenerator generator = new XmlSchemaTypeGenerator(true); CodeTypeDeclarationCollection types = generator.GenerateTypes(ConfigurationLoader.GetConfigurationFilePath(@"SampleData\DescriptionModel\Company.xsd")); Assert.IsNotNull(types, "No exception thrown"); }
public void ShouldGenerateIsNullableCollectionType() { XmlSchemaTypeGenerator generator = new XmlSchemaTypeGenerator(true); CodeCompileUnit unit = generator.GenerateCodeCompileUnit( ConfigurationLoader.GetConfigurationFilePath(@"SampleData\DescriptionModel\Restriction.xsd")); Assert.AreEqual<int>(3, unit.Namespaces[0].Types.Count); foreach (CodeAttributeDeclaration attribute in unit.Namespaces[0].Types[0].CustomAttributes) { if (attribute.AttributeType.BaseType == typeof(XmlRootAttribute).FullName) { foreach (CodeAttributeArgument argument in attribute.Arguments) { if (argument.Name == "IsNullable") { Assert.IsFalse((bool)((CodePrimitiveExpression)argument.Value).Value); return; } } } } Assert.Fail("No XmlRootAttribute or IsNullable argument found"); }
public void ShouldGenerateWithXmlSchemaImporter() { XmlSchemaTypeGenerator generator = new XmlSchemaTypeGenerator(true); CodeTypeDeclarationCollection types = generator.GenerateTypes(ConfigurationLoader.GetConfigurationFilePath(@"SampleData\DescriptionModel\ComplexTypeSchema.xsd")); Assert.AreEqual<int>(2, types.Count); Assert.IsTrue(types[0].IsClass); Assert.IsTrue(types[1].IsClass); Assert.AreEqual<string>("complexType", types[1].Name); Assert.AreEqual<int>(2, types[0].Members.Count); Assert.AreEqual<string>("element1", types[0].Name); Assert.AreEqual<int>(8, types[1].Members.Count); Assert.AreEqual<string>("ComplexData", types[0].Members[1].Name); Assert.AreEqual("http://tempuri.org/ComplexTypeSchema.xsd", ((CodePrimitiveExpression)types[0].CustomAttributes[5].Arguments[0].Value).Value); }
public void ShouldGenerateWithDataSetSchemaAndImportXmlType() { XmlSchemaTypeGenerator generator = new XmlSchemaTypeGenerator(true); CodeTypeDeclarationCollection types = generator.GenerateTypes(ConfigurationLoader.GetConfigurationFilePath(@"SampleData\DescriptionModel\DataSetSchema.xsd")); Assert.AreEqual<int>(2, types.Count); }