--- /dev/null
+// CommandInterpreter.java\r
+// $Id: CommandInterpreter.java,v 1.1 2010/06/15 12:26:29 smhuang Exp $\r
+// (c) COPYRIGHT MIT and INRIA, 2002.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.tools.offline.command;\r
+\r
+import org.w3c.tools.offline.browse.StoreFinder ;\r
+\r
+import java.io.BufferedReader;\r
+import java.io.FileReader;\r
+import java.io.InputStreamReader;\r
+import java.io.IOException;\r
+\r
+import java.util.Vector;\r
+\r
+\r
+/**\r
+ * <p>The jigshell interpreter class.\r
+ */\r
+public class CommandInterpreter {\r
+\r
+ /**\r
+ * The StoreFinder instance to handle the store files.\r
+ */\r
+ protected static StoreFinder sf = null ;\r
+\r
+ /**\r
+ * A boolean to switch to batch mode when a script file is given\r
+ */\r
+ protected static boolean interactive = true ;\r
+\r
+ /**\r
+ * A reader for commands input. \r
+ */\r
+ protected static BufferedReader reader = null ;\r
+\r
+ \r
+ /**\r
+ * A CommandInterpreter initializer.\r
+ * @param args arguments provided when launching jigshell\r
+ */\r
+ public CommandInterpreter(String [] args) {\r
+ boolean interactive = true ;\r
+ if(args.length < 2 ) {\r
+ String[] arg = {"config/stores","http-server"};\r
+ System.out.println("assuming arguments are `config/stores http-server`");\r
+ sf = new org.w3c.tools.offline.browse.StoreFinder(arg[0],arg[1]);\r
+ } else {\r
+ if (args.length > 3) {\r
+ System.out.println("usage :");\r
+ System.out.print("java org.w3c.tools.offline.command.Main ");\r
+ System.out.print("stores_directory server [batch_file]\n");\r
+ System.exit(1) ;\r
+ }\r
+ if (args.length == 3){\r
+ System.out.println("entering batch mode...");\r
+ interactive=false;\r
+ } \r
+ sf = new org.w3c.tools.offline.browse.StoreFinder(args[0],args[1]);\r
+ }\r
+ try {\r
+ sf.readStoreIndex() ;\r
+ }\r
+ catch (IOException e){\r
+ System.out.println("Can't read store. Exiting.");\r
+ System.exit(2);\r
+ }\r
+ try {\r
+ sf.readStoreRoot() ;\r
+ }\r
+ catch (Exception e){\r
+ System.out.println("can't find root file");\r
+ System.exit(3);\r
+ }\r
+ if (interactive) runInteractive();\r
+ else runScriptFile(args[2]);\r
+ }\r
+\r
+ /**\r
+ * Run a script file (batch mode)\r
+ * @param batchFile the script file name.\r
+ */\r
+ private void runScriptFile(String batchFile){\r
+ interactive = false ;\r
+ try {\r
+ reader = new BufferedReader(new FileReader(batchFile)); \r
+ } \r
+ catch (java.io.FileNotFoundException e) { \r
+ System.out.println("batch not found: " + batchFile); \r
+ System.exit(4);\r
+ }\r
+ run() ;\r
+ }\r
+\r
+ /**\r
+ * Run the interactive mode.\r
+ */\r
+ private void runInteractive(){\r
+ interactive = true ;\r
+ reader = new BufferedReader(new InputStreamReader(System.in), 1);\r
+ run() ;\r
+ }\r
+\r
+\r
+ /**\r
+ * Run the interpreter\r
+ */\r
+ private void run(){\r
+ \r
+ while (true) try {\r
+\r
+ if ( interactive ) { \r
+ System.out.print(prompt()); \r
+ System.out.flush(); \r
+ }\r
+\r
+ String theLine = reader.readLine(); \r
+ if (theLine == null) break; \r
+ if (theLine.equals("exit")) break; \r
+ CommandLine cl = new CommandLine (theLine) ;\r
+ this.ProcessCommand(cl);\r
+ } \r
+ catch (IOException e) { \r
+ System.err.println(e); \r
+ }\r
+ }\r
+\r
+ \r
+ private String prompt (){\r
+ return ("jigshell-alpha> ");\r
+ }\r
+\r
+\r
+ /**\r
+ * Process a command line instance and initiate the action\r
+ * through the StoreFinder.\r
+ * @param cl the CommandLine.\r
+ */\r
+ protected void ProcessCommand(CommandLine cl){\r
+ try {\r
+ cl.parse() ;\r
+ String action = cl.getAction();\r
+ boolean recursive = false ;\r
+ String opt = cl.getOption();\r
+ if (opt.indexOf(cl.REC) != -1){\r
+ recursive = true ;\r
+ // System.out.println ("recursive action"+recursive);\r
+ }\r
+ if (opt.indexOf(cl.ATTR) != -1 && action.compareTo(cl.LIST)==0){\r
+ action = "listatt" ;\r
+ // System.out.println ("list with attributes");\r
+ }\r
+ if (action != null){\r
+ if (action.compareTo(cl.WHERE)==0) {\r
+ sf.printWorkingRep();\r
+ } else if (action.compareTo(cl.GO)==0) {\r
+ sf.setWorkingRep(cl.getTarget()); \r
+ } else {\r
+ sf.finderAction(action,cl.getTarget(), recursive); \r
+ }\r
+ }\r
+ }\r
+ catch (CommandParseException e) {\r
+ System.out.println("syntax error"); \r
+ }\r
+ }\r
+ \r
+}\r