XMLからオブジェクトを作る その3

コンストラクタに対応。

XMLのデータ

こんな感じ。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE bodybuilder>
<bodybuilder>
  <list>
    <add value="value1"/>
    <add value="value2"/>
    <add>
      <list>
        <constructor>
          <arg type="java.util.Collection">
            <list>
              <add value="constructor value1"/>
              <add value="constructor value2"/>
            </list>
          </arg>
        </constructor>
        <add value="nested value1"/>
        <add value="nested value2"/>
        <add>
          <bean type="sample.FakeBean">
            <constructor>
              <arg value="FOO"/>
              <arg value="BAR"/>
            </constructor>
          </bean>
        </add>
      </list>
    </add>
    <add value="value4"/>
    <add>
      <map>
        <constructor>
          <arg type="java.util.Map">
            <map>
              <constkey1 value="const map value1"/>
              <constkey2 value="const map value2"/>
              <constkey3>
                <map>
                  <mapvals foo="bar" hoge="hogehoge"/>
                </map>
              </constkey3>
            </map>
          </arg>
        </constructor>
        <key1 value="map value1"/>
        <key2 value="map value2"/>
      </map>
    </add>
  </list>
</bodybuilder>

サンプルのコード

こんな感じ。


package sample;

import bodybuilder.builder.Bullworker;
import bodybuilder.viewer.Viewer;

public class Sample4 {

public static void main(String[] args) {
Bullworker bullworker = new Bullworker(
"C:\\eclipse3\\workspace\\bodybuilder\\data\\sample4.xml");
Object obj = bullworker.getMuscle();
Viewer.dump(obj);
}

}

サンプルの実行結果

こんな感じ。


ArrayList(5) {
[0]=>
String(6) "value1"
[1]=>
String(6) "value2"
[2]=>
ArrayList(5) {
[0]=>
String(18) "constructor value1"
[1]=>
String(18) "constructor value2"
[2]=>
String(13) "nested value1"
[3]=>
String(13) "nested value2"
[4]=>
sample.FakeBean {
["foo"]=>
String(3) "FOO"
["bar"]=>
String(3) "BAR"
}
}
[3]=>
String(6) "value4"
[4]=>
HashMap(5) {
["key1"]=>
String(10) "map value1"
["constkey3"]=>
HashMap(2) {
["foo"]=>
String(3) "bar"
["hoge"]=>
String(8) "hogehoge"
}
["constkey1"]=>
String(16) "const map value1"
["constkey2"]=>
String(16) "const map value2"
["key2"]=>
String(10) "map value2"
}
}

TODO

  • サーブレットAPIへの対応。
    • MockRunner、StrutsTestCaseへの対応。
  • オブジェクトを再帰的に比較するAssertクラスの作成。
    • 精査しない(できない)値への対応。
  • XMLの一ファイルを一テストケースとするテストユーティリティの作成。
  • ロギングの追加。
  • コンストラクタへの対応。
  • 特殊値(null日付型、改行を含む文字列)への対応。
  • Bullworkerクラスの進退。
  • スタックトレースをも少し見やすく。
  • メモリリークしてるかも。

その他

Eclipseのプロジェクト一式*1

*1:JUnit、Commons BeanUtils/Logging、JDOM、Xerces