xml parsing - JAXB XmlJavaTypeAdapter unmarshal method not being called -
i trying marshal/unmarshal color xml. jaxb project had example code doing exact thing via xmljavatypeadapter https://jaxb.java.net/guide/xml_layout_and_in_memory_data_layout.html.
the marshaling works fine , output expect:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <beanwithcolor> <foreground>#ff0000ff</foreground> <name>clevername</name> </beanwithcolor>
however, when trying go xml object unmarshal method never called. can provide insight why? after unmarshalled foreground color null , have confirmed debugger unmarshal never being called:
beanwithcolor{foreground=null, name='clevername'}
scce:
@xmlrootelement public class beanwithcolor { private string name; private color foreground; public beanwithcolor() { } public beanwithcolor(color foreground, string name) { this.foreground = foreground; this.name = name; } @xmljavatypeadapter(coloradapter.class) public color getforeground() { return this.foreground; } public void setforeground(color foreground) { this.foreground = foreground; } public string getname() { return name; } public void setname(string name) { this.name = name; } @override public string tostring() { final stringbuilder sb = new stringbuilder("beanwithcolor{"); sb.append("foreground=").append(foreground); sb.append(", name='").append(name).append('\''); sb.append('}'); return sb.tostring(); } public static void main(string[] args) { beanwithcolor bean = new beanwithcolor(color.blue, "clevername"); try { stringwriter writer = new stringwriter(1000); jaxbcontext context = jaxbcontext.newinstance(beanwithcolor.class); final marshaller marshaller = context.createmarshaller(); marshaller.marshal(bean, writer); system.out.println("marshaled xml: " + writer); final unmarshaller unmarshaller = context.createunmarshaller(); beanwithcolor beanwithcolor = (beanwithcolor) unmarshaller.unmarshal(new stringreader(writer.tostring())); system.out.println("beanwithcolor = " + beanwithcolor); } catch (jaxbexception e) { e.printstacktrace(); } } static class coloradapter extends xmladapter<string, color> { public color unmarshal(string s) { return color.decode(s); } public string marshal(color color) { return '#' + integer.tohexstring(color.getrgb()); } } }
i suspect xmladapter
being called unmarshal
color.decode
method failing (this happens when debugged code).
color.decode("#ff0000ff");
results in following:
exception in thread "main" java.lang.numberformatexception: input string: "ff0000ff" @ java.lang.numberformatexception.forinputstring(numberformatexception.java:48) @ java.lang.integer.parseint(integer.java:461) @ java.lang.integer.valueof(integer.java:528) @ java.lang.integer.decode(integer.java:958) @ java.awt.color.decode(color.java:707)
you can set validationeventhandler
on unmarshaller
hook on failures. default jaxb impl wouldn't report problem.
the fix
the coloradapter.marshal
method needs fixed return correct value.
string rgb = integer.tohexstring(color.getrgb()); return "#" + rgb.substring(2, rgb.length());
Comments
Post a Comment