001: // CommandLine.java
002: // $Id: CommandLine.java,v 1.7 2002/02/04 17:28:12 cbournez Exp $
003: // (c) COPYRIGHT MIT and INRIA, 2002.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.tools.offline.command;
007:
008: import java.io.StringReader;
009: import java.io.StreamTokenizer;
010: import java.io.IOException;
011: import java.util.Vector;
012:
013: import org.apache.oro.text.regex.Perl5Matcher;
014: import org.apache.oro.text.regex.Perl5Compiler;
015: import org.apache.oro.text.regex.Pattern;
016: import org.apache.oro.text.regex.MalformedPatternException;
017:
018: /**
019: * <p>The jigshell command line class.
020: */
021: public class CommandLine {
022:
023: /** static members: jigshell commands syntax */
024: public static String WHERE = "where";
025: public static String LIST = "list";
026: public static String GO = "go";
027: public static String REC = "r";
028: public static String ATTR = "a";
029: public static String UP = "..";
030:
031: public static String NO_OPT = "none";
032:
033: /* private class members */
034: private String cmd = null;
035: private String action = null;
036: private String target = null;
037: private String option = NO_OPT;
038:
039: /**
040: * a Vector to handle elements of a parsed command
041: */
042: protected Vector parsedCmd;
043:
044: /* protected static members */
045: protected static Perl5Matcher pmatcher = new Perl5Matcher();
046: protected static Perl5Compiler pcompiler = new Perl5Compiler();
047: protected static Pattern srPattern;
048:
049: /**
050: * Initialize a CommandLine instance.
051: * @param s the command line.
052: */
053: public CommandLine(String s) {
054: try {
055: cmd = s;
056: parsedCmd = new Vector();
057: srPattern = pcompiler.compile(
058: "^s/[\\w|=|\\*|\\-|\\\\/]+?/[\\w|\\-|\\\\/]+/$",
059: Perl5Compiler.DEFAULT_MASK);
060: } catch (org.apache.oro.text.regex.MalformedPatternException ex) {
061: ex.printStackTrace();
062: }
063: }
064:
065: /**
066: * Parse a CommandLine instance.
067: */
068: public void parse() throws CommandParseException {
069:
070: StringReader r = new StringReader(cmd);
071: StreamTokenizer st = new StreamTokenizer(r);
072:
073: st.ordinaryChar('.');
074: //equivalent to (ascii codes): st.wordChars(33,44)
075: st.wordChars('!', ',');
076: //equivalent to: st.wordChars(46,47)
077: st.wordChars('.', '/');
078: st.wordChars('=', '=');
079: //equivalent to: st.wordChars(63,64)
080: st.wordChars('?', '@');
081: //equivalent to: st.wordChars(91,96)
082: st.wordChars('[', '`');
083:
084: try {
085: while (st.nextToken() != st.TT_EOF) {
086: if (st.ttype == st.TT_WORD) {
087: parsedCmd.addElement(new String(st.sval));
088: }
089: if (st.ttype == '-') {
090: parsedCmd.addElement(new String("-"));
091: }
092: if (st.ttype == st.TT_NUMBER) {
093: }
094: }
095: } catch (IOException e) {
096: throw new CommandParseException();
097: }
098:
099: switch (parsedCmd.size()) {
100: case 0:
101: break;
102: case 1: // simple command
103: action = (String) parsedCmd.elementAt(0);
104: if (action.compareTo(LIST) == 0
105: || action.compareTo(WHERE) == 0) {
106: target = ".*";
107: } else {
108: throw new CommandParseException();
109: }
110: break;
111:
112: default: // more than 1 element in the command line
113: action = (String) parsedCmd.elementAt(0);
114: if (isaReplaceAction(action) == true
115: || action.compareTo(LIST) == 0
116: || action.compareTo(GO) == 0) {
117:
118: boolean isOption = false;
119: for (int i = 1; i < parsedCmd.size(); i++) {
120: String curWord = (String) parsedCmd.elementAt(i);
121: // System.out.println(curWord+" "+option);
122: if (isOption) {
123: /* we already met an option modifier, we're
124: waiting for an option */
125: isOption = false;
126: if (curWord.compareTo(REC) == 0
127: || curWord.compareTo(ATTR) == 0
128: || curWord.compareTo(REC + ATTR) == 0
129: || curWord.compareTo(ATTR + REC) == 0) {
130: option = curWord;
131: } else {
132: // unknown option
133: System.out.println("option discarded "
134: + curWord);
135: }
136: } else {
137: /* beginning of an option (modifier) */
138: if (curWord.compareTo("-") == 0) {
139: isOption = true;
140: } else {
141: /* we're not waiting for an option so
142: it's the command target */
143: target = curWord;
144: break;
145: }
146: }
147: }
148: } else {
149: throw new CommandParseException();
150: }
151: if (target == null) {
152: throw new CommandParseException();
153: }
154: }
155: }
156:
157: /**
158: * Get the command line action
159: * @return the string action (should be a jigshell action).
160: */
161: public String getAction() {
162: return (action);
163: }
164:
165: /**
166: * Get the command target
167: * @return the string target (should be a name or regexp).
168: */
169: public String getTarget() {
170: return (target);
171: }
172:
173: /**
174: * Get the command option
175: * @return the command option ("none" if no option specified in
176: * the command line).
177: */
178: public String getOption() {
179: return (option);
180: }
181:
182: /* check whether a string is a s/truc/chose/ regexp. */
183: private boolean isaReplaceAction(String s) {
184:
185: if (pmatcher.matches(s, srPattern)) {
186: return true;
187: }
188: return false;
189: }
190: }
|