Best way to generate XSD from Java

Discussion in 'Java' started by the don, Sep 28, 2012.

  1. the don

    the don Guest

    Want to tag Methods first, then input tagged Java file to code generator,
    like JavaDoc or Velocity, then output XML Schema with elements generated
    according to parameters provided by tags.

    Better to use JavaDoc Tags or Annotations? What Templating engine(s) do you
    recommend?

    Thanks
     
    the don, Sep 28, 2012
    #1
    1. Advertising

  2. the don

    Arne Vajhøj Guest

    On 9/28/2012 2:30 PM, the don wrote:
    > Want to tag Methods first, then input tagged Java file to code generator,
    > like JavaDoc or Velocity, then output XML Schema with elements generated
    > according to parameters provided by tags.
    >
    > Better to use JavaDoc Tags or Annotations? What Templating engine(s) do you
    > recommend?


    Annotations are a lot easier to process than source code parsing.

    And I assume that you want to write a customer generator.

    The easy solution is JAXB annotations and schemagen.

    Arne
     
    Arne Vajhøj, Sep 28, 2012
    #2
    1. Advertising

  3. the don

    the don Guest

    On Fri, 28 Sep 2012 14:53:43 -0400, Arne Vajhøj wrote:

    > On 9/28/2012 2:30 PM, the don wrote:
    >> Want to tag Methods first, then input tagged Java file to code generator,
    >> like JavaDoc or Velocity, then output XML Schema with elements generated
    >> according to parameters provided by tags.
    >>
    >> Better to use JavaDoc Tags or Annotations? What Templating engine(s) do you
    >> recommend?

    >
    > Annotations are a lot easier to process than source code parsing.
    >
    > And I assume that you want to write a customer generator.
    >
    > The easy solution is JAXB annotations and schemagen.
    >
    > Arne


    Thank you, I will take a look
     
    the don, Sep 28, 2012
    #3
  4. On 28.09.2012 21:21, the don wrote:
    > On Fri, 28 Sep 2012 14:53:43 -0400, Arne Vajhøj wrote:
    >
    >> On 9/28/2012 2:30 PM, the don wrote:
    >>> Want to tag Methods first, then input tagged Java file to code generator,
    >>> like JavaDoc or Velocity, then output XML Schema with elements generated
    >>> according to parameters provided by tags.
    >>>
    >>> Better to use JavaDoc Tags or Annotations? What Templating engine(s) do you
    >>> recommend?

    >>
    >> Annotations are a lot easier to process than source code parsing.
    >>
    >> And I assume that you want to write a customer generator.
    >>
    >> The easy solution is JAXB annotations and schemagen.
    >>
    >> Arne

    >
    > Thank you, I will take a look


    Just a side note: I prefer to create the schema _first_ because that is
    something the user will face (I am thinking of configuration files
    here). So first comes usability for the user and only then I bother how
    that will be transferred into Java land. I use "trang" to generate an
    XSD and modify it manually afterwards.

    http://www.thaiopensource.com/relaxng/trang.html

    If you are designing XML based persistence of Java classes then of
    course your approach is at least equally valid. But I would do that
    only if I am sure no human being needs to edit those XML files.

    Kind regards

    robert


    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Sep 28, 2012
    #4
  5. the don

    the don Guest

    On Fri, 28 Sep 2012 21:34:51 +0200, Robert Klemme wrote:

    > On 28.09.2012 21:21, the don wrote:
    >> On Fri, 28 Sep 2012 14:53:43 -0400, Arne Vajhøj wrote:
    >>
    >>> On 9/28/2012 2:30 PM, the don wrote:
    >>>> Want to tag Methods first, then input tagged Java file to code generator,
    >>>> like JavaDoc or Velocity, then output XML Schema with elements generated
    >>>> according to parameters provided by tags.
    >>>>
    >>>> Better to use JavaDoc Tags or Annotations? What Templating engine(s) do you
    >>>> recommend?
    >>>
    >>> Annotations are a lot easier to process than source code parsing.
    >>>
    >>> And I assume that you want to write a customer generator.
    >>>
    >>> The easy solution is JAXB annotations and schemagen.
    >>>
    >>> Arne

    >>
    >> Thank you, I will take a look

    >
    > Just a side note: I prefer to create the schema _first_ because that is
    > something the user will face (I am thinking of configuration files
    > here). So first comes usability for the user and only then I bother how
    > that will be transferred into Java land. I use "trang" to generate an
    > XSD and modify it manually afterwards.
    >
    > http://www.thaiopensource.com/relaxng/trang.html
    >
    > If you are designing XML based persistence of Java classes then of
    > course your approach is at least equally valid. But I would do that
    > only if I am sure no human being needs to edit those XML files.
    >
    > Kind regards
    >
    > robert


    Let me clarify a little bit, I'm not trying to do what JAXB does. I'm not
    trying to marshall and unmarshall XML info to and from Java. In fact, The
    XSD files produced *will* be compiled with xjc for that purpose but they
    will *not* translate between XML and the annotated Java Classes which were
    used to produce them.

    I intend to automate the Methods in the annotated Java Classes, ie store
    sequences of Method calls in XML files, read them into memory and then
    invoke them in instantiated Object(s) of that type. I would like to start
    with the Classes themselves, tag or annotate the Methods I want to make
    available for automation, then generate an XSD which can be used by JAXB
    for marshalling and unmarshalling, as well as for creating and editing
    these "Command Strings" with validation. Eclipse, for instance, provides
    something like the latter capability

    There is probably some way to tag (or annotate!) a Java file so that a
    Templating engine could generate the desired output, and I doubt that it is
    very difficult. I was just asking to see what experience people here have
    had with this sort of thing and give me some direction.

    Thank you

    P.S. the annotations I'm speaking of would indicate things like a Method's
    name, it's return type and it's arguments. Right now such Methods only take
    and return Java Primitives but that is because XML Schema makes it easier
    to do since it provides simple types that correspond to each of them, and
    that makes validation possible
     
    the don, Sep 28, 2012
    #5
  6. On 28/09/2012 22:54, the don allegedly wrote:
    > Let me clarify a little bit, I'm not trying to do what JAXB does. I'm not
    > trying to marshall and unmarshall XML info to and from Java. In fact, The
    > XSD files produced *will* be compiled with xjc for that purpose but they
    > will *not* translate between XML and the annotated Java Classes which were
    > used to produce them.
    >
    > I intend to automate the Methods in the annotated Java Classes, ie store
    > sequences of Method calls in XML files, read them into memory and then
    > invoke them in instantiated Object(s) of that type. I would like to start
    > with the Classes themselves, tag or annotate the Methods I want to make
    > available for automation, then generate an XSD which can be used by JAXB
    > for marshalling and unmarshalling, as well as for creating and editing
    > these "Command Strings" with validation. Eclipse, for instance, provides
    > something like the latter capability
    >
    > There is probably some way to tag (or annotate!) a Java file so that a
    > Templating engine could generate the desired output, and I doubt that it is
    > very difficult. I was just asking to see what experience people here have
    > had with this sort of thing and give me some direction.
    >
    > Thank you
    >
    > P.S. the annotations I'm speaking of would indicate things like a Method's
    > name, it's return type and it's arguments. Right now such Methods only take
    > and return Java Primitives but that is because XML Schema makes it easier
    > to do since it provides simple types that correspond to each of them, and
    > that makes validation possible


    Uh, indeed, you're not trying to do what JAXB does, and I am doubtful
    whether it will be of use to you at all. An XML document is, an XSD
    describes, and the Java Binding API binds, *data structures*. They have
    no concept of methods as such.

    You'll probably be better off writing your own annotation parser and
    generating the schema on the fly (and pondering whether to use JAXB
    annotations or your own). See
    <http://docs.oracle.com/javase/6/docs/technotes/guides/apt/index.html>
    and
    <http://docs.oracle.com/javase/6/docs/jdk/api/apt/mirror/overview-summary.html>
    for more information.

    --
    DF.
     
    Daniele Futtorovic, Sep 28, 2012
    #6
  7. the don

    Arne Vajhøj Guest

    On 9/28/2012 3:34 PM, Robert Klemme wrote:
    > Just a side note: I prefer to create the schema _first_ because that is
    > something the user will face (I am thinking of configuration files
    > here).


    Contract first vs code first is a classic discussion.

    Arne
     
    Arne Vajhøj, Sep 29, 2012
    #7
  8. the don

    Arne Vajhøj Guest

    On 9/28/2012 4:54 PM, the don wrote:
    > I intend to automate the Methods in the annotated Java Classes, ie store
    > sequences of Method calls in XML files, read them into memory and then
    > invoke them in instantiated Object(s) of that type. I would like to start
    > with the Classes themselves, tag or annotate the Methods I want to make
    > available for automation, then generate an XSD which can be used by JAXB
    > for marshalling and unmarshalling, as well as for creating and editing
    > these "Command Strings" with validation. Eclipse, for instance, provides
    > something like the latter capability
    >
    > There is probably some way to tag (or annotate!) a Java file so that a
    > Templating engine could generate the desired output, and I doubt that it is
    > very difficult. I was just asking to see what experience people here have
    > had with this sort of thing and give me some direction.


    JAXB is for data classes. I don't think it will work for you.

    Annotations and a processor that generates Java code must be the
    right way forward for your problem.

    Arne
     
    Arne Vajhøj, Sep 29, 2012
    #8
  9. the don

    markspace Guest

    On 9/28/2012 1:54 PM, the don wrote:

    > P.S. the annotations I'm speaking of would indicate things like a Method's
    > name, it's return type and it's arguments.



    This sounds suspiciously like information that's already in the class
    files. Are you planning on parsing source files, or interpreting byte
    codes? The javap already does this, to a degree.

    Possibly all you need to do is mark a method for testing; the return
    type and argument list is retained in the byte code .class file.
     
    markspace, Sep 29, 2012
    #9
  10. On 09/29/2012 10:34 AM, markspace wrote:
    > On 9/28/2012 1:54 PM, the don wrote:
    >
    >> P.S. the annotations I'm speaking of would indicate things like a
    >> Method's
    >> name, it's return type and it's arguments.

    >
    > This sounds suspiciously like information that's already in the class
    > files. Are you planning on parsing source files, or interpreting byte
    > codes? The javap already does this, to a degree.


    Exactly what I would have remarked: _all_ that information is already
    available in the class file.

    > Possibly all you need to do is mark a method for testing; the return
    > type and argument list is retained in the byte code .class file.


    I would even go as far as to claim that it is harmful to include said
    information in the annotation because it is redundant and won't
    automatically be adjusted if the class is changed. Then there will be
    conflicting information.

    Kind regards

    robert
     
    Robert Klemme, Sep 29, 2012
    #10
  11. On 09/28/2012 10:54 PM, the don wrote:

    > Let me clarify a little bit, I'm not trying to do what JAXB does. I'm not
    > trying to marshall and unmarshall XML info to and from Java. In fact, The
    > XSD files produced *will* be compiled with xjc for that purpose but they
    > will *not* translate between XML and the annotated Java Classes which were
    > used to produce them.


    A few lines down you say "generate an XSD which can be used by JAXB for
    marshalling and unmarshalling".

    > I intend to automate the Methods in the annotated Java Classes, ie store
    > sequences of Method calls in XML files, read them into memory and then
    > invoke them in instantiated Object(s) of that type. I would like to start
    > with the Classes themselves, tag or annotate the Methods I want to make
    > available for automation, then generate an XSD which can be used by JAXB
    > for marshalling and unmarshalling, as well as for creating and editing
    > these "Command Strings" with validation. Eclipse, for instance, provides
    > something like the latter capability
    >
    > There is probably some way to tag (or annotate!) a Java file so that a
    > Templating engine could generate the desired output, and I doubt that it is
    > very difficult. I was just asking to see what experience people here have
    > had with this sort of thing and give me some direction.


    You can have javac use an annotation processor, if you want to do your
    work on compilation time. In your case you would likely need to to some
    processing after compiling all classes in order to be able to combine
    information obtained from individual classes into a single XSD (if only
    for including all other XSD snippets).

    From what I understand about your goal you should probably generally
    distinguish two things here:

    1. creating objects
    2. invoking methods

    JAXB might help you with 1 but certainly not with 2. Also, you need a
    way to define variables to store references to created objects in so you
    can use them in method calls. So, basically your XML needs to be able
    to store... what? Basically you want an interpreter to execute Java
    code, don't you? Thinking a few corners further you'll notice that
    there is actually already a way to specify sequences of method
    invocations: Java code. You might be better off with some BeanShell
    solution in the end. You get editors with semantic insight for free
    (it's your IDE) etc.

    Kind regards

    robert

    http://www.beanshell.org/
     
    Robert Klemme, Sep 29, 2012
    #11
  12. the don

    Roedy Green Guest

    On Fri, 28 Sep 2012 14:30:51 -0400, the don <> wrote,
    quoted or indirectly quoted someone who said :

    >Want to tag Methods first, then input tagged Java file to code generator,
    >like JavaDoc or Velocity, then output XML Schema with elements generated
    >according to parameters provided by tags.


    You might want to tackle this by parsing the Java to a tree, then
    generating what you need directly. See
    http://mindprod.com/jgloss/parser.html
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    The iPhone 5 is a low end Rolex.
     
    Roedy Green, Sep 30, 2012
    #12
  13. On 30.09.2012 13:03, Roedy Green wrote:
    > On Fri, 28 Sep 2012 14:30:51 -0400, the don <> wrote,
    > quoted or indirectly quoted someone who said :
    >
    >> Want to tag Methods first, then input tagged Java file to code generator,
    >> like JavaDoc or Velocity, then output XML Schema with elements generated
    >> according to parameters provided by tags.

    >
    > You might want to tackle this by parsing the Java to a tree, then
    > generating what you need directly. See


    .... or create a DSL right away if the functionality should be restricted
    in some way compared to what one can do with pure Java.

    Cheers

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Sep 30, 2012
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus
    Replies:
    1
    Views:
    1,109
    Markus
    Nov 22, 2005
  2. Rick Razzano

    XSD document for XSD defintion

    Rick Razzano, Sep 26, 2003, in forum: XML
    Replies:
    1
    Views:
    493
    C. M. Sperberg-McQueen
    Sep 26, 2003
  3. Replies:
    1
    Views:
    864
    Martin Honnen
    Jan 14, 2004
  4. Peter Aberline

    xsd:any as a child of xsd:all

    Peter Aberline, Apr 5, 2004, in forum: XML
    Replies:
    0
    Views:
    794
    Peter Aberline
    Apr 5, 2004
  5. beethoven_forever
    Replies:
    0
    Views:
    357
    beethoven_forever
    Jul 15, 2005
Loading...

Share This Page