📄 path.java
字号:
* @return an array of strings, one for each path element */ public static String[] translatePath(Project project, String source) { final Vector result = new Vector(); if (source == null) { return new String[0]; } PathTokenizer tok = new PathTokenizer(source); StringBuffer element = new StringBuffer(); while (tok.hasMoreTokens()) { String pathElement = tok.nextToken(); try { element.append(resolveFile(project, pathElement).getPath()); } catch (BuildException e) { project.log("Dropping path element " + pathElement + " as it is not valid relative to the project", Project.MSG_VERBOSE); } for (int i = 0; i < element.length(); i++) { translateFileSep(element, i); } result.addElement(element.toString()); element = new StringBuffer(); } String[] res = new String[result.size()]; result.copyInto(res); return res; } /** * Returns its argument with all file separator characters * replaced so that they match the local OS conventions. * @param source the path to convert * @return the converted path */ public static String translateFile(String source) { if (source == null) { return ""; } final StringBuffer result = new StringBuffer(source); for (int i = 0; i < result.length(); i++) { translateFileSep(result, i); } return result.toString(); } /** * Translates occurrences at a position of / or \ to correct separator of the * current platform and returns whether it had to do a * replacement. * @param buffer a buffer containing a string * @param pos the position in the string buffer to convert * @return true if the character was a / or \ */ protected static boolean translateFileSep(StringBuffer buffer, int pos) { if (buffer.charAt(pos) == '/' || buffer.charAt(pos) == '\\') { buffer.setCharAt(pos, File.separatorChar); return true; } return false; } /** * Fulfill the ResourceCollection contract. * @return number of elements as int. */ public synchronized int size() { if (isReference()) { return ((Path) getCheckedRef()).size(); } dieOnCircularReference(); return union == null ? 0 : assertFilesystemOnly(union).size(); } /** * Clone this Path. * @return Path with shallowly cloned Resource children. */ public Object clone() { try { Path result = (Path) super.clone(); result.union = union == null ? union : (Union) union.clone(); return result; } catch (CloneNotSupportedException e) { throw new BuildException(e); } } /** * Overrides the version of DataType to recurse on all DataType * child elements that may have been added. * @param stk the stack of data types to use (recursively). * @param p the project to use to dereference the references. * @throws BuildException on error. */ protected synchronized void dieOnCircularReference(Stack stk, Project p) throws BuildException { if (isChecked()) { return; } if (isReference()) { super.dieOnCircularReference(stk, p); } else { if (union != null) { stk.push(union); invokeCircularReferenceCheck(union, stk, p); stk.pop(); } setChecked(true); } } /** * Resolve a filename with Project's help - if we know one that is. */ private static File resolveFile(Project project, String relativeName) { return FileUtils.getFileUtils().resolveFile( (project == null) ? null : project.getBaseDir(), relativeName); } /** * Concatenates the system class path in the order specified by * the ${build.sysclasspath} property - using "last" as * default value. * @return the concatenated path */ public Path concatSystemClasspath() { return concatSystemClasspath("last"); } /** * Concatenates the system class path in the order specified by * the ${build.sysclasspath} property - using the supplied value * if ${build.sysclasspath} has not been set. * @param defValue the order ("first", "last", "only") * @return the concatenated path */ public Path concatSystemClasspath(String defValue) { return concatSpecialPath(defValue, Path.systemClasspath); } /** * Concatenates the system boot class path in the order specified * by the ${build.sysclasspath} property - using the supplied * value if ${build.sysclasspath} has not been set. * @param defValue the order ("first", "last", "only") * @return the concatenated path */ public Path concatSystemBootClasspath(String defValue) { return concatSpecialPath(defValue, Path.systemBootClasspath); } /** * Concatenates a class path in the order specified by the * ${build.sysclasspath} property - using the supplied value if * ${build.sysclasspath} has not been set. */ private Path concatSpecialPath(String defValue, Path p) { Path result = new Path(getProject()); String order = defValue; if (getProject() != null) { String o = getProject().getProperty("build.sysclasspath"); if (o != null) { order = o; } } if (order.equals("only")) { // only: the developer knows what (s)he is doing result.addExisting(p, true); } else if (order.equals("first")) { // first: developer could use a little help result.addExisting(p, true); result.addExisting(this); } else if (order.equals("ignore")) { // ignore: don't trust anyone result.addExisting(this); } else { // last: don't trust the developer if (!order.equals("last")) { log("invalid value for build.sysclasspath: " + order, Project.MSG_WARN); } result.addExisting(this); result.addExisting(p, true); } return result; } /** * Add the Java Runtime classes to this Path instance. */ public void addJavaRuntime() { if (JavaEnvUtils.isKaffe()) { // newer versions of Kaffe (1.1.1+) won't have this, // but this will be sorted by FileSet anyway. File kaffeShare = new File(System.getProperty("java.home") + File.separator + "share" + File.separator + "kaffe"); if (kaffeShare.isDirectory()) { FileSet kaffeJarFiles = new FileSet(); kaffeJarFiles.setDir(kaffeShare); kaffeJarFiles.setIncludes("*.jar"); addFileset(kaffeJarFiles); } } else if ("GNU libgcj".equals(System.getProperty("java.vm.name"))) { addExisting(systemBootClasspath); } if (System.getProperty("java.vendor").toLowerCase(Locale.US).indexOf("microsoft") >= 0) { // XXX is this code still necessary? is there any 1.2+ port? // Pull in *.zip from packages directory FileSet msZipFiles = new FileSet(); msZipFiles.setDir(new File(System.getProperty("java.home") + File.separator + "Packages")); msZipFiles.setIncludes("*.ZIP"); addFileset(msZipFiles); } else { // JDK 1.2+ seems to set java.home to the JRE directory. addExisting(new Path(null, System.getProperty("java.home") + File.separator + "lib" + File.separator + "rt.jar")); // Just keep the old version as well and let addExisting // sort it out. addExisting(new Path(null, System.getProperty("java.home") + File.separator + "jre" + File.separator + "lib" + File.separator + "rt.jar")); // Sun's and Apple's 1.4 have JCE and JSSE in separate jars. String[] secJars = {"jce", "jsse"}; for (int i = 0; i < secJars.length; i++) { addExisting(new Path(null, System.getProperty("java.home") + File.separator + "lib" + File.separator + secJars[i] + ".jar")); addExisting(new Path(null, System.getProperty("java.home") + File.separator + ".." + File.separator + "Classes" + File.separator + secJars[i] + ".jar")); } // IBM's 1.4 has rt.jar split into 4 smaller jars and a combined // JCE/JSSE in security.jar. String[] ibmJars = {"core", "graphics", "security", "server", "xml"}; for (int i = 0; i < ibmJars.length; i++) { addExisting(new Path(null, System.getProperty("java.home") + File.separator + "lib" + File.separator + ibmJars[i] + ".jar")); } // Added for MacOS X addExisting(new Path(null, System.getProperty("java.home") + File.separator + ".." + File.separator + "Classes" + File.separator + "classes.jar")); addExisting(new Path(null, System.getProperty("java.home") + File.separator + ".." + File.separator + "Classes" + File.separator + "ui.jar")); } } /** * Emulation of extdirs feature in java >= 1.2. * This method adds all files in the given * directories (but not in sub-directories!) to the classpath, * so that you don't have to specify them all one by one. * @param extdirs - Path to append files to */ public void addExtdirs(Path extdirs) { if (extdirs == null) { String extProp = System.getProperty("java.ext.dirs"); if (extProp != null) { extdirs = new Path(getProject(), extProp); } else { return; } } String[] dirs = extdirs.list(); for (int i = 0; i < dirs.length; i++) { File dir = resolveFile(getProject(), dirs[i]); if (dir.exists() && dir.isDirectory()) { FileSet fs = new FileSet(); fs.setDir(dir); fs.setIncludes("*"); addFileset(fs); } } } /** * Fulfill the ResourceCollection contract. The Iterator returned * will throw ConcurrentModificationExceptions if ResourceCollections * are added to this container while the Iterator is in use. * @return a "fail-fast" Iterator. */ public final synchronized Iterator iterator() { if (isReference()) { return ((Path) getCheckedRef()).iterator(); } dieOnCircularReference(); if (getPreserveBC()) { return new FileResourceIterator(null, list()); } return union == null ? EMPTY_ITERATOR : assertFilesystemOnly(union).iterator(); } /** * Fulfill the ResourceCollection contract. * @return whether this is a filesystem-only resource collection. */ public synchronized boolean isFilesystemOnly() { if (isReference()) { return ((Path) getCheckedRef()).isFilesystemOnly(); } dieOnCircularReference(); assertFilesystemOnly(union); return true; } /** * Verify the specified ResourceCollection is filesystem-only. * @param rc the ResourceCollection to check. * @throws BuildException if <code>rc</code> is not filesystem-only. * @return the passed in ResourceCollection. */ protected ResourceCollection assertFilesystemOnly(ResourceCollection rc) { if (rc != null && !(rc.isFilesystemOnly())) { throw new BuildException(getDataTypeName() + " allows only filesystem resources."); } return rc; } /** * Helps determine whether to preserve BC by calling <code>list()</code> on subclasses. * The default behavior of this method is to return <code>true</code> for any subclass * that implements <code>list()</code>; this can, of course, be avoided by overriding * this method to return <code>false</code>. It is not expected that the result of this * method should change over time, thus it is called only once. * @return <code>true</code> if <code>iterator()</code> should delegate to <code>list()</code>. */ protected boolean delegateIteratorToList() { if (getClass().equals(Path.class)) { return false; } try { Method listMethod = getClass().getMethod("list", (Class[]) null); return !listMethod.getDeclaringClass().equals(Path.class); } catch (Exception e) { //shouldn't happen, but return false; } } private synchronized boolean getPreserveBC() { if (preserveBC == null) { preserveBC = delegateIteratorToList() ? Boolean.TRUE : Boolean.FALSE; } return preserveBC.booleanValue(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -