History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: CASTOR-1966
Type: New Feature New Feature
Status: Open Open
Priority: Major Major
Assignee: Werner Guttmann
Reporter: Régis Décamps
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
castor

Code generation should allow to access <xs:annotation> node

Created: 07/May/07 03:21 PM   Updated: 04/Jan/08 03:13 AM
Component/s: XML code generator
Affects Version/s: 1.1.2.1
Fix Version/s: None

Time Tracking:
Issue & Sub-Tasks
Issue Only
Not Specified

File Attachments: 1. Text File patch.c1966.20070508.txt (4 kb)
2. Text File patch.c1966.20070510.txt (5 kb)
3. Text File patch.c1966.20070511.diff (6 kb)
4. Text File patch.c1966.20070511.txt (10 kb)
5. Text File patch.c1966.20070512.diff (12 kb)
6. Text File patch.c1966.20070515.txt (13 kb)
7. Text File patch.c1967.20070511.txt (7 kb)
8. Java Source File StringUtil.java (0.5 kb)


Sub-Tasks  All   Open   

 Description  « Hide
I would like to access the content of <xs:annotation> from within my java code.

Suppose you have a XSD containing this (this is from maven settings.xsd)
<xs:complexType name="Settings">
<xs:annotation>
<xs:documentation source="version">1.0.0</xs:documentation>
<xs:documentation source="description">Root element of the user
configuration file.</xs:documentation>
</xs:annotation>
<xs:all>
<xs:element name="localRepository" minOccurs="0" type="xs:string">
<xs:annotation>
<xs:documentation source="version">1.0.0</xs:documentation>
<xs:documentation source="description">The local
repository.</xs:documentation>
</xs:annotation>
</xs:element>
</xs:all>
</xs:complexType name="Settings">

Castor will nicely generate a Settings.java Class.
/*

  • This class was automatically generated with
  • <a href="http://www.castor.org">Castor 0.9.7</a>, using an XML
  • Schema.
  • $Id$
    */
    [...]
    /**
  • 1.0.0Root element of the user configuration file.
    *
  • <at> version $Revision$ $Date$
    */
    public class Settings extends SettingsType
    implements java.io.Serializable
    {
    [...]
    }

Here the <xs:documentation source="description"> was converted into javadoc ("root element of the user configuration file.")

I would like to access the content of this from within my java code.

For instance, I'd like this test to pass:
public void testGetDescription() {
Settings settings = new Settings();
assertTrue("Root element of the user configuration file.",settings.annotation.getDescription());
}

My use case is that I write a GUI to edit a XML file, and I'd tooltips to show the description of each XML node. I generate the XML-Java binding with castor from a XSD. Since the XSD already has the description for each XML node, I don't want to hardcode the description, but I'd rather see my tooltips
reflect the XSD content.



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Werner Guttmann - 08/May/07 01:08 AM
Given this is a feature request, I wonder what the Java member to be created as a result of all this should be (really) named ?

private List annotations;

or

private List documentations;

given that it does not really make sense to expose the content of the <xsd:appInfo> elements ?


Werner Guttmann - 08/May/07 02:55 AM
Initial patch to facilitate discussions .....

Werner Guttmann - 09/May/07 07:07 AM
Any feedback ? Would the solution offered be sufficient for your needs ? If yes, I think this could me made available through an extra property - which by default is turned off.

Régis Décamps - 09/May/07 06:26 PM
Thanks for this early implementation. Here are my suggestions and initial feedback.

The content is indeed what I was trying to get.

I would be even better with the "source" information. For instance, I'd like that getDocumentations("version") equals "1.0.0" and getDocumentations("description") equals "Root element of the user configuration file.".
Castor is large, but based on what you've done here, I can provide you the patch that uses a HashMap instead of a Vector to store the documentationList in the Annotation class.

Also, I think the name getDocumentations is not a good idea. That would conflict with a generated class in case where the XSD contains a node named "documentations". I think accessing the attribute directly would be better, or we need to break the Java naming convention (eg get_Documentations). I suppose this could lead to a bug.

The content needs to have backslashes added before the line breaks, otherwise the generated code won't compile.:

