3.入れ子Beanの読み込み |
|
|
|
|
|
|
|
|
|
|
|
JavaBean
sample.util.digester.ListBean.java |
|
|
package
sample.util.digester; |
|
|
import
java.util.ArrayList; |
|
import
java.util.List; |
|
import
sample.util.db.SoftwareBean; |
|
|
public
class ListBean { |
|
|
|
private String pref =
""; |
|
|
private String city =
""; |
|
|
private List list_ = new
ArrayList(); |
|
|
|
private static final
String LS = System.getProperty("line.separator"); |
|
|
|
public String toString()
{ |
|
|
StringBuffer sb = new
StringBuffer(); |
|
|
sb.append(pref); |
|
|
sb.append("|"); |
|
|
sb.append(city); |
|
|
sb.append(LS); |
|
|
|
for(int i = 0 ; i <
list_.size() ; i++) { |
|
|
SoftwareBean softBean =
(SoftwareBean)list_.get(i); |
|
|
sb.append("
"+softBean.toString() + LS); |
|
|
} |
|
|
|
return sb.toString(); |
|
|
} |
|
|
|
|
|
public void
addSoftwareBean(SoftwareBean swBean) { |
|
|
list_.add(swBean); |
|
|
} |
|
|
|
public List
getSoftwareBeanList() { |
|
|
return list_; |
|
|
} |
|
|
/** |
|
|
* @return |
|
|
*/ |
|
|
public String getCity() { |
|
|
return city; |
|
|
} |
|
|
|
/** |
|
|
* @return |
|
|
*/ |
|
|
public String getPref() { |
|
|
return pref; |
|
|
} |
|
|
|
/** |
|
|
* @param string |
|
|
*/ |
|
|
public void
setCity(String string) { |
|
|
city = string; |
|
|
} |
|
|
|
/** |
|
|
* @param string |
|
|
*/ |
|
|
public void
setPref(String string) { |
|
|
pref = string; |
|
|
} |
|
|
} |
|
|
SampleBean softBean =
(SampleBean)list_.get(i); |
|
|
sb.append("
"+softBean.toString() + LS); |
|
|
} |
|
|
|
return sb.toString(); |
|
|
} |
|
|
public void
addSampleBean(SampleBean swBean) { |
|
|
list_.add(swBean); |
|
|
} |
|
|
|
public List
getSampleBeanList() { |
|
|
return list_; |
|
|
} |
|
|
public String getCity() { |
|
|
return city; |
|
|
} |
|
|
public String getPref() { |
|
|
return pref; |
|
|
} |
|
|
public void
setCity(String string) { |
|
|
city = string; |
|
|
} |
|
|
|
public void
setPref(String string) { |
|
|
pref = string; |
|
|
} |
|
} |
|
|
XMLデータファイルc:\usr\local\digester\ListBeanData.xml |
|
<?xml
version='1.0' encoding='UTF-8'?> |
|
<dataset> |
|
<address pref="東京"
city="高輪"/> |
|
<pcinfo id="PC2 "
os="win2000" cpumhz="500" memmb="256"
hdgb="16" http_server="" |
|
db="" java=""
visual_studio="" virus_tool="" etc=""
maker="FujitsuDeskPower" remarks=" "/> |
|
<pcinfo id="PC3 "
os="win2000" cpumhz="500" memmb="192"
hdgb="12" http_server="" |
|
db="" java=""
visual_studio="あぱっち" virus_tool="" etc="漢字テスト"
maker="FujitsuDeskPower" remarks=" "/> |
|
</dataset> |
|
|
ルールファイル c:\usr\local\digester\ListBeanRule.xml |
|
<?xml
version = "1.0" encoding = "Shift_JIS" ?> |
|
|
<digester-rules> |
|
|
<object-create-rule
pattern="dataset"
classname="sample.util.digester.ListBean" /> |
|
|
|
<pattern
value="dataset/address"> |
|
|
<set-properties-rule
/> |
|
|
</pattern> |
|
|
|
<pattern
value="dataset/pcinfo"> |
|
|
<object-create-rule
classname="sample.util.db.SoftwareBean" /> |
|
|
<set-properties-rule
/> |
|
|
<set-next-rule
methodname="addSoftwareBean" /> |
|
|
</pattern> |
|
</digester-rules> |
|
|
BeanのListと違うのは、最初のpatternタグで、detaset/addressを見つけると、 |
|
ListBeanの属性にaddressタグの属性をセットするところである。 |
|
|
入れ子(List)がないjavaBeanの場合、ここだけでjavaBeanにセットできる。 |
|
|
読み込みテストクラス |
|
package
sample.util.digester; |
|
|
import
java.io.File; |
|
|
import
org.apache.commons.digester.Digester; |
|
import
org.apache.commons.digester.xmlrules.DigesterLoader; |
|
|
import
java.rmi.*; |
|
|
public
class ListBeanTest implements java.rmi.Remote { |
|
|
|
public ListBeanTest() {} |
|
|
|
public ListBean
getListBean() throws RemoteException { |
|
|
ListBean lb = null; |
|
|
try { |
|
|
|
Digester d =
DigesterLoader.createDigester(new
File("/usr/local/digester/ListBeanRule.xml").toURL()); |
|
|
lb = (ListBean)d.parse(new
File("/usr/local/digester/ListBeanData.xml")); |
|
|
|
} catch( Exception exc )
{ |
|
|
exc.printStackTrace(); |
|
|
} |
|
|
return lb; |
|
|
} |
|
|
|
public static void
main(String[] args) throws Exception { |
|
|
ListBeanTest lbt = new
ListBeanTest(); |
|
|
ListBean lb =
lbt.getListBean(); |
|
|
System.out.println(lb.toString()
); |
|
|
} |
|
} |
|
|
|
ListBeanTest実行結果 |
|
|
東京|高輪 |
|
PC2,FujitsuDeskPower,500,256,16,,,,,,, |
|
PC3,FujitsuDeskPower,500,192,12,,漢字テスト,,,,あぱっち, |
|
|
|
JSP結果は以下のようになる。 |
|
|
|
|
|
|
|
|
S |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|