/** * Tests a regular expression against each line of text in a Resource. * * @param r the Resource to check. * @return whether the Resource is selected or not */ public boolean isSelected(Resource r) { String teststr = null; BufferedReader in = null; // throw BuildException on error validate(); if (r.isDirectory()) { return true; } if (myRegExp == null) { myRegExp = new RegularExpression(); myRegExp.setPattern(userProvidedExpression); myExpression = myRegExp.getRegexp(getProject()); } try { in = new BufferedReader(new InputStreamReader(r.getInputStream())); } catch (Exception e) { throw new BuildException("Could not get InputStream from " + r.toLongString(), e); } try { teststr = in.readLine(); while (teststr != null) { if (myExpression.matches( teststr, RegexpUtil.asOptions(caseSensitive, multiLine, singleLine))) { return true; } teststr = in.readLine(); } return false; } catch (IOException ioe) { throw new BuildException("Could not read " + r.toLongString()); } finally { try { in.close(); } catch (Exception e) { throw new BuildException("Could not close " + r.toLongString()); } } }
/** * Returns list of mapped files, that should be transformed. Files can by specified via attributes * (srcFile, srcDir) or resources (FileSet, FileList, DirSet, etc). Mapped file represents input * and output file for transformation. * * @return list of mapped files */ public List<MappedFile> getMappedFiles() { mappedFiles.clear(); // one src file if (getSrcFile() != null) { addMappedFile(getSrcFile()); } if (getSrcDir() != null) { addMappedFile(getSrcDir()); } Iterator element = resources.iterator(); while (element.hasNext()) { ResourceCollection rc = (ResourceCollection) element.next(); if (rc instanceof FileSet && rc.isFilesystemOnly()) { FileSet fs = (FileSet) rc; File fromDir = fs.getDir(getProject()); DirectoryScanner ds; try { ds = fs.getDirectoryScanner(getProject()); } catch (BuildException ex) { log("Could not scan directory " + fromDir, ex, Project.MSG_ERR); continue; } for (String f : ds.getIncludedFiles()) { addMappedFile(new File(fromDir + System.getProperty("file.separator") + f), fromDir); } } else { if (!rc.isFilesystemOnly()) { log("Only filesystem resources are supported", Project.MSG_WARN); continue; } Iterator rcIt = rc.iterator(); while (rcIt.hasNext()) { Resource r = (Resource) rcIt.next(); if (!r.isExists()) { log("Could not find resource " + r.toLongString(), Project.MSG_VERBOSE); continue; } if (r instanceof FileResource) { FileResource fr = (FileResource) r; addMappedFile(fr.getFile(), fr.getBaseDir()); } else { log( "Only file resources are supported (" + r.getClass().getSimpleName() + " found)", Project.MSG_WARN); continue; } } } } return mappedFiles; }
/** * Add a resource to the source list. * * @since Ant 1.7.1 * @param sourceResource the resource to load * @throws BuildException if the resource cannot be read */ public void loadResource(Resource sourceResource) { String name = sourceResource.toLongString(); InputStream in = null; try { in = sourceResource.getInputStream(); } catch (IOException e) { throw new BuildException("Failed to open " + name, e); } catch (UnsupportedOperationException e) { throw new BuildException("Failed to open " + name + " -it is not readable", e); } readSource(new InputStreamReader(in), name); }
/** * 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, false); Resource tmpResource = new FileResource(tmpFile); ResourceUtils.copyResource(resource, tmpResource); boolean isSelected = isSelected(tmpFile.getParentFile(), tmpFile.getName(), resource.toLongString()); tmpFile.delete(); 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); } } }