Search in sources :

Example 11 with FileProvider

use of org.apache.tools.ant.types.resources.FileProvider 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 12 with FileProvider

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

the class SQLExec method execute.

/**
 * Load the sql file and then execute it
 * @throws BuildException on error.
 */
@Override
public void execute() throws BuildException {
    List<Transaction> savedTransaction = new Vector<>(transactions);
    String savedSqlCommand = sqlCommand;
    sqlCommand = sqlCommand.trim();
    try {
        if (srcFile == null && sqlCommand.isEmpty() && resources == null) {
            if (transactions.isEmpty()) {
                throw new BuildException("Source file or resource collection, transactions or sql statement must be set!", getLocation());
            }
        }
        if (srcFile != null && !srcFile.isFile()) {
            throw new BuildException("Source file " + srcFile + " is not a file!", getLocation());
        }
        if (resources != null) {
            // deal with the resources
            for (Resource r : resources) {
                // Make a transaction for each resource
                Transaction t = createTransaction();
                t.setSrcResource(r);
            }
        }
        // Make a transaction group for the outer command
        Transaction t = createTransaction();
        t.setSrc(srcFile);
        t.addText(sqlCommand);
        if (getConnection() == null) {
            // not a valid rdbms
            return;
        }
        try {
            PrintStream out = KeepAliveOutputStream.wrapSystemOut();
            try {
                if (output != null) {
                    log("Opening PrintStream to output Resource " + output, Project.MSG_VERBOSE);
                    OutputStream os = null;
                    FileProvider fp = output.as(FileProvider.class);
                    if (fp != null) {
                        os = FileUtils.newOutputStream(fp.getFile().toPath(), append);
                    } else {
                        if (append) {
                            Appendable a = output.as(Appendable.class);
                            if (a != null) {
                                os = a.getAppendOutputStream();
                            }
                        }
                        if (os == null) {
                            os = output.getOutputStream();
                            if (append) {
                                log("Ignoring append=true for non-appendable" + " resource " + output, Project.MSG_WARN);
                            }
                        }
                    }
                    if (outputEncoding != null) {
                        out = new PrintStream(new BufferedOutputStream(os), false, outputEncoding);
                    } else {
                        out = new PrintStream(new BufferedOutputStream(os));
                    }
                }
                // Process all transactions
                for (Transaction txn : transactions) {
                    txn.runTransaction(out);
                    if (!isAutocommit()) {
                        log("Committing transaction", Project.MSG_VERBOSE);
                        getConnection().commit();
                    }
                }
            } finally {
                FileUtils.close(out);
            }
        } catch (IOException | SQLException e) {
            closeQuietly();
            setErrorProperty();
            if ("abort".equals(onError)) {
                throw new BuildException(e, getLocation());
            }
        } finally {
            try {
                FileUtils.close(getStatement());
            } catch (SQLException ex) {
            // ignore
            }
            FileUtils.close(getConnection());
        }
        log(goodSql + " of " + totalSql + " SQL statements executed successfully");
    } finally {
        transactions = savedTransaction;
        sqlCommand = savedSqlCommand;
    }
}
Also used : PrintStream(java.io.PrintStream) SQLException(java.sql.SQLException) KeepAliveOutputStream(org.apache.tools.ant.util.KeepAliveOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) Appendable(org.apache.tools.ant.types.resources.Appendable) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) IOException(java.io.IOException) FileProvider(org.apache.tools.ant.types.resources.FileProvider) BuildException(org.apache.tools.ant.BuildException) Vector(java.util.Vector) BufferedOutputStream(java.io.BufferedOutputStream)

Example 13 with FileProvider

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

the class Copy method validateAttributes.

/**
 * Ensure we have a consistent and legal set of attributes, and set
 * any internal flags necessary based on different combinations
 * of attributes.
 * @exception BuildException if an error occurs.
 */
