package net.sf.jasperreports.engine.base;

import java.io.Serializable;
import net.sf.jasperreports.engine.JRQueryChunk;
import net.sf.jasperreports.engine.JRRuntimeException;
import net.sf.jasperreports.engine.util.JRQueryParser;

public class JRBaseQueryChunk implements JRQueryChunk, Serializable {
  private static final long serialVersionUID = 10200L;
  
  protected byte type = 1;
  
  protected String text = null;
  
  protected String[] tokens;
  
  protected JRBaseQueryChunk() {}
  
  protected JRBaseQueryChunk(JRQueryChunk queryChunk, JRBaseObjectFactory factory) {
    factory.put(queryChunk, this);
    this.type = queryChunk.getType();
    this.text = queryChunk.getText();
    String[] chunkTokens = queryChunk.getTokens();
    if (chunkTokens == null) {
      this.tokens = null;
    } else {
      this.tokens = new String[chunkTokens.length];
      System.arraycopy(chunkTokens, 0, this.tokens, 0, chunkTokens.length);
    } 
  }
  
  public byte getType() {
    return this.type;
  }
  
  public String getText() {
    if (this.type == 4)
      return JRQueryParser.instance().asClauseText(getTokens()); 
    return this.text;
  }
  
  public String[] getTokens() {
    return this.tokens;
  }
  
  public Object clone() {
    try {
      return super.clone();
    } catch (CloneNotSupportedException e) {
      throw new JRRuntimeException(e);
    } 
  }
}
