Headline

Object-XML mapping for Language:Java and Language:XSD with Technology:JAXB

Characteristics

See Contribution:jaxbComposition for the overall motivation of exercising Technology:JAXB and for arguably a simple baseline of a schema and corresponding schema-derived classes. The present implementation represents an attempt to model different kinds of subunits of department (i.e., sub-departments and employees) as subtypes of a common supertype of subunits, where subtyping is meant here in the sense of XSD's substitution grouping mechanism. The result is rather complex at both ends.

Illustration

The data model is implemented as xml files (sampleCompany.xml) that conform to a schema (Company.xsd). For example departments:

<xs:complexType name="department">
 <xs:complexContent>
  <xs:extension base="subunit">
   <xs:sequence>
    <xs:element ref="name"/>
    <xs:element name="manager" type="employee"/>
    <xs:element ref="subunit" maxOccurs="unbounded" minOccurs="0"/>
   </xs:sequence>
  </xs:extension>
 </xs:complexcontent>
</xs:complextype>

Actual Language:Java classes will be generated using Technology:xjc.

Feature:Open serialization is implemented using Technology:JAXB Un-/Marshaller:

    public static Company deserializeCompany(File input)
    throws JAXBException 
    {
        initializeJaxbContext();
        Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
        return (Company) unMarshaller.unmarshal(input);
    }

    public static void serializeCompany(File output, Company c)
    throws     JAXBException,
            FileNotFoundException,
            XMLStreamException 
    {
        initializeJaxbContext();
        OutputStream os = new FileOutputStream(output);
        Marshaller marshaller = jaxbContext.createMarshaller();
        XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
        XMLStreamWriter writer = outputFactory.createXMLStreamWriter(os);
        marshaller.marshal(c, writer); // TODO: need a stream writer that does indentation
    }

Feature:Total and Feature:Cut are implemented as static methods:

public class Total {

	public static double total(Company c) {
		double total = 0;
		for (Department d : c.getDepartment())
			total += total(d);
		return total;
	}

	public static double total(Department d) {
		double total = total(d.getManager());
		for (JAXBElement<? extends Subunit> s : d.getSubunit())
			total += total(s.getValue());
		return total;
	}

	public static double total(Employee e) {
		return e.getSalary();
	}

	//
	// We cannot use virtual methods.
	// That is, we do not allow ourselves modifying schema-derived classes.
	//
	public static double total(Subunit s) {
		if (s instanceof Department)
			return total(((Department)s));
		else if (s instanceof Employee)
			return total(((Employee)s));
		else throw new IllegalArgumentException();
	}

}
public class Cut {

    public static void cut(Company c) {
        for (Department d : c.getDepartment())
            cut(d);
    }

    public static void cut(Department d) {
        cut(d.getManager());
        for (JAXBElement<? extends Subunit> s : d.getSubunit())
            cut(s.getValue());
    }

    public static void cut(Employee e) {
        e.setSalary(e.getSalary() / 2);
    }

    public static void cut(Subunit s) {
        if (s instanceof Department)
            cut(((Department)s));
        else if (s instanceof Employee)
            cut(((Employee)s));
        else throw new IllegalArgumentException();
    }

}

Test cases are implemented for all Namespace:Features.

Relationships

For DOM-like implementations of in-memory XML processing see Contribution:dom, Contribution:jdom and Contribution:xom.

For a query-based implementation of in-memory XML processing see Contribution:xpathAPI.

For push-based XML processing see Contribution:sax.

For Object/XML mapping see Contribution:jaxbChoice (XSD with choice for different subunits), Contribution:jaxbComposition (XSD with object composition), Contribution:jaxbExtension (XSD with type extension) and Contribution:jaxbSubstitution (XSD with substitution groups).

Architecture

The contribution follows a standardized structure:

  • inputs contains input files for tests
  • src/main/java contains the following packages:
  • src/test/java contains the following packages:

Usage

This contribution uses Technology:Gradle for building. Technology:Eclipse is supported.

See https://github.com/101companies/101simplejava/blob/master/README.md


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.