1 package org.ibex.xt.shell;
5 import java.util.regex.*;
9 public abstract class Request implements Serializable {
11 public abstract Response process(JSScope root) throws JSExn;
13 public static class Response implements Serializable {
14 protected Exception ex;
15 public Response() { ex = null; }
16 public Response(Exception e) { ex = e; }
17 public Exception error() { return ex; }
20 public static class Key extends Request {
21 protected String path, matcher;
24 /** Expects a shell-path that uses '.' as a seperator and * for wildcard. */
25 public Key(String c) {
26 int pos = c.lastIndexOf('.');
27 path = c.substring(0, pos);
28 matcher = c.substring(pos + 1).replaceAll("\\*+", ".*");
31 public Key(String path, String matcher) {
32 this.path = path; this.matcher = matcher;
35 /** Returns the object referenced by path. */
36 protected Object path(JSScope root) throws JSExn {
37 String p = path == null ? "" : path.replaceAll("\\.+", "\\.");
38 if (p.length() > 0 && p.charAt(0) == '.')
40 if (p.length() > 0 && p.charAt(p.length() - 1) == '.')
41 p = p.substring(0, p.length() - 1);
43 return p.equals("") ? root : root.get(p);
46 /** Returns the keys in <tt>js</tt> that match <tt>matcher</tt>. */
47 protected List matches(JS js) throws JSExn {
48 Pattern pat = Pattern.compile(matcher);
49 List keys = new ArrayList();
51 Iterator i = js.keys().iterator(); while(i.hasNext()) {
52 String k = i.next().toString();
53 if (pat.matcher(k).matches()) keys.add(k);
59 /** Returns <tt>o</tt> cast as a JS if it is such and has keys,
60 * otherwise returns null. */
61 protected JS keyed(Object o) {
62 // FIXME: replace this with a canHaveKeys() function in org.ibex.js.JS
63 return o == null || !(o instanceof JS) ||
64 o instanceof JSDate || o instanceof Directory ||
65 o instanceof Grammar || o instanceof JSMath ||
66 o instanceof JSReflection || o instanceof JSRegexp ?
70 public Response process(JSScope root) throws JSExn {
71 JS js = keyed(path(root));
72 return js == null ? new Res() : new Res(matches(js));
75 public static class Res extends Response {
77 public Res() { keys = null; }
78 public Res(List l) { keys = l; }
79 public List keys() { return keys; }
80 public boolean isPath() { return keys != null; }
84 public static class RemoveKey extends Key {
85 public RemoveKey(String c) { super(c); }
86 public RemoveKey(String p, String m) { super(p, m); }
87 public Response process(JSScope root) throws JSExn {
88 JS js = keyed(path(root));
89 if (js == null) throw new JSExn("no such path");
90 boolean rm = js.containsKey(matcher);
91 if (rm) js.remove(matcher);
95 public static class Res extends Response {
96 private boolean removed;
98 public Res(boolean rm) { removed = rm; }
99 public boolean removed() { return removed; }
103 public static class SetKey extends Key {
106 public SetKey(String p, String m, JS val) { super(p, m); this.value = val; }
108 public Response process(JSScope root) throws JSExn {
109 JS js = keyed(path(root));
110 if (js == null) throw new JSExn("no such path");
112 js.put(matcher, JS.eval(JS.cloneWithNewParentScope(
113 value, new JSResolve(root, js))));
114 return new Res(); // TODO: return put() value when it has one
117 public class Res extends Response {
120 public class JSResolve extends JSScope {
122 public JSResolve(JSScope parent, JS path) { super(parent); this.path = path; }
123 public Object get(Object key) throws JSExn {
124 if (key != null && key instanceof String) {
125 String k = (String)key;
126 if (k.startsWith(".")) k = k.substring(1);
127 else return path.get(k);
129 return super.get(key);
131 public void put(Object key, Object val) throws JSExn {
132 if (key != null && key instanceof String) {
133 String k = (String)key;
134 if (k.startsWith(".")) k = k.substring(1);
135 else { path.put(key, val); return; }
142 /** Runs a series of requests. */
143 public static class Composite extends Request {
144 private boolean breakOnError = false;
145 private Request[] requests;
146 public Composite() {}
147 public Composite(Request[] r, boolean breakOnError) {
149 this.breakOnError = breakOnError;
151 public Composite(List r, boolean breakOnError) {
152 Request[] req = new Request[r.size()];
155 this.breakOnError = breakOnError;
158 public Response process(JSScope root) {
159 Response[] res = new Response[requests.length];
161 for (int i=0; i < requests.length; i++) {
162 try { res[i] = requests[i].process(root); }
164 res[i] = new Response(e);
167 Response[] newres = new Response[i + 1];
168 System.arraycopy(res, 0, newres, 0, newres.length);
178 public static class Res extends Response {
179 private Response[] responses;
181 public Res(Response[] r) { responses = r; }
182 public Response get(int i) { return responses[i]; }
183 public int size() { return responses.length; }
184 public Response[] responses() { return responses; }