Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / jigsaw / src / org / w3c / jigsaw / ssi / commands / BasicCommandRegistry.java
diff --git a/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigsaw/ssi/commands/BasicCommandRegistry.java b/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigsaw/ssi/commands/BasicCommandRegistry.java
new file mode 100644 (file)
index 0000000..a7aa73f
--- /dev/null
@@ -0,0 +1,209 @@
+// BasicCommandRegistry.java\r
+// $Id: BasicCommandRegistry.java,v 1.1 2010/06/15 12:21:53 smhuang Exp $\r
+// (c) COPYRIGHT MIT and INRIA, 1996.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.jigsaw.ssi.commands ;\r
+\r
+import java.util.Dictionary;\r
+import java.util.Hashtable;\r
+\r
+import java.io.PrintStream;\r
+\r
+import org.w3c.www.http.HTTP ;\r
+\r
+import org.w3c.util.ArrayDictionary;\r
+\r
+import org.w3c.jigsaw.http.Reply;\r
+import org.w3c.jigsaw.http.Request;\r
+\r
+import org.w3c.jigsaw.ssi.SSIFrame;\r
+\r
+/**\r
+ * An implementation of CommandRegistry that uses a hash table\r
+ * to store the commands.\r
+ * @author Antonio Ramirez <anto@mit.edu>\r
+ */\r
+public class BasicCommandRegistry extends CommandRegistry\r
+{\r
+\r
+    Hashtable /*<String,Command>*/ commands = null ;\r
+\r
+    public BasicCommandRegistry()\r
+    {\r
+       commands = new Hashtable(23) ;\r
+    }\r
+\r
+    public void registerCommand(Command cmd)\r
+    {\r
+       commands.put( cmd.getName().toLowerCase(),\r
+                     SSIFrame.debug\r
+                     ? getDebugWrapperCommand (cmd)\r
+                     : cmd ) ;\r
+    }\r
+\r
+    protected Command getDebugWrapperCommand(Command cmd) {\r
+       if (cmd instanceof ControlCommand)\r
+           return new DebugWrapperControlCommand(cmd);\r
+       else\r
+           return new DebugWrapperCommand(cmd);\r
+    }\r
+\r
+    public Command lookupCommand(String name)\r
+    {\r
+       Command cmd = (Command)\r
+           commands.get(name.toLowerCase()) ;\r
+       if(cmd != null) return cmd ;\r
+       else return new DefaultCommand(name) ;\r
+    }\r
+\r
+    public Dictionary initVariables(SSIFrame ssiframe,\r
+                                   Request request,\r
+                                   Dictionary variables)\r
+    {\r
+       return variables ;\r
+    }\r
+}\r
+\r
+class DefaultCommand implements Command {\r
+    private String badCommand ;\r
+\r
+    /**\r
+     * return true if reply can be cached.\r
+     * @return a boolean.\r
+     */\r
+    public boolean acceptCaching() {\r
+       return true;\r
+    }\r
+\r
+    DefaultCommand(String badCommand)\r
+    {\r
+       this.badCommand = badCommand ;\r
+    }\r
+\r
+    public String getName()\r
+    {\r
+       return null ;\r
+    }\r
+\r
+    public Reply execute(SSIFrame ssiframe,\r
+                        Request request,\r
+                        ArrayDictionary parameters,\r
+                        Dictionary variables)\r
+    {\r
+       Reply reply =\r
+           ssiframe.createCommandReply(request,HTTP.OK) ;\r
+\r
+       reply.setContent("[SSIFrame: unknown command \""+badCommand+"\"]");\r
+       \r
+       return reply ;\r
+    }\r
+\r
+    public boolean modifiedSince(long date,\r
+                                SSIFrame ssiframe,\r
+                                Request request,\r
+                                ArrayDictionary parameters,\r
+                                Dictionary variables)\r
+    {\r
+       return false;\r
+    }\r
+\r
+  public String getValue(Dictionary variables, String variable, \r
+                        Request request) {\r
+    return "null";\r
+  }\r
+\r
+}\r
+\r
+class DebugWrapperCommand implements Command {\r
+\r
+    Command cmd ;\r
+\r
+    /**\r
+     * return true if reply can be cached.\r
+     * @return a boolean.\r
+     */\r
+    public boolean acceptCaching() {\r
+       System.out.println("@@@@ command accept caching : "+\r
+                          cmd.acceptCaching());\r
+       return cmd.acceptCaching();\r
+    }\r
+\r
+    public DebugWrapperCommand(Command cmd)\r
+    {\r
+       this.cmd = cmd ;\r
+       System.out.println("@@@@ Added command: "+cmd.getName()) ;\r
+    }\r
+\r
+    public final Reply execute(SSIFrame ssiframe,\r
+                              Request request,\r
+                              ArrayDictionary parameters,\r
+                              Dictionary variables)\r
+    {\r
+       System.out.println("@@@@ Executing command: "+cmd.getName()+\r
+                          " "+parameters) ;\r
+       return cmd.execute(ssiframe,request,parameters,variables) ;\r
+    }\r
+                        \r
+    public final String getName()\r
+    {\r
+       return cmd.getName() ;\r
+    }\r
+\r
+    public String getValue(Dictionary variables, \r
+                          String variable, \r
+                          Request request) \r
+    {\r
+       String value = cmd.getValue(variables, variable, request) ;\r
+       System.out.println("@@@@ Get Value "+cmd.getName()+" : "+value);\r
+       return value;\r
+    }\r
+\r
+}\r
+\r
+class DebugWrapperControlCommand extends DebugWrapperCommand \r
+                                 implements ControlCommand \r
+{\r
+    /**\r
+     * register the command position in the structure\r
+     * witch store the SSIFrame.\r
+     */\r
+    public void setPosition(SSIFrame ssiframe,\r
+                           Request request,\r
+                           CommandRegistry registry,\r
+                           ArrayDictionary parameters,\r
+                           Dictionary variables,\r
+                           int position)\r
+    {\r
+       System.out.println("@@@@ "+cmd.getName()+\r
+                          " SetPosition ["+position+"]");\r
+       ((ControlCommand)cmd).setPosition(ssiframe, request, registry, \r
+                                         parameters, variables, position);\r
+    }\r
+\r
+    /**\r
+     * Give the next position in the structure witch\r
+     * store the SSIFrame.\r
+     * @return An integer\r
+     * @exception ControlCommandException if action failed.\r
+     */\r
+    public int jumpTo(SSIFrame ssiframe,\r
+                     Request request,\r
+                     CommandRegistry registry,\r
+                     ArrayDictionary parameters,\r
+                     Dictionary variables)\r
+       throws ControlCommandException\r
+    {\r
+       int pos = ((ControlCommand)cmd).jumpTo(ssiframe, request, \r
+                                              registry, parameters, \r
+                                              variables);\r
+       System.out.println("@@@@ "+cmd.getName()+" Jump to "+pos);\r
+       return pos;\r
+    }\r
+\r
+    public DebugWrapperControlCommand(Command cmd) {\r
+       super(cmd);\r
+    }\r
+}\r
+\r
+\r