{"id":13816077,"url":"https://github.com/reaster/schema-gen","last_synced_at":"2025-04-29T21:30:41.336Z","repository":{"id":42653727,"uuid":"113472309","full_name":"reaster/schema-gen","owner":"reaster","description":"XML Schema code generator outputting Swift, Kotlin and Java","archived":false,"fork":false,"pushed_at":"2021-02-25T19:44:49.000Z","size":425,"stargazers_count":29,"open_issues_count":6,"forks_count":7,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-05T19:23:39.377Z","etag":null,"topics":["code-generation","dart","flutter","jackson","jackson-json-processor","java","json","kotlin","marshalling","schema-gen","schema-generation","serialization","swift","xml-schema"],"latest_commit_sha":null,"homepage":null,"language":"Groovy","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/reaster.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-12-07T16:13:20.000Z","updated_at":"2025-02-07T17:22:48.000Z","dependencies_parsed_at":"2022-08-29T08:11:14.270Z","dependency_job_id":null,"html_url":"https://github.com/reaster/schema-gen","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reaster%2Fschema-gen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reaster%2Fschema-gen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reaster%2Fschema-gen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/reaster%2Fschema-gen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/reaster","download_url":"https://codeload.github.com/reaster/schema-gen/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251585738,"owners_count":21613270,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["code-generation","dart","flutter","jackson","jackson-json-processor","java","json","kotlin","marshalling","schema-gen","schema-generation","serialization","swift","xml-schema"],"created_at":"2024-08-04T05:00:33.194Z","updated_at":"2025-04-29T21:30:40.874Z","avatar_url":"https://github.com/reaster.png","language":"Groovy","readme":"# schema-gen\nschema-gen is a multi-language, interoperable, XML Schema code generator. \nThe generated code allows reading, writing, manipulating, and transmitting data in the two most widely used industry formats: XML and JSON. \nCurrently supported languages are Java 8, Kotlin 1, Swift 4 and Dart 2.1. The code is interoperable, meaning it's well suited for developing cross-language, mobile, client-server applications.\n\nProducing concise, readable code is a top schema-gen priority, providing a high level of flexibility and configurability.\n\nThis software is written in Groovy and is packaged as a Gradle plugin.\n\n[![Build Status](https://travis-ci.org/reaster/schema-gen.svg?branch=master)](https://travis-ci.org/reaster/schema-gen)\n\n## Currently Supported Languages\n\n### Java\n**Features:** Generated POJOs contain [Jackson](https://github.com/FasterXML/jackson-dataformat-xml) annotations supporting reading and writing both JSON and XML documents. XML Schema restrictions are translated into [Bean Validation 2.0](http://beanvalidation.org/) annotations. Equals, hashCode and toString methods are also generated to facilitate testing. See the [java-gpx](https://github.com/reaster/schema-gen-examples/tree/master/java-gpx) sample project.\n\n**Usage:** The main entry point is [com.javagen.schema.java.JavaGen](https://github.com/reaster/schema-gen/blob/master/src/main/groovy/com/javagen/schema/java/JavaGen.groovy) which can be invoked directly or via the gradle plugin. By default, the generated code is placed in the src/main/java-gen folder to keep it separate from hand-written code.\n\n**Limitations:** A limitation of the Java langauge is that it does not support the extension features of more recent languages, making it harder to maintain a clean separation between hand-written and generated code. You may be forced to put your business logic in the generated files with the downside being, if you ever have to regenerate, you'll have to manually merge your code back in.\n\n### Kotlin\n**Features:** By leveraging [data classes](https://kotlinlang.org/docs/reference/data-classes.html), Kotlin generated code is very concise. Classes contain [Jackson](https://github.com/FasterXML/jackson-dataformat-xml) annotations supporting reading and writing both JSON and XML documents. XML Schema restrictions are translated into [Bean Validation 2.0](http://beanvalidation.org/) annotations. Extending generated code with business logic can be achieved using [extensions](https://kotlinlang.org/docs/reference/extensions.html). See the [kotlin-gpx](https://github.com/reaster/schema-gen-examples/tree/master/kotlin-gpx) sample project.\n\n**Usage:** The main entry point is [com.javagen.schema.kotlin.KotlinGen](https://github.com/reaster/schema-gen/blob/master/src/main/groovy/com/javagen/schema/kotlin/KotlinGen.groovy) which can be invoked directly or via the gradle plugin. By default, the generated code is placed in the src/main/kotlin-gen folder to keep it separate from hand-written code.\n\n**Limitations:** The code generator attempts to create no-argument constructors by setting default values on every property, which in some cases can cause problems. To minimize unessasary annotations, the generated code requires Java 8 parameter name support. See the build.gradle and unit tests in [kotlin-gpx](https://github.com/reaster/schema-gen-examples/tree/master/kotlin-gpx) for proper setup and configuration.\n\n### Swift\n**Features:** schema-gen generates code utilizing the built-in Encodable and Decodable JSON support introduced in Swift 4. Extending generated code with business logic can be achieved using Extensions. See the [swift-gpx](https://github.com/reaster/schema-gen-examples/tree/master/swift-gpx) sample project.\n\n**Usage:** The main entry point is [com.javagen.schema.swift.SwiftGen](https://github.com/reaster/schema-gen/blob/master/src/main/groovy/com/javagen/schema/swift/SwiftGen.groovy) which can be invoked directly or via the gradle plugin. By default, the generated code is placed in the src/swift-gen folder to keep it separate from hand-written code.\n\n**Limitations:** Swift only supports JSON serialization. Assuming you're server is written in Kotlin or Java, communication with a Swift client can utilize JSON, even if the documents are stored as XML, thanks to Jackson's support of both. However, given a good XMLEncoder, XML support should be straight forward to add. The author wrote [saxy](https://github.com/reaster/saxy) in Objective-C the last time around and it's somebody else's turn to do this for Swift ;-)\n\n### Dart\n**Features:** schema-gen generates model code decorated with [json_annotation](https://pub.dartlang.org/packages/json_annotation) JSON directives. Extending generated code with business logic can be achieved using Dart's **part** support.\n\n**Usage:** The main entry point is [com.javagen.schema.dart.DartGen](https://github.com/reaster/schema-gen/blob/master/src/main/groovy/com/javagen/schema/dart/DartGen.groovy) which can be invoked directly or via the gradle plugin. By default, the generated code is placed in the *lib* folder.\n\n**Limitations:** Dart only supports JSON serialization. Assuming you're server is written in Kotlin or Java, communication with a Dart client can utilize JSON, even if the documents are stored as XML, thanks to Jackson's support of both. \n\n## Usage\n\n### Gradle Plugin\n**Note: currently not being supported.**\n\nschema-gen includes a Gradle Plugin which can be added to your gradle.build file by including it in your buildscript, and applying and configuring it:\n  \n```\nbuildscript {\n    repositories {\n        maven {\n            url \"https://plugins.gradle.org/m2/\"\n        }\n    }\n    dependencies {\n        classpath 'com.javagen:schema-gen:0.9.1'\n    }\n}\n\napply plugin: 'com.javagen.schema-gen'\n\nschemaGen {\n    swift {\n        schemaURL = new URL('http://www.topografix.com/gpx/1/1/gpx.xsd')\n    }\n}\n\n```  \nTo generate code use the gen task:\n```\nswift-gpx\u003e gradle gen\n```\nTo remove the generated code, use the genClean task:\n```\nswift-gpx\u003e gradle genClean\n```\n\nTo install the Gradle plugin locally, use the maven install task:\n```\nschema-gen\u003e gradle install\n```\nA good starting point is to download the [schema-gen-examples](https://github.com/reaster/schema-gen-examples) project, regenerate the code and run the tests:\n```\nschema-gen-examples\u003e gradle genClean gen test\n```\n### Running the Generators in your IDE\n\nFor code generator development work, you'll want to run the target language gen class (JavaGen, KotlinGen, SwiftGen) directly.  There are three main launcher classes you can adpot for this purpose (JavaGenMain, KotlinGenMain, SwiftGenMain). If you place your project (say, java-atom) in the same parent directory as [schema-gen](https://github.com/reaster/schema-gen), you can hard-code your configuration in the [JavaGenMain](https://github.com/reaster/schema-gen/blob/master/src/main/groovy/com/javagen/schema/java/JavaGenMain.groovy) class as follows:\n```\nclass JavaGenMain extends JavaGen\n{\n    def initAtom()\n    {\n        schemaURL = new URL('file:../java-atom/src/main/resources/atom.xsd')\n        srcDir = new File('../java-atom/src/main/java-gen')\n        packageName = 'org.w3.atom.java'\n        addSuffixToEnumClass = null\n        anyPropertyName = 'text'\n    }\n\n    JavaGenMain()\n    {\n        super()\n        initAtom()\n    }\n\n    static void main(String[] args) {\n        new JavaGenMain().gen()\n    }\n}\n```\nThen you can point your IDE at JavaGenMain in the  [schema-gen](https://github.com/reaster/schema-gen) project and the generated code will be put in your java-atom project. See [schema-gen-examples/java-atom](https://github.com/reaster/schema-gen-examples/tree/master/java-atom) for working atom.xsd and xml.xsd files.\n\n#### Configuration\nThe code generator is designed to be highly customizable. In particular, how it names classes, properties, and enumerations is all encoded in configurable properties and lambdas. Here is a sampling of the properties that can be overridden in the `Gen` base class:\n```\nURL schemaURL = new URL('http://www.topografix.com/gpx/1/1/gpx.xsd')\nFile srcDir = new File('src/main/java-gen')\nList\u003cMVisitor\u003e pipeline = []\nPluralService pluralService = new PluralService()\ndef customPluralMappings = [:] //needed for irregular nouns: tooth-\u003eteeth, person-\u003epeople\nboolean useOptional = false //just effects Java code: Integer vs Optional\u003cInteger\u003e\nString packageName = null\nString addSuffixToEnumClass = 'Enum'\nString removeSuffixFromType = 'Type'\nString fileExtension = 'java'\n\nFunction\u003cString,String\u003e packageNameFunction = { ns -\u003e packageName ?: ns ? GlobalFunctionsUtil.javaPackageFromNamespace(ns, true) : 'com.javagen.model' }\nFunction\u003cString,String\u003e enumNameFunction = { text -\u003e GlobalFunctionsUtil.javaEnumName(text, false) }\nFunction\u003cString,String\u003e enumValueFunction = { text -\u003e text }\nFunction\u003cString,String\u003e enumClassNameFunction = { text -\u003e GlobalFunctionsUtil.enumClassName(text, addSuffixToEnumClass) }\nFunction\u003cString,String\u003e classNameFunction = { text -\u003e GlobalFunctionsUtil.className(text, removeSuffixFromType) }\nFunction\u003cString,String\u003e propertyNameFunction = { text -\u003e GlobalFunctionsUtil.legalJavaName(lowerCase(text)) }\nFunction\u003cString,String\u003e constantNameFunction = { text -\u003e GlobalFunctionsUtil.javaConstName(text) }\nFunction\u003cString,String\u003e collectionNameFunction = { singular -\u003e customPluralMappings[singular] ?: pluralService.toPlural(singular) }\nFunction\u003cString,String\u003e simpleXmlTypeToPropertyType\nBiFunction\u003cGen,MClass,File\u003e classOutputFileFunction = { gen,clazz -\u003e new File(gen.srcDir, GlobalFunctionsUtil.pathFromPackage(clazz.fullName(),fileExtension))} //default works for Java\n\n```\nThese properties can be set in the Gradle plugin for each of the target languages:\n```\nschemaGen {\n    kotlin {\n        srcDir = new File('../kotlin-gpx/src/main/kotlin')\n    }\n    swift{\n        srcDir = new File('../swift-gpx/src/swift-gen')\n    }\n    java {\n        srcDir = new File('src/main/java')\n        packageName = 'com.javagen.model'\n    }\n\n }\n\n```\n\n## Status\nAlthough the design went through three iterations and is now stable as of 2017, the code is still being cleaned up. It has yet to be tested against a wide variety of schemas, so issues should be expected when trying new schemas. \n\n## Limitations\n#### Xml Schema\n##### Mixed Content\nThis code generator is not intended to support document-centric (versus data-centric) code like HTML. In particular, mixed content (mixed tags and text) is not well supported and will often be mapped to a single string property.\n##### AttributeGroup\nCurrently, AttributeGroup are in-lined (i.e. expaned where they are referenced) and not mapped to a specific, re-usable entity (interface, class, trait, etc.)\n##### Group\nCurrently, Group elements are in-lined (i.e. expanded where they are referenced) and not mapped to a specific, re-usable entity (interface, class, trait, etc.)\n##### Union\nUnions often consist of 2 or more `TextOnlyType`s merged together with their attached restrictions. If these restrictions are all enumerations, the union of unique values will be modeled properly. However, in other cases - say mixing numeric and string values - you will get a warning and probably an incorrect mapping.\n\n___\n\n## Architecture\nFor developers wishing to extend this framework, here is a quick overview of how it's put together.\n#### XmlSchemaNormalizer\nTo simplify the schema-to-code translation process, the XML schema is normalized (references removed, etc.) as the first step in the code generation process. The results are stored in a Schema instance which tracks schema types (TestOnlyType, SimpleType and ComplextType), elements, attributes and other needed constructs. QNames support mixed namespace schemas.\n#### Gen\nThe translation classes (JavaGen, KotlinGen, SwiftGen, etc.) walk the normalized schema and generate an abstract code model: MModule, MClass, MProperty, etc.\n#### Callbacks\nCode generation for supported third party libraries is handled via callback classes that typically set annotations and interfaces expected by the library. See [KotlinJacksonCallback](https://github.com/reaster/schema-gen/blob/master/src/main/groovy/com/javagen/schema/kotlin/KotlinJacksonCallback.groovy) for an example. \n#### Emitters\nAn emitter exists for each supported language and takes the abstact model and converts it into the target computer langauge. See [JavaEmitter](https://github.com/reaster/schema-gen/blob/master/src/main/groovy/com/javagen/schema/java/JavaEmitter.groovy) as an example.\n#### PreEmitters\nThese do the grunt work of generating boiler-plate code for methods such as equals, hashCode and toString. The developer can simply add a method with the proper stereotype and the PreEmitter will generate the rest. See [SwiftPreEmitter](https://github.com/reaster/schema-gen/blob/master/src/main/groovy/com/javagen/schema/swift/SwiftPreEmitter.groovy) as an example.\n#### TypeRegistry\nEach langauge needs a type registry, specific to it's supported types along with how to translate these from XML schema types.\n\n___\n\n## Change Log\n\n#### 0.9.1\nAt the schema level, add suport for compositors (all, sequence, choice), support for substitutionGroup, add double and float types, and proper targetNamespace vs default namespace handling. Kotlin reserved words now handled properly. Java inheritance support added.\n#### 0.9.0\nFirst release. Language support: Java, Kotlin and Swift 4. Tested XML schemas: GPX, Atom.\n\n___\n\n## Support\nPull requests are welcome! Professional support is available from the Outsource Cafe, Inc.\n","funding_links":[],"categories":["Groovy"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freaster%2Fschema-gen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Freaster%2Fschema-gen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freaster%2Fschema-gen/lists"}