📄 commandline.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package org.apache.tools.ant.types;import java.io.File;import java.util.StringTokenizer;import java.util.Vector;import java.util.ArrayList;import java.util.List;import java.util.ListIterator;import java.util.LinkedList;import java.util.Iterator;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.ProjectComponent;import org.apache.tools.ant.util.StringUtils;import org.apache.tools.ant.taskdefs.condition.Os;/** * Commandline objects help handling command lines specifying processes to * execute. * * The class can be used to define a command line as nested elements or as a * helper to define a command line by an application. * <p> * <code> * <someelement><br> * <acommandline executable="/executable/to/run"><br> * <argument value="argument 1" /><br> * <argument line="argument_1 argument_2 argument_3" /><br> * <argument value="argument 4" /><br> * </acommandline><br> * </someelement><br> * </code> * The element <code>someelement</code> must provide a method * <code>createAcommandline</code> which returns an instance of this class. * */public class Commandline implements Cloneable { /** win9x uses a (shudder) bat file (antRun.bat) for executing commands */ private static final boolean IS_WIN_9X = Os.isFamily("win9x"); /** * The arguments of the command */ private Vector arguments = new Vector(); /** * the program to execute */ private String executable = null; protected static final String DISCLAIMER = StringUtils.LINE_SEP + "The \' characters around the executable and arguments are" + StringUtils.LINE_SEP + "not part of the command." + StringUtils.LINE_SEP; /** * Create a command line from a string. * @param toProcess the line: the first element becomes the executable, the rest * the arguments. */ public Commandline(String toProcess) { super(); String[] tmp = translateCommandline(toProcess); if (tmp != null && tmp.length > 0) { setExecutable(tmp[0]); for (int i = 1; i < tmp.length; i++) { createArgument().setValue(tmp[i]); } } } /** * Create an empty command line. */ public Commandline() { super(); } /** * Used for nested xml command line definitions. */ public static class Argument extends ProjectComponent { private String[] parts; /** * Set a single commandline argument. * * @param value a single commandline argument. */ public void setValue(String value) { parts = new String[] {value}; } /** * Set the line to split into several commandline arguments. * * @param line line to split into several commandline arguments. */ public void setLine(String line) { if (line == null) { return; } parts = translateCommandline(line); } /** * Set a single commandline argument and treats it like a * PATH--ensuring the right separator for the local platform * is used. * * @param value a single commandline argument. */ public void setPath(Path value) { parts = new String[] {value.toString()}; } /** * Set a single commandline argument from a reference to a * path--ensuring the right separator for the local platform * is used. * * @param value a single commandline argument. */ public void setPathref(Reference value) { Path p = new Path(getProject()); p.setRefid(value); parts = new String[] {p.toString()}; } /** * Set a single commandline argument to the absolute filename * of the given file. * * @param value a single commandline argument. */ public void setFile(File value) { parts = new String[] {value.getAbsolutePath()}; } /** * Return the constituent parts of this Argument. * @return an array of strings. */ public String[] getParts() { return parts; } } /** * Class to keep track of the position of an Argument. <p>This class is there to support the srcfile and targetfile elements of <execon> and <transform> - don't know whether there might be additional use cases.</p> --SB */ public class Marker { private int position; private int realPos = -1; /** * Construct a marker for the specified position. * @param position the position to mark. */ Marker(int position) { this.position = position; } /** * Return the number of arguments that preceded this marker. * * <p>The name of the executable -- if set -- is counted as the * first argument.</p> * @return the position of this marker. */ public int getPosition() { if (realPos == -1) { realPos = (executable == null ? 0 : 1); for (int i = 0; i < position; i++) { Argument arg = (Argument) arguments.elementAt(i); realPos += arg.getParts().length; } } return realPos; } } /** * Create an argument object. * * <p>Each commandline object has at most one instance of the * argument class. This method calls * <code>this.createArgument(false)</code>.</p> * * @see #createArgument(boolean) * @return the argument object. */ public Argument createArgument() { return this.createArgument(false); } /** * Create an argument object and add it to our list of args. * * <p>Each commandline object has at most one instance of the * argument class.</p> * * @param insertAtStart if true, the argument is inserted at the * beginning of the list of args, otherwise it is appended. * @return an argument to be configured */ public Argument createArgument(boolean insertAtStart) { Argument argument = new Argument(); if (insertAtStart) { arguments.insertElementAt(argument, 0); } else { arguments.addElement(argument); } return argument; } /** * Set the executable to run. All file separators in the string * are converted to the platform specific value. * @param executable the String executable name. */ public void setExecutable(String executable) { if (executable == null || executable.length() == 0) { return; } this.executable = executable.replace('/', File.separatorChar) .replace('\\', File.separatorChar); } /** * Get the executable. * @return the program to run--null if not yet set. */ public String getExecutable() { return executable; } /** * Append the arguments to the existing command. * @param line an array of arguments to append. */ public void addArguments(String[] line) { for (int i = 0; i < line.length; i++) { createArgument().setValue(line[i]); } } /** * Return the executable and all defined arguments. * @return the commandline as an array of strings. */ public String[] getCommandline() { List commands = new LinkedList(); ListIterator list = commands.listIterator(); addCommandToList(list); final String[] result = new String[commands.size()]; return (String[]) commands.toArray(result); } /** * Add the entire command, including (optional) executable to a list. * @param list the list to add to. * @since Ant 1.6 */ public void addCommandToList(ListIterator list) { if (executable != null) { list.add(executable); } addArgumentsToList(list); } /** * Returns all arguments defined by <code>addLine</code>, * <code>addValue</code> or the argument object. * @return the arguments as an array of strings. */ public String[] getArguments() { List result = new ArrayList(arguments.size() * 2); addArgumentsToList(result.listIterator()); String [] res = new String[result.size()]; return (String[]) result.toArray(res); } /** * Append all the arguments to the tail of a supplied list. * @param list the list of arguments.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -