Castor (framework)

From Wikipedia, the free encyclopedia
(Redirected from Castor (Framework))
Castor
Stable release
1.4.1 / May 15, 2016; 7 years ago (2016-05-15)
Written inJava
Operating systemCross-platform (JVM)
PlatformJava Virtual Machine
TypeData binding
LicenseApache 2.0
Websitecastor-data-binding.github.io/castor/

Castor is a data binding framework for Java with some features like Java to Java-to-XML binding, Java-to-SQL persistence, paths between Java objects, XML documents, relational tables, etc.[1][2][3] Castor is one of the oldest data binding projects.[3]

Process flow[edit]

Basic process flows include class generation, marshalling, unmarshalling, etc.[2] Marshalling framework includes a set of ClassDescriptors and FieldDescription to describe objects.[3]

Class generation[edit]

Class generation is similar to JAXB and Zeus. Castor supports XML Schema instead of DTDs (DTDs are not supported by Castor).[2][3][4]

Unmarshalling and marshalling[edit]

Unmarshalling and marshalling are dealt with marshall() and unmarshall() methods respectively. During marshalling, conversion process from Java to XML is carried out, and, during unmarshalling, conversion process from XML to Java is carried out. Mapping files are the equivalent of a binding schema, which allows to transforms names from XML to Java and vice versa.[2]

Additional features[edit]

Castor offers some additional features which are not present in JAXB. Additional features include:

  • Database and directory server mappings - mapping between databases and directory servers to Java
  • JDO - Caster supports Java Data Objects.[2]

Code samples[edit]

Code for marshalling may look like as follows:

package javajaxb;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
// Castor
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.ValidationException;
// Generated hr.xml classes
import javajaxb.generated.hr.*;
public class EmployeeLister {
    // Existing methods
    public void modify()
        throws IOException, MarshalException, ValidationException {
        // Add a new employee
        Employee employee = new Employee();
        employee.setName("Ben Rochester");
        Address address = new Address();
        address.setStreet1("708 Teakwood Drive");
        address.setCity("Flower Mound");
        address.setState("TX");
        address.setZipCode("75028");
        employee.addAddress(address);
        Organization organization = new Organization();
        organization.setId(43);
        organization.setName("Technical Services");
        employee.setOrganization(organization);
        Office office = new Office();
        office.setId(241);
        Address officeAddress = new Address();
        officeAddress.setStreet1("1202 Business Square");
        officeAddress.setStreet2("Suite 302");
        officeAddress.setCity("Dallas");
        officeAddress.setState("TX");
        officeAddress.setZipCode("75218-8921");
        office.setAddress(officeAddress);
        employee.setOffice(office);
        // Add employee to list
        employees.addEmployee(employee);
        // marshal
        employees.marshal(new FileWriter(outputFile));
    }
    public static void main(String[] args) {
        try {
            if (args.length != 2) {
                System.out.println("Usage: java javajaxb.EmployeeLister
" +
                    "[web.xml filename] [output.xml filename]");
                return;
            }
            EmployeeLister lister = 
                new EmployeeLister(new File(args[0]), new
File(args[1]));
            lister.list(true);
            lister.modify();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }   
}

[2]

Code for unmarshalling may look like as follows:

package javajaxb;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
// Castor
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.ValidationException;
// Generated hr.xml classes
import javajaxb.generated.hr.*;
public class EmployeeLister {
    /** The descriptor to read in */
    private File descriptor;
    /** The output file to write to */
150
    private File outputFile;
    /** The object tree read in */
    private Employees employees;
    public EmployeeLister(File descriptor, File outputFile) {
        employees = null;
        this.descriptor = descriptor;
        this.outputFile = outputFile;
    }
    public void list(boolean validate) 
        throws IOException, MarshalException, ValidationException {
        // Unmarshall
        employees = Employees.unmarshal(new FileReader(descriptor));
        // Do some basic printing
        System.out.println("--- Employee Listing ---\n");
        Employee[] employeeList = employees.getEmployee();
        for (int i=0; i<employeeList.length; i++) {
            Employee employee = employeeList[i];
            System.out.println("Employee: " + employee.getName());
            System.out.println("Organization: " + 
                employee.getOrganization().getName());
            System.out.println("Office: " + 
                employee.getOffice().getAddress().getCity() + ", " +
                employee.getOffice().getAddress().getState() + "\n");
        }
    }
    public static void main(String[] args) {
        try {
            if (args.length != 2) {
                System.out.println("Usage: java javajaxb.EmployeeLister
" +
                    "[web.xml filename] [output.xml filename]");
                return;
            }
            EmployeeLister lister = 
                new EmployeeLister(new File(args[0]), new
File(args[1]));
            lister.list(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }   
}

[2]

Sample mapping file may look like as follows:

<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Object Mapping DTD Version 1.0//EN" "http://Castor.exolab.org/mapping.dtd">
<mapping>
  <class name="javajaxb.generated.hr.Employees">
    <map-to xml="emp-list"/>
    <field name="Employee"
           type="javajaxb.generated.hr.Employee">
      <bind-xml name="emp" />
    </field>
  </class>
  <class name="javajaxb.generated.hr.Employee">
    <field name="Id"
           type="integer">
      <bind-xml name="emp-id" node="attribute"/>
    </field>
    <field name="name"
           type="java.lang.String">
      <bind-xml name="emp-name" node="attribute"/>
    </field>
    <field name="Address"
           type="javajaxb.generated.hr.Address">
      <bind-xml name="emp-address" />
    </field>
    <field name="Organization"
           type="javajaxb.generated.hr.Organization">
      <bind-xml name="emp-org"/>
    </field>
    <field name="Office"
           type="javajaxb.generated.hr.Office">
      <bind-xml name="emp-office"/>
    </field>
  </class>
  <class name="javajaxb.generated.hr.Address">
    <field name="Street1"
           type="java.lang.String">
      <bind-xml name="line-1" node="element"/>
    </field>
    <field name="Street2"
           type="java.lang.String">
      <bind-xml name="line-2" node="element"/>
    </field>
    <field name="City"
           type="java.lang.String">
      <bind-xml name="city" node="element"/>
    </field>
    <field name="State"
           type="java.lang.String">
      <bind-xml name="state" node="element"/>
    </field>
    <field name="ZipCode"
           type="java.lang.String">
      <bind-xml name="zip-code" node="element"/>
    </field>
  </class>
  <class name="javajaxb.generated.hr.Office">
    <field name="Id"
           type="integer">
      <bind-xml name="office-id" node="attribute"/>
    </field>
    <field name="Address"
           type="javajaxb.generated.hr.Address">
      <bind-xml name="office-address" node="element"/>
    </field>
  </class>
  <class name="javajaxb.generated.hr.Organization">
    <field name="Id"
           type="integer">
      <bind-xml name="org-id" node="element"/>
    </field>
    <field name="Name"
           type="java.lang.String">
      <bind-xml name="org-name" node="element"/>
    </field>
  </class>
</mapping>

[2]

See also[edit]

References[edit]

  1. ^ "About". Castor. Github. Retrieved 11 February 2016.
  2. ^ a b c d e f g h McLaughin, B (2002). Java and XML Data Binding. Sebastopol: O'Reilly & Associates, Inc. pp. 143–165.
  3. ^ a b c d XML Developer's Guide. Scotts Valley, USA: Borland Software Corporation. 2002. pp. (2–29)–(2–31).
  4. ^ "The Source Code Generator". Castor. Github. Retrieved 11 February 2016.

External links[edit]