📄 path.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.lang.reflect.Method;import java.util.Collections;import java.util.Iterator;import java.util.Locale;import java.util.Stack;import java.util.Vector;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.PathTokenizer;import org.apache.tools.ant.Project;import org.apache.tools.ant.types.resources.Union;import org.apache.tools.ant.types.resources.FileResourceIterator;import org.apache.tools.ant.util.FileUtils;import org.apache.tools.ant.util.JavaEnvUtils;/** * This object represents a path as used by CLASSPATH or PATH * environment variable. A path might also be described as a collection * of unique filesystem resources. * <p> * <code> * <sometask><br> * <somepath><br> * <pathelement location="/path/to/file.jar" /><br> * <pathelement * path="/path/to/file2.jar:/path/to/class2;/path/to/class3" /> * <br> * <pathelement location="/path/to/file3.jar" /><br> * <pathelement location="/path/to/file4.jar" /><br> * </somepath><br> * </sometask><br> * </code> * <p> * The object implemention <code>sometask</code> must provide a method called * <code>createSomepath</code> which returns an instance of <code>Path</code>. * Nested path definitions are handled by the Path object and must be labeled * <code>pathelement</code>.<p> * * The path element takes a parameter <code>path</code> which will be parsed * and split into single elements. It will usually be used * to define a path from an environment variable. */public class Path extends DataType implements Cloneable, ResourceCollection { // CheckStyle:VisibilityModifier OFF - bc /** The system classpath as a Path object */ public static Path systemClasspath = new Path(null, System.getProperty("java.class.path")); /** * The system bootclasspath as a Path object. * * @since Ant 1.6.2 */ public static Path systemBootClasspath = new Path(null, System.getProperty("sun.boot.class.path")); private static final Iterator EMPTY_ITERATOR = Collections.EMPTY_SET.iterator(); // CheckStyle:VisibilityModifier OFF - bc /** * Helper class, holds the nested <code><pathelement></code> values. */ public class PathElement implements ResourceCollection { private String[] parts; /** * Set the location. * * @param loc a <code>File</code> value */ public void setLocation(File loc) { parts = new String[] {translateFile(loc.getAbsolutePath())}; } /** * Set the path. * * @param path a <code>String</code> value */ public void setPath(String path) { parts = Path.translatePath(getProject(), path); } /** * Return the converted pathelements. * * @return a <code>String[]</code> value */ public String[] getParts() { return parts; } /** * Create an iterator. * @return an iterator. */ public Iterator iterator() { return new FileResourceIterator(null, parts); } /** * Check if this resource is only for filesystems. * @return true. */ public boolean isFilesystemOnly() { return true; } /** * Get the number of resources. * @return the number of parts. */ public int size() { return parts == null ? 0 : parts.length; } } private Boolean preserveBC; private Union union = null; /** * Invoked by IntrospectionHelper for <code>setXXX(Path p)</code> * attribute setters. * @param p the <code>Project</code> for this path. * @param path the <code>String</code> path definition. */ public Path(Project p, String path) { this(p); createPathElement().setPath(path); } /** * Construct an empty <code>Path</code>. * @param project the <code>Project</code> for this path. */ public Path(Project project) { setProject(project); } /** * Adds a element definition to the path. * @param location the location of the element to add (must not be * <code>null</code> nor empty. * @throws BuildException on error */ public void setLocation(File location) throws BuildException { checkAttributesAllowed(); createPathElement().setLocation(location); } /** * Parses a path definition and creates single PathElements. * @param path the <code>String</code> path definition. * @throws BuildException on error */ public void setPath(String path) throws BuildException { checkAttributesAllowed(); createPathElement().setPath(path); } /** * Makes this instance in effect a reference to another Path instance. * * <p>You must not set another attribute or nest elements inside * this element if you make it a reference.</p> * @param r the reference to another Path * @throws BuildException on error */ public void setRefid(Reference r) throws BuildException { if (union != null) { throw tooManyAttributes(); } super.setRefid(r); } /** * Creates the nested <code><pathelement></code> element. * @return the <code>PathElement</code> to be configured * @throws BuildException on error */ public PathElement createPathElement() throws BuildException { if (isReference()) { throw noChildrenAllowed(); } PathElement pe = new PathElement(); add(pe); return pe; } /** * Adds a nested <code><fileset></code> element. * @param fs a <code>FileSet</code> to be added to the path * @throws BuildException on error */ public void addFileset(FileSet fs) throws BuildException { if (fs.getProject() == null) { fs.setProject(getProject()); } add(fs); } /** * Adds a nested <code><filelist></code> element. * @param fl a <code>FileList</code> to be added to the path * @throws BuildException on error */ public void addFilelist(FileList fl) throws BuildException { if (fl.getProject() == null) { fl.setProject(getProject()); } add(fl); } /** * Adds a nested <code><dirset></code> element. * @param dset a <code>DirSet</code> to be added to the path * @throws BuildException on error */ public void addDirset(DirSet dset) throws BuildException { if (dset.getProject() == null) { dset.setProject(getProject()); } add(dset); } /** * Adds a nested path * @param path a <code>Path</code> to be added to the path * @throws BuildException on error * @since Ant 1.6 */ public void add(Path path) throws BuildException { if (path == this) { throw circularReference(); } if (path.getProject() == null) { path.setProject(getProject()); } add((ResourceCollection) path); } /** * Add a nested <code>ResourceCollection</code>. * @param c the ResourceCollection to add. * @since Ant 1.7 */ public void add(ResourceCollection c) { checkChildrenAllowed(); if (c == null) { return; } if (union == null) { union = new Union(); union.setProject(getProject()); union.setCache(false); } union.add(c); setChecked(false); } /** * Creates a nested <code><path></code> element. * @return a <code>Path</code> to be configured * @throws BuildException on error */ public Path createPath() throws BuildException { Path p = new Path(getProject()); add(p); return p; } /** * Append the contents of the other Path instance to this. * @param other a <code>Path</code> to be added to the path */ public void append(Path other) { if (other == null) { return; } add(other); } /** * Adds the components on the given path which exist to this * Path. Components that don't exist aren't added. * * @param source - source path whose components are examined for existence */ public void addExisting(Path source) { addExisting(source, false); } /** * Same as addExisting, but support classpath behavior if tryUserDir * is true. Classpaths are relative to user dir, not the project base. * That used to break jspc test * * @param source the source path * @param tryUserDir if true try the user directory if the file is not present */ public void addExisting(Path source, boolean tryUserDir) { String[] list = source.list(); File userDir = (tryUserDir) ? new File(System.getProperty("user.dir")) : null; for (int i = 0; i < list.length; i++) { File f = resolveFile(getProject(), list[i]); // probably not the best choice, but it solves the problem of // relative paths in CLASSPATH if (tryUserDir && !f.exists()) { f = new File(userDir, list[i]); } if (f.exists()) { setLocation(f); } else { log("dropping " + f + " from path as it doesn't exist", Project.MSG_VERBOSE); } } } /** * Returns all path elements defined by this and nested path objects. * @return list of path elements. */ public String[] list() { if (isReference()) { return ((Path) getCheckedRef()).list(); } return assertFilesystemOnly(union) == null ? new String[0] : union.list(); } /** * Returns a textual representation of the path, which can be used as * CLASSPATH or PATH environment variable definition. * @return a textual representation of the path. */ public String toString() { return isReference() ? getCheckedRef().toString() : union == null ? "" : union.toString(); } /** * Splits a PATH (with : or ; as separators) into its parts. * @param project the project to use * @param source a <code>String</code> value
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -