Package jfun.yan.xml.nuts.optional

Source Code of jfun.yan.xml.nuts.optional.SubscriptNut

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);
  }

}
TOP

Related Classes of jfun.yan.xml.nuts.optional.SubscriptNut

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.