JAXB2 Basics, toString generation

E

Erik

How can I have a toString method generated by xjc (using Jaxb2-Basics)
for class "person" that gives me his/her "name"?
Here is the schema, i want to use:

<code>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.1"
xmlns:basic="http://jaxb2-commons.dev.java.net/basic"
xmlns:ci="http://jaxb.dev.java.net/plugin/code-injector"
xmlns:ts="http://jaxb2-commons.dev.java.net/basic/toString"
jaxb:extensionBindingPrefixes="basic ci ts">

<xs:element name="names">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="person" form="unqualified">
<xs:complexType>
<xs:all>
<xs:element name="name" form="unqualified" type="xs:string"/>
<xs:element name="age" form="unqualified" type="xs:int"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</code>
 
L

Lew

Erik said:
How can I have a toString method generated by xjc (using Jaxb2-Basics)
for class "person" that gives me his/her "name"?
Here is the schema, i want to use:

I'll add indentation and blank lines for readability.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.1"
xmlns:basic="http://jaxb2-commons.dev.java.net/basic"
xmlns:ci="http://jaxb.dev.java.net/plugin/code-injector"
xmlns:ts="http://jaxb2-commons.dev.java.net/basic/toString"
jaxb:extensionBindingPrefixes="basic ci ts">

<xs:element name="names">
<xs:complexType>
<xs:sequence>

<xs:element maxOccurs="unbounded" name="person" form="unqualified">
<xs:complexType>
<xs:all>
<xs:element name="name" form="unqualified" type="xs:string"/>
<xs:element name="age" form="unqualified" type="xs:int"/>
</xs:all>
</xs:complexType>
</xs:element>

</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>

I don't know how to get JAXB to generate a 'toString()' for you unless
I check the documentation, sorry.

I've used two approaches with JAXB-generated classes where I needed
something beyond the generator's output: modify the output or wrap it
in an adapter. The latter makes for a simpler build process.

For some reason I had a bookmark link to
http://java.net/projects/jaxb2-commons/pages/Home
which seems to indicate that others might have faced this question
before:
http://confluence.highsource.org/display/J2B/Equals+plugin

I don't remember precisely how I found that site to bookmark it. I've
been doing a lot of JAXB work and I suppose I must have googled around
for useful information.
 
E

Erik

I can get it done with code injection (-Xinject-code cmd line option),
via a Netbeans project and an ANT build file. I add this to the xsd in
the appropriate spot:

<xs:annotation>
<xs:appinfo>
<ci:code>
// any code you can think of:
/**
public String toString() {
return name;
}
*/
</ci:code>
</xs:appinfo>
</xs:annotation>


But JAXB2 Basics advertises an -XtoString option, that is supposed to
do it neatly.
I know the links you mentioned: been there, done that.
As a matter of high probability,I think I saw ALL the (ir-)relevant
doc on this subject. Pity there is no example code. You'd say the
maker would have little trouble providing that... but alas.
..
 
L

Lew

Erik said:
I can get it done with code injection (-Xinject-code cmd line option),
via a Netbeans project and an ANT build file. I add this to the xsd in
the appropriate spot:

Is that a NetBeans-specific solution or does it work more generally?

--
Lew
A: Inline posts, editing out immaterial material.
Q: What should I do instead?
A: It disrupts the flow and makes the conversation harder to follow.
Q: Why is it bad?
A: Placing your answer prior to the quoted material.
Q: What is top-posting?
 
E

Erik

Is that a NetBeans-specific solution or does it work more generally?

I put an import in build.xml, that does all the work. So it should
work independent of NB with a few alterations:
I use the NB -pre-compile task to execute the ANT script.
Thing is, you can run xjc independently.
All the libraries etc shoould be in the right place, of course.
The script has all its own settings for lib's, independent of NB.
I'm not sure if all those lib's are really necessary.
This is the file that I import:


<?xml version="1.0" encoding="UTF-8"?>
<project name="XMLComboBox_jaxb" basedir="." default="-pre-compile">
<target name="-pre-compile">
<echo>Define the xjc task </echo>
<taskdef name="xjc" classname="com.sun.tools.xjc.XJC2Task">
<!-- XJC2 Task classpath -->
<classpath>
<fileset dir="${basedir}/lib">
<include name="activation.jar"/>
<include name="jaxb-api.jar"/>
<include name="jaxb-impl.jar"/>
<include name="jsr173_api.jar"/>
<include name="stax-api-*.jar"/>
<include name="jaxb-xjc1.jar"/>
<include
name="jaxb2-basics-ant-*.jar"/>
</fileset>
</classpath>
</taskdef>

<echo>Generate the code</echo>

<xjc destdir="${basedir}/src" extension="true">
<arg line="-Xinject-code"/>
<binding dir="${basedir}/src/xmlcombobox/resources">
<include name="**/*.xjb"/>
</binding>

<schema dir="${basedir}/src/xmlcombobox/resources">
<include name="**/*.xsd"/>
</schema>
<!-- Plugins -->
<classpath>
<fileset dir="${basedir}/lib">
<!-- JAXB2 Basics library -->
<include name="jaxb2-basics-*.jar"/>
<!-- JAXB2 Basics library dependencies
-->
<include
name="jaxb2-basics-runtime-*.jar"/>
<include
name="jaxb2-basics-tools-*.jar"/>
<include
name="commons-beanutils-*.jar"/>
<include name="commons-lang-*.jar"/>
<include
name="commons-logging-*.jar"/>
</fileset>
</classpath>
</xjc>
</target>
</project>
 
E

Erik

Is that a NetBeans-specific solution or does it work more generally?

I got an answer from the maker of the Basics plugins.
He says the toString plugin is not about defining your own toString
method.
The plugin jyst generates a toString method for all the fields of a
complex Class.
I need a toString for a class Person( name, age, sex).
The toString should return the name only, for use in a JCombobox.
It all means I need to write my own plugin.
I'm going to try that now.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top