Search in sources :

Example 26 with FileResource

use of org.apache.tools.ant.types.resources.FileResource in project lucene-solr by apache.

the class LibVersionsCheckTask method execute.

/**
   * Execute the task.
   */
@Override
public void execute() throws BuildException {
    log("Starting scan.", verboseLevel);
    long start = System.currentTimeMillis();
    setupIvy();
    int numErrors = 0;
    if (!verifySortedCoordinatesPropertiesFile(centralizedVersionsFile)) {
        ++numErrors;
    }
    if (!verifySortedCoordinatesPropertiesFile(ignoreConflictsFile)) {
        ++numErrors;
    }
    collectDirectDependencies();
    if (!collectVersionConflictsToIgnore()) {
        ++numErrors;
    }
    int numChecked = 0;
    @SuppressWarnings("unchecked") Iterator<Resource> iter = (Iterator<Resource>) ivyXmlResources.iterator();
    while (iter.hasNext()) {
        final Resource resource = iter.next();
        if (!resource.isExists()) {
            throw new BuildException("Resource does not exist: " + resource.getName());
        }
        if (!(resource instanceof FileResource)) {
            throw new BuildException("Only filesystem resources are supported: " + resource.getName() + ", was: " + resource.getClass().getName());
        }
        File ivyXmlFile = ((FileResource) resource).getFile();
        try {
            if (!checkIvyXmlFile(ivyXmlFile)) {
                ++numErrors;
            }
            if (!resolveTransitively(ivyXmlFile)) {
                ++numErrors;
            }
            if (!findLatestConflictVersions()) {
                ++numErrors;
            }
        } catch (Exception e) {
            throw new BuildException("Exception reading file " + ivyXmlFile.getPath() + " - " + e.toString(), e);
        }
        ++numChecked;
    }
    log("Checking for orphans in " + centralizedVersionsFile.getName(), verboseLevel);
    for (Map.Entry<String, Dependency> entry : directDependencies.entrySet()) {
        String coordinateKey = entry.getKey();
        if (!entry.getValue().directlyReferenced) {
            log("ORPHAN coordinate key '" + coordinateKey + "' in " + centralizedVersionsFile.getName() + " is not found in any " + IVY_XML_FILENAME + " file.", Project.MSG_ERR);
            ++numErrors;
        }
    }
    int numConflicts = emitConflicts();
    int messageLevel = numErrors > 0 ? Project.MSG_ERR : Project.MSG_INFO;
    log("Checked that " + centralizedVersionsFile.getName() + " and " + ignoreConflictsFile.getName() + " have lexically sorted '/org/name' keys and no duplicates or orphans.", messageLevel);
    log("Scanned " + numChecked + " " + IVY_XML_FILENAME + " files for rev=\"${/org/name}\" format.", messageLevel);
    log("Found " + numConflicts + " indirect dependency version conflicts.");
    log(String.format(Locale.ROOT, "Completed in %.2fs., %d error(s).", (System.currentTimeMillis() - start) / 1000.0, numErrors), messageLevel);
    if (numConflicts > 0 || numErrors > 0) {
        throw new BuildException("Lib versions check failed. Check the logs.");
    }
}
Also used : FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) FileResource(org.apache.tools.ant.types.resources.FileResource) ParseException(java.text.ParseException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Iterator(java.util.Iterator) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 27 with FileResource

use of org.apache.tools.ant.types.resources.FileResource in project lucene-solr by apache.

the class LicenseCheckTask method processJars.

/**
   * Process all JARs.
   */
private void processJars() {
    log("Starting scan.", verboseLevel);
    long start = System.currentTimeMillis();
    @SuppressWarnings("unchecked") Iterator<Resource> iter = (Iterator<Resource>) jarResources.iterator();
    int checked = 0;
    int errors = 0;
    while (iter.hasNext()) {
        final Resource r = iter.next();
        if (!r.isExists()) {
            throw new BuildException("JAR resource does not exist: " + r.getName());
        }
        if (!(r instanceof FileResource)) {
            throw new BuildException("Only filesystem resource are supported: " + r.getName() + ", was: " + r.getClass().getName());
        }
        File jarFile = ((FileResource) r).getFile();
        if (!checkJarFile(jarFile)) {
            errors++;
        }
        checked++;
    }
    log(String.format(Locale.ROOT, "Scanned %d JAR file(s) for licenses (in %.2fs.), %d error(s).", checked, (System.currentTimeMillis() - start) / 1000.0, errors), errors > 0 ? Project.MSG_ERR : Project.MSG_INFO);
}
Also used : Iterator(java.util.Iterator) 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)

Example 28 with FileResource

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

the class SCRDescriptorTask method getSourceFiles.

protected Collection<Source> getSourceFiles(final FileSet sourceFiles) {
    final String prefix = sourceFiles.getDir().getAbsolutePath();
    final int prefixLength = prefix.length() + 1;
    final List<Source> result = new ArrayList<Source>();
    @SuppressWarnings("unchecked") final Iterator<Resource> resources = sourceFiles.iterator();
    final String ext;
    if (scanClasses) {
        ext = ".class";
    } else {
        ext = ".java";
    }
    while (resources.hasNext()) {
        final Resource r = resources.next();
        if (r instanceof FileResource) {
            final File file = ((FileResource) r).getFile();
            if (file.getName().endsWith(ext)) {
                result.add(new Source() {

                    public File getFile() {
                        return file;
                    }

                    public String getClassName() {
                        String name = file.getAbsolutePath().substring(prefixLength).replace(File.separatorChar, '/').replace('/', '.');
                        return name.substring(0, name.length() - ext.length());
                    }
                });
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Resource(org.apache.tools.ant.types.Resource) FileResource(org.apache.tools.ant.types.resources.FileResource) FileResource(org.apache.tools.ant.types.resources.FileResource) File(java.io.File) Source(org.apache.felix.scrplugin.Source)

Example 29 with FileResource

use of org.apache.tools.ant.types.resources.FileResource in project ph-schematron by phax.

the class Schematron method _performValidation.

private void _performValidation(@Nonnull final ISchematronResource aSch, @Nonnull final ICommonsList<ResourceCollection> aResCollections, @Nullable final File aSVRLDirectory, final boolean bExpectSuccess) throws BuildException {
    // Resolve resourceCollections - pain in the ass
    final ICommonsMap<File, DirectoryData> aFiles = new CommonsHashMap<>();
    for (final ResourceCollection aResCollection : aResCollections) {
        if (!aResCollection.isFilesystemOnly())
            _error("Only FileSystem resources are supported.");
        else
            for (final Resource aRes : aResCollection) {
                if (!aRes.isExists()) {
                    _error("Could not find resource " + aRes.toLongString() + " to copy.");
                    continue;
                }
                File baseDir = NULL_FILE_PLACEHOLDER;
                String name = aRes.getName();
                final FileProvider fp = aRes.as(FileProvider.class);
                if (fp != null) {
                    final FileResource fr = ResourceUtils.asFileResource(fp);
                    baseDir = _getKeyFile(fr.getBaseDir());
                    if (baseDir == NULL_FILE_PLACEHOLDER)
                        name = fr.getFile().getAbsolutePath();
                }
                if ((aRes.isDirectory() || fp != null) && name != null) {
                    final DirectoryData aBaseDir = aFiles.computeIfAbsent(_getKeyFile(baseDir), k -> new DirectoryData(k));
                    if (aRes.isDirectory())
                        aBaseDir.addDir(name);
                    else
                        aBaseDir.addFile(name);
                } else
                    _error("Could not resolve resource " + aRes.toLongString() + " to a file.");
            }
    }
    for (final DirectoryData aBaseDir : aFiles.values()) {
        log("Scanning directory " + aBaseDir.getBaseDir() + " for XMLs to be Schematron validated", Project.MSG_DEBUG);
        final ICommonsList<String> aIncludes = new CommonsArrayList<>();
        aIncludes.addAll(aBaseDir.getFiles());
        for (final String sFile : aBaseDir.getDirs()) aIncludes.add(sFile + "/**");
        final DirectoryScanner aScanner = new DirectoryScanner();
        aScanner.setBasedir(aBaseDir.getBaseDir());
        if (aIncludes.isNotEmpty())
            aScanner.setIncludes(aIncludes.toArray(new String[0]));
        aScanner.setCaseSensitive(true);
        aScanner.scan();
        final String[] aXMLFilenames = aScanner.getIncludedFiles();
        if (aXMLFilenames != null) {
            for (final String sXMLFilename : aXMLFilenames) {
                final File aXMLFile = new File(aBaseDir.getBaseDir(), sXMLFilename);
                // Validate XML file
                log("Validating XML file '" + aXMLFile.getPath() + "' against Schematron rules from '" + m_aSchematronFile.getName() + "' expecting " + (bExpectSuccess ? "success" : "failure"), Project.MSG_INFO);
                try {
                    final SchematronOutputType aSOT = aSch.applySchematronValidationToSVRL(TransformSourceFactory.create(aXMLFile));
                    if (aSVRLDirectory != null) {
                        // Save SVRL
                        final File aSVRLFile = new File(aSVRLDirectory, sXMLFilename + ".svrl");
                        if (!aSVRLFile.getParentFile().mkdirs())
                            log("Failed to create parent directory of '" + aSVRLFile.getAbsolutePath() + "'!", Project.MSG_ERR);
                        if (new SVRLMarshaller().write(aSOT, aSVRLFile).isSuccess())
                            log("Successfully saved SVRL file '" + aSVRLFile.getPath() + "'", Project.MSG_INFO);
                        else
                            log("Error saving SVRL file '" + aSVRLFile.getPath() + "'", Project.MSG_ERR);
                    }
                    if (false)
                        System.out.println(new SVRLMarshaller().getAsString(aSOT));
                    final ICommonsList<AbstractSVRLMessage> aMessages = SVRLHelper.getAllFailedAssertionsAndSuccessfulReports(aSOT);
                    final int nErrorMessages = aMessages.getCount(x -> x.getFlag().isError());
                    final int nWarningMessages = aMessages.size() - nErrorMessages;
                    final String sErrors = nErrorMessages + " Schematron error" + (nErrorMessages == 1 ? "" : "s");
                    final String sWarnings = nWarningMessages + " Schematron warning" + (nWarningMessages == 1 ? "" : "s");
                    if (bExpectSuccess) {
                        // No failed assertions expected
                        if (nErrorMessages > 0) {
                            final StringBuilder aMessage = new StringBuilder();
                            aMessage.append(sErrors + (nWarningMessages > 0 ? " and " + sWarnings : "") + " for XML file '" + aXMLFile.getPath() + "'");
                            for (final AbstractSVRLMessage aMsg : aMessages) {
                                aMessage.append("\n  ").append(ErrorTextProvider.DEFAULT.getErrorText(aMsg.getAsResourceError(aXMLFile.getPath()), Locale.US));
                            }
                            // As at least one error is contained, it's okay to throw an
                            // exception in case
                            _error(aMessage.toString());
                            continue;
                        }
                        // Success as expected
                        log("XML file '" + aXMLFile.getPath() + "' was validated against Schematron '" + aSch.getResource().getPath() + "' and matches the rules" + (nWarningMessages > 0 ? " - only " + sWarnings + " are contained" : ""), Project.MSG_INFO);
                    } else {
                        // At least one failed assertions expected
                        if (nErrorMessages == 0) {
                            String sMessage = "No Schematron errors for erroneous XML file '" + aXMLFile.getPath() + "'";
                            if (nWarningMessages > 0)
                                sMessage += " - only " + sWarnings + " are contained";
                            _error(sMessage);
                            continue;
                        }
                        // Success as expected
                        log("XML file '" + aXMLFile.getPath() + "' was validated against Schematron '" + aSch.getResource().getPath() + "' " + sErrors + (nWarningMessages > 0 ? " and " + sWarnings : "") + " were found (as expected)", Project.MSG_INFO);
                    }
                } catch (final BuildException up) {
                    throw up;
                } catch (final Exception ex) {
                    final String sMessage = "Exception validating XML '" + aXMLFile.getPath() + "' against Schematron rules from '" + m_aSchematronFile.getName() + "'. Technical details: " + ex.getClass().getSimpleName() + " - " + ex.getMessage();
                    _error(sMessage, ex);
                    continue;
                }
            }
        }
    }
}
Also used : OverrideOnDemand(com.helger.commons.annotation.OverrideOnDemand) Task(org.apache.tools.ant.Task) IError(com.helger.commons.error.IError) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) URIResolver(javax.xml.transform.URIResolver) ResourceCollection(org.apache.tools.ant.types.ResourceCollection) FileResource(org.apache.tools.ant.types.resources.FileResource) SchematronResourceXSLT(com.helger.schematron.xslt.SchematronResourceXSLT) AbstractSVRLMessage(com.helger.schematron.svrl.AbstractSVRLMessage) SVRLHelper(com.helger.schematron.svrl.SVRLHelper) ESchematronMode(com.helger.schematron.ESchematronMode) Locale(java.util.Locale) Project(org.apache.tools.ant.Project) CollectingTransformErrorListener(com.helger.xml.transform.CollectingTransformErrorListener) Nonnull(javax.annotation.Nonnull) TransformSourceFactory(com.helger.xml.transform.TransformSourceFactory) Nullable(javax.annotation.Nullable) EntityResolver(org.xml.sax.EntityResolver) ErrorTextProvider(com.helger.commons.error.ErrorTextProvider) Resource(org.apache.tools.ant.types.Resource) EErrorLevel(com.helger.commons.error.level.EErrorLevel) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) StringHelper(com.helger.commons.string.StringHelper) SVRLMarshaller(com.helger.schematron.svrl.SVRLMarshaller) CollectingPSErrorHandler(com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler) BuildException(org.apache.tools.ant.BuildException) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) File(java.io.File) SchematronOutputType(org.oclc.purl.dsdl.svrl.SchematronOutputType) XMLCatalog(org.apache.tools.ant.types.XMLCatalog) ISchematronResource(com.helger.schematron.ISchematronResource) CommonsHashMap(com.helger.commons.collection.impl.CommonsHashMap) ICommonsList(com.helger.commons.collection.impl.ICommonsList) SchematronResourceSCH(com.helger.schematron.xslt.SchematronResourceSCH) FileProvider(org.apache.tools.ant.types.resources.FileProvider) ICommonsMap(com.helger.commons.collection.impl.ICommonsMap) SchematronResourcePure(com.helger.schematron.pure.SchematronResourcePure) IErrorList(com.helger.commons.error.list.IErrorList) ResourceUtils(org.apache.tools.ant.util.ResourceUtils) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) ISchematronResource(com.helger.schematron.ISchematronResource) FileResource(org.apache.tools.ant.types.resources.FileResource) SVRLMarshaller(com.helger.schematron.svrl.SVRLMarshaller) AbstractSVRLMessage(com.helger.schematron.svrl.AbstractSVRLMessage) BuildException(org.apache.tools.ant.BuildException) SchematronOutputType(org.oclc.purl.dsdl.svrl.SchematronOutputType) CommonsHashMap(com.helger.commons.collection.impl.CommonsHashMap) FileProvider(org.apache.tools.ant.types.resources.FileProvider) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) ResourceCollection(org.apache.tools.ant.types.ResourceCollection)

Example 30 with FileResource

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

the class ProjectHelper method configureProject.

/**
 * Configures the project with the contents of the specified build file.
 *
 * @param project The project to configure. Must not be <code>null</code>.
 * @param buildFile A build file giving the project's configuration.
 *                  Must not be <code>null</code>.
 *
 * @exception BuildException if the configuration is invalid or cannot be read
 */
public static void configureProject(Project project, File buildFile) throws BuildException {
    FileResource resource = new FileResource(buildFile);
    ProjectHelper helper = ProjectHelperRepository.getInstance().getProjectHelperForBuildFile(resource);
    project.addReference(PROJECTHELPER_REFERENCE, helper);
    helper.parse(project, buildFile);
}
Also used : FileResource(org.apache.tools.ant.types.resources.FileResource)

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