Loading article…
| 安定リリース | 1.4.1 / 2016年5月15日 |
|---|---|
| 書かれた | ジャワ |
| オペレーティング·システム | クロスプラットフォーム( JVM ) |
| プラットフォーム | Java仮想マシン |
| タイプ | データバインディング |
| ライセンス | アパッチ2.0 |
| Webサイト | Castor-data-binding.github.io/castor/ |
CastorはJavaのデータバインディングフレームワークで、JavaからJava-XMLへのバインディング、Java-SQLへの永続性、Javaオブジェクト、XMLドキュメント、リレーショナルテーブル間のパスなどの機能を備えています。 [1] [2] [3] Castorは最も古いデータバインディングプロジェクトの1つです。[3]
プロセスフロー
基本的なプロセスフローには、クラスの生成、マーシャリング、アンマーシャリングなどが含まれます。 [2] マーシャリングフレームワークには、オブジェクトを記述するためのClassDescriptorsとFieldDescriptionのセットが含まれています。[3]
クラス生成
クラス生成はJAXBやZeusに似ています。CastorはDTDの代わりにXMLスキーマをサポートしています(DTDはCastorではサポートされていません)。[2] [3] [4]
アンマーシャリングとマーシャリング
アンマーシャリングとマーシャリングはそれぞれ marshall() メソッドと unmarshall() メソッドで処理されます。マーシャリング中は Java から XML への変換プロセスが実行され、アンマーシャリング中は XML から Java への変換プロセスが実行されます。マッピング ファイルはバインディング スキーマに相当し、XML から Java へ、またはその逆の名前の変換を可能にします。[2]
追加機能
Castor は、JAXB にはないいくつかの追加機能を提供します。追加機能には次のものがあります。
- データベースとディレクトリ サーバーのマッピング - データベースとディレクトリ サーバー間の Java へのマッピング
- JDO - CasterはJava Data Objectsをサポートしています。[2]
コードサンプル
マーシャリングのコードは次のようになります。
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 ; // 生成された hr.xml クラスimport javajaxb.generated.hr.* ; public class EmployeeLister { //既存のメソッドpublic void modify () throws IOException , MarshalException , ValidationException { // 新しい従業員を追加します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 ); // 従業員をリストに追加しますemployees . addEmployee ( employee ); // 従業員をマーシャリングします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 . modification (); } catch ( Exception e ) { e . printStackTrace (); } } }
[2]
アンマーシャリングのコードは次のようになります。
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 ; //生成された hr.xml クラスimport javajaxb.generated.hr.* ; public class EmployeeLister { /** 読み込む記述子 */ private File description ; /** 書き込む出力ファイル */ 150 private File outputFile ; / ** 読み込むオブジェクト ツリー */ private Employees employees ; public EmployeeLister ( File description , File outputFile ) { employees = null ; this.descriptor = description ; this.outputFile = outputFile ; } public void list ( boolean valid ) throws IOException , MarshalException , ValidationException { //アンマーシャリングemployees = Employees . unmarshal ( new FileReader ( description ) ); //基本的な印刷を実行しますSystem.out.println ( " ---従業員リスト ---\ 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
( "組織: " + employee . getOrganization (). getName ()); System . out . println ( "オフィス: " + employee . getOffice (). getAddress (). getCity () + ", " + employee . getOffice (). getAddress (). getState () + "\n" ); } } public static void main ( String [] args ) { try { if ( args . length != 2 ) { System . out . println ( "使用方法: java javajaxb.EmployeeLister " + "[web.xml ファイル名] [output.xml ファイル名]" ); return ; } EmployeeLister lister = new EmployeeLister ( new File ( args [ 0 ] ), new File ( args [ 1 ] )); lister . list ( true ); } catch ( Exception e ) { e . printStackTrace (); } } }
[2]
サンプル マッピング ファイルは次のようになります。
<?xml version="1.0"?>
<!DOCTYPE マッピング PUBLIC "-//EXOLAB/Castor オブジェクト マッピング DTD バージョン 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> <フィールド名= "Address"タイプ= "javajaxb.generated.hr.Address" > <bind-xml名= "emp-address" /> </field> <フィールド名= "Organization"タイプ= "javajaxb.generated.hr.Organization" > <bind-xml名= "emp-org" /> </field> <フィールド名= "Office"タイプ= "javajaxb.generated.hr.Office" > <bind-xml名= "emp-office" /> </field> </class> <class名= "javajaxb.generated.hr.Address" > <フィールド名= "Street1"タイプ= "java.lang.String" > <bind-xml名= "line-1"ノード= "element" /> </field> <フィールド名= "Street2"タイプ= "java.lang.文字列" > <bind-xml name= "line-2"ノード= "element" /> </field> <field name= "City"タイプ= "java.lang.String" > <bind-xml name= "city"ノード= "element" /> </field> <field name= "State"タイプ= "java.lang.String" > <bind-xml name= "state"
node= "要素" />
</field> <フィールド名= "ZipCode"タイプ= "java.lang.String" > <bind-xml名= "郵便番号" node= "要素" /> </field> </class> <class名= "javajaxb.generated.hr.Office" > <フィールド名= "Id"タイプ= "整数" > <bind-xml名= "オフィス ID" node= "属性" /> </field> <フィールド名= "住所"タイプ= "javajaxb.generated.hr.Address" > <bind-xml名= "オフィス アドレス" node= "要素" /> </field> </class> <class名= "javajaxb.generated.hr.Organization" > <フィールド名= "Id"タイプ= "整数" > <bind-xml名= "組織 ID" node= "要素" /> </field> <フィールド名= "名前"タイプ= "java.lang.String" > <bind-xml name= "org-name" node= "element" /> </field> </class> </mapping>
[2]
参照
参考文献
- ^ 「About」. Castor . Github . 2016年2月11日閲覧。
- ^ abcdefgh McLaughin, B (2002). Java および XML データバインディング. セバストポル: O'Reilly & Associates, Inc. pp. 143–165.
- ^ abcd XML開発者ガイド. スコッツバレー、米国: Borland Software Corporation. 2002年. pp. (2–29)–(2–31).
- ^ 「ソースコードジェネレーター」。Castor。Github。2016年2月11日閲覧。
外部リンク
- Webサイト
- GitHubの Castor
- Maven リポジトリ
