Search in sources :

Example 31 with FileResource

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);
    }
}
Also used : FileUtils(org.apache.tools.ant.util.FileUtils) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) FileResource(org.apache.tools.ant.types.resources.FileResource) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) BuildException(org.apache.tools.ant.BuildException)

Example 32 with FileResource

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);
}
Also used : ProjectHelper2(org.apache.tools.ant.helper.ProjectHelper2) StringResource(org.apache.tools.ant.types.resources.StringResource) Resource(org.apache.tools.ant.types.Resource) StringResource(org.apache.tools.ant.types.resources.StringResource) FileResource(org.apache.tools.ant.types.resources.FileResource) FileResource(org.apache.tools.ant.types.resources.FileResource) File(java.io.File) Test(org.junit.Test)

Example 33 with FileResource

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)));
}
Also used : FileResource(org.apache.tools.ant.types.resources.FileResource) File(java.io.File) Test(org.junit.Test)

Example 34 with FileResource

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);
            }
        }
    };
}
Also used : Resource(org.apache.tools.ant.types.Resource) FileResource(org.apache.tools.ant.types.resources.FileResource) FileResource(org.apache.tools.ant.types.resources.FileResource) EnumeratedAttribute(org.apache.tools.ant.types.EnumeratedAttribute) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) FileProvider(org.apache.tools.ant.types.resources.FileProvider)

Example 35 with FileResource

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);
        }
    }
}
Also used : FileSet(org.apache.tools.ant.types.FileSet) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) FileResource(org.apache.tools.ant.types.resources.FileResource) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) Touchable(org.apache.tools.ant.types.resources.Touchable)

Aggregations

FileResource (org.apache.tools.ant.types.resources.FileResource)48 File (java.io.File)29 BuildException (org.apache.tools.ant.BuildException)25 Resource (org.apache.tools.ant.types.Resource)23 IOException (java.io.IOException)15 FileProvider (org.apache.tools.ant.types.resources.FileProvider)10 Test (org.junit.Test)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 Liquibase (liquibase.Liquibase)6 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)6 FileSet (org.apache.tools.ant.types.FileSet)6 Contexts (liquibase.Contexts)5 LiquibaseException (liquibase.exception.LiquibaseException)5 BufferedReader (java.io.BufferedReader)4 Reader (java.io.Reader)4 Writer (java.io.Writer)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Iterator (java.util.Iterator)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4