Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / jigsaw / src / org / w3c / tools / offline / command / CommandLine.java
diff --git a/JMCR-Stable/real-world application/jigsaw/src/org/w3c/tools/offline/command/CommandLine.java b/JMCR-Stable/real-world application/jigsaw/src/org/w3c/tools/offline/command/CommandLine.java
new file mode 100644 (file)
index 0000000..f90a3c0
--- /dev/null
@@ -0,0 +1,197 @@
+// CommandLine.java\r
+// $Id: CommandLine.java,v 1.1 2010/06/15 12:26:30 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 java.io.StringReader;\r
+import java.io.StreamTokenizer;\r
+import java.io.IOException;\r
+import java.util.Vector;\r
+\r
+\r
+import org.apache.oro.text.regex.Perl5Matcher;\r
+import org.apache.oro.text.regex.Perl5Compiler;\r
+import org.apache.oro.text.regex.Pattern;\r
+import org.apache.oro.text.regex.MalformedPatternException;\r
+\r
+/**\r
+ * <p>The jigshell command line class.\r
+ */\r
+public class CommandLine {\r
+\r
+       /** static members: jigshell commands syntax */\r
+       public static String WHERE = "where";\r
+       public static String LIST = "list";\r
+       public static String GO = "go" ;\r
+       public static String REC = "r" ;\r
+       public static String ATTR = "a" ;\r
+       public static String UP = ".." ;\r
+\r
+       public static String NO_OPT = "none" ;\r
+\r
+       /* private class members */\r
+       private String cmd = null ;\r
+       private String action = null ;\r
+       private String target = null ;\r
+       private String option = NO_OPT ;\r
+\r
+       /** \r
+        * a Vector to handle elements of a parsed command\r
+        */\r
+       protected Vector parsedCmd ;\r
+\r
+       /* protected static members */\r
+       protected static Perl5Matcher pmatcher = new Perl5Matcher() ;\r
+       protected static Perl5Compiler pcompiler = new Perl5Compiler() ;\r
+       protected static Pattern srPattern  ;\r
+\r
+\r
+   /** \r
+       *  Initialize a CommandLine instance.  \r
+       * @param s the command line.\r
+       */\r
+       public CommandLine(String s){\r
+               try {\r
+                       cmd = s ;\r
+                       parsedCmd = new Vector ();\r
+                       srPattern = pcompiler.compile(\r
+                       "^s/[\\w|=|\\*|\\-|\\\\/]+?/[\\w|\\-|\\\\/]+/$",\r
+                                               Perl5Compiler.DEFAULT_MASK);\r
+               }\r
+               catch (org.apache.oro.text.regex.MalformedPatternException ex){\r
+                       ex.printStackTrace();\r
+               }\r
+       }\r
+\r
+\r
+   /** \r
+       * Parse a CommandLine instance.  \r
+       */\r
+       public void parse() throws CommandParseException {\r
+\r
+               StringReader r = new StringReader(cmd) ;\r
+               StreamTokenizer st = new StreamTokenizer(r);\r
+\r
+               st.ordinaryChar('.');\r
+       //equivalent to (ascii codes):  st.wordChars(33,44)\r
+               st.wordChars('!',',');\r
+       //equivalent to:        st.wordChars(46,47)\r
+               st.wordChars('.','/');\r
+               st.wordChars('=','=');\r
+       //equivalent to:        st.wordChars(63,64)\r
+               st.wordChars('?','@');\r
+       //equivalent to:        st.wordChars(91,96)\r
+               st.wordChars('[','`');\r
+\r
+               try {\r
+                       while (st.nextToken() != st.TT_EOF){\r
+                               if (st.ttype == st.TT_WORD){\r
+                                       parsedCmd.addElement(new String(st.sval));\r
+                               }\r
+                               if (st.ttype == '-'){\r
+                                       parsedCmd.addElement(new String("-"));\r
+                               }\r
+                               if (st.ttype == st.TT_NUMBER){\r
+                               }\r
+                       }\r
+               }\r
+               catch (IOException e){\r
+                       throw new CommandParseException() ;\r
+               }\r
+\r
+               switch (parsedCmd.size()){\r
+                       case 0:\r
+                               break;\r
+                       case 1: // simple command\r
+                               action = (String)parsedCmd.elementAt(0);\r
+                               if (action.compareTo(LIST) ==0 ||\r
+                                       action.compareTo(WHERE) == 0){\r
+                                       target = ".*";  \r
+                               }\r
+                               else {\r
+                                       throw new CommandParseException() ;\r
+                               }\r
+                               break;\r
+\r
+                       default : // more than 1 element in the command line\r
+                               action = (String)parsedCmd.elementAt(0);\r
+                               if ( isaReplaceAction(action) == true ||\r
+                                       action.compareTo(LIST)==0 ||\r
+                                       action.compareTo(GO)==0){\r
+\r
+                                       boolean isOption = false ;\r
+                                       for (int i = 1 ; i < parsedCmd.size() ; i++){\r
+                                               String curWord = (String)parsedCmd.elementAt(i);\r
+                               //              System.out.println(curWord+"  "+option);\r
+                                               if (isOption){\r
+                                                       /* we already met an option modifier, we're \r
+                                                       waiting for an option */\r
+                                                       isOption = false ;\r
+                                                       if (curWord.compareTo(REC) == 0 \r
+                                                                               || curWord.compareTo(ATTR) == 0 \r
+                                                                               || curWord.compareTo(REC+ATTR) == 0\r
+                                                                               || curWord.compareTo(ATTR+REC) == 0 ){\r
+                                                               option = curWord ;\r
+                                                       } else {\r
+                                                               // unknown option\r
+                                                               System.out.println("option discarded "+curWord);\r
+                                                       }\r
+                                               } else {\r
+                                                               /* beginning of an option (modifier) */\r
+                                                       if (curWord.compareTo("-")==0){\r
+                                                               isOption = true ;\r
+                                                       } else { \r
+                                                               /* we're not waiting for an option so\r
+                                                               it's the command target */\r
+                                                               target = curWord ;\r
+                                                               break ;\r
+                                                       }\r
+                                               }\r
+                                       }\r
+                               }\r
+                               else {\r
+                                       throw new CommandParseException() ;\r
+                               }\r
+                               if (target == null){\r
+                                       throw new CommandParseException() ;\r
+                               }\r
+               }\r
+       }\r
+\r
+       /**\r
+        * Get the command line action\r
+        * @return the string action (should be a jigshell action).\r
+        */\r
+       public String getAction(){ \r
+               return (action) ;\r
+       }\r
+\r
+       /**\r
+        * Get the command target \r
+        * @return the string target (should be a name or regexp).\r
+        */\r
+       public String getTarget(){\r
+               return (target) ;\r
+       }\r
+\r
+       /**\r
+        * Get the command option \r
+        * @return the command option ("none" if no option specified in\r
+        * the command line).\r
+        */\r
+       public String getOption(){\r
+               return (option) ;\r
+       }\r
+\r
+       /* check whether a string is a s/truc/chose/ regexp. */\r
+       private boolean isaReplaceAction(String s){\r
+               \r
+               \r
+               if (pmatcher.matches(s, srPattern)){\r
+                       return true ;\r
+               }\r
+               return false ;\r
+       }\r
+}\r