/**
 *  Find the given regular expression in your sequence
 *  THIS AGENT IS NOT COMPLETE.
 *  It is skeleton for a wrapper that computes result from request.
 *  You must complete the go(..) method self dependently.
 *  Generated 2002.9.21  by Sight 2.0, Ulm university.
 */
package impl.manipulators;

import Sight.Agents.*;
import Sight.Agents.Prototypes.*;
import Sight.dds.*;
import java.io.*;
import java.util.*;
import java.util.regex.*;

public class regExpFinder extends Sight.Agents.Prototypes.Wrapper implements Serializable, Fast {



  /* Data structure to pass query to this robot. */
  public class Request extends Sight.Request implements java.io.Serializable {
        /** Regular expression to find. See http://java.sun.com/j2se/1.4/docs/api/java/util/regex/Pattern.html for syntax. */
        public CharSequence Expression;
        /** Sequence to search. */
        public CharSequence Sequence;
       /** Submit request, compute key by SHA digesting algorithm. */
       public Sight.Agents.Request submit() { return Sight.Agents.Request.submit(Public, this, getKey()); };
       /** Submit request with the known key. */
       public Sight.Agents.Request submit(String key)
        { return Sight.Agents.Request.submit(Public, this, key); };
       };

  /** Get default request for this robot. */
  public static Request getDefaultRequest() {
     return Public.getRequest(); }
  private Request getRequest() { return new Request(); }; // must be non static

    /** Type definitions for Request. */
   public dStructure getRequestDds() {
    return new Sight.dds.Records(true,
     new dField[] {
        new dField("Expression","CharSequence","Regular expression to find. See http://java.sun.com/j2se/1.4/docs/api/java/util/regex/Pattern.html for syntax.",""),
        new dField("Sequence","CharSequence","Sequence to search.","")                 }
    );
   }

 /* Record type, used in results for this robot. */
 /* The result of this robot is an array of records. */
  public static class Record extends Sight.Record implements Serializable {
        /** Start of the found expression, inclusive */
        public CharSequence From;
        /** End of the found expression, inclusive */
        public CharSequence To;
        /** String, matching the found expression */
        public CharSequence Inside;
        /** Upstream sequence of the found expression */
        public CharSequence Upstream;
        /** Downstream sequence of the found expression */
        public CharSequence Downstream;
      };
      /** The result of this robot. */
      public static class Result extends Sight.Result implements Serializable {
         public Record[] a; // array of results.
         /** Create report for one record. */
         public CharSequence getReportForRecord(Object ro, int level)
          { Record r = (Record) ro;
           switch (level) {
              case 0:  return "["+r.From+".."+r.To+"]";
              case 1:  return "["+r.From+".."+r.To+"] found <textarea>"+r.Inside+"</textarea>";
              default: return "["+r.From+".."+r.To+"] found"+
               "<br>Inside:<textarea>"+r.Inside+"</textarea>"+
               "<br>Upstream:<textarea>"+r.Upstream+"</textarea>"+
               "<br><Downstream>"+r.Downstream+"</textarea>";
            }
          };
      }
    /** Type definitions for Result. */
   public dStructure getResultDds() {
    return new Sight.dds.Records(false,
     new dField[] {
        new dField("From","CharSequence","Start of the found expression, inclusive",""),
        new dField("To","CharSequence","End of the found expression, inclusive",""),
        new dField("Inside","CharSequence","String, matching the found expression",""),
        new dField("Upstream","CharSequence","Upstream sequence of the found expression",""),
        new dField("Downstream","CharSequence","Downstream sequence of the found expression","")                 }
    );
   }


   Pattern pattern = null;
   /** Implementation of the central go method.  */
   public Object go(Object o_request) throws Exception
    {
     /** @todo: complete the go(...) method for this wrapper. */
     Request request = (Request) o_request;

     if (pattern==null ||
      !pattern.pattern().equals(request.Expression))
       {
         pattern = Pattern.compile(request.Expression.toString(),
          Pattern.CASE_INSENSITIVE);
       };

     //
      ArrayList list = new ArrayList();
      Matcher m = pattern.matcher(request.Sequence);
      try {
      do
       {
         m.find();
         Record r = new Record();
         r.From = ""+m.start();
         r.To   = ""+(m.end()-1);
         r.Inside =   request.Sequence.subSequence(m.start(), m.end());
         r.Upstream = request.Sequence.subSequence(0, m.start());
         r.Downstream = request.Sequence.subSequence(m.end(), request.Sequence.length());
         list.add(r);
       } while (true);

       } catch (java.lang.IllegalStateException exc)
         {
         };

     Result  result  = new Result();

     result.a = new Record[list.size()];
     for (int i = 0; i < result.a.length; i++) {
       result.a[i] = (Record) list.get(i);
     }

     // set the agent signature:
     result.setAgentSignature("search for '"+pattern.pattern()+"'");
     return result;
    };

  final Result EMPTY;
  public regExpFinder() {
   setDescription("Find the given regular expression in your sequence");
   setMasterURL("http://java.sun.com/j2se/1.4/docs/api/java/util/regex/Pattern.html");
   EMPTY = new Result();
   EMPTY.a = new Record[0];
   // this robot newer uses caching:
   setWriteCache(false);
   setReadCache(false);
   };
  /** Public instance of agent for user. */
  public static regExpFinder Public = new regExpFinder();
  public static Agent getAvailableAgent() { return Public; };
  /** main(..) method provided to check robot, launching it
   * as standalone program. */
  public static void main(String[] args) {
      try {
        Request request = getDefaultRequest();
        request.Sequence = "1234A67891234B6789";
        request.Expression = "34.6";
        // Now modify and assign request fields in accordance with task:
        Sight.Agents.Request submission = request.submit();
        // processing now started in separate thread:
        Result response = (Result) submission.getResult();
        // print report, verbosity level 2:
        System.out.println(response.getReport(2));
      } catch (Exception exc)
         { if (exc!=null) System.out.println(exc.getMessage());
           exc.printStackTrace();
         };
   }
;
  }