Either I'm doing something wrong or jaxb is crappola

M

Mark

I'm betting it me. Here is the simple schema I'm using:

<?xml version="1.0"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:version="1.0">
<xs:annotation>
<xs:appinfo>
<jaxb:schemaBindings>
<jaxb:package name="com.some.package.name"/>
</jaxb:schemaBindings>
</xs:appinfo>
</xs:annotation>
<xs:element name="editableTemplateParameters">
<xs:complexType>
<xs:sequence>
<xs:element ref="parameter" minOccurs="0"
maxOccurs="unbounded"/>
<xs:element ref="interface" minOccurs="0"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="parameter" type="xs:string"/>
<xs:element name="interface">
<xs:complexType>
<xs:sequence>
<xs:element name="interfaceType" type="xs:string"/>
<xs:element ref="parameter" minOccurs="1"
maxOccurs="unbounded"/>
<xs:element ref="parameterBlock" minOccurs="0"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="parameterBlock">
<xs:complexType>
<xs:sequence>
<xs:element name="blockName" type="xs:string"/>
<xs:element ref="parameter" minOccurs="2"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>


The compiler handles this just fine. It generates the classes & they
compile. The problem shows up when I try to create a doc from java
classes. Here's the java code that does this....

// Create jaxb containers for XML data
JAXBContext jaxbContext = JAXBContext.newInstance(
"com.some.package.name" );

ObjectFactory templateObjFactory = new ObjectFactory();
EditableTemplateParameters editableTemplateParams =
templateObjFactory.createEditableTemplateParameters();

List currentParamList = editableTemplateParams.getParameter();
Parameter newParam = templateObjFactory.createParameter();
newParam.setValue( "jaxbSUX!!!" );
currentParamList.add( newParam );

StringWriter sw = new StringWriter();
Marshaller m = jaxbContext.createMarshaller();
m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
try
{
javax.xml.bind.ValidationEventHandler veh = new
javax.xml.bind.ValidationEventHandler()
{
public boolean handleEvent( javax.xml.bind.ValidationEvent ve
)
{
com.some.package.name.impl.EditableTemplateParametersImpl
etp=
(com.some.package.name.impl.EditableTemplateParametersImpl)
ve.getLocator().getObject();
Parameter p = (Parameter) etp.getParameter().get(0);
StringBuffer locBuff = new StringBuffer( "Locator : Col
Num :"
+ ve.getLocator().getColumnNumber() + "\n");
locBuff.append( "Locator : Line Num :"
+ ve.getLocator().getLineNumber() + "\n");
locBuff.append( "Locator : Node :"
+ ve.getLocator().getNode() + "\n");
locBuff.append( "Locator : Object :"
+ ve.getLocator().getObject().getClass().getName() + "\n");
locBuff.append( "Locator : param list size :"
+ etp.getParameter().size() + "\n");
locBuff.append( "Locator : list object :"
+ etp.getParameter().get(0).getClass().getName() + "\n");
locBuff.append( "Locator : list object data:"
+ p.getValue() + "\n");
locBuff.append( "Locator : Offset :"
+ ve.getLocator().getOffset() + "\n");
locBuff.append( "Locator : URL :"
+ ve.getLocator().getURL() + "\n");

_logger.debug(method + "Validation Event: Linked
Exception: "
+ ve.getLinkedException() );
_logger.debug(method + "Validation Event : Locator "
+ locBuff.toString() );
_logger.debug(method + "Validation Event : Message "
+ ve.getMessage() );
_logger.debug(method + "Validation Event : Severity "
+ ve.getSeverity() );
return true;
}
};
m.setEventHandler( veh );
m.marshal( editableTemplateParams, sw );
}
catch(JAXBException je)
{
_logger.warning( method + " JAXB Exception message: " +
je.getMessage() );
}

...... End of relevant code snippet

I added the bogus event handler to gather up some details as to what
was going on. Without it the code throws a Serializer exception when
I marshal the code. The problem is in how one of the ipml classes
pulls the "Parameter" data from the list object. Note that I create a
Parameter object using the ObjectFactory, then set the value
(jaxbSUX!!!) then add the Parameter to the list. In the serialization
method generated by xjc the object pulled from the list is cast as a
string. Here is a snippet from the method....

public void serializeElements(.....)
throws org.xml.sax.SAXException
{
int idx1 = 0;
final int len1 = _Interface.size();
int idx2 = 0;
final int len2 = _Parameter.size();
while (idx2 != len2) {
context.startElement("", "parameter");
int idx_0 = idx2;
try {
idx_0 += 1;
} catch (java.lang.Exception e) {
com........handlePrintConversionException(this, e,
context);
}
context.endAttributes();
try
{ <b> vvvvvv Why isn't this a Parameter
object
context.text(((java.lang.String) _Parameter.get(idx2
++)));
} catch (java.lang.Exception e) {
com.......handlePrintConversionException(this, e,
context);
}
context.endElement();
}
.............. End of snippet

So obviously, if I put the string directly into the list all is well.
But why create a Parameter object if we cant use it. Is there a
problem with the schema that is causing this apparently strange/buggy
behavior or is jaxb really this dumb. I tried using the current
version of the jwsdk (1.3) but we have some dependencies on java 1.3.1
and even when I generated the newer jaxb code the generated class
still cast the object returned from the list to a string.

What am I doing wrong??

Thanks,

Mark
 

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,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top