public void ShouldGenerateWithDataSetSchemaAndImportXmlType() { XmlSchemaTypeGenerator generator = new XmlSchemaTypeGenerator(true); CodeTypeDeclarationCollection types = generator.GenerateTypes(ConfigurationLoader.GetConfigurationFilePath(@"SampleData\DescriptionModel\DataSetSchema.xsd")); Assert.AreEqual <int>(2, types.Count); }
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); }
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 ShouldGeneratedWithIncludesAndXmlSerializer() { XmlSchemaTypeGenerator generator = new XmlSchemaTypeGenerator(true); CodeTypeDeclarationCollection types = generator.GenerateTypes(ConfigurationLoader.GetConfigurationFilePath(@"SampleData\DescriptionModel\Company.xsd")); Assert.IsNotNull(types, "No exception thrown"); }
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); }
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)); } } }
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()); } }
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"); }
/// <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); }
public void TestInitialize() { generator = new XmlSchemaTypeGenerator(); }
protected override void DoValidate(string objectToValidate, object currentTarget, string key, ValidationResults validationResults) { if (string.IsNullOrEmpty(objectToValidate)) { return; } ModelElement mel = currentTarget as ModelElement; if (mel == null || (!(mel is XsdMessage) && !(mel is XsdElementFault))) { return; } XmlSchemaElementMoniker elementUri = new XmlSchemaElementMoniker(objectToValidate); // It's a primite type if (string.IsNullOrEmpty(elementUri.ElementName)) { return; } string fileName = Path.GetFileName(elementUri.XmlSchemaPath); string fullPath = GetXsdFullPath(mel, elementUri.XmlSchemaPath); string melName = string.Empty; DomainClassInfo.TryGetName(mel, out melName); if (string.IsNullOrEmpty(fullPath)) { this.LogValidationResult(validationResults, string.Format(CultureInfo.CurrentUICulture, this.invalidFilePathMessage, melName, fileName, schemaDirectory), currentTarget, key); return; } XmlSchemaTypeGenerator generator = new XmlSchemaTypeGenerator(IsXmlSerializer(mel)); CodeCompileUnit unit = null; try { unit = generator.GenerateCodeCompileUnit(fullPath); } catch (InvalidDataContractException exception) { this.LogValidationResult(validationResults, exception.Message, currentTarget, key); return; } catch (InvalidSerializerException serializationException) { if (!IsXmlSerializer(mel)) { this.LogValidationResult(validationResults, string.Format(CultureInfo.CurrentUICulture, this.notCompliantWithDataContractSerializerMessage + ". " + serializationException.Message, fileName), currentTarget, key); return; } } foreach (CodeNamespace ns in unit.Namespaces) { foreach (CodeTypeDeclaration codeType in ns.Types) { if (codeType.Name.Equals(elementUri.ElementName, StringComparison.Ordinal)) { return; } } } this.LogValidationResult(validationResults, string.Format(CultureInfo.CurrentUICulture, this.MessageTemplate, melName, elementUri.ElementName, fileName), currentTarget, key); }