X-Git-Url: http://git.megacz.com/?p=org.ibex.xt-crawshaw.git;a=blobdiff_plain;f=src%2Fjava%2Forg%2Fibex%2Fxt%2Fshell%2FRequest.java;fp=src%2Fjava%2Forg%2Fibex%2Fxt%2Fshell%2FRequest.java;h=f0d36976cca5e738dd0e2aed77bfa7b3a3129f68;hp=eec18b791f71519cdb6909fcfbd09a7f280f4bb5;hb=fc9f528e7acb1baf7c145b22dd0b6469968aaec0;hpb=79f04e59b0a9997c56f0bc3859be30f6fd262717
diff --git a/src/java/org/ibex/xt/shell/Request.java b/src/java/org/ibex/xt/shell/Request.java
index eec18b7..f0d3697 100644
--- a/src/java/org/ibex/xt/shell/Request.java
+++ b/src/java/org/ibex/xt/shell/Request.java
@@ -18,44 +18,58 @@ public abstract class Request implements Serializable {
}
public static class Key extends Request {
- private String path, matcher;
+ protected String path, matcher;
public Key() {}
+
+ /** Expects a shell-path that uses '.' as a seperator and * for wildcard. */
public Key(String c) {
int pos = c.lastIndexOf('.');
path = c.substring(0, pos);
matcher = c.substring(pos + 1).replaceAll("\\*+", ".*");
}
+
public Key(String path, String matcher) {
this.path = path; this.matcher = matcher;
}
- public Response process(JSScope root) throws JSExn {
+ /** Returns the object referenced by path. */
+ protected Object path(JSScope root) throws JSExn {
String p = path == null ? "" : path.replaceAll("\\.+", "\\.");
if (p.length() > 0 && p.charAt(0) == '.')
p = p.substring(1);
if (p.length() > 0 && p.charAt(p.length() - 1) == '.')
p = p.substring(0, p.length() - 1);
- System.out.println("searching path '"+p+"' for pattern '"+matcher+"'");
-
- Object o = p.equals("") ? root : root.get(p);
- if (o == null || o instanceof JSDate ||
- o instanceof JSArray ||
- !(o instanceof JS)) {
- System.out.println("hit bad object: "+o+", class="+
- (o == null ? null : o.getClass().getName()));
- return new Key.Res();
- } else {
- Pattern pat = Pattern.compile(matcher);
- List keys = new ArrayList();
-
- Iterator i = ((JS)o).keys().iterator(); while(i.hasNext()) {
- String k = i.next().toString();
- if (pat.matcher(k).matches()) keys.add(k);
- }
+ return p.equals("") ? root : root.get(p);
+ }
+
+ /** Returns the keys in js that match matcher. */
+ protected List matches(JS js) throws JSExn {
+ Pattern pat = Pattern.compile(matcher);
+ List keys = new ArrayList();
- return new Res(keys);
+ Iterator i = js.keys().iterator(); while(i.hasNext()) {
+ String k = i.next().toString();
+ if (pat.matcher(k).matches()) keys.add(k);
}
+
+ return keys;
+ }
+
+ /** Returns o cast as a JS if it is such and has keys,
+ * otherwise returns null. */
+ protected JS keyed(Object o) {
+ // FIXME: replace this with a canHaveKeys() function in org.ibex.js.JS
+ return o == null || !(o instanceof JS) ||
+ o instanceof JSDate || o instanceof Directory ||
+ o instanceof Grammar || o instanceof JSMath ||
+ o instanceof JSReflection || o instanceof JSRegexp ?
+ null : (JS)o;
+ }
+
+ public Response process(JSScope root) throws JSExn {
+ JS js = keyed(path(root));
+ return js == null ? new Res() : new Res(matches(js));
}
public static class Res extends Response {
@@ -67,14 +81,39 @@ public abstract class Request implements Serializable {
}
}
+ public static class RemoveKey extends Key {
+ public RemoveKey(String c) { super(c); }
+ public RemoveKey(String p, String m) { super(p, m); }
+ public Response process(JSScope root) throws JSExn {
+ JS js = keyed(path(root));
+ if (js == null) throw new JSExn("no such path");
+ boolean rm = js.containsKey(matcher);
+ if (rm) js.remove(matcher);
+ return new Res(rm);
+ }
+
+ public static class Res extends Response {
+ private boolean removed;
+ public Res() { }
+ public Res(boolean rm) { removed = rm; }
+ public boolean removed() { return removed; }
+ }
+ }
+
+ /** Runs a series of requests. */
public static class Composite extends Request {
+ private boolean breakOnError = false;
private Request[] requests;
public Composite() {}
- public Composite(Request[] r) { requests = r; }
- public Composite(List r) {
+ public Composite(Request[] r, boolean breakOnError) {
+ requests = r;
+ this.breakOnError = breakOnError;
+ }
+ public Composite(List r, boolean breakOnError) {
Request[] req = new Request[r.size()];
r.toArray(req);
requests = req;
+ this.breakOnError = breakOnError;
}
public Response process(JSScope root) {
@@ -82,7 +121,16 @@ public abstract class Request implements Serializable {
for (int i=0; i < requests.length; i++) {
try { res[i] = requests[i].process(root); }
- catch (JSExn e) { res[i] = new Response(e); }
+ catch (JSExn e) {
+ res[i] = new Response(e);
+
+ if (breakOnError) {
+ Response[] newres = new Response[i + 1];
+ System.arraycopy(res, 0, newres, 0, newres.length);
+ res = newres;
+ break;
+ }
+ }
}
return new Res(res);
@@ -94,6 +142,7 @@ public abstract class Request implements Serializable {
public Res(Response[] r) { responses = r; }
public Response get(int i) { return responses[i]; }
public int size() { return responses.length; }
+ public Response[] responses() { return responses; }
}
}
}