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 arguably a simple baseline for a schema and the corresponding schema-derived classes. The present implementation exercises an XSD choice for different kinds of subunits. The schema-derived class Subunit encodes the choice in an arguably sub-optimal manner (as a result of the lack of a good match for XSD choices in the Java type system). One could expect that XSD choices (of a restricted kind) could be translated into class inheritance, but this does not happen here (and this may be understandable).

Illustration

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

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

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 (Subunit s : d.getSubunit())
            total += total(s);
        return total;
    }

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

    public static double total(Subunit s) {
        double total = 0;
        if (s.getEmployee() != null) total += total(s.getEmployee());
        if (s.getDepartment() != null) total += total(s.getDepartment());
        return total;
    }

}
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 (Subunit s : d.getSubunit())
            cut(s);
    }

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

    public static void cut(Subunit s) {
        if (s.getEmployee()!=null) cut(s.getEmployee());
        if (s.getDepartment()!=null) cut(s.getDepartment());
    }

}

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.