package jfun.yan.xml.nuts.optional;
import java.util.List;
import jfun.util.Misc;
import jfun.yan.Component;
import jfun.yan.Components;
import jfun.yan.xml.nuts.DelegatingNut;
/**
* Nut class for getting an element from a list/array.
*
* <p>
* @author Ben Yu
* Nov 9, 2005 11:57:47 PM
*/
public class SubscriptNut extends DelegatingNut {
private int ind = -1;
public int getInd() {
return ind;
}
public void setInd(int ind) {
checkIndex(ind);
this.ind = ind;
}
private void checkIndex(int ind){
if(ind<0){
raise("invalid subscript: "+ind);
}
}
public Component eval(){
final Component cc = getComponent();
checkMandatory("component", cc);
checkIndex(ind);
final Class type = cc.getType();
if(type!=null && !type.isArray() &&
!jfun.yan.util.Utils.isCompatible(List.class, cc)
){
throw raise("cannot apply subscript against " +
Misc.getTypeName(type));
}
return Components.subscript(cc, ind);
}
}