001: package org.objectweb.celtix.tools.common.toolspec;
002:
003: import java.io.IOException;
004: import java.io.OutputStream;
005: import java.io.PrintStream;
006: import java.util.logging.Level;
007: import java.util.logging.Logger;
008:
009: import org.objectweb.celtix.common.i18n.Message;
010: import org.objectweb.celtix.common.logging.LogUtils;
011: import org.objectweb.celtix.tools.common.ToolException;
012: import org.objectweb.celtix.tools.common.toolspec.parser.BadUsageException;
013: import org.objectweb.celtix.tools.common.toolspec.parser.CommandDocument;
014: import org.objectweb.celtix.tools.common.toolspec.parser.CommandLineParser;
015:
016: public abstract class AbstractToolContainer implements ToolContainer {
017: private static final Logger LOG = LogUtils
018: .getL7dLogger(AbstractToolContainer.class);
019: private static boolean isVerbose;
020: private static String arguments[];
021:
022: protected ToolSpec toolspec;
023:
024: private boolean isQuiet;
025: private CommandDocument commandDoc;
026: private CommandLineParser parser;
027: private OutputStream outOutputStream;
028: private OutputStream errOutputStream;
029:
030: public class GenericOutputStream extends OutputStream {
031: public void write(int b) throws IOException {
032:
033: }
034: }
035:
036: public AbstractToolContainer(ToolSpec ts) throws BadUsageException {
037: toolspec = ts;
038: }
039:
040: public void setCommandLine(String[] args) throws BadUsageException {
041: arguments = new String[args.length];
042: System.arraycopy(args, 0, arguments, 0, args.length);
043: setMode(args);
044: if (isQuietMode()) {
045: redirectOutput();
046: }
047: if (toolspec != null) {
048: parser = new CommandLineParser(toolspec);
049: commandDoc = parser.parseArguments(args);
050: }
051: }
052:
053: public void setMode(String[] args) {
054: for (int i = 0; i < args.length; i++) {
055: if ("-q".equals(args[i])) {
056: isQuiet = true;
057: }
058: if ("-quiet".equals(args[i])) {
059: isQuiet = true;
060: }
061: if ("-V".equals(args[i])) {
062: isVerbose = true;
063: }
064: if ("-verbose".equals(args[i])) {
065: isVerbose = true;
066: }
067: }
068: }
069:
070: public void init() throws ToolException {
071: // initialize
072: if (toolspec == null) {
073: Message message = new Message("TOOLSPEC_NOT_INITIALIZED",
074: LOG);
075: LOG.log(Level.SEVERE, message.toString());
076: throw new ToolException(message);
077: }
078: }
079:
080: public CommandDocument getCommandDocument() {
081: return commandDoc;
082: }
083:
084: public CommandLineParser getCommandLineParser() {
085: return parser;
086: }
087:
088: public void redirectOutput() {
089: outOutputStream = new GenericOutputStream();
090: errOutputStream = new GenericOutputStream();
091: System.setErr(new PrintStream(errOutputStream));
092: System.setOut(new PrintStream(outOutputStream));
093: }
094:
095: public boolean isQuietMode() {
096: return isQuiet;
097: }
098:
099: public static boolean isVerboseMode() {
100: return isVerbose;
101: }
102:
103: public static String[] getArgument() {
104: return arguments;
105: }
106:
107: public OutputStream getOutOutputStream() {
108: return outOutputStream;
109: }
110:
111: public OutputStream getErrOutputStream() {
112: return errOutputStream;
113: }
114:
115: public abstract void execute(boolean exitOnFinish)
116: throws ToolException;
117:
118: }
|