Search in sources :

Example 21 with FileNameMapper

use of org.apache.tools.ant.util.FileNameMapper in project ant by apache.

the class XSLTProcess method process.

/**
 * Processes the given input XML file and stores the result
 * in the given resultFile.
 *
 * @param baseDir the base directory for resolving files.
 * @param xmlFile the input file
 * @param destDir the destination directory
 * @param stylesheet the stylesheet to use.
 * @exception BuildException if the processing fails.
 */
private void process(final File baseDir, final String xmlFile, final File destDir, final Resource stylesheet) throws BuildException {
    File outF = null;
    try {
        final long styleSheetLastModified = stylesheet.getLastModified();
        File inF = new File(baseDir, xmlFile);
        if (inF.isDirectory()) {
            log("Skipping " + inF + " it is a directory.", Project.MSG_VERBOSE);
            return;
        }
        FileNameMapper mapper = mapperElement == null ? new StyleMapper() : mapperElement.getImplementation();
        final String[] outFileName = mapper.mapFileName(xmlFile);
        if (outFileName == null || outFileName.length == 0) {
            log("Skipping " + inFile + " it cannot get mapped to output.", Project.MSG_VERBOSE);
            return;
        }
        if (outFileName.length > 1) {
            log("Skipping " + inFile + " its mapping is ambiguous.", Project.MSG_VERBOSE);
            return;
        }
        outF = new File(destDir, outFileName[0]);
        if (force || inF.lastModified() > outF.lastModified() || styleSheetLastModified > outF.lastModified()) {
            ensureDirectoryFor(outF);
            log("Processing " + inF + " to " + outF);
            configureLiaison(stylesheet);
            setLiaisonDynamicFileParameters(liaison, inF);
            liaison.transform(inF, outF);
        }
    } catch (final Exception ex) {
        // If failed to process document, must delete target document,
        // or it will not attempt to process it the second time
        log("Failed to process " + inFile, Project.MSG_INFO);
        if (outF != null) {
            outF.delete();
        }
        handleTransformationError(ex);
    }
}
Also used : FileNameMapper(org.apache.tools.ant.util.FileNameMapper) File(java.io.File) XPathExpressionException(javax.xml.xpath.XPathExpressionException) BuildException(org.apache.tools.ant.BuildException)

Example 22 with FileNameMapper

use of org.apache.tools.ant.util.FileNameMapper in project ant by apache.

the class Mapper method getImplementation.

/**
 * Returns a fully configured FileNameMapper implementation.
 * @return a FileNameMapper object to be configured
 * @throws BuildException on error
 */
public FileNameMapper getImplementation() throws BuildException {
    if (isReference()) {
        dieOnCircularReference();
        Reference r = getRefid();
        Object o = r.getReferencedObject(getProject());
        if (o instanceof FileNameMapper) {
            return (FileNameMapper) o;
        }
        if (o instanceof Mapper) {
            return ((Mapper) o).getImplementation();
        }
        String od = o == null ? "null" : o.getClass().getName();
        throw new BuildException(od + " at reference '" + r.getRefId() + "' is not a valid mapper reference.");
    }
    if (type == null && classname == null && container == null) {
        throw new BuildException("nested mapper or " + "one of the attributes type or classname is required");
    }
    if (container != null) {
        return container;
    }
    if (type != null && classname != null) {
        throw new BuildException("must not specify both type and classname attribute");
    }
    try {
        FileNameMapper m = getImplementationClass().newInstance();
        final Project p = getProject();
        if (p != null) {
            p.setProjectReference(m);
        }
        m.setFrom(from);
        m.setTo(to);
        return m;
    } catch (BuildException be) {
        throw be;
    } catch (Throwable t) {
        throw new BuildException(t);
    }
}
Also used : ContainerMapper(org.apache.tools.ant.util.ContainerMapper) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) CompositeMapper(org.apache.tools.ant.util.CompositeMapper) Project(org.apache.tools.ant.Project) FileNameMapper(org.apache.tools.ant.util.FileNameMapper) BuildException(org.apache.tools.ant.BuildException)

Example 23 with FileNameMapper

use of org.apache.tools.ant.util.FileNameMapper in project ant by apache.

the class PropertySet method getPropertyMap.

/**
 * @return Map
 * @since 1.9.0
 */
private Map<String, Object> getPropertyMap() {
    if (isReference()) {
        return getRef().getPropertyMap();
    }
    dieOnCircularReference();
    final Mapper myMapper = getMapper();
    final FileNameMapper m = myMapper == null ? null : myMapper.getImplementation();
    final Map<String, Object> effectiveProperties = getEffectiveProperties();
    final Set<String> propertyNames = getPropertyNames(effectiveProperties);
    final Map<String, Object> result = new HashMap<>();
    // iterate through the names, get the matching values
    for (String name : propertyNames) {
        Object value = effectiveProperties.get(name);
        // TODO should we query the PropertyHelper for property value to grab potentially shadowed values?
        if (value != null) {
            // after the project instance has been initialized
            if (m != null) {
                // map the names
                String[] newname = m.mapFileName(name);
                if (newname != null) {
                    name = newname[0];
                }
            }
            result.put(name, value);
        }
    }
    return result;
}
Also used : FileNameMapper(org.apache.tools.ant.util.FileNameMapper) HashMap(java.util.HashMap) FileNameMapper(org.apache.tools.ant.util.FileNameMapper)

Example 24 with FileNameMapper

use of org.apache.tools.ant.util.FileNameMapper in project ant-ivy by apache.

the class IvyRetrieve method add.

/**
 * Add a nested filenamemapper.
 *
 * @param fileNameMapper
 *            the mapper to add.
 */
public void add(FileNameMapper fileNameMapper) {
    Mapper m = new Mapper(getProject());
    m.add(fileNameMapper);
    addMapper(m);
}
Also used : Mapper(org.apache.tools.ant.types.Mapper) FileNameMapper(org.apache.tools.ant.util.FileNameMapper)

Aggregations

FileNameMapper (org.apache.tools.ant.util.FileNameMapper)24 BuildException (org.apache.tools.ant.BuildException)16 File (java.io.File)9 IdentityMapper (org.apache.tools.ant.util.IdentityMapper)8 Resource (org.apache.tools.ant.types.Resource)6 IOException (java.io.IOException)5 Project (org.apache.tools.ant.Project)5 Mapper (org.apache.tools.ant.types.Mapper)5 FlatFileNameMapper (org.apache.tools.ant.util.FlatFileNameMapper)5 MergingMapper (org.apache.tools.ant.util.MergingMapper)5 List (java.util.List)4 GlobPatternMapper (org.apache.tools.ant.util.GlobPatternMapper)4 SourceFileScanner (org.apache.tools.ant.util.SourceFileScanner)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Stream (java.util.stream.Stream)3 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)3 ResourceCollection (org.apache.tools.ant.types.ResourceCollection)3 FileResource (org.apache.tools.ant.types.resources.FileResource)3 HashSet (java.util.HashSet)2