1 package org.objectweb.celtix.tools.common.toolspec; 2 3 import java.io.IOException ; 4 import java.io.OutputStream ; 5 import java.io.PrintStream ; 6 import java.util.logging.Level ; 7 import java.util.logging.Logger ; 8 9 import org.objectweb.celtix.common.i18n.Message; 10 import org.objectweb.celtix.common.logging.LogUtils; 11 import org.objectweb.celtix.tools.common.ToolException; 12 import org.objectweb.celtix.tools.common.toolspec.parser.BadUsageException; 13 import org.objectweb.celtix.tools.common.toolspec.parser.CommandDocument; 14 import org.objectweb.celtix.tools.common.toolspec.parser.CommandLineParser; 15 public abstract class AbstractToolContainer implements ToolContainer { 16 private static final Logger LOG = LogUtils.getL7dLogger(AbstractToolContainer.class); 17 private static boolean isVerbose; 18 private static String arguments[]; 19 20 protected ToolSpec toolspec; 21 22 private boolean isQuiet; 23 private CommandDocument commandDoc; 24 private CommandLineParser parser; 25 private OutputStream outOutputStream; 26 private OutputStream errOutputStream; 27 28 public class GenericOutputStream extends OutputStream { 29 public void write(int b) throws IOException { 30 31 } 32 } 33 34 public AbstractToolContainer(ToolSpec ts) throws BadUsageException { 35 toolspec = ts; 36 } 37 38 public void setCommandLine(String [] args) throws BadUsageException { 39 arguments = new String [args.length]; 40 System.arraycopy(args, 0, arguments, 0, args.length); 41 setMode(args); 42 if (isQuietMode()) { 43 redirectOutput(); 44 } 45 if (toolspec != null) { 46 parser = new CommandLineParser(toolspec); 47 commandDoc = parser.parseArguments(args); 48 } 49 } 50 51 public void setMode(String [] args) { 52 for (int i = 0; i < args.length; i++) { 53 if ("-q".equals(args[i])) { 54 isQuiet = true; 55 } 56 if ("-quiet".equals(args[i])) { 57 isQuiet = true; 58 } 59 if ("-V".equals(args[i])) { 60 isVerbose = true; 61 } 62 if ("-verbose".equals(args[i])) { 63 isVerbose = true; 64 } 65 } 66 } 67 68 public void init() throws ToolException { 69 if (toolspec == null) { 71 Message message = new Message("TOOLSPEC_NOT_INITIALIZED", LOG); 72 LOG.log(Level.SEVERE, message.toString()); 73 throw new ToolException(message); 74 } 75 } 76 77 public CommandDocument getCommandDocument() { 78 return commandDoc; 79 } 80 81 public CommandLineParser getCommandLineParser() { 82 return parser; 83 } 84 85 public void redirectOutput() { 86 outOutputStream = new GenericOutputStream(); 87 errOutputStream = new GenericOutputStream(); 88 System.setErr(new PrintStream (errOutputStream)); 89 System.setOut(new PrintStream (outOutputStream)); 90 } 91 92 public boolean isQuietMode() { 93 return isQuiet; 94 } 95 96 public static boolean isVerboseMode() { 97 return isVerbose; 98 } 99 100 public static String [] getArgument() { 101 return arguments; 102 } 103 104 public OutputStream getOutOutputStream() { 105 return outOutputStream; 106 } 107 108 public OutputStream getErrOutputStream() { 109 return errOutputStream; 110 } 111 112 public abstract void execute(boolean exitOnFinish) throws ToolException; 113 114 } 115 | Popular Tags |