eec18b791f71519cdb6909fcfbd09a7f280f4bb5
[org.ibex.xt-crawshaw.git] / src / java / org / ibex / xt / shell / Request.java
1 package org.ibex.xt.shell;
2
3 import java.io.*;
4 import java.util.*;
5 import java.util.regex.*;
6
7 import org.ibex.js.*;
8
9 public abstract class Request implements Serializable {
10
11     public abstract Response process(JSScope root) throws JSExn;
12
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; }
18     }
19
20     public static class Key extends Request {
21         private String path, matcher;
22         public Key() {}
23         public Key(String c) {
24             int pos = c.lastIndexOf('.');
25             path = c.substring(0, pos);
26             matcher = c.substring(pos + 1).replaceAll("\\*+", ".*");
27         }
28         public Key(String path, String matcher) {
29             this.path = path; this.matcher = matcher;
30         }
31
32         public Response process(JSScope root) throws JSExn {
33             String p = path == null ? "" : path.replaceAll("\\.+", "\\.");
34             if (p.length() > 0 && p.charAt(0) == '.')
35                 p = p.substring(1);
36             if (p.length() > 0 && p.charAt(p.length() - 1) == '.')
37                 p = p.substring(0, p.length() - 1);
38
39             System.out.println("searching path '"+p+"' for pattern '"+matcher+"'");
40
41             Object o = p.equals("") ? root : root.get(p);
42             if (o == null || o instanceof JSDate ||
43                              o instanceof JSArray ||
44                              !(o instanceof JS)) {
45                 System.out.println("hit bad object: "+o+", class="+
46                     (o == null ? null : o.getClass().getName()));
47                 return new Key.Res();
48             } else {
49                 Pattern pat = Pattern.compile(matcher);
50                 List keys = new ArrayList();
51
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);
55                 }
56
57                 return new Res(keys);
58             }
59         }
60
61         public static class Res extends Response {
62             private List keys;
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; }
67         }
68     }
69
70     public static class Composite extends Request {
71         private Request[] requests;
72         public Composite() {}
73         public Composite(Request[] r) { requests = r; }
74         public Composite(List r) {
75             Request[] req = new Request[r.size()];
76             r.toArray(req);
77             requests = req;
78         }
79
80         public Response process(JSScope root) {
81             Response[] res = new Response[requests.length];
82
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); }
86             }
87
88             return new Res(res);
89         }
90
91         public static class Res extends Response {
92             private Response[] responses;
93             public Res() {}
94             public Res(Response[] r) { responses = r; }
95             public Response get(int i) { return responses[i]; }
96             public int size() { return responses.length; }
97         }
98     }
99 }