6

I want to generate an object model out of an RelaxNG Schema.

Therefore I want to use the RNGOM Object Model/Parser (mainly because I could not find any alternative - although I don't even care about the language the parser is written in/generates). Now that I checked out the RNGOM source from SVN, I don't have ANY idea how to use RNGOM, since there is not any piece of information out there about the usage.

A useful hint how to start with RNGOM - a link, example, or any description which saves me from having to read understand the whole source code of RNGOM - will be awarded as an answer. Even better would be a simple example how to use the parser to generate an Object model out of an RNG file.

More infos:

I want to generate Java classes out of the following RelaxNG Schema:
http://libvirt.org/git/?p=libvirt.git;a=tree;f=docs/schemas;hb=HEAD

I found out that the Glassfish guys are using rngom to generate the same object model I need, but I could not yet find out how they are using rngom.

Wolkenarchitekt
  • 20,170
  • 29
  • 111
  • 174
  • Haven't used it myself. Have you considered contacting the author? He has Twitter/LinkedIn/Facebook accounts up at http://kohsuke.org/ – Ryan Schipper Sep 30 '11 at 11:08

2 Answers2

3

A way to proceed could be to :

  1. use jing to convert from Relax NG to XML Schema (see here)
  2. use more common tools to generate classes (e.g. JaxB).
Vincent Biragnet
  • 2,950
  • 15
  • 22
  • 2
    He asks "How to convert RelaxNG to object model" and your response is effectively, "Don't use RelaxNG, use something else". That is fine as a comment, but it is most definitely not an answer. – Recurse Dec 13 '12 at 01:11
  • Let me clarify my point of view : for me it's perfect to use Relax NG (it's easier to implement and more expressive than XML Schema, so I never say "don't use it"). If you want to have Objects, nothing forbid you to use Relax Ng as a data contract and then produce XML Schema code with Jing (one shot), and then use JaxB. Relax NG Data contract is here as a strong data interface, you validate with it. When you have valid data : XML files can be transformed into object with JaxB. – Vincent Biragnet Dec 13 '12 at 09:40
  • @Recurse no; this is the correct answer. You misunderstand what RelaxNG is. RelaxNG is a grammar extension to the xml grammar. The fact that is can be expressed via a compact syntax is irelavant. The entire point of the compact syntax is that it converts to xml. Once you have xml, you then use pre-existing tools to generate your object model automatically. – dcow Nov 03 '13 at 08:37
  • @DavidCowden that is a very odd response. RelaxNG has two standardised serialisations, a compact syntax, and an xml syntax; however, the latter is NOT an XSchema, but merely a RelaxNG schema formatted in xml. XSchema tools WILL NOT work on such a file. If you want to use XSchema tools on a RelaxNG file, you must first convert it to XML Schema (@Vincent suggests jing). Depending on your usecase that may or may not be appropriate. The OP explicitly asked for help with a direct RelaxNG->Model tool, hence my comment. – Recurse Feb 10 '14 at 03:40
  • @Recurse Both valid points. I didn't know there was a distinction between XML Schema and the xml serialization of RelaxNG. Thanks for clarifying. I see you point now. Do you have a solution that will take either of the RelaxNG syntax forms and convert to directly to Java POJOs without going through XSchema tools? – dcow Feb 10 '14 at 23:26
  • @DavidCowden Unfortunately, no. This is a great pity as RelaxNG is superior to XSchema in every way I can think of, _except_ for tooling. I found this question in the process of investigating if this situation had improved since I last looked. It should be possible to map RelaxNG to JAXB, but I haven't had the time to write the mapping myself—but that is probably the way to go. – Recurse Feb 11 '14 at 01:10
  • @Recurse hmm okay. Well I will keep this thread updated if we decide to write tooling ourselves. I doubt that will happen anytime in the near future though /= – dcow Feb 11 '14 at 02:40
  • FYI, there are perfectly valid Relax NG schemas that cannot be converted to XML Schemas (for example, atom's schema) – asm0dey Aug 03 '23 at 19:01
0

Hi I ran into mostly the same requirement except I am concentrating on the Compact Syntax. Here is one way of doing what you want but YMMV.

To give some context, my goal in 2 phases: (a) Trying to slurp RelaxNG Compact Syntax and traverse an object/tree to create Spring 4 POJOs usable in Spring 4 Rest Controller. (b) From there I want to develop a request validator that uses the RNG Compact and automatically validates the request before Spring de-serializes the request. Basically scaffolding JSON REST API development using RelaxNG Compact Syntax as both design/documentation and JSON schema definition/validation.

For the first objective I thought about annotating the CompactSyntax with JJTree but I am obviously not fluent in JavaCC so I decided to go a more programatic approach...

I analyzed and tested the code in several ways to determine if there was a tree implementation in binary, digested and/or nc packages but I don't think there is one (an om/tree) as such.

So my latest, actually successful approach, has been to build upon binary and extend SchemaBuilderImpl, implement the visitor interface, and passing my custom SchemaBuilderImpl to CompactSyntax using the long constructor: CompactSyntax(CompactParseable parseable, Reader r, String sourceUri, SchemaBuilder sb, ErrorHandler eh, String inheritedNs)

When you call CompactParseable.parse you will get structured events in the visitor interface and I think this is good enough to traverse the rng schema and from here you could easily create an OM or tree.

But I am not sure this is the best approach. Maybe I missed something and there is in fact an OM/Tree built by the rngom implementation (in my case CompactSyntax) that you can traverse to determine parent/child relationships more easily. Or maybe there are other approaches to this.

Anyway, this is one approach that seems to be working for what I want. Is mostly visitor pattern based and since the interfaces were there I decided to use them. Maybe it will work for you. Bottom line, I could not find an OM/AST that can be traversed implemented anywhere in the implementation packages (nc, binary, digested).

aimass
  • 408
  • 5
  • 11