give xt.shell its own package
[org.ibex.xt-crawshaw.git] / src / java / org / ibex / xt / shell / Env.java
diff --git a/src/java/org/ibex/xt/shell/Env.java b/src/java/org/ibex/xt/shell/Env.java
new file mode 100644 (file)
index 0000000..6f37146
--- /dev/null
@@ -0,0 +1,35 @@
+package org.ibex.xt.shell;
+
+import java.io.IOException;
+
+public abstract class Env {
+    /** Current JS path using '.' as a seperator. */
+    public String path = "";
+
+    public Command[] commands = new Command[0];
+
+    /** Returns the command matching the given name. */
+    public Command command(String name) {
+        for (int i=0; i < commands.length; i++)
+            if (commands[i].name().equals(name)) return commands[i];
+        return null;
+    }
+
+    /** Returns a path, based on console-style representation. */
+    public String path(String c) {
+        if (c.equals("") || c.equals(".") || c.equals("/")) {
+            c = ".";
+        } else if (c.equals("..")) {
+            c = c.substring(0, c.lastIndexOf('.'));
+            if (c.equals("")) c = ".";
+        } else {
+            if (c.charAt(0) != '/') c = path + "." + c;
+            c = c.replaceAll("/+", ".");
+            if (c.length() > 1 && c.charAt(c.length() - 1) == '.')
+                c = c.substring(0, c.length() - 1);
+        }
+        return c;
+    }
+
+    public abstract Request.Response send(Request request) throws IOException;
+}