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 {
public class Request extends Sight.Request implements java.io.Serializable {
public CharSequence Expression;
public CharSequence Sequence;
public Sight.Agents.Request submit() { return Sight.Agents.Request.submit(Public, this, getKey()); };
public Sight.Agents.Request submit(String key)
{ return Sight.Agents.Request.submit(Public, this, key); };
};
public static Request getDefaultRequest() {
return Public.getRequest(); }
private Request getRequest() { return new 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.","") }
);
}
public static class Record extends Sight.Record implements Serializable {
public CharSequence From;
public CharSequence To;
public CharSequence Inside;
public CharSequence Upstream;
public CharSequence Downstream;
};
public static class Result extends Sight.Result implements Serializable {
public Record[] a;
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>";
}
};
}
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;
public Object go(Object o_request) throws Exception
{
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);
}
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];
setWriteCache(false);
setReadCache(false);
};
public static regExpFinder Public = new regExpFinder();
public static Agent getAvailableAgent() { return Public; };
public static void main(String[] args) {
try {
Request request = getDefaultRequest();
request.Sequence = "1234A67891234B6789";
request.Expression = "34.6";
Sight.Agents.Request submission = request.submit();
Result response = (Result) submission.getResult();
System.out.println(response.getReport(2));
} catch (Exception exc)
{ if (exc!=null) System.out.println(exc.getMessage());
exc.printStackTrace();
};
}
;
}