ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nuno Costa" <nco...@ef.pt>
Subject Missing findResource org.apache.tools.ant.AntClassLoader (ANT 1.2)
Date Mon, 27 Nov 2000 16:33:52 GMT

Hello!

I'm starting to use ANT and I found some dificulties with
the ClassLoader.getResource() method when inside AntClassLoader.
The problem, as I found out, is that the AntClassLoader does'n
define the method "public URL findResource(String name)" as it
should.
I send you my version of the AntClassLoader.findResource, hopping
that someone includes it in the next realease of ANT.

see you,
Nuno Costa

   public URL findResource(String name) {
        String[] pathElements = classpath.list();
        for (int i = 0; i < pathElements.length; ++i) {
            File pathComponent =
project.resolveFile((String)pathElements[i]);
            if (pathComponent.exists())

                if (pathComponent.isDirectory())

                    File resource = new File(pathComponent, name);
                    if (resource.exists()) {
                        try {
                            return new URL("file:" + resource.getPath());
                        } catch (MalformedURLException ex) {
                            return null;
                        }
                    }
                } else {
                    try {
                        ZipFile zipFile = null;
                        try {
                            zipFile = new ZipFile(pathComponent);

                            ZipEntry entry = zipFile.getEntry(name);
                            if (entry != null) {
                                return new
URL("jar:file:"+pathComponent+"!/"+entry.getName());
                            }
                        } finally {
                            if (zipFile != null) {
                                zipFile.close();
                            }
                        }
                    } catch (IOException ex) {
                        ex.printStackTrace();
                        return null;
                    }
                }
            }
        }
        return null;
    }


Mime
View raw message