protected void validateAttributes() throws BuildException {
    if (file == null && rcs.isEmpty()) {
        throw new BuildException("Specify at least one source--a file or a resource collection.");
    }
    if (destFile != null && destDir != null) {
        throw new BuildException("Only one of tofile and todir may be set.");
    }
    if (destFile == null && destDir == null) {
        throw new BuildException("One of tofile or todir must be set.");
    }
    if (file != null && file.isDirectory()) {
        throw new BuildException("Use a resource collection to copy directories.");
    }
    if (destFile != null && !rcs.isEmpty()) {
        if (rcs.size() > 1) {
            throw new BuildException("Cannot concatenate multiple files into a single file.");
        }
        final ResourceCollection rc = rcs.elementAt(0);
        if (!rc.isFilesystemOnly() && !supportsNonFileResources()) {
            throw new BuildException("Only FileSystem resources are supported.");
        }
        if (rc.isEmpty()) {
            throw new BuildException(MSG_WHEN_COPYING_EMPTY_RC_TO_FILE);
        }
        if (rc.size() == 1) {
            final Resource res = rc.iterator().next();
            final FileProvider r = res.as(FileProvider.class);
            if (file == null) {
                if (r != null) {
                    file = r.getFile();
                } else {
                    singleResource = res;
                }
                rcs.removeElementAt(0);
            } else {
                throw new BuildException("Cannot concatenate multiple files into a single file.");
            }
        } else {
            throw new BuildException("Cannot concatenate multiple files into a single file.");
        }
    }
    if (destFile != null) {
        destDir = destFile.getParentFile();
    }
}
Also used : FileProvider(org.apache.tools.ant.types.resources.FileProvider) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) BuildException(org.apache.tools.ant.BuildException) ResourceCollection(org.apache.tools.ant.types.ResourceCollection)

Example 14 with FileProvider

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

the class Pack method setSrcResource.

/**
 * The resource to pack; required.
 * @param src resource to expand
 */
public void setSrcResource(Resource src) {
    if (src.isDirectory()) {
        throw new BuildException("the source can't be a directory");
    }
    FileProvider fp = src.as(FileProvider.class);
    if (fp != null) {
        source = fp.getFile();
    } else if (!supportsNonFileResources()) {
        throw new BuildException("Only FileSystem resources are supported.");
    }
    this.src = src;
}
Also used : FileProvider(org.apache.tools.ant.types.resources.FileProvider) BuildException(org.apache.tools.ant.BuildException)

Example 15 with FileProvider

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

the class PermissionUtils method getPermissions.

/**
 * Sets permissions of a {@link Resource} - returns an empty set
 * for unsupported resource types or file systems that don't
 * support PosixFilePermissions and no fallback has been
 * provided..
 *
 * <p>Supported types are:</p>
 * <ul>
 *  <li>any {@link FileProvider}</li>
 *  <li>{@link ArchiveResource}</li>
 * </ul>
 *
 * @param r the resource to read permissions from
 * @param posixNotSupportedFallback optional fallback function to provide
 * permissions for file system that don't support
 * PosixFilePermissions. The Path corresponding to the file is
 * passed to the callback.
 * @return the permissions
 * @throws IOException if something goes wrong
 */
public static Set<PosixFilePermission> getPermissions(Resource r, Function<Path, Set<PosixFilePermission>> posixNotSupportedFallback) throws IOException {
    FileProvider f = r.as(FileProvider.class);
    if (f != null) {
        Path p = f.getFile().toPath();
        PosixFileAttributeView view = Files.getFileAttributeView(p, PosixFileAttributeView.class);
        if (view != null) {
            return view.readAttributes().permissions();
        } else if (posixNotSupportedFallback != null) {
            return posixNotSupportedFallback.apply(p);
        }
    } else if (r instanceof ArchiveResource) {
        return permissionsFromMode(((ArchiveResource) r).getMode());
    }
    return EnumSet.noneOf(PosixFilePermission.class);
}
Also used : Path(java.nio.file.Path) FileProvider(org.apache.tools.ant.types.resources.FileProvider) ArchiveResource(org.apache.tools.ant.types.resources.ArchiveResource) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Aggregations

FileProvider (org.apache.tools.ant.types.resources.FileProvider)22 BuildException (org.apache.tools.ant.BuildException)13 Resource (org.apache.tools.ant.types.Resource)13 FileResource (org.apache.tools.ant.types.resources.FileResource)12 File (java.io.File)8 IOException (java.io.IOException)4 ArchiveResource (org.apache.tools.ant.types.resources.ArchiveResource)4 ResourceCollection (org.apache.tools.ant.types.ResourceCollection)3 URL (java.net.URL)2 Path (java.nio.file.Path)2 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)2 ArrayList (java.util.ArrayList)2 Vector (java.util.Vector)2 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)2 FileSet (org.apache.tools.ant.types.FileSet)2 URLProvider (org.apache.tools.ant.types.resources.URLProvider)2 ZipResource (org.apache.tools.ant.types.resources.ZipResource)2 ZipFile (org.apache.tools.zip.ZipFile)2 OverrideOnDemand (com.helger.commons.annotation.OverrideOnDemand)1 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1