📄 modifiedselector.java
字号:
} else if ("digest".equals(algoName.getValue())) { algorithm = new DigestAlgorithm(); } else if ("checksum".equals(algoName.getValue())) { algorithm = new ChecksumAlgorithm(); } } else { if (algorithmClass != null) { // use Algorithm specified by classname algorithm = (Algorithm) loadClass( algorithmClass, "is not an Algorithm.", Algorithm.class); } else { // nothing specified - use default algorithm = defaultAlgorithm; } } // specify the cache classname if (cacheName != null) { // use Cache defined via name if ("propertyfile".equals(cacheName.getValue())) { cache = new PropertiesfileCache(); } } else { if (cacheClass != null) { // use Cache specified by classname cache = (Cache) loadClass(cacheClass, "is not a Cache.", Cache.class); } else { // nothing specified - use default cache = defaultCache; } } // specify the comparator classname if (compName != null) { // use Algorithm defined via name if ("equal".equals(compName.getValue())) { comparator = new EqualComparator(); } else if ("rule".equals(compName.getValue())) { // TODO there is a problem with the constructor for the RBC. // you have to provide the rules in the constructors - no setters // available. throw new BuildException("RuleBasedCollator not yet supported."); // Have to think about lazy initialization here... JHM // comparator = new java.text.RuleBasedCollator(); } } else { if (comparatorClass != null) { // use Algorithm specified by classname comparator = (Comparator) loadClass( comparatorClass, "is not a Comparator.", Comparator.class); } else { // nothing specified - use default comparator = defaultComparator; } } // // ----- Set the special attributes, pattern '*.*' ----- // for (Iterator itSpecial = specialParameter.iterator(); itSpecial.hasNext();) { Parameter par = (Parameter) itSpecial.next(); useParameter(par); } specialParameter = new Vector(); } /** * Loads the specified class and initializes an object of that class. * Throws a BuildException using the given message if an error occurs during * loading/instantiation or if the object is not from the given type. * @param classname the classname * @param msg the message-part for the BuildException * @param type the type to check against * @return a castable object */ protected Object loadClass(String classname, String msg, Class type) { try { // load the specified class ClassLoader cl = getClassLoader(); Class clazz = null; if (cl != null) { clazz = cl.loadClass(classname); } else { clazz = Class.forName(classname); } Object rv = clazz.newInstance(); if (!type.isInstance(rv)) { throw new BuildException("Specified class (" + classname + ") " + msg); } return rv; } catch (ClassNotFoundException e) { throw new BuildException("Specified class (" + classname + ") not found."); } catch (Exception e) { throw new BuildException(e); } } // ----- the selection work ----- /** * Implementation of ResourceSelector.isSelected(). * * @param resource The resource to check * @return whether the resource is selected * @see ResourceSelector#isSelected(Resource) */ public boolean isSelected(Resource resource) { if (resource.isFilesystemOnly()) { // We have a 'resourced' file, so reconvert it and use // the 'old' implementation. FileResource fileResource = (FileResource) resource; File file = fileResource.getFile(); String filename = fileResource.getName(); File basedir = fileResource.getBaseDir(); return isSelected(basedir, filename, file); } else { try { // How to handle non-file-Resources? I copy temporarily the // resource to a file and use the file-implementation. FileUtils fu = FileUtils.getFileUtils(); File tmpFile = fu.createTempFile("modified-", ".tmp", null, true, true); Resource tmpResource = new FileResource(tmpFile); ResourceUtils.copyResource(resource, tmpResource); boolean isSelected = isSelected(tmpFile.getParentFile(), tmpFile.getName(), resource.toLongString()); return isSelected; } catch (UnsupportedOperationException uoe) { log("The resource '" + resource.getName() + "' does not provide an InputStream, so it is not checked. " + "Akkording to 'selres' attribute value it is " + ((selectResourcesWithoutInputStream) ? "" : " not") + "selected.", Project.MSG_INFO); return selectResourcesWithoutInputStream; } catch (Exception e) { throw new BuildException(e); } } } /** * Implementation of BaseExtendSelector.isSelected(). * * @param basedir as described in BaseExtendSelector * @param filename as described in BaseExtendSelector * @param file as described in BaseExtendSelector * @return as described in BaseExtendSelector */ public boolean isSelected(File basedir, String filename, File file) { return isSelected(basedir, filename, file.getAbsolutePath()); } /** * The business logic of this selector for use as ResourceSelector of * FileSelector. * * @param basedir as described in BaseExtendSelector * @param filename as described in BaseExtendSelector * @param cacheKey the name for the key for storing the hashvalue * @return */ private boolean isSelected(File basedir, String filename, String cacheKey) { validate(); File f = new File(basedir, filename); // You can not compute a value for a directory if (f.isDirectory()) { return selectDirectories; } // Get the values and do the comparison String cachedValue = String.valueOf(cache.get(f.getAbsolutePath())); String newValue = algorithm.getValue(f); boolean rv = (comparator.compare(cachedValue, newValue) != 0); // Maybe update the cache if (update && rv) { cache.put(f.getAbsolutePath(), newValue); setModified(getModified() + 1); if (!getDelayUpdate()) { saveCache(); } } return rv; } /** * save the cache file */ protected void saveCache() { if (getModified() > 0) { cache.save(); setModified(0); } } // ----- attribute and nested element support ----- /** * Setter for algorithmClass. * @param classname new value */ public void setAlgorithmClass(String classname) { algorithmClass = classname; } /** * Setter for comparatorClass. * @param classname new value */ public void setComparatorClass(String classname) { comparatorClass = classname; } /** * Setter for cacheClass. * @param classname new value */ public void setCacheClass(String classname) { cacheClass = classname; } /** * Support for <i>update</i> attribute. * @param update new value */ public void setUpdate(boolean update) { this.update = update; } /** * Support for <i>seldirs</i> attribute. * @param seldirs new value */ public void setSeldirs(boolean seldirs) { selectDirectories = seldirs; } /** * Support for <i>selres</i> attribute. * @param newValue the new value */ public void setSelres(boolean newValue) { this.selectResourcesWithoutInputStream = newValue; } /** * Getter for the modified count * @return modified count */ public int getModified() { return modified; } /** * Setter for the modified count * @param modified count */ public void setModified(int modified) { this.modified = modified; } /** * Getter for the delay update * @return true if we should delay for performance */ public boolean getDelayUpdate() { return delayUpdate; } /** * Setter for the delay update * @param delayUpdate true if we should delay for performance */ public void setDelayUpdate(boolean delayUpdate) { this.delayUpdate = delayUpdate; } /** * Add the classpath. * @param path the classpath */ public void addClasspath(Path path) { if (classpath != null) { throw new BuildException("<classpath> can be set only once."); } classpath = path; } /** * Returns and initializes the classloader for this class. * @return the classloader */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -