Coder Social home page Coder Social logo

sshaw / jaxb2ruby Goto Github PK

View Code? Open in Web Editor NEW
12.0 4.0 9.0 111 KB

Generate pure Ruby objects from an XML schema (XSD) using JAXB and JRuby. Use your favorite XML to object mapper or a custom ERB template.

Ruby 89.11% HTML 10.89%
ruby xml jruby xml-serialization xml-format jaxb xml-schema erb object-mapping

jaxb2ruby's Introduction

jaxb2ruby

Build Status Code Climate

Generate pure Ruby classes from an XML schema using JAXB and JRuby

Usage

usage: jaxb2ruby [options] schema
    -c, --classes=MAP1[,MAP2,...]    XML Schema type to Ruby class mappings
                                     MAP can be a string in the form type=class or a YAML file of type/class pairs
    -h, --help                       Show this message
    -I, --include=DIRECTORY          Add DIRECTORY to the load path, usefull for using custom template helpers
    -J, --jvm=[ARG1[,ARG2,...]]      Options to pass to the JVM when calling XJC
    -n, --namespace=MAP1[,MAP2,...]  XML namespace to ruby class mappings
                                     MAP can be a string in the form namespace=class or a YAML file of namespace/class pairs
    -o, --output=DIRECTORY           Directory to output the generated ruby classes, defaults to ruby
    -t, --template=NAME              Template used to generate the ruby classes
                                     Can be a path to an ERB template or one of: roxml (default), happymapper, ruby
    -v, --version                    jaxb2ruby version
    -w, --wsdl                       Treat the schema as a WSDL
                                     Automatically set if the schema has a `.wsdl' extension
    -x, --xjc=PATH                   Path to the XJC executable to use; defaults to xjc

Instalation

gem install jaxb2ruby

jaxb2ruby must be installed and ran under JRuby. The generated classes will not depend on JRuby.

If your underlying JDK version is >= 9 you must use JRuby >= 9.2.12.0.

If your underlying JDK version is >= 11, you must manually install JAXB. In this case you'll likely run jaxb2ruby using the following (on *nix):

CLASSPATH=path/to/jaxb-ri/mod/jakarta.xml.bind-api.jar jaxb2ruby -x path/to/xjc.sh schema.xsd

Ruby Class Mappings

XML Schema Built-in Types

Certain XML schema types are converted to Symbols. You can specify your own XML Schema to Ruby type mapping(s) via the -c option.

For a complete list of mappings see the SCHEMA_TO_RUBY constant.

XML Schema Complex Types

Complex schema types will camelized and turned into Ruby classes. If a type has a namespace the namespace will be converted into a module and the resulting class will be placed inside.

Namespaces are turned into modules using a slightly modified version of the rules outlined in the The Java Architecture for XML Binding (JAXB) 2.0 Section D.5.1 Mapping from a Namespace URI. The differences being:

  • The list of module/package strings are joined on "::"
  • A module/package string beginning with "_" is replaced with "V"
  • Nested, anonymous XML schema types become Ruby inner classes

Some examples:

{http://example.com}User becomes Com::Example::User

{http://example.com/api/15}User becomes Com::Example::Api::V15::User

An XML schema type {http://example.com}User that contains the nested complex type Addresses, which itself contains the type Address will result in the creation of 3 classes: User, User::Addresses and User::Addresses::Address, all within the Com::Example namespace.

You can specify your own namespace to class mapping(s) via the -n option. Namespace mappings have a lower precedence than type mappings.

Code Templates

jaxb2ruby uses ERB templates to create Ruby classes. You can use one of the bundled templates or create your own. Use the -t option to specify the path to a custom template or one of the following bundled ones:

Note that "plain 'ol Ruby classes" does not perform XML serialization.

Rolling out your own templates

Use the -t option to specify the path to your template. This must be a path else it will be interpreted as a jaxb2ruby template. Two variables will be provided to your template:

  1. @class, an instance of RubyClass
  2. VERSION the version of jaxb2ruby

See lib/templates for some examples.

You can use helper functions in your templates by providing the helper file's directory to the -I option.

TODO

  • Map java.util.Hash
  • Do something with org.w3c.dom.*
  • Don't treat XML Schema types as elements
  • Circular dependencies, currently can be resolved by manually adding forward declarations
  • Fix other things that surely don't work

See Also

  • class2 - easily create class hierarchies that support nested attributes, type conversion, equality, and more, all from a hash specification

Author

Skye Shaw [sshaw AT gmail.com]

License

Released under the MIT License: www.opensource.org/licenses/MIT

jaxb2ruby's People

Contributors

flavorjones avatar jweir avatar sshaw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

jaxb2ruby's Issues

What do I put in the class mappings YAML file?

Thank you for releasing jaxb2ruby. I am trying to convert a schema that I don’t control into Ruby classes. However, I get the recommendation:

• [ERROR] Complex type and its child element share the same name "ScenarioType". Use a class customization to resolve this conflict. line 2265 of file:/C:/jaxb/BuildingSync.xsd

I see that I can use the –c option to specify a YAML file, but I’m not really sure what to put in the YAML file. I have attached the full XSD for reference and have also made an excerpt below that illustrates my specific problem. I think a small sample YAML file will allow me to find my way forward on my own.

--- cut out extra ---
<xs:complexType name="ScenarioType">
xs:sequence
<xs:element name="ScenarioName" type="xs:string" minOccurs="0">
xs:annotation
xs:documentationName of the scenario for which energy use data is included. This may include benchmarks, baselines, and improved cases. For retrofits, each package represents a different scenario.</xs:documentation>
</xs:annotation>
</xs:element>
--- cut out extra ---
<xs:element name="ScenarioType" minOccurs="0">
xs:annotation
xs:documentationType of scenario for which energy use is presented.</xs:documentation>
</xs:annotation>
xs:complexType
xs:choice
--- cut out extra ---

By the way , I tried to send this request to your email sshaw AT gmail.com and GMail rejected it. Yes, I made the obvious corrections.

Thanks!

@sshaw I wasn't sure how to get in contact with you, but this gem worked brilliantly (inside of a Docker container)! Thanks for making it 🎉

Class generation failed: undefined method `split' for nil:NilClass

Error appears while running generator with schema files taken from OpenTravel site:

http://opentravelmodel.net/pubs/content/specifications/downloads/2007A/OTA_1_0/2007A_10_Publication.zip

(this link requires accepting license agreement and some form filling to access)

$ unzip 2007A_10_Publication.zip
$ jaxb2ruby -t roxml _OTA2007A_XML/OTA_VehAvailRateRQ.xsd
outputting classes to ruby
generating: org/opentravel/ota/v2003/v05/acceptable_payment_cards_info_type/acceptable_payment_cards/acceptable_payment_card.rb
generating: org/opentravel/ota/v2003/v05/acceptable_payment_cards_info_type/acceptable_payment_cards.rb
generating: org/opentravel/ota/v2003/v05/acceptable_payment_cards_info_type.rb
generating: org/opentravel/ota/v2003/v05/accepted_payments_type.rb
generating: org/opentravel/ota/v2003/v05/address_info_type.rb
generating: org/opentravel/ota/v2003/v05/address_pref_type.rb
generating: org/opentravel/ota/v2003/v05/address_type/bldg_room.rb
generating: org/opentravel/ota/v2003/v05/address_type/street_nmbr.rb
generating: org/opentravel/ota/v2003/v05/address_type.rb
generating: org/opentravel/ota/v2003/v05/bank_acct_type.rb
generating: org/opentravel/ota/v2003/v05/blackout_date_type.rb
generating: org/opentravel/ota/v2003/v05/cancel_info_rq_type.rb
generating: org/opentravel/ota/v2003/v05/cancel_info_rs_type/cancel_rules.rb
generating: org/opentravel/ota/v2003/v05/cancel_info_rs_type.rb
generating: org/opentravel/ota/v2003/v05/cancel_rule_type.rb
generating: org/opentravel/ota/v2003/v05/comment_type/comment.rb
generating: org/opentravel/ota/v2003/v05/comment_type.rb
generating: org/opentravel/ota/v2003/v05/commission_type/commission_payable_amount.rb
generating: org/opentravel/ota/v2003/v05/commission_type/commissionable_amount.rb
generating: org/opentravel/ota/v2003/v05/commission_type/flat_commission.rb
generating: org/opentravel/ota/v2003/v05/commission_type/prepaid_amount.rb
generating: org/opentravel/ota/v2003/v05/commission_type.rb
generating: org/opentravel/ota/v2003/v05/common_pref_type.rb
class generation failed: undefined method `split' for nil:NilClass
backtrace: 
/home/dev/.rvm/gems/jruby-1.7.16@jaxb/gems/jaxb2ruby-0.0.1-java/lib/jaxb2ruby/classes.rb:171:in `requires'
org/jruby/RubyArray.java:2412:in `map'
/home/dev/.rvm/gems/jruby-1.7.16@jaxb/gems/jaxb2ruby-0.0.1-java/lib/jaxb2ruby/classes.rb:171:in `requires'
(erb):8:in `result'
org/jruby/RubyKernel.java:1101:in `eval'
/home/dev/.rvm/rubies/jruby-1.7.16/lib/ruby/1.9/erb.rb:838:in `result'
/home/dev/.rvm/gems/jruby-1.7.16@jaxb/gems/jaxb2ruby-0.0.1-java/lib/jaxb2ruby/template.rb:30:in `build'
/home/dev/.rvm/gems/jruby-1.7.16@jaxb/gems/jaxb2ruby-0.0.1-java/bin/jaxb2ruby:81:in `(root)'
org/jruby/RubyIO.java:1181:in `open'
/home/dev/.rvm/gems/jruby-1.7.16@jaxb/gems/jaxb2ruby-0.0.1-java/bin/jaxb2ruby:81:in `(root)'
org/jruby/RubyArray.java:1613:in `each'
/home/dev/.rvm/gems/jruby-1.7.16@jaxb/gems/jaxb2ruby-0.0.1-java/bin/jaxb2ruby:78:in `(root)'
org/jruby/RubyKernel.java:1081:in `load'
/home/dev/.rvm/gems/jruby-1.7.16@jaxb/bin/jaxb2ruby:1:in `(root)'
org/jruby/RubyKernel.java:1101:in `eval'
/home/dev/.rvm/gems/jruby-1.7.16@jaxb/bin/jruby_executable_hooks:15:in `(root)'

Support for JRuby > 1.9.3

This looks to be the only problem:

JAXB2Ruby::Template::a class created by the ruby template#test_0003_has singular read/write accessors for attributes:
ArgumentError: wrong number of arguments (1 for 0)
    /Users/sshaw/code/ruby/jaxb2ruby/jaxb2ruby/lib/jaxb2ruby/converter.rb:75:in `find_namespace'
    /Users/sshaw/code/ruby/jaxb2ruby/jaxb2ruby/lib/jaxb2ruby/converter.rb:93:in `translate_type'
    /Users/sshaw/code/ruby/jaxb2ruby/jaxb2ruby/lib/jaxb2ruby/converter.rb:135:in `extract_class'
    /Users/sshaw/code/ruby/jaxb2ruby/jaxb2ruby/lib/jaxb2ruby/converter.rb:233:in `block in extract_classes'
    org/jruby/RubyArray.java:1733:in `each'
    /Users/sshaw/code/ruby/jaxb2ruby/jaxb2ruby/lib/jaxb2ruby/converter.rb:230:in `extract_classes'
    /Users/sshaw/code/ruby/jaxb2ruby/jaxb2ruby/lib/jaxb2ruby/converter.rb:42:in `create_ruby_classes'
    /Users/sshaw/code/ruby/jaxb2ruby/jaxb2ruby/lib/jaxb2ruby/converter.rb:29:in `convert'
    /Users/sshaw/code/ruby/jaxb2ruby/jaxb2ruby/lib/jaxb2ruby/converter.rb:14:in `convert'
    /Users/sshaw/code/ruby/jaxb2ruby/spec/spec_helper.rb:10:in `convert'
    /Users/sshaw/code/ruby/jaxb2ruby/spec/template_spec.rb:68:in `block in (root)'
    org/jruby/RubyBasicObject.java:1687:in `instance_eval'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.