package impl.manipulators;
import Sight.Agents.*;
import Sight.Agents.Prototypes.*;
import Sight.dds.*;
import java.io.*;
import java.util.*;
import Sight.Agents.util.*;
public class rangeSelector extends Wrapper implements Serializable, Fast {
public class Request extends Sight.Request implements java.io.Serializable {
public CharSequence Sequence;
public CharSequence from;
public CharSequence to;
public CharSequence Region = null;
public CharSequence take = "inside";
public CharSequence reg_len = "-1";
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("take","CharSequence","Take region","inside",
new String[] {"left","inside","right","all","outside"},new String[]
{"left","inside (default)","right","all three as separate records","left and right regions"}),
new dField("from","CharSequence","Take from, inclusive (0=from start)",""),
new dField("to","CharSequence","Take to, inclusive (-1=till end)",""),
new dField("Region","CharSequence","Region to take, has priority if set.",""),
new dField("reg_len","CharSequence",
"Region length (valid for left and right sub-regions only, -1=unlimited)","-1"),
new dField("Sequence","CharSequence","Sequence to manipulate",""),
}
);
}
public static class Record extends Sight.Record implements Serializable {
public CharSequence Sequence;
public String howTaken = "";
};
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.howTaken+", length "+r.Sequence.length();
case 1: return r.howTaken+", length "+r.Sequence.length();
default: return r.howTaken+", length "+r.Sequence.length()+
"<br><textarea>"+r.Sequence+"</textarea>";
}
};
}
public dStructure getResultDds() {
return new Sight.dds.Records(false,
new dField[] {
new dField("Sequence","CharSequence","Truncated sub-sequence","") }
);
}
public Object go(Object o_request) throws Exception
{
try {
Request request = (Request) o_request;
Result result = new Result();
Record record = new Record();
int from, to;
if (request.Region==null || request.Region.toString().trim().length()==0)
{
from = Integer.parseInt(request.from.toString().trim());
to = Integer.parseInt(request.to.toString().trim());
} else
{
StringTokenizer st = new StringTokenizer(request.Region.toString()," \t\r\n.,-(){}[];:");
from = 0;
if (st.hasMoreTokens()) from =
Integer.parseInt(st.nextToken());
to = request.Sequence.length();
if (st.hasMoreTokens()) to =
Integer.parseInt(st.nextToken());
};
if (to<0) to = request.Sequence.length()+1;
CharSequence sLeft = request.Sequence.subSequence(0, from);
CharSequence sRight = request.Sequence.subSequence(to, request.Sequence.length());
CharSequence sInside = request.Sequence.subSequence(from, to+1);
try {
int limit = Integer.parseInt(request.reg_len.toString().trim());
if (limit>0)
{
if (sLeft.length()>limit)
sLeft = sLeft.subSequence(sLeft.length()-limit, sLeft.length());
if (sRight.length()>limit)
sRight = sRight.subSequence(0, limit);
};
} catch (NumberFormatException exc) {};
String mLeft = " left before "+from+" ";
String mRight = " right after "+(to-1)+" ";
String mInside = " inside ["+from+".."+to+"] ";
if (request.take.equals("all"))
{
result.a = new Record[3];
for (int i = 0; i < result.a.length; i++) {
result.a[i] = new Record();
}
result.a[0].Sequence=sLeft;
result.a[0].howTaken=mLeft;
result.a[1].Sequence=sInside;
result.a[1].howTaken=mInside;
result.a[2].Sequence=sRight;
result.a[2].howTaken=mRight;
} else
if (request.take.equals("outside"))
{
result.a = new Record[2];
for (int i = 0; i < result.a.length; i++) {
result.a[i] = new Record();
}
result.a[0].Sequence=sLeft;
result.a[0].howTaken=mLeft;
result.a[1].Sequence=sRight;
result.a[1].howTaken=mRight;
} else
{
if (request.take.equals("left"))
{
record.Sequence = sLeft;
record.howTaken = mLeft;
}
else
if (request.take.equals("right"))
{
record.Sequence = sRight;
record.howTaken = mRight;
}
else
if (request.take.equals("inside"))
{
record.Sequence = sInside;
record.howTaken = mInside;
};
result.a = new Record[1];
result.a[0] = record;
};
result.setAgentSignature(getHTMLSignature());
return result;
} catch (Exception exc)
{ exc.printStackTrace();
};
return null;
};
public rangeSelector() {
setDescription("Allows to select the specified sub-sequence in a sequence.");
setMasterURL("");
setWriteCache(false);
setReadCache(false);
};
public static rangeSelector Public = new rangeSelector();
public static Agent getAvailableAgent() { return Public; };
public static void main(String[] args) {
Sight.Agents.util.Pind.showConsoles();
try {
Request request = getDefaultRequest();
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();
};
}
;
}