Search in sources :

Example 1 with URLProvider

use of org.apache.tools.ant.types.resources.URLProvider in project ant by apache.

the class ProjectHelper2 method parse.

/**
 * Parses the project file, configuring the project as it goes.
 *
 * @param project the current project
 * @param source  the xml source
 * @param handler the root handler to use (contains the current context)
 * @exception BuildException if the configuration is invalid or cannot
 *                           be read
 */
public void parse(Project project, Object source, RootHandler handler) throws BuildException {
    AntXMLContext context = handler.context;
    File buildFile = null;
    URL url = null;
    String buildFileName = null;
    if (source instanceof File) {
        buildFile = (File) source;
    } else if (source instanceof URL) {
        url = (URL) source;
    } else if (source instanceof Resource) {
        FileProvider fp = ((Resource) source).as(FileProvider.class);
        if (fp != null) {
            buildFile = fp.getFile();
        } else {
            URLProvider up = ((Resource) source).as(URLProvider.class);
            if (up != null) {
                url = up.getURL();
            }
        }
    }
    if (buildFile != null) {
        buildFile = FILE_UTILS.normalize(buildFile.getAbsolutePath());
        context.setBuildFile(buildFile);
        buildFileName = buildFile.toString();
    } else if (url != null) {
        try {
            context.setBuildFile((File) null);
            context.setBuildFile(url);
        } catch (java.net.MalformedURLException ex) {
            throw new BuildException(ex);
        }
        buildFileName = url.toString();
    } else {
        throw new BuildException("Source " + source.getClass().getName() + " not supported by this plugin");
    }
    InputStream inputStream = null;
    InputSource inputSource = null;
    ZipFile zf = null;
    try {
        /**
         * SAX 2 style parser used to parse the given file.
         */
        XMLReader parser = JAXPUtils.getNamespaceXMLReader();
        String uri = null;
        if (buildFile != null) {
            uri = FILE_UTILS.toURI(buildFile.getAbsolutePath());
            inputStream = Files.newInputStream(buildFile.toPath());
        } else {
            uri = url.toString();
            int pling = -1;
            if (uri.startsWith("jar:file") && (pling = uri.indexOf("!/")) > -1) {
                zf = new ZipFile(org.apache.tools.ant.launch.Locator.fromJarURI(uri), "UTF-8");
                inputStream = zf.getInputStream(zf.getEntry(uri.substring(pling + 2)));
            } else {
                URLConnection conn = url.openConnection();
                conn.setUseCaches(false);
                inputStream = conn.getInputStream();
            }
        }
        inputSource = new InputSource(inputStream);
        if (uri != null) {
            inputSource.setSystemId(uri);
        }
        project.log("parsing buildfile " + buildFileName + " with URI = " + uri + (zf != null ? " from a zip file" : ""), Project.MSG_VERBOSE);
        DefaultHandler hb = handler;
        parser.setContentHandler(hb);
        parser.setEntityResolver(hb);
        parser.setErrorHandler(hb);
        parser.setDTDHandler(hb);
        parser.parse(inputSource);
    } catch (SAXParseException exc) {
        Location location = new Location(exc.getSystemId(), exc.getLineNumber(), exc.getColumnNumber());
        Throwable t = exc.getException();
        if (t instanceof BuildException) {
            BuildException be = (BuildException) t;
            if (be.getLocation() == Location.UNKNOWN_LOCATION) {
                be.setLocation(location);
            }
            throw be;
        }
        throw new BuildException(exc.getMessage(), t == null ? exc : t, location);
    } catch (SAXException exc) {
        Throwable t = exc.getException();
        if (t instanceof BuildException) {
            throw (BuildException) t;
        }
        throw new BuildException(exc.getMessage(), t == null ? exc : t);
    } catch (FileNotFoundException exc) {
        throw new BuildException(exc);
    } catch (UnsupportedEncodingException exc) {
        throw new BuildException("Encoding of project file " + buildFileName + " is invalid.", exc);
    } catch (IOException exc) {
        throw new BuildException("Error reading project file " + buildFileName + ": " + exc.getMessage(), exc);
    } finally {
        FileUtils.close(inputStream);
        ZipFile.closeQuietly(zf);
    }
}
Also used : InputSource(org.xml.sax.InputSource) URLProvider(org.apache.tools.ant.types.resources.URLProvider) InputStream(java.io.InputStream) Resource(org.apache.tools.ant.types.Resource) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) URL(java.net.URL) ExtensionPoint(org.apache.tools.ant.ExtensionPoint) URLConnection(java.net.URLConnection) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) ZipFile(org.apache.tools.zip.ZipFile) SAXParseException(org.xml.sax.SAXParseException) FileProvider(org.apache.tools.ant.types.resources.FileProvider) BuildException(org.apache.tools.ant.BuildException) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File) XMLReader(org.xml.sax.XMLReader) Location(org.apache.tools.ant.Location)

Example 2 with URLProvider

