users@jaxb.java.net

derived fields

From: Jason Sachs <jmsachs_at_gmail.com>
Date: Fri, 13 Feb 2009 11:18:15 -0500

I have a fairly simple schema used to autogenerate a few Java classes. So
far it works fine. I would like to add some functionality on the Java side,
however, and would like to get a recommendation for how to best do this.

My schema contains an element <signal> that I am mapping to a Java class
ESignal. (using jaxb:nameXmlTransform which works fine) It has an attribute
called "type" that is a choice of the following 6 values: uint8, int8,
uint16, int16, uint32, int32. The autogenerated ESignal class contains the
method public String getType(). This is fine but I would like to somehow add
or modify this to something like

public enum SignalType {
  UINT8(1,false), INT8(1,true), UINT16(2,false), INT16(2,true),
UINT32(4,false), INT32(4,true);

  final int bytelength;
  final boolean signed;
  public SignalType(bytelength, signed) { this.bytelength = bytelength;
this.signed = signed; }
  public static SignalType fromString(String s)
  { ... exercise for the reader to convert from string to one of the enums
... }
}

// ... then in ESignal:
public SignalType getType() { ... somehow this is supposed to return the
proper enum. }

Since the java class is autogenerated from the .xsd file (which I generate
from a RELAX NG .rng file) and the .xjb file, I am not quite sure how to
graft hand-coded methods onto the autogenerated class...

This seems like a fairly trivial exercise but I am not sure. Any advice?

--Jason