KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > xml > nuts > MapNut


1 package jfun.yan.xml.nuts;
2
3
4 import java.util.HashMap JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import jfun.util.Misc;
9 import jfun.yan.Component;
10 import jfun.yan.Components;
11 import jfun.yan.SimpleComponent;
12 import jfun.yan.util.Utils;
13 /**
14  * Nut class for <map> tag.
15  * <p>
16  * @author Ben Yu
17  * Nov 9, 2005 11:42:15 PM
18  */

19 public class MapNut extends EntriesNut {
20   private Class JavaDoc type = HashMap JavaDoc.class;
21   public Class JavaDoc getType() {
22     return type;
23   }
24   
25   public void setType(Class JavaDoc type) {
26     if(!Map JavaDoc.class.isAssignableFrom(type)){
27       raise(Misc.getTypeName(type)+" is not a subtype of java.util.Map");
28     }
29     this.type = type;
30   }
31   public Map JavaDoc createMap(int sz){
32     try{
33       return Utils.createMap(type, sz);
34     }
35     catch(Exception JavaDoc e){
36       throw raise(e);
37     }
38   }
39   public Component eval(){
40     checkMandatory("map type", type);
41     final List JavaDoc keys = getKeys();
42     final int sz = keys.size();
43     final Component[] vals = getEntryComponents();
44     final Component step1 = new SimpleComponent(type){
45       public Object JavaDoc create(){
46         return createMap(sz);
47       }
48     };
49     return Components.storeMap(step1, keys.toArray(), vals);
50     /*
51     return Components.array(vals, Object.class)
52     .map(new jfun.yan.Map(){
53       public Object map(Object a){
54         final Object[] arr = (Object[])a;
55         final int sz = keys.size();
56         final Map result = createMap(sz);
57         for(int i=0; i<sz;i++){
58           result.put(keys.get(i), arr[i]);
59         }
60         return result;
61       }
62     }).cast(type);*/

63   }
64 }
65
Popular Tags