001: // CommandInterpreter.java
002: // $Id: CommandInterpreter.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 org.w3c.tools.offline.browse.StoreFinder;
009:
010: import java.io.BufferedReader;
011: import java.io.FileReader;
012: import java.io.InputStreamReader;
013: import java.io.IOException;
014:
015: import java.util.Vector;
016:
017: /**
018: * <p>The jigshell interpreter class.
019: */
020: public class CommandInterpreter {
021:
022: /**
023: * The StoreFinder instance to handle the store files.
024: */
025: protected static StoreFinder sf = null;
026:
027: /**
028: * A boolean to switch to batch mode when a script file is given
029: */
030: protected static boolean interactive = true;
031:
032: /**
033: * A reader for commands input.
034: */
035: protected static BufferedReader reader = null;
036:
037: /**
038: * A CommandInterpreter initializer.
039: * @param args arguments provided when launching jigshell
040: */
041: public CommandInterpreter(String[] args) {
042: boolean interactive = true;
043: if (args.length < 2) {
044: String[] arg = { "config/stores", "http-server" };
045: System.out
046: .println("assuming arguments are `config/stores http-server`");
047: sf = new org.w3c.tools.offline.browse.StoreFinder(arg[0],
048: arg[1]);
049: } else {
050: if (args.length > 3) {
051: System.out.println("usage :");
052: System.out
053: .print("java org.w3c.tools.offline.command.Main ");
054: System.out
055: .print("stores_directory server [batch_file]\n");
056: System.exit(1);
057: }
058: if (args.length == 3) {
059: System.out.println("entering batch mode...");
060: interactive = false;
061: }
062: sf = new org.w3c.tools.offline.browse.StoreFinder(args[0],
063: args[1]);
064: }
065: try {
066: sf.readStoreIndex();
067: } catch (IOException e) {
068: System.out.println("Can't read store. Exiting.");
069: System.exit(2);
070: }
071: try {
072: sf.readStoreRoot();
073: } catch (Exception e) {
074: System.out.println("can't find root file");
075: System.exit(3);
076: }
077: if (interactive)
078: runInteractive();
079: else
080: runScriptFile(args[2]);
081: }
082:
083: /**
084: * Run a script file (batch mode)
085: * @param batchFile the script file name.
086: */
087: private void runScriptFile(String batchFile) {
088: interactive = false;
089: try {
090: reader = new BufferedReader(new FileReader(batchFile));
091: } catch (java.io.FileNotFoundException e) {
092: System.out.println("batch not found: " + batchFile);
093: System.exit(4);
094: }
095: run();
096: }
097:
098: /**
099: * Run the interactive mode.
100: */
101: private void runInteractive() {
102: interactive = true;
103: reader = new BufferedReader(new InputStreamReader(System.in), 1);
104: run();
105: }
106:
107: /**
108: * Run the interpreter
109: */
110: private void run() {
111:
112: while (true)
113: try {
114:
115: if (interactive) {
116: System.out.print(prompt());
117: System.out.flush();
118: }
119:
120: String theLine = reader.readLine();
121: if (theLine == null)
122: break;
123: if (theLine.equals("exit"))
124: break;
125: CommandLine cl = new CommandLine(theLine);
126: this .ProcessCommand(cl);
127: } catch (IOException e) {
128: System.err.println(e);
129: }
130: }
131:
132: private String prompt() {
133: return ("jigshell-alpha> ");
134: }
135:
136: /**
137: * Process a command line instance and initiate the action
138: * through the StoreFinder.
139: * @param cl the CommandLine.
140: */
141: protected void ProcessCommand(CommandLine cl) {
142: try {
143: cl.parse();
144: String action = cl.getAction();
145: boolean recursive = false;
146: String opt = cl.getOption();
147: if (opt.indexOf(cl.REC) != -1) {
148: recursive = true;
149: // System.out.println ("recursive action"+recursive);
150: }
151: if (opt.indexOf(cl.ATTR) != -1
152: && action.compareTo(cl.LIST) == 0) {
153: action = "listatt";
154: // System.out.println ("list with attributes");
155: }
156: if (action != null) {
157: if (action.compareTo(cl.WHERE) == 0) {
158: sf.printWorkingRep();
159: } else if (action.compareTo(cl.GO) == 0) {
160: sf.setWorkingRep(cl.getTarget());
161: } else {
162: sf.finderAction(action, cl.getTarget(), recursive);
163: }
164: }
165: } catch (CommandParseException e) {
166: System.out.println("syntax error");
167: }
168: }
169:
170: }
|