KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > xml > nuts > optional > SubscriptNut


1 package jfun.yan.xml.nuts.optional;
2
3 import java.util.List JavaDoc;
4
5 import jfun.util.Misc;
6 import jfun.yan.Component;
7 import jfun.yan.Components;
8 import jfun.yan.xml.nuts.DelegatingNut;
9 /**
10  * Nut class for getting an element from a list/array.
11  *
12  * <p>
13  * @author Ben Yu
14  * Nov 9, 2005 11:57:47 PM
15  */

16 public class SubscriptNut extends DelegatingNut {
17   private int ind = -1;
18   
19   public int getInd() {
20     return ind;
21   }
22
23   public void setInd(int ind) {
24     checkIndex(ind);
25     this.ind = ind;
26   }
27   private void checkIndex(int ind){
28     if(ind<0){
29       raise("invalid subscript: "+ind);
30     }
31   }
32   public Component eval(){
33     final Component cc = getComponent();
34     checkMandatory("component", cc);
35     checkIndex(ind);
36     final Class JavaDoc type = cc.getType();
37     if(type!=null && !type.isArray() &&
38         !jfun.yan.util.Utils.isCompatible(List JavaDoc.class, cc)
39     ){
40       throw raise("cannot apply subscript against " +
41           Misc.getTypeName(type));
42     }
43     return Components.subscript(cc, ind);
44   }
45
46 }
47
Popular Tags