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 private String path, matcher;
23 public Key(String c) {
24 int pos = c.lastIndexOf('.');
25 path = c.substring(0, pos);
26 matcher = c.substring(pos + 1).replaceAll("\\*+", ".*");
28 public Key(String path, String matcher) {
29 this.path = path; this.matcher = matcher;
32 public Response process(JSScope root) throws JSExn {
33 String p = path == null ? "" : path.replaceAll("\\.+", "\\.");
34 if (p.length() > 0 && p.charAt(0) == '.')
36 if (p.length() > 0 && p.charAt(p.length() - 1) == '.')
37 p = p.substring(0, p.length() - 1);
39 System.out.println("searching path '"+p+"' for pattern '"+matcher+"'");
41 Object o = p.equals("") ? root : root.get(p);
42 if (o == null || o instanceof JSDate ||
43 o instanceof JSArray ||
45 System.out.println("hit bad object: "+o+", class="+
46 (o == null ? null : o.getClass().getName()));
49 Pattern pat = Pattern.compile(matcher);
50 List keys = new ArrayList();
52 Iterator i = ((JS)o).keys().iterator(); while(i.hasNext()) {
53 String k = i.next().toString();
54 if (pat.matcher(k).matches()) keys.add(k);
61 public static class Res extends Response {
63 public Res() { keys = null; }
64 public Res(List l) { keys = l; }
65 public List keys() { return keys; }
66 public boolean isPath() { return keys != null; }
70 public static class Composite extends Request {
71 private Request[] requests;
73 public Composite(Request[] r) { requests = r; }
74 public Composite(List r) {
75 Request[] req = new Request[r.size()];
80 public Response process(JSScope root) {
81 Response[] res = new Response[requests.length];
83 for (int i=0; i < requests.length; i++) {
84 try { res[i] = requests[i].process(root); }
85 catch (JSExn e) { res[i] = new Response(e); }
91 public static class Res extends Response {
92 private Response[] responses;
94 public Res(Response[] r) { responses = r; }
95 public Response get(int i) { return responses[i]; }
96 public int size() { return responses.length; }