use of org.apache.tools.ant.types.resources.URLProvider in project ant by apache.

the class TraXLiaison method resourceToURI.

private String resourceToURI(final Resource resource) {
    final FileProvider fp = resource.as(FileProvider.class);
    if (fp != null) {
        return FILE_UTILS.toURI(fp.getFile().getAbsolutePath());
    }
    final URLProvider up = resource.as(URLProvider.class);
    if (up != null) {
        final URL u = up.getURL();
        return String.valueOf(u);
    } else {
        return resource.getName();
    }
}
Also used : URLProvider(org.apache.tools.ant.types.resources.URLProvider) FileProvider(org.apache.tools.ant.types.resources.FileProvider) URL(java.net.URL)

Example 3 with URLProvider

use of org.apache.tools.ant.types.resources.URLProvider in project ant by apache.

the class Get method execute.

/**
 * Does the work.
 *
 * @exception BuildException Thrown in unrecoverable error.
 */
@Override
public void execute() throws BuildException {
    checkAttributes();
    for (final Resource r : sources) {
        final URLProvider up = r.as(URLProvider.class);
        final URL source = up.getURL();
        File dest = destination;
        if (destination.isDirectory()) {
            if (mapperElement == null) {
                String path = source.getPath();
                if (path.endsWith("/")) {
                    path = path.substring(0, path.length() - 1);
                }
                final int slash = path.lastIndexOf('/');
                if (slash > -1) {
                    path = path.substring(slash + 1);
                }
                dest = new File(destination, path);
            } else {
                final FileNameMapper mapper = mapperElement.getImplementation();
                final String[] d = mapper.mapFileName(source.toString());
                if (d == null) {
                    log("skipping " + r + " - mapper can't handle it", Project.MSG_WARN);
                    continue;
                }
                if (d.length == 0) {
                    log("skipping " + r + " - mapper returns no file name", Project.MSG_WARN);
                    continue;
                }
                if (d.length > 1) {
                    log("skipping " + r + " - mapper returns multiple file" + " names", Project.MSG_WARN);
                    continue;
                }
                dest = new File(destination, d[0]);
            }
        }
        // set up logging
        final int logLevel = Project.MSG_INFO;
        DownloadProgress progress = null;
        if (verbose) {
            progress = new VerboseProgress(System.out);
        }
        // execute the get
        try {
            doGet(source, dest, logLevel, progress);
        } catch (final IOException ioe) {
            log("Error getting " + source + " to " + dest);
            if (!ignoreErrors) {
                throw new BuildException(ioe, getLocation());
            }
        }
    }
}
Also used : URLProvider(org.apache.tools.ant.types.resources.URLProvider) Resource(org.apache.tools.ant.types.Resource) URLResource(org.apache.tools.ant.types.resources.URLResource) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) URL(java.net.URL)

Example 4 with URLProvider

use of org.apache.tools.ant.types.resources.URLProvider in project ant by apache.

the class Get method doGet.

/**
 * make a get request, with the supplied progress and logging info.
 * All the other config parameters are set at the task level,
 * source, dest, ignoreErrors, etc.
 * @param logLevel level to log at, see {@link Project#log(String, int)}
 * @param progress progress callback; null for no-callbacks
 * @return true for a successful download, false otherwise.
 * The return value is only relevant when {@link #ignoreErrors} is true, as
 * when false all failures raise BuildExceptions.
 * @throws IOException for network trouble
 * @throws BuildException for argument errors, or other trouble when ignoreErrors
 * is false.
 * @deprecated only gets the first configured resource
 */
@Deprecated
public boolean doGet(final int logLevel, final DownloadProgress progress) throws IOException {
    checkAttributes();
    for (final Resource r : sources) {
        final URLProvider up = r.as(URLProvider.class);
        final URL source = up.getURL();
        return doGet(source, destination, logLevel, progress);
    }
    /*NOTREACHED*/
    return false;
}
Also used : URLProvider(org.apache.tools.ant.types.resources.URLProvider) Resource(org.apache.tools.ant.types.Resource) URLResource(org.apache.tools.ant.types.resources.URLResource) URL(java.net.URL)

Aggregations

URL (java.net.URL)4 URLProvider (org.apache.tools.ant.types.resources.URLProvider)4 Resource (org.apache.tools.ant.types.Resource)3 File (java.io.File)2 IOException (java.io.IOException)2 BuildException (org.apache.tools.ant.BuildException)2 FileProvider (org.apache.tools.ant.types.resources.FileProvider)2 URLResource (org.apache.tools.ant.types.resources.URLResource)2 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URLConnection (java.net.URLConnection)1 ExtensionPoint (org.apache.tools.ant.ExtensionPoint)1 Location (org.apache.tools.ant.Location)1 FileNameMapper (org.apache.tools.ant.util.FileNameMapper)1 ZipFile (org.apache.tools.zip.ZipFile)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1 SAXParseException (org.xml.sax.SAXParseException)1 XMLReader (org.xml.sax.XMLReader)1