use of org.apache.tools.ant.types.resources.FileResource in project ant by apache.
the class ModifiedSelector method isSelected.
// ----- the selection work -----
/**
* Implementation of ResourceSelector.isSelected().
*
* @param resource The resource to check
* @return whether the resource is selected
* @see ResourceSelector#isSelected(Resource)
*/
@Override
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);
}
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. " + "According to 'selres' attribute value it is " + ((selectResourcesWithoutInputStream) ? "" : " not") + "selected.", Project.MSG_INFO);
return selectResourcesWithoutInputStream;
} catch (Exception e) {
throw new BuildException(e);
}
}
use of org.apache.tools.ant.types.resources.FileResource in project ant by apache.
the class ProjectHelperRepositoryTest method testFind.
@Test
public void testFind() {
ProjectHelperRepository repo = ProjectHelperRepository.getInstance();
repo.registerProjectHelper(SomeHelper.class);
Resource r = new FileResource(new File("test.xml"));
ProjectHelper helper = repo.getProjectHelperForBuildFile(r);
assertTrue(helper instanceof ProjectHelper2);
helper = repo.getProjectHelperForAntlib(r);
assertTrue(helper instanceof ProjectHelper2);
r = new FileResource(new File("test.myext"));
helper = repo.getProjectHelperForBuildFile(r);
assertTrue(helper instanceof SomeHelper);
helper = repo.getProjectHelperForAntlib(r);
assertTrue(helper instanceof SomeHelper);
r = new StringResource("test.myext");
helper = repo.getProjectHelperForBuildFile(r);
assertTrue(helper instanceof ProjectHelper2);
helper = repo.getProjectHelperForAntlib(r);
assertTrue(helper instanceof ProjectHelper2);
r = new StringResource("test.other");
helper = repo.getProjectHelperForBuildFile(r);
assertTrue(helper instanceof ProjectHelper2);
helper = repo.getProjectHelperForAntlib(r);
assertTrue(helper instanceof ProjectHelper2);
}
use of org.apache.tools.ant.types.resources.FileResource in project ant by apache.
the class PermissionUtilsTest method detectsFileTypeOfRegularFileFromResource.
@Test
public void detectsFileTypeOfRegularFileFromResource() throws IOException {
File f = File.createTempFile("ant", ".tst");
f.deleteOnExit();
assertEquals(PermissionUtils.FileType.REGULAR_FILE, PermissionUtils.FileType.of(new FileResource(f)));
}
use of org.apache.tools.ant.types.resources.FileResource in project ant by apache.
the class IntrospectionHelper method createAttributeSetter.
/**
* Creates an implementation of AttributeSetter for the given
* attribute type. Conversions (where necessary) are automatically
* made for the following types:
* <ul>
* <li>String (left as it is)
* <li>Character/char (first character is used)
* <li>Boolean/boolean
* ({@link Project#toBoolean(String) Project.toBoolean(String)} is used)
* <li>Class (Class.forName is used)
* <li>File (resolved relative to the appropriate project)
* <li>Path (resolve relative to the appropriate project)
* <li>Resource (resolved as a FileResource relative to the appropriate project)
* <li>FileProvider (resolved as a FileResource relative to the appropriate project)
* <li>EnumeratedAttribute (uses its own
* {@link EnumeratedAttribute#setValue(String) setValue} method)
* <li>Other primitive types (wrapper classes are used with constructors
* taking String)
* </ul>
*
* If none of the above covers the given parameters, a constructor for the
* appropriate class taking a String parameter is used if it is available.
*
* @param m The method to invoke on the bean when the setter is invoked.
* Must not be <code>null</code>.
* @param arg The type of the single argument of the bean's method.
* Must not be <code>null</code>.
* @param attrName the name of the attribute for which the setter is being
* created.
*
* @return an appropriate AttributeSetter instance, or <code>null</code>
* if no appropriate conversion is available.
*/
private AttributeSetter createAttributeSetter(final Method m, final Class<?> arg, final String attrName) {
// use wrappers for primitive classes, e.g. int and
// Integer are treated identically
final Class<?> reflectedArg = PRIMITIVE_TYPE_MAP.containsKey(arg) ? PRIMITIVE_TYPE_MAP.get(arg) : arg;
// Object.class - it gets handled differently by AttributeSetter
if (java.lang.Object.class == reflectedArg) {
return new AttributeSetter(m, arg) {
@Override
public void set(final Project p, final Object parent, final String value) throws InvocationTargetException, IllegalAccessException {
throw new BuildException("Internal ant problem - this should not get called");
}
};
}
// simplest case - setAttribute expects String
if (String.class.equals(reflectedArg)) {
return new AttributeSetter(m, arg) {
@Override
public void set(final Project p, final Object parent, final String value) throws InvocationTargetException, IllegalAccessException {
m.invoke(parent, (Object[]) new String[] { value });
}
};
}
// char and Character get special treatment - take the first character
if (java.lang.Character.class.equals(reflectedArg)) {
return new AttributeSetter(m, arg) {
@Override
public void set(final Project p, final Object parent, final String value) throws InvocationTargetException, IllegalAccessException {
if (value.length() == 0) {
throw new BuildException("The value \"\" is not a " + "legal value for attribute \"" + attrName + "\"");
}
m.invoke(parent, (Object[]) new Character[] { value.charAt(0) });
}
};
}
// boolean and Boolean get special treatment because we have a nice method in Project
if (java.lang.Boolean.class.equals(reflectedArg)) {
return new AttributeSetter(m, arg) {
@Override
public void set(final Project p, final Object parent, final String value) throws InvocationTargetException, IllegalAccessException {
m.invoke(parent, (Object[]) new Boolean[] { Project.toBoolean(value) ? Boolean.TRUE : Boolean.FALSE });
}
};
}
// Class doesn't have a String constructor but a decent factory method
if (java.lang.Class.class.equals(reflectedArg)) {
return new AttributeSetter(m, arg) {
@Override
public void set(final Project p, final Object parent, final String value) throws InvocationTargetException, IllegalAccessException, BuildException {
try {
m.invoke(parent, Class.forName(value));
} catch (final ClassNotFoundException ce) {
throw new BuildException(ce);
}
}
};
}
// resolve relative paths through Project
if (java.io.File.class.equals(reflectedArg)) {
return new AttributeSetter(m, arg) {
@Override
public void set(final Project p, final Object parent, final String value) throws InvocationTargetException, IllegalAccessException {
m.invoke(parent, p.resolveFile(value));
}
};
}
// resolve relative nio paths through Project
if (java.nio.file.Path.class.equals(reflectedArg)) {
return new AttributeSetter(m, arg) {
@Override
public void set(final Project p, final Object parent, final String value) throws InvocationTargetException, IllegalAccessException {
m.invoke(parent, p.resolveFile(value).toPath());
}
};
}
// resolve Resources/FileProviders as FileResources relative to Project:
if (Resource.class.equals(reflectedArg) || FileProvider.class.equals(reflectedArg)) {
return new AttributeSetter(m, arg) {
@Override
void set(final Project p, final Object parent, final String value) throws InvocationTargetException, IllegalAccessException, BuildException {
m.invoke(parent, new FileResource(p, p.resolveFile(value)));
}
};
}
// EnumeratedAttributes have their own helper class
if (EnumeratedAttribute.class.isAssignableFrom(reflectedArg)) {
return new AttributeSetter(m, arg) {
@Override
public void set(final Project p, final Object parent, final String value) throws InvocationTargetException, IllegalAccessException, BuildException {
try {
final EnumeratedAttribute ea = (EnumeratedAttribute) reflectedArg.newInstance();
ea.setValue(value);
m.invoke(parent, ea);
} catch (final InstantiationException ie) {
throw new BuildException(ie);
}
}
};
}
final AttributeSetter setter = getEnumSetter(reflectedArg, m, arg);
if (setter != null) {
return setter;
}
if (java.lang.Long.class.equals(reflectedArg)) {
return new AttributeSetter(m, arg) {
@Override
public void set(final Project p, final Object parent, final String value) throws InvocationTargetException, IllegalAccessException, BuildException {
try {
m.invoke(parent, StringUtils.parseHumanSizes(value));
} catch (final NumberFormatException e) {
throw new BuildException("Can't assign non-numeric" + " value '" + value + "' to" + " attribute " + attrName);
} catch (final InvocationTargetException e) {
throw e;
} catch (final IllegalAccessException e) {
throw e;
} catch (final Exception e) {
throw new BuildException(e);
}
}
};
}
// worst case. look for a public String constructor and use it
// also supports new Whatever(Project, String) as for Path or Reference
// This is used (deliberately) for all primitives/wrappers other than
// char, boolean, and long.
boolean includeProject;
Constructor<?> c;
try {
// First try with Project.
c = reflectedArg.getConstructor(Project.class, String.class);
includeProject = true;
} catch (final NoSuchMethodException nme) {
// OK, try without.
try {
c = reflectedArg.getConstructor(String.class);
includeProject = false;
} catch (final NoSuchMethodException nme2) {
// Well, no matching constructor.
return null;
}
}
final boolean finalIncludeProject = includeProject;
final Constructor<?> finalConstructor = c;
return new AttributeSetter(m, arg) {
@Override
public void set(final Project p, final Object parent, final String value) throws InvocationTargetException, IllegalAccessException, BuildException {
try {
final Object[] args = finalIncludeProject ? new Object[] { p, value } : new Object[] { value };
final Object attribute = finalConstructor.newInstance(args);
if (p != null) {
p.setProjectReference(attribute);
}
m.invoke(parent, attribute);
} catch (final InvocationTargetException e) {
final Throwable cause = e.getCause();
if (cause instanceof IllegalArgumentException) {
throw new BuildException("Can't assign value '" + value + "' to attribute " + attrName + ", reason: " + cause.getClass() + " with message '" + cause.getMessage() + "'");
}
throw e;
} catch (final InstantiationException ie) {
throw new BuildException(ie);
}
}
};
}
use of org.apache.tools.ant.types.resources.FileResource in project ant by apache.
the class Touch method touch.
/**
* Does the actual work; assumes everything has been checked by now.
* @throws BuildException if an error occurs.
*/
protected void touch() throws BuildException {
long defaultTimestamp = getTimestamp();
if (file != null) {
touch(new FileResource(file.getParentFile(), file.getName()), defaultTimestamp);
}
if (resources == null) {
return;
}
// deal with the resource collections
for (Resource r : resources) {
Touchable t = r.as(Touchable.class);
if (t == null) {
throw new BuildException("Can't touch " + r);
}
touch(r, defaultTimestamp);
}
// them.
for (FileSet fs : filesets) {
DirectoryScanner ds = fs.getDirectoryScanner(getProject());
File fromDir = fs.getDir(getProject());
for (String srcDir : ds.getIncludedDirectories()) {
touch(new FileResource(fromDir, srcDir), defaultTimestamp);
}
}
}
Aggregations