give xt.shell its own package
[org.ibex.xt-crawshaw.git] / src / java / org / ibex / xt / shell / Env.java
1 package org.ibex.xt.shell;
2
3 import java.io.IOException;
4
5 public abstract class Env {
6     /** Current JS path using '.' as a seperator. */
7     public String path = "";
8
9     public Command[] commands = new Command[0];
10
11     /** Returns the command matching the given name. */
12     public Command command(String name) {
13         for (int i=0; i < commands.length; i++)
14             if (commands[i].name().equals(name)) return commands[i];
15         return null;
16     }
17
18     /** Returns a path, based on console-style representation. */
19     public String path(String c) {
20         if (c.equals("") || c.equals(".") || c.equals("/")) {
21             c = ".";
22         } else if (c.equals("..")) {
23             c = c.substring(0, c.lastIndexOf('.'));
24             if (c.equals("")) c = ".";
25         } else {
26             if (c.charAt(0) != '/') c = path + "." + c;
27             c = c.replaceAll("/+", ".");
28             if (c.length() > 1 && c.charAt(c.length() - 1) == '.')
29                 c = c.substring(0, c.length() - 1);
30         }
31         return c;
32     }
33
34     public abstract Request.Response send(Request request) throws IOException;
35 }