Castor (framework)

Last updated
Castor
Stable release
1.4.1 / May 15, 2016;4 years ago (2016-05-15)
Written in Java
Operating system Cross-platform (JVM)
Platform Java Virtual Machine
Type Data binding
License Apache 2.0
Website castor-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]

Contents

Process flow

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

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

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

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

Code samples

Code for marshalling may look like as follows:

packagejavajaxb;importjava.io.File;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;// Castorimportorg.exolab.castor.xml.MarshalException;importorg.exolab.castor.xml.ValidationException;// Generated hr.xml classesimportjavajaxb.generated.hr.*;publicclassEmployeeLister{// Existing methodspublicvoidmodify()throwsIOException,MarshalException,ValidationException{// Add a new employeeEmployeeemployee=newEmployee();employee.setName("Ben Rochester");Addressaddress=newAddress();address.setStreet1("708 Teakwood Drive");address.setCity("Flower Mound");address.setState("TX");address.setZipCode("75028");employee.addAddress(address);Organizationorganization=newOrganization();organization.setId(43);organization.setName("Technical Services");employee.setOrganization(organization);Officeoffice=newOffice();office.setId(241);AddressofficeAddress=newAddress();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 listemployees.addEmployee(employee);// marshalemployees.marshal(newFileWriter(outputFile));}publicstaticvoidmain(String[]args){try{if(args.length!=2){System.out.println("Usage: java javajaxb.EmployeeLister"+"[web.xml filename] [output.xml filename]");return;}EmployeeListerlister=newEmployeeLister(newFile(args[0]),newFile(args[1]));lister.list(true);lister.modify();}catch(Exceptione){e.printStackTrace();}}}

[2]

Code for unmarshalling may look like as follows:

packagejavajaxb;importjava.io.File;importjava.io.FileReader;importjava.io.IOException;// Castorimportorg.exolab.castor.xml.MarshalException;importorg.exolab.castor.xml.ValidationException;// Generated hr.xml classesimportjavajaxb.generated.hr.*;publicclassEmployeeLister{/** The descriptor to read in */privateFiledescriptor;/** The output file to write to */150privateFileoutputFile;/** The object tree read in */privateEmployeesemployees;publicEmployeeLister(Filedescriptor,FileoutputFile){employees=null;this.descriptor=descriptor;this.outputFile=outputFile;}publicvoidlist(booleanvalidate)throwsIOException,MarshalException,ValidationException{// Unmarshallemployees=Employees.unmarshal(newFileReader(descriptor));// Do some basic printingSystem.out.println("--- Employee Listing ---\n");Employee[]employeeList=employees.getEmployee();for(inti=0;i<employeeList.length;i++){Employeeemployee=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");}}publicstaticvoidmain(String[]args){try{if(args.length!=2){System.out.println("Usage: java javajaxb.EmployeeLister"+"[web.xml filename] [output.xml filename]");return;}EmployeeListerlister=newEmployeeLister(newFile(args[0]),newFile(args[1]));lister.list(true);}catch(Exceptione){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><classname="javajaxb.generated.hr.Employees"><map-toxml="emp-list"/><fieldname="Employee"type="javajaxb.generated.hr.Employee"><bind-xmlname="emp"/></field></class><classname="javajaxb.generated.hr.Employee"><fieldname="Id"type="integer"><bind-xmlname="emp-id"node="attribute"/></field><fieldname="name"type="java.lang.String"><bind-xmlname="emp-name"node="attribute"/></field><fieldname="Address"type="javajaxb.generated.hr.Address"><bind-xmlname="emp-address"/></field><fieldname="Organization"type="javajaxb.generated.hr.Organization"><bind-xmlname="emp-org"/></field><fieldname="Office"type="javajaxb.generated.hr.Office"><bind-xmlname="emp-office"/></field></class><classname="javajaxb.generated.hr.Address"><fieldname="Street1"type="java.lang.String"><bind-xmlname="line-1"node="element"/></field><fieldname="Street2"type="java.lang.String"><bind-xmlname="line-2"node="element"/></field><fieldname="City"type="java.lang.String"><bind-xmlname="city"node="element"/></field><fieldname="State"type="java.lang.String"><bind-xmlname="state"node="element"/></field><fieldname="ZipCode"type="java.lang.String"><bind-xmlname="zip-code"node="element"/></field></class><classname="javajaxb.generated.hr.Office"><fieldname="Id"type="integer"><bind-xmlname="office-id"node="attribute"/></field><fieldname="Address"type="javajaxb.generated.hr.Address"><bind-xmlname="office-address"node="element"/></field></class><classname="javajaxb.generated.hr.Organization"><fieldname="Id"type="integer"><bind-xmlname="org-id"node="element"/></field><fieldname="Name"type="java.lang.String"><bind-xmlname="org-name"node="element"/></field></class></mapping>

[2]

See also

Related Research Articles

