Castor (framework): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
KolbertBot (talk | contribs)
m Bot: HTTP→HTTPS (v485)
 
(11 intermediate revisions by 10 users not shown)
Line 1: Line 1:
{{Short description|Java data binding framework}}
{{Infobox software
{{Infobox software
| logo =
| logo =
Line 16: Line 17:
}}
}}


'''Castor''' is a [[data binding]] framework for [[Java (programming language)|Java]] with some features like Java to Java-to-XML binding, Java-to-SQL persistence, paths between Java objects, [[XML]] documents, relational tables, etc.<ref name=":c_a">{{Cite web|url = https://castor-data-binding.github.io/castor/main/index.html|title = About|date = |accessdate = 11 February 2016|website = Castor|publisher = Github|last = |first = }}</ref><ref name=":jaxdb_c">{{Cite book|title = Java and XML Data Binding|last = McLaughin|first = B|publisher = O'Reilly & Associates, Inc.|year = 2002|location = Sebastopol|volume = |pages = 143–165}}</ref><ref name=":cfg_c">{{Cite book|title = XML Developer's Guide|last = |first = |publisher = Borland Software Corporation|year = 2002|location = Scotts Valley, USA|volume = |pages = (2–29)–(2–31)}}</ref> Castor is one of the oldest data binding projects.<ref name=":cfg_c"/>
'''Castor''' is a [[data binding]] framework for [[Java (programming language)|Java]] with some features like Java to Java-to-XML binding, Java-to-SQL persistence, paths between Java objects, [[XML]] documents, relational tables, etc.<ref name=":c_a">{{Cite web|url = https://castor-data-binding.github.io/castor/main/index.html|title = About|access-date = 11 February 2016|website = Castor|publisher = Github}}</ref><ref name=":jaxdb_c">{{Cite book|title = Java and XML Data Binding|last = McLaughin|first = B|publisher = O'Reilly & Associates, Inc.|year = 2002|location = Sebastopol|pages = 143–165}}</ref><ref name=":cfg_c">{{Cite book|title = XML Developer's Guide|publisher = Borland Software Corporation|year = 2002|location = Scotts Valley, USA|pages = (2–29)–(2–31)}}</ref> Castor is one of the oldest data binding projects.<ref name=":cfg_c"/>


==Process flow==
==Process flow==
Basic process flows include class generation, [[Marshalling (computer science)|marshalling]], unmarshalling, etc.<ref name=":jaxdb_c"/> Marshalling framework includes a set of ClassDescriptors and FieldDescription to describe objects.<ref name=":cfg_c"/>
Basic process flows include class generation, [[Marshalling (computer science)|marshalling]], unmarshalling, etc.<ref name=":jaxdb_c"/> Marshalling framework includes a set of ClassDescriptors and FieldDescription to describe objects.<ref name=":cfg_c"/>


==Class Generation==
==Class generation==
Class generation is similar to [[Java Architecture for XML Binding|JAXB]] and Zeus. Castor supports XML Schema instead of DTDs (DTDs are not supported by Castor).<ref name=":jaxdb_c"/><ref name=":cfg_c"/><ref name=":c_tscg">{{Cite web|url = https://www2.informatik.hu-berlin.de/~xing/Lib/Docs/Castor/sourcegen.html|title = The Source Code Generator|date = |accessdate = 11 February 2016|website = Castor|publisher = Github|last = |first = }}</ref>
Class generation is similar to [[Java Architecture for XML Binding|JAXB]] and Zeus. Castor supports XML Schema instead of DTDs (DTDs are not supported by Castor).<ref name=":jaxdb_c"/><ref name=":cfg_c"/><ref name=":c_tscg">{{Cite web|url = https://www2.informatik.hu-berlin.de/~xing/Lib/Docs/Castor/sourcegen.html|title = The Source Code Generator|access-date = 11 February 2016|website = Castor|publisher = Github}}</ref>


==Unmarshalling and marshalling==
==Unmarshalling and marshalling==
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.<ref name=":jaxdb_c"/>
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.<ref name=":jaxdb_c"/>


==Additional Features==
==Additional features==
Castor offers some additional features which are not present in JAXB. Additional features include:
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
* Database and directory server mappings - mapping between databases and directory servers to Java
* JDO - Caster supports [[Java Data Objects]].<ref name=":jaxdb_c"/>
* JDO - Caster supports [[Java Data Objects]].<ref name=":jaxdb_c"/>


==Code Samples==
==Code samples==


Code for marshalling may look like as follows:
Code for marshalling may look like as follows:
<source lang="Java">
<syntaxhighlight lang="Java">
package javajaxb;
package javajaxb;
import java.io.File;
import java.io.File;
Line 96: Line 97:
}
}
}
}
</source><ref name=":jaxdb_c"/>
</syntaxhighlight><ref name=":jaxdb_c"/>


Code for unmarshalling may look like as follows:
Code for unmarshalling may look like as follows:
<source lang="Java">
<syntaxhighlight lang="Java">
package javajaxb;
package javajaxb;
import java.io.File;
import java.io.File;
Line 156: Line 157:
}
}
}
}
</source><ref name=":jaxdb_c"/>
</syntaxhighlight><ref name=":jaxdb_c"/>


Sample mapping file may look like as follows:
Sample mapping file may look like as follows:
<source lang="xml">
<syntaxhighlight lang="xml">
<?xml version="1.0"?>
<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Object Mapping DTD Version 1.0//EN" "http://Castor.exolab.org/mapping.dtd">
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Object Mapping DTD Version 1.0//EN" "http://Castor.exolab.org/mapping.dtd">
Line 235: Line 236:
</class>
</class>
</mapping>
</mapping>
</source><ref name=":jaxdb_c"/>
</syntaxhighlight><ref name=":jaxdb_c"/>


== See also ==
== See also ==
{{Portal|Free software|Java}}
{{Portal|Free and open-source software|Computer programming}}
* [[XML data binding]]
* [[List of object-relational mapping software]]
* [[List of object-relational mapping software]]
* [[Serialization]]
* [[Serialization]]
Line 254: Line 256:
{{Authority control}}
{{Authority control}}


[[Category:Object-relational mapping]]
[[Category:Object–relational mapping]]
[[Category:Java platform]]
[[Category:Java enterprise platform]]
[[Category:Java enterprise platform]]
[[Category:Cross-platform software]]
[[Category:Cross-platform software]]

Latest revision as of 06:48, 30 October 2023

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]