📄 available.java
字号:
+ "resource) is required", getLocation()); } if (type != null) { if (file == null) { throw new BuildException("The type attribute is only valid " + "when specifying the file " + "attribute.", getLocation()); } } if (classpath != null) { classpath.setProject(getProject()); this.loader = getProject().createClassLoader(classpath); } String appendix = ""; if (isTask) { appendix = " to set property " + property; } else { setTaskName("available"); } if ((classname != null) && !checkClass(classname)) { log("Unable to load class " + classname + appendix, Project.MSG_VERBOSE); return false; } if ((file != null) && !checkFile()) { StringBuffer buf = new StringBuffer("Unable to find "); if (type != null) { buf.append(type).append(' '); } buf.append(filename).append(appendix); log(buf.toString(), Project.MSG_VERBOSE); return false; } if ((resource != null) && !checkResource(resource)) { log("Unable to load resource " + resource + appendix, Project.MSG_VERBOSE); return false; } } finally { if (loader != null) { loader.cleanup(); loader = null; } if (!isTask) { setTaskName(null); } } return true; } /** * Search for file/directory either relative to project's * basedir or in the path given as filepath. * * <p>filepath can be a list of directory and/or file names (gen'd * via <fileset>)</p> * * <p>look for:</p><ul> * <li>full-pathname specified == path in list</li> * <li>full-pathname specified == parent dir of path in list</li> * <li>simple name specified == path in list</li> * <li>simple name specified == path in list + name</li> * <li>simple name specified == parent dir + name</li> * <li>simple name specified == parent of parent dir + name</li> * </ul> */ private boolean checkFile() { if (filepath == null) { return checkFile(file, filename); } else { String[] paths = filepath.list(); for (int i = 0; i < paths.length; ++i) { log("Searching " + paths[i], Project.MSG_VERBOSE); File path = new File(paths[i]); // ** full-pathname specified == path in list // ** simple name specified == path in list if (path.exists() && (filename.equals(paths[i]) || filename.equals(path.getName()))) { if (type == null) { log("Found: " + path, Project.MSG_VERBOSE); return true; } else if (type.isDir() && path.isDirectory()) { log("Found directory: " + path, Project.MSG_VERBOSE); return true; } else if (type.isFile() && path.isFile()) { log("Found file: " + path, Project.MSG_VERBOSE); return true; } // not the requested type return false; } File parent = path.getParentFile(); // ** full-pathname specified == parent dir of path in list if (parent != null && parent.exists() && filename.equals(parent.getAbsolutePath())) { if (type == null) { log("Found: " + parent, Project.MSG_VERBOSE); return true; } else if (type.isDir()) { log("Found directory: " + parent, Project.MSG_VERBOSE); return true; } // not the requested type return false; } // ** simple name specified == path in list + name if (path.exists() && path.isDirectory()) { if (checkFile(new File(path, filename), filename + " in " + path)) { return true; } } // ** simple name specified == parent dir + name while (searchParents && parent != null && parent.exists()) { if (checkFile(new File(parent, filename), filename + " in " + parent)) { return true; } parent = parent.getParentFile(); } } } return false; } /** * Check if a given file exists and matches the required type. */ private boolean checkFile(File f, String text) { if (type != null) { if (type.isDir()) { if (f.isDirectory()) { log("Found directory: " + text, Project.MSG_VERBOSE); } return f.isDirectory(); } else if (type.isFile()) { if (f.isFile()) { log("Found file: " + text, Project.MSG_VERBOSE); } return f.isFile(); } } if (f.exists()) { log("Found: " + text, Project.MSG_VERBOSE); } return f.exists(); } /** * Check if a given resource can be loaded. */ private boolean checkResource(String resource) { if (loader != null) { return (loader.getResourceAsStream(resource) != null); } else { ClassLoader cL = this.getClass().getClassLoader(); if (cL != null) { return (cL.getResourceAsStream(resource) != null); } else { return (ClassLoader.getSystemResourceAsStream(resource) != null); } } } /** * Check if a given class can be loaded. */ private boolean checkClass(String classname) { try { if (ignoreSystemclasses) { loader = getProject().createClassLoader(classpath); loader.setParentFirst(false); loader.addJavaLibraries(); if (loader != null) { try { loader.findClass(classname); } catch (SecurityException se) { // class found but restricted name; this is // actually the case we're looking for in JDK 1.3+, // so catch the exception and return return true; } } else { return false; } } else if (loader != null) { loader.loadClass(classname); } else { ClassLoader l = this.getClass().getClassLoader(); // Can return null to represent the bootstrap class loader. // see API docs of Class.getClassLoader. if (l != null) { Class.forName(classname, true, l); } else { Class.forName(classname); } } return true; } catch (ClassNotFoundException e) { log("class \"" + classname + "\" was not found", Project.MSG_DEBUG); return false; } catch (NoClassDefFoundError e) { log("Could not load dependent class \"" + e.getMessage() + "\" for class \"" + classname + "\"", Project.MSG_DEBUG); return false; } } /** * EnumeratedAttribute covering the file types to be checked for, either * file or dir. */ public static class FileDir extends EnumeratedAttribute { private static final String[] VALUES = {"file", "dir"}; /** * @see EnumeratedAttribute#getValues */ /** {@inheritDoc}. */ public String[] getValues() { return VALUES; } /** * Indicate if the value specifies a directory. * * @return true if the value specifies a directory. */ public boolean isDir() { return "dir".equalsIgnoreCase(getValue()); } /** * Indicate if the value specifies a file. * * @return true if the value specifies a file. */ public boolean isFile() { return "file".equalsIgnoreCase(getValue()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -