Search in sources :

Example 1 with Location

use of org.apache.tools.ant.Location in project felix by apache.

the class SCRDescriptorTask method execute.

@Override
public void execute() throws BuildException {
    // ensure we know the source
    if (getImplicitFileSet().getDir() == null) {
        throw new BuildException("srcdir attribute must be set!", getLocation());
    }
    // while debugging
    final org.apache.felix.scrplugin.Log scrLog = new AntLog(this);
    scrLog.debug("SCRDescriptorTask Configuration");
    scrLog.debug("  implicitFileset: " + getImplicitFileSet());
    scrLog.debug("  outputDirectory: " + destdir);
    scrLog.debug("  classpath: " + classpath);
    scrLog.debug("  generateAccessors: " + generateAccessors);
    scrLog.debug("  strictMode: " + strictMode);
    scrLog.debug("  specVersion: " + specVersion);
    try {
        final Path classPath = createClasspath();
        final org.apache.felix.scrplugin.Project project = new org.apache.felix.scrplugin.Project();
        project.setClassLoader(getClassLoader(this.getClass().getClassLoader()));
        project.setDependencies(getDependencies(classPath));
        project.setSources(getSourceFiles(getImplicitFileSet()));
        project.setClassesDirectory(destdir.getAbsolutePath());
        // create options
        final Options options = new Options();
        options.setOutputDirectory(destdir);
        options.setGenerateAccessors(generateAccessors);
        options.setStrictMode(strictMode);
        options.setProperties(new HashMap<String, String>());
        options.setSpecVersion(SpecVersion.fromName(specVersion));
        if (specVersion != null && options.getSpecVersion() == null) {
            throw new BuildException("Unknown spec version specified: " + specVersion);
        }
        final SCRDescriptorGenerator generator = new SCRDescriptorGenerator(scrLog);
        // setup from plugin configuration
        generator.setOptions(options);
        generator.setProject(project);
        generator.execute();
    } catch (final SCRDescriptorException sde) {
        if (sde.getSourceLocation() != null) {
            final Location loc = new Location(sde.getSourceLocation(), -1, 0);
            throw new BuildException(sde.getMessage(), sde.getCause(), loc);
        }
        throw new BuildException(sde.getMessage(), sde.getCause());
    } catch (SCRDescriptorFailureException sdfe) {
        throw new BuildException(sdfe.getMessage(), sdfe.getCause());
    }
}
Also used : Path(org.apache.tools.ant.types.Path) Options(org.apache.felix.scrplugin.Options) SCRDescriptorFailureException(org.apache.felix.scrplugin.SCRDescriptorFailureException) SCRDescriptorGenerator(org.apache.felix.scrplugin.SCRDescriptorGenerator) Project(org.apache.tools.ant.Project) BuildException(org.apache.tools.ant.BuildException) SCRDescriptorException(org.apache.felix.scrplugin.SCRDescriptorException) Location(org.apache.tools.ant.Location)

Example 2 with Location

use of org.apache.tools.ant.Location in project ant by apache.

the class AntXMLContext method setBuildFile.

/**
 * sets the build file to which the XML context belongs
 * @param buildFile  ant build file
 */
public void setBuildFile(File buildFile) {
    this.buildFile = buildFile;
    if (buildFile != null) {
        this.buildFileParent = new File(buildFile.getParent());
        implicitTarget.setLocation(new Location(buildFile.getAbsolutePath()));
        try {
            setBuildFile(FileUtils.getFileUtils().getFileURL(buildFile));
        } catch (MalformedURLException ex) {
            throw new BuildException(ex);
        }
    } else {
        this.buildFileParent = null;
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) Location(org.apache.tools.ant.Location)

Example 3 with Location

use of org.apache.tools.ant.Location in project ant by apache.

the class AntXMLContext method setBuildFile.

/**
 * sets the build file to which the XML context belongs
 * @param buildFile Ant build file
 * @throws MalformedURLException if parent URL cannot be constructed
 * @since Ant 1.8.0
 */
public void setBuildFile(URL buildFile) throws MalformedURLException {
    this.buildFileURL = buildFile;
    this.buildFileParentURL = new URL(buildFile, ".");
    if (implicitTarget.getLocation() == null) {
        implicitTarget.setLocation(new Location(buildFile.toString()));
    }
}
Also used : URL(java.net.URL) Location(org.apache.tools.ant.Location)

Example 4 with Location

use of org.apache.tools.ant.Location 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 5 with Location

use of org.apache.tools.ant.Location in project ant by apache.

the class ProjectHelperImpl method parse.

/**
 * Parses the project file, configuring the project as it goes.
 *
 * @param project project instance to be configured.
 * @param source the source from which the project is read.
 * @exception BuildException if the configuration is invalid or cannot
 *                           be read.
 */
public void parse(Project project, Object source) throws BuildException {
    if (!(source instanceof File)) {
        throw new BuildException("Only File source supported by " + "default plugin");
    }
    File bFile = (File) source;
    InputStream inputStream = null;
    InputSource inputSource = null;
    this.project = project;
    this.buildFile = new File(bFile.getAbsolutePath());
    buildFileParent = new File(this.buildFile.getParent());
    try {
        try {
            parser = JAXPUtils.getParser();
        } catch (BuildException e) {
            parser = new XMLReaderAdapter(JAXPUtils.getXMLReader());
        }
        String uri = FILE_UTILS.toURI(bFile.getAbsolutePath());
        inputStream = Files.newInputStream(bFile.toPath());
        inputSource = new InputSource(inputStream);
        inputSource.setSystemId(uri);
        project.log("parsing buildfile " + bFile + " with URI = " + uri, Project.MSG_VERBOSE);
        HandlerBase hb = new RootHandler(this);
        parser.setDocumentHandler(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, location);
    } catch (SAXException exc) {
        Throwable t = exc.getException();
        if (t instanceof BuildException) {
            throw (BuildException) t;
        }
        throw new BuildException(exc.getMessage(), t);
    } catch (FileNotFoundException exc) {
        throw new BuildException(exc);
    } catch (UnsupportedEncodingException exc) {
        throw new BuildException("Encoding of project file is invalid.", exc);
    } catch (IOException exc) {
        throw new BuildException("Error reading project file: " + exc.getMessage(), exc);
    } finally {
        FileUtils.close(inputStream);
    }
}
Also used : InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException) HandlerBase(org.xml.sax.HandlerBase) XMLReaderAdapter(org.xml.sax.helpers.XMLReaderAdapter) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) Location(org.apache.tools.ant.Location)

Aggregations

Location (org.apache.tools.ant.Location)7 BuildException (org.apache.tools.ant.BuildException)4 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URL (java.net.URL)2 Project (org.apache.tools.ant.Project)2 InputSource (org.xml.sax.InputSource)2 SAXException (org.xml.sax.SAXException)2 SAXParseException (org.xml.sax.SAXParseException)2 MalformedURLException (java.net.MalformedURLException)1 URLConnection (java.net.URLConnection)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Options (org.apache.felix.scrplugin.Options)1 SCRDescriptorException (org.apache.felix.scrplugin.SCRDescriptorException)1 SCRDescriptorFailureException (org.apache.felix.scrplugin.SCRDescriptorFailureException)1 SCRDescriptorGenerator (org.apache.felix.scrplugin.SCRDescriptorGenerator)1