In computing, serialization or serialisation is the process of translating a data structure or object state into a format that can be stored or transmitted and reconstructed later. When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object. For many complex objects, such as those that make extensive use of references, this process is not straightforward. Serialization of object-oriented objects does not include any of their associated methods with which they were previously linked.

In computing, the Java Remote Method Invocation is a Java API that performs remote method invocation, the object-oriented equivalent of remote procedure calls (RPC), with support for direct transfer of serialized Java classes and distributed garbage-collection.

In computing, the Java API for XML Processing, or JAXP, one of the Java XML Application programming interfaces (API)s, provides the capability of validating and parsing XML documents. It has three basic parsing interfaces:

ActionScript object-oriented programming language

ActionScript is an object-oriented programming language originally developed by Macromedia Inc.. It is influenced by HyperTalk, the scripting language for HyperCard. It is now an implementation of ECMAScript, though it originally arose as a sibling, both being influenced by HyperTalk.

Jakarta XML Binding is a software framework that allows Jakarta EE developers to map Java classes to XML representations. JAXB provides two main features: the ability to marshal Java objects into XML and the inverse, i.e. to unmarshal XML back into Java objects. In other words, JAXB allows storing and retrieving data in memory in any XML format, without the need to implement a specific set of XML loading and saving routines for the program's class structure. It is similar to xsd.exe and XmlSerializer in the .NET Framework.

Java syntax

The syntax of Java refers to the set of rules defining how a Java program is written and interpreted.

In computer programming, an entry point is where the first instructions of a program are executed, and where the program has access to command line arguments.

A Java compiler is a compiler for the programming language Java. The most common form of output from a Java compiler is Java class files containing platform-neutral Java bytecode, but there are also compilers that output optimized native machine code for a particular hardware/operating system combination.

XMLBeans is a Java-to-XML binding framework which is part of the Apache Software Foundation XML project.

In the Java computer programming language, an annotation is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and Java packages may be annotated. Like Javadoc tags, Java annotations can be read from source files. Unlike Javadoc tags, Java annotations can also be embedded in and read from Java class files generated by the Java compiler. This allows annotations to be retained by the Java virtual machine at run-time and read via reflection. It is possible to create meta-annotations out of the existing ones in Java.

JGroups is a library for reliable one-to-one or one-to-many communication written in the Java language.

In computer science, marshalling or marshaling is the process of transforming the memory representation of an object to a data format suitable for storage or transmission, and it is typically used when data must be moved between different parts of a computer program or from one program to another. Marshalling is similar to serialization and is used to communicate to remote objects with an object, in this case a serialized object. It simplifies complex communication, using composite objects in order to communicate instead of primitives. The inverse of marshalling is called unmarshalling. An unmarshalling interface takes the serialized object and transforms it into a internal data structure, which can be referred to as executable.

Apache Empire-db is a Java library that provides a high level object-oriented API for accessing relational database management systems (RDBMS) through JDBC. Apache Empire-db is open source and provided under the Apache License 2.0 from the Apache Software Foundation.

Static import is a feature introduced in the Java programming language that allows members which have been scoped within their container class as public static, to be used in Java code without specifying the class in which the field has been defined. This feature was introduced into the language in version 5.0.

Java Database Connectivity (JDBC) is an application programming interface (API) for the programming language Java, which defines how a client may access a database. It is a Java-based data access technology used for Java database connectivity. It is part of the Java Standard Edition platform, from Oracle Corporation. It provides methods to query and update data in a database, and is oriented toward relational databases. A JDBC-to-ODBC bridge enables connections to any ODBC-accessible data source in the Java virtual machine (JVM) host environment.

XQuery API for Java

XQuery API for Java (XQJ) refers to the common Java API for the W3C XQuery 1.0 specification.

Gson is an open-source Java library to serialize and deserialize Java objects to JSON.

Kotlin is a cross-platform, statically typed, general-purpose programming language with type inference. Kotlin is designed to interoperate fully with Java, and the JVM version of Kotlin's standard library depends on the Java Class Library, but type inference allows its syntax to be more concise. Kotlin mainly targets the JVM, but also compiles to JavaScript or native code, e.g. for native iOS apps sharing business logic with Android apps. Language development costs are borne by JetBrains, while the Kotlin Foundation protects the Kotlin trademark.

JExcel is a library (API) to read, write, display, and modify Excel files with .xls or .xlsx formats. API can be embedded with Java Swing and AWT.

Apache Commons Logging is a Java-based logging utility and a programming model for logging and for other toolkits. It provides APIs, log implementations, and wrapper implementations over some other tools.

References

  1. "About". Castor. Github. Retrieved 11 February 2016.
  2. 1 2 3 4 5 6 7 8 McLaughin, B (2002). Java and XML Data Binding. Sebastopol: O'Reilly & Associates, Inc. pp. 143–165.
  3. 1 2 3 4 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.