I know that castor can generate Java classes (what I would consider
POJOs); but, is there an option to generate a file or files from any
arbitrary template and XSD? I'll provide examples in case my
explanation is not clear.
In the example below, there's an Adjustment.xsd, containing three
attributes. The generator writes out a class header (package and
import statements of my choosing), then writes a statement
representing the "singleton" instance. Then, a "format" method is
generated. Again, AdjustmentNativeFormatter (below) is the result of
applying the Adjustment.xsd to a generator, say "FormatterGenerator"
because this generator generates "Formatter" objects.
What I can't find in castor is the piece of code that I could write,
either in XML or more likely Java code, which has access to the
metadata in the XSD (the root elements, nested elements, attributes,
whether something is a simple type or complex type, XML Schema type,
etc). From that, I would write code to iterate over the elements and
attributes, write out statements that will be written to a file on disk.
As you can see, this is different from generating simply POJOs. I'd
like to be able to generate any arbitrary code from XSD.
And, dare I ask, if castor does not have this feature, can you
recommend other open source projects that do have this feature?
Thanks in advance,
Alex
Generated code:
package com.athieme.KToW.formatters;
import org.apache.log4j.Logger;
import java.math.BigInteger;
import java.util.Calendar;
import com.athieme.AbstractFormatter;
import com.athieme.interfaces.IAdjustment;
public class AdjustmentFormatter extends
AbstractFormatter<IAdjustment> {
public static final AdjustmentFormatter singleton = new
AdjustmentFormatter();
public String format(final IAdjustment obj) {
final StringBuffer sb = new StringBuffer();
sb.append(obj.company());
sb.append(obj.userId());
sb.append(obj.dateStamp());
sb.append(obj.timeStamp());
return sb.toString();
}
}
XSD:
<?xml version="1.0" encoding="iso-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fd="http://athieme/FieldDef"
xmlns:common="http://athieme/Common"
targetNamespace="http://athieme/KToW"
elementFormDefault="qualified">
<xs:import namespace="http://athieme/FieldDef" schemaLocation="../
FieldDef.xsd"/>
<xs:import namespace="http://athieme/Common" schemaLocation="../
Common.xsd"/>
<xs:complexType name="Adjustment">
<xs:attribute name="company"
type="fd:fixedString10"
fd:externalName="LDCO"
fd:internalName="Company"/>
<xs:attribute name="userId"
type="fd:fixedString10"
fd:externalName="PLUSER"
fd:internalName="user_id"/>
<xs:attribute name="dateStamp"
type="fd:fixedInt8"
fd:externalName="DateStamp"
fd:internalName="DateStamp"
fd:fieldDefType="int"/>
</xs:complexType>
</xs:schema>