_documentations.add("
This is the file specification used to activate a profile. The missing value will be a the location
of a file that needs to exist, and if it doesn't the profile must run. On the other hand exists will test
for the existence of the file and if it is there will run the profile.
");

Finally, how could such a documentation be available for the base-type elements that are transformed into methods? My idea was to use Java annotations, but I've just realized castor wants to be Java-1.3 compatible.

Thanks for all this.


Werner Guttmann - 10/May/07 03:40 AM
Thanks for the feedback. Here's some initial comments ....

a) Don't worry about the change to a java.util.Map. I will be looking into this right now.
b) I would not worry about this too much, as Castor should - in theory - be able to warn the user about such clashes. I agree that getDocumentations() is not specific enough, but somethinc) g along the lines of getXMLSchemaDocumentations() should be sufficient.
c) Rather than using backslashes for line-breaks, I think 'normalizing' the documentation content should be sufficient. And should be straight-forward to implement.

And finally, not sure what you are referring to with a sentence like ....

> Finally, how could such a documentation be available for the base-type elements that are transformed
> into methods? My idea was to use Java annotations, but I've just realized castor wants to be
> Java-1.3 compatible.


Werner Guttmann - 10/May/07 04:06 AM
Updatd patch, incl. additional code for a) and b).

Régis Décamps - 10/May/07 08:27 AM

a

Thanks!

b

Yes indeed. A collision is far less probable.

c

Yes, it was my initial idea, too. I eventually changed it to the backslashes idea, because the line breaks could have a meaning.

d

And finally, not sure what you are referring to with a sentence like ....

> Finally, how could such a documentation be available for the base-type elements that are transformed
> into methods? My idea was to use Java annotations, but I've just realized castor wants to be
> Java-1.3 compatible.

Sorry for not being clear once again.
From my initial XSD

<xs:complexType name="Settings">
  <xs:annotation>
    <xs:documentation source="version">1.0.0</xs:documentation>
    <xs:documentation source="description">Root element of the user
    configuration file.</xs:documentation>
  </xs:annotation>
  <xs:all>
    <xs:element name="localRepository" minOccurs="0"
    type="xs:string">
      <xs:annotation>
        <xs:documentation source="version">1.0.0</xs:documentation>
        <xs:documentation source="description">The local
        repository.</xs:documentation>
      </xs:annotation>
    </xs:element>
  </xs:all>
</xs:complexType>

Castor will generate code. This code allows to

Settings settings=new Settings();
settings.setLocalRepository("toto");

Now we can even get documentation for "<settings>"

settings.getXMLSchemaDocumentations("description");

But localRepository is a base-type (xsd:string) and is acceded through methods: getLocalrepository() which returns a String.

How do you get documentation for <localRepository> ?

  • The documentation for both <settings> and <localRepository> could be accessible via getAnnotations() (I think) if Castor produces java annotated code like the following
    /* generated class
    */
    @Castor.Annotations.Documentation("description","Root element of the user configuration file")
    public class Settings extends SettingsType implements java.io.Serializable {
    [...]
      @Castor.Annotations.Documentation("descritpion","The local repository")
      public 
    }
  • But this is actually not necessary. Another possible is that getXMLSchemaDocumentations() has two parameters : the "source" attribute and the field name, so that
    assertEquals("Root element of the user configuration file.",settings.getXMLSchemaDocumentations("description"));
    assertEquals("The local repository.",settings.getXMLSchemaDocumentations("localRepository","description"));

What do you think? Am I missing an easy way to retrieve the description for localRepository?


Werner Guttmann - 10/May/07 08:49 AM
I am actually not sure whether I like any of these ideas sufficiently. Yes, using an annotation for Java 5.0 and above could be an option. Let me think about this a bit.

How did you implement the backslash idea ?


Werner Guttmann - 10/May/07 08:57 AM
I actually wonder whether the following annotation

<xs:documentation source="version">1.0.0</xs:documentation>

should be translated anyhow to the following fragment in the Javadoc for the localRepositrory member:

/**

  • Returns the value of field 'localRepository'.
  • The field 'localRepository' has the following descriptions:
  • @version 1.0.0
  • @description tja, was eigentlich
  • @return the value of field 'LocalRepository'.
    */
    public java.lang.String getLocalRepository() { return this._localRepository; }

