📄 commandline.java
字号:
* @since Ant 1.6 */ public void addArgumentsToList(ListIterator list) { for (int i = 0; i < arguments.size(); i++) { Argument arg = (Argument) arguments.elementAt(i); String[] s = arg.getParts(); if (s != null) { for (int j = 0; j < s.length; j++) { list.add(s[j]); } } } } /** * Return the command line as a string. * @return the command line. */ public String toString() { return toString(getCommandline()); } /** * Put quotes around the given String if necessary. * * <p>If the argument doesn't include spaces or quotes, return it * as is. If it contains double quotes, use single quotes - else * surround the argument by double quotes.</p> * @param argument the argument to quote if necessary. * @return the quoted argument. * @exception BuildException if the argument contains both, single * and double quotes. */ public static String quoteArgument(String argument) { if (argument.indexOf("\"") > -1) { if (argument.indexOf("\'") > -1) { throw new BuildException("Can\'t handle single and double" + " quotes in same argument"); } else { return '\'' + argument + '\''; } } else if (argument.indexOf("\'") > -1 || argument.indexOf(" ") > -1 // WIN9x uses a bat file for executing commands || (IS_WIN_9X && argument.indexOf(';') != -1)) { return '\"' + argument + '\"'; } else { return argument; } } /** * Quote the parts of the given array in way that makes them * usable as command line arguments. * @param line the list of arguments to quote. * @return empty string for null or no command, else every argument split * by spaces and quoted by quoting rules. */ public static String toString(String[] line) { // empty path return empty string if (line == null || line.length == 0) { return ""; } // path containing one or more elements final StringBuffer result = new StringBuffer(); for (int i = 0; i < line.length; i++) { if (i > 0) { result.append(' '); } result.append(quoteArgument(line[i])); } return result.toString(); } /** * Crack a command line. * @param toProcess the command line to process. * @return the command line broken into strings. * An empty or null toProcess parameter results in a zero sized array. */ public static String[] translateCommandline(String toProcess) { if (toProcess == null || toProcess.length() == 0) { //no command? no string return new String[0]; } // parse with a simple finite state machine final int normal = 0; final int inQuote = 1; final int inDoubleQuote = 2; int state = normal; StringTokenizer tok = new StringTokenizer(toProcess, "\"\' ", true); Vector v = new Vector(); StringBuffer current = new StringBuffer(); boolean lastTokenHasBeenQuoted = false; while (tok.hasMoreTokens()) { String nextTok = tok.nextToken(); switch (state) { case inQuote: if ("\'".equals(nextTok)) { lastTokenHasBeenQuoted = true; state = normal; } else { current.append(nextTok); } break; case inDoubleQuote: if ("\"".equals(nextTok)) { lastTokenHasBeenQuoted = true; state = normal; } else { current.append(nextTok); } break; default: if ("\'".equals(nextTok)) { state = inQuote; } else if ("\"".equals(nextTok)) { state = inDoubleQuote; } else if (" ".equals(nextTok)) { if (lastTokenHasBeenQuoted || current.length() != 0) { v.addElement(current.toString()); current = new StringBuffer(); } } else { current.append(nextTok); } lastTokenHasBeenQuoted = false; break; } } if (lastTokenHasBeenQuoted || current.length() != 0) { v.addElement(current.toString()); } if (state == inQuote || state == inDoubleQuote) { throw new BuildException("unbalanced quotes in " + toProcess); } String[] args = new String[v.size()]; v.copyInto(args); return args; } /** * Size operator. This actually creates the command line, so it is not * a zero cost operation. * @return number of elements in the command, including the executable. */ public int size() { return getCommandline().length; } /** * Generate a deep clone of the contained object. * @return a clone of the contained object */ public Object clone() { try { Commandline c = (Commandline) super.clone(); c.arguments = (Vector) arguments.clone(); return c; } catch (CloneNotSupportedException e) { throw new BuildException(e); } } /** * Clear out the whole command line. */ public void clear() { executable = null; arguments.removeAllElements(); } /** * Clear out the arguments but leave the executable in place for * another operation. */ public void clearArgs() { arguments.removeAllElements(); } /** * Return a marker. * * <p>This marker can be used to locate a position on the * commandline--to insert something for example--when all * parameters have been set.</p> * @return a marker */ public Marker createMarker() { return new Marker(arguments.size()); } /** * Return a String that describes the command and arguments suitable for * verbose output before a call to <code>Runtime.exec(String[])<code>. * @return a string that describes the command and arguments. * @since Ant 1.5 */ public String describeCommand() { return describeCommand(this); } /** * Return a String that describes the arguments suitable for * verbose output before a call to <code>Runtime.exec(String[])<code>. * @return a string that describes the arguments. * @since Ant 1.5 */ public String describeArguments() { return describeArguments(this); } /** * Return a String that describes the command and arguments suitable for * verbose output before a call to <code>Runtime.exec(String[])<code>. * @param line the Commandline to describe. * @return a string that describes the command and arguments. * @since Ant 1.5 */ public static String describeCommand(Commandline line) { return describeCommand(line.getCommandline()); } /** * Return a String that describes the arguments suitable for * verbose output before a call to <code>Runtime.exec(String[])<code>. * @param line the Commandline whose arguments to describe. * @return a string that describes the arguments. * @since Ant 1.5 */ public static String describeArguments(Commandline line) { return describeArguments(line.getArguments()); } /** * Return a String that describes the command and arguments suitable for * verbose output before a call to <code>Runtime.exec(String[])<code>. * * <p>This method assumes that the first entry in the array is the * executable to run.</p> * @param args the command line to describe as an array of strings * @return a string that describes the command and arguments. * @since Ant 1.5 */ public static String describeCommand(String[] args) { if (args == null || args.length == 0) { return ""; } StringBuffer buf = new StringBuffer("Executing \'"); buf.append(args[0]); buf.append("\'"); if (args.length > 1) { buf.append(" with "); buf.append(describeArguments(args, 1)); } else { buf.append(DISCLAIMER); } return buf.toString(); } /** * Return a String that describes the arguments suitable for * verbose output before a call to <code>Runtime.exec(String[])<code>. * @param args the command line to describe as an array of strings. * @return a string that describes the arguments. * @since Ant 1.5 */ public static String describeArguments(String[] args) { return describeArguments(args, 0); } /** * Return a String that describes the arguments suitable for * verbose output before a call to <code>Runtime.exec(String[])<code>. * * @param args the command line to describe as an array of strings. * @param offset ignore entries before this index. * @return a string that describes the arguments * * @since Ant 1.5 */ protected static String describeArguments(String[] args, int offset) { if (args == null || args.length <= offset) { return ""; } StringBuffer buf = new StringBuffer("argument"); if (args.length > offset) { buf.append("s"); } buf.append(":").append(StringUtils.LINE_SEP); for (int i = offset; i < args.length; i++) { buf.append("\'").append(args[i]).append("\'") .append(StringUtils.LINE_SEP); } buf.append(DISCLAIMER); return buf.toString(); } /** * Get an iterator to the arguments list. * @since Ant 1.7 * @return an Iterator. */ public Iterator iterator() { return arguments.iterator(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -