Search in sources :

Example 6 with ResourceCollection

use of org.apache.tools.ant.types.ResourceCollection in project ant by apache.

the class Scp method upload.

private void upload(final List<ResourceCollection> rcs, final String toSshUri) throws IOException, JSchException {
    final String file = parseUri(toSshUri);
    Session session = null;
    try {
        final List<Directory> list = new ArrayList<>(rcs.size());
        for (ResourceCollection rc : rcs) {
            if (rc instanceof FileSet && rc.isFilesystemOnly()) {
                FileSet fs = (FileSet) rc;
                final Directory d = createDirectory(fs);
                if (d != null) {
                    list.add(d);
                }
            } else {
                List<Directory> ds = createDirectoryCollection(rc);
                if (ds != null) {
                    list.addAll(ds);
                }
            }
        }
        if (!list.isEmpty()) {
            session = openSession();
            ScpToMessage message;
            if (!isSftp) {
                message = new ScpToMessage(getVerbose(), compressed, session, list, file, preserveLastModified);
            } else {
                message = new ScpToMessageBySftp(getVerbose(), session, list, file, preserveLastModified);
            }
            message.setLogListener(this);
            if (fileMode != null) {
                message.setFileMode(fileMode);
            }
            if (dirMode != null) {
                message.setDirMode(dirMode);
            }
            message.execute();
        }
    } finally {
        if (session != null) {
            session.disconnect();
        }
    }
}
Also used : FileSet(org.apache.tools.ant.types.FileSet) ArrayList(java.util.ArrayList) Session(com.jcraft.jsch.Session) ResourceCollection(org.apache.tools.ant.types.ResourceCollection)

Example 7 with ResourceCollection

use of org.apache.tools.ant.types.ResourceCollection in project ant by apache.

the class Tokens method getCollection.

/**
 * Sort the contained elements.
 * @return a Collection of Resources.
 */
protected synchronized Collection<Resource> getCollection() {
    ResourceCollection rc = getResourceCollection();
    if (rc.isEmpty()) {
        return Collections.emptySet();
    }
    if (tokenizer == null) {
        tokenizer = new LineTokenizer();
    }
    try (ConcatResourceInputStream cat = new ConcatResourceInputStream(rc);
        InputStreamReader rdr = new InputStreamReader(cat, encoding == null ? Charset.defaultCharset() : Charset.forName(encoding))) {
        cat.setManagingComponent(this);
        List<Resource> result = new ArrayList<>();
        for (String s = tokenizer.getToken(rdr); s != null; s = tokenizer.getToken(rdr)) {
            // do not send the Project to the constructor of StringResource, since
            // the semantics of that constructor clearly state that property value
            // replacement takes place on the passed string value. We don't want
            // that to happen.
            final StringResource resource = new StringResource(s);
            resource.setProject(getProject());
            result.add(resource);
        }
        return result;
    } catch (IOException e) {
        throw new BuildException("Error reading tokens", e);
    }
}
Also used : ConcatResourceInputStream(org.apache.tools.ant.util.ConcatResourceInputStream) InputStreamReader(java.io.InputStreamReader) LineTokenizer(org.apache.tools.ant.util.LineTokenizer) Resource(org.apache.tools.ant.types.Resource) ArrayList(java.util.ArrayList) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) ResourceCollection(org.apache.tools.ant.types.ResourceCollection)

Example 8 with ResourceCollection

use of org.apache.tools.ant.types.ResourceCollection in project ant by apache.

the class Zip method getResourcesToAdd.

/**
 * Collect the resources that are newer than the corresponding
 * entries (or missing) in the original archive.
 *
 * <p>If we are going to recreate the archive instead of updating
 * it, all resources should be considered as new, if a single one
 * is.  Because of this, subclasses overriding this method must
 * call <code>super.getResourcesToAdd</code> and indicate with the
 * third arg if they already know that the archive is
 * out-of-date.</p>
 *
 * <p>This method first delegates to getNonFileSetResourcesToAdd
 * and then invokes the FileSet-arg version.  All this to keep
 * backwards compatibility for subclasses that don't know how to
 * deal with non-FileSet ResourceCollections.</p>
 *
 * @param rcs The resource collections to grab resources from
 * @param zipFile intended archive file (may or may not exist)
 * @param needsUpdate whether we already know that the archive is
 * out-of-date.  Subclasses overriding this method are supposed to
 * set this value correctly in their call to
 * <code>super.getResourcesToAdd</code>.
 * @return an array of resources to add for each fileset passed in as well
 *         as a flag that indicates whether the archive is uptodate.
 *
 * @exception BuildException if it likes
 * @since Ant 1.7
 */
protected ArchiveState getResourcesToAdd(final ResourceCollection[] rcs, final File zipFile, final boolean needsUpdate) throws BuildException {
    final List<FileSet> filesets = new ArrayList<>();
    final List<ResourceCollection> rest = new ArrayList<>();
    for (ResourceCollection rc : rcs) {
        if (rc instanceof FileSet) {
            filesets.add((FileSet) rc);
        } else {
            rest.add(rc);
        }
    }
    final ResourceCollection[] rc = rest.toArray(new ResourceCollection[rest.size()]);
    ArchiveState as = getNonFileSetResourcesToAdd(rc, zipFile, needsUpdate);
    final FileSet[] fs = filesets.toArray(new FileSet[filesets.size()]);
    final ArchiveState as2 = getResourcesToAdd(fs, zipFile, as.isOutOfDate());
    if (!as.isOutOfDate() && as2.isOutOfDate()) {
        /*
             * Bad luck.
             *
             * There are resources in the filesets that make the
             * archive out of date, but not in the non-fileset
             * resources. We need to rescan the non-FileSets to grab
             * all of them now.
             */
        as = getNonFileSetResourcesToAdd(rc, zipFile, true);
    }
    final Resource[][] toAdd = new Resource[rcs.length][];
    int fsIndex = 0;
    int restIndex = 0;
    for (int i = 0; i < rcs.length; i++) {
        if (rcs[i] instanceof FileSet) {
            toAdd[i] = as2.getResourcesToAdd()[fsIndex++];
        } else {
            toAdd[i] = as.getResourcesToAdd()[restIndex++];
        }
    }
    return new ArchiveState(as2.isOutOfDate(), toAdd);
}
Also used : ArchiveFileSet(org.apache.tools.ant.types.ArchiveFileSet) FileSet(org.apache.tools.ant.types.FileSet) ZipFileSet(org.apache.tools.ant.types.ZipFileSet) ArrayList(java.util.ArrayList) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) ArchiveResource(org.apache.tools.ant.types.resources.ArchiveResource) ZipResource(org.apache.tools.ant.types.resources.ZipResource) ResourceCollection(org.apache.tools.ant.types.ResourceCollection)

Example 9 with ResourceCollection

use of org.apache.tools.ant.types.ResourceCollection in project randomizedtesting by randomizedtesting.

the class ExecutionTimesReport method mergeHints.

/**
 * Read hints from all resources in a collection, retaining
 * <code>suiteNames</code>. If <code>suiteNames</code> is null,
 * everything is retained.
 */
public static Map<String, List<Long>> mergeHints(Collection<ResourceCollection> resources, Collection<String> suiteNames) {
    final Map<String, List<Long>> hints = new HashMap<>();
    for (ResourceCollection rc : resources) {
        Iterator<Resource> i = rc.iterator();
        while (i.hasNext()) {
            InputStream is = null;
            Resource r = i.next();
            try {
                is = r.getInputStream();
                mergeHints(is, hints);
                // Early prune the hints to those we have on the list.
                if (suiteNames != null) {
                    hints.keySet().retainAll(suiteNames);
                }
            } catch (IOException e) {
                throw new BuildException("Could not read hints from resource: " + r.getDescription(), e);
            } finally {
                try {
                    if (is != null)
                        is.close();
                } catch (IOException e) {
                    throw new BuildException("Could not close hints file: " + r.getDescription());
                }
            }
        }
    }
    return hints;
}
Also used : HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Resource(org.apache.tools.ant.types.Resource) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) ResourceCollection(org.apache.tools.ant.types.ResourceCollection)

Example 10 with ResourceCollection

use of org.apache.tools.ant.types.ResourceCollection 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)

Aggregations

ResourceCollection (org.apache.tools.ant.types.ResourceCollection)21 Resource (org.apache.tools.ant.types.Resource)16 BuildException (org.apache.tools.ant.BuildException)15 ArrayList (java.util.ArrayList)9 FileResource (org.apache.tools.ant.types.resources.FileResource)8 File (java.io.File)7 IOException (java.io.IOException)6 FileSet (org.apache.tools.ant.types.FileSet)6 FileProvider (org.apache.tools.ant.types.resources.FileProvider)5 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)4 ZipResource (org.apache.tools.ant.types.resources.ZipResource)4 ArchiveResource (org.apache.tools.ant.types.resources.ArchiveResource)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Union (org.apache.tools.ant.types.resources.Union)2 ZipFile (org.apache.tools.zip.ZipFile)2 OverrideOnDemand (com.helger.commons.annotation.OverrideOnDemand)1 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 CommonsHashMap (com.helger.commons.collection.impl.CommonsHashMap)1