Régis Décamps - 10/May/07 04:53 PM
Class that provides the replaceAll method in JDK 1.3

Régis Décamps - 10/May/07 05:41 PM

How did you implement the backslash idea ?

Sometimes I just say stupid things (or confuse programming languages)

My implementation is only based on going to the next line. The generated code looks like this:

Unable to find source-code formatter for language: servers.java. Available languages are: xhtml, javascript, java, none, html, actionscript, xml, sql
public Servers() {
        super();
        this._serverList = new java.util.Vector();
        _xmlSchemaDocumentations.put("version", "1.0.0");
        _xmlSchemaDocumentations.put("description", "Configuration of server-specific settings, mainly authentication" +
 +" method. This allows configuration of authentication on a per-server" +
 +" basis." +
 +" ");
    }

Régis Décamps - 10/May/07 05:45 PM

I actually wonder whether the following annotation

<xs:documentation source="version">1.0.0</xs:documentation>

should be translated anyhow to the following fragment in the Javadoc for the localRepositrory member:

I don't need it, indeed.

I have no idea whether this could be useful to anyone else.


Werner Guttmann - 11/May/07 02:00 AM
Updated patch, incl. StringUtil functionality.

Werner Guttmann - 11/May/07 02:01 AM
I am actually tempted to commit this as is, and to address the remainder of the functionality through a new (follow-up) issue.

Werner Guttmann - 11/May/07 03:22 AM
Updated patch, where I made the new code configurable via a new property (default = false).

Régis Décamps - 12/May/07 07:08 AM
Completes your patch.c1966.20070511.txt (10 kb) (new configuration, lines breaks, managed with property) with a TestCase for StringUtil (and fixes a NPE exception when the source is null).

I'll so some more testing, but I think this can soon be committed to trunk, indeed.


Werner Guttmann - 15/May/07 02:27 AM
Updated patch, with small structural changes to the new JUnit test case.

Werner Guttmann - 15/May/07 02:32 AM
Your little fix in StringUtil to add a check for null just made me wonder about the validity of the solution when somebody adds multiple documentation elements without specifying a source attribute ?

Werner Guttmann - 15/May/07 04:51 AM
Just for your information, I moved all code/issues that can be committed as it stands to a new sub-task. Makes it easier for us (me) in terms of release management.

Werner Guttmann - 01/Jan/08 05:11 PM
Regis, I will commit CASTOR-1983 as is, but I would love to continue the discussions about the remainder of your requests in this very issue.

Werner Guttmann - 02/Jan/08 04:25 PM
Returning to your suggestion to add additional code (methods) to allow access to documentation information for first-class members of top-level element/type definitions, one could easily expand the concept applied to the top-level elements/type deds, and add the following artifacts:
private Map _xmlSchemaDocumentationOfFields = new HashMap();

and allow access to its content by a method as follows:

private String getXmlSchemaDocumentation(String fieldName, String source);

Werner Guttmann - 02/Jan/08 04:27 PM
It just occurred to me that al these members/methods to access the XML schema documentation information could (and in my humble opinion) should be moved to a class' descriptor, where it could be stored with the corresponding XMLClassDescriptor/XMLFieldDescriptor. Let me know whether you'd find this still useful.

Régis Décamps - 02/Jan/08 04:38 PM
Hi Werner,

First of all, happy new year, and thanks for this nice piece of software called castor.

To answer your question, yes, having the descritption in a separate class would answer my use case. And it will not increase the size of the main class, which is probably a good thing indeed.

Cheers


Werner Guttmann - 02/Jan/08 04:45 PM
> And it will not increase the size of the main class, which is probably a good thing indeed.
Which is exactly my main train of thought .. . Let me come up with some ideas, and I will be back to you soon.

Just out of curiosity, what tool is it you are using Castor for ?


Régis Décamps - 02/Jan/08 05:20 PM
My use case a GUI to edit the settings.xml file used to configure Maven.
http://code.google.com/p/m2settings

My current workaround is based on a 'DocumentationProvider' that parses the XSD (again) to extract the xs:documentation.