All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xmlcalabash.drivers.CalabashTask Maven / Gradle / Ivy

The newest version!
/*
 * CalabashTask.java
 *
 * Copyright 2012 Mentea.
 * All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common
 * Development and Distribution License("CDDL") (collectively, the
 * "License"). You may not use this file except in compliance with the
 * License. You can obtain a copy of the License at
 * https://xproc.dev.java.net/public/CDDL+GPL.html or
 * docs/CDDL+GPL.txt in the distribution. See the License for the
 * specific language governing permissions and limitations under the
 * License. When distributing the software, include this License Header
 * Notice in each file and include the License file at docs/CDDL+GPL.txt.
 */

package com.xmlcalabash.drivers;

import java.io.File;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.xml.transform.URIResolver;

import com.xmlcalabash.util.Input.Type;
import com.xmlcalabash.util.UserArgs;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.PropertyHelper;
import org.apache.tools.ant.taskdefs.LogOutputStream;
import org.apache.tools.ant.taskdefs.MatchingTask;
import org.apache.tools.ant.types.CommandlineJava;
import org.apache.tools.ant.types.Environment;
import org.apache.tools.ant.types.Mapper;
import org.apache.tools.ant.types.PropertySet;
import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.ResourceCollection;
import org.apache.tools.ant.types.resources.FileResource;
import org.apache.tools.ant.types.resources.Resources;
import org.apache.tools.ant.types.resources.Union;
import org.apache.tools.ant.util.FileNameMapper;
import org.xml.sax.EntityResolver;

import static com.xmlcalabash.util.Input.Type.DATA;
import static com.xmlcalabash.util.Input.Type.XML;
import static java.lang.Long.MAX_VALUE;
import static java.util.Arrays.asList;

/**
 * Ant task to run Calabash.
 *
 * 

Owes a lot to Ant's <xslt> task, but this task can't become * part of Ant because this task relies on Calabash, which is licensed * under LGPL. * * @author MenteaXML */ public class CalabashTask extends MatchingTask { /* * Ant can reuse a Task multiple times in the one build file, so * all of these fields need to be reset to their initial values * at the end of evaluate(). * * Fields with null or false initial value are explicitly set so * it's obvious what to reset them to at the end of evaluate(). * * Fields vaguely follow the sequence input, pipeline, output, * then the rest. */ private UserArgs userArgs = new UserArgs(); /** * Input ports and the resources associated with each. */ private Map> inputResources = new HashMap>(); /** * Input ports and the mapper associated with each. */ private Map inputMappers = new HashMap(); /** * Where to find the source XML file, default is the project's basedir */ private File baseDir = null; /** * Port of the pipeline input. As attribute. */ private String inPort = null; /** * URI of the input XML. As attribute. */ private Resource inResource = null; /** * Type of the input resource. */ private Type inType = XML; /** * Whether the build should fail if the nested resource collection is empty. */ private boolean failOnNoResources = true; /** * The pipeline to run as a {@link org.apache.tools.ant.types.Resource} */ private Resource pipelineResource = null; /** * destination directory */ private File destDir = null; /** * Port of the pipeline output. As attribute. */ private String outPort = null; /** * Resource of the output XML. As attribute. */ private Resource outResource = null; /** * Output ports and the resources associated with each. */ private HashMap outputResources = new HashMap(); /** * Output ports and the mapper associated with each. */ private Map outputMappers = new HashMap(); /** * extension of the files produced by pipeline processing */ private String targetExtension = "-out.xml"; /** * whether target extension has been set from build file */ private boolean isTargetExtensionSet = false; /** * Whether to fail the build if an error occurs. */ private boolean failOnError = true; /** * Additional resource collections to process. */ private Union resources = new Union(); /** * Whether to use the implicit fileset. */ private boolean useImplicitFileset = true; /** * Whether to process all files in the included directories as well. */ private boolean performDirectoryScan = true; /** * Mapper to use when a set of files gets processed. */ private FileNameMapper mapper = null; /** * force output of target files even if they already exist */ private boolean force = false; /** * System properties to set during transformation. */ private CommandlineJava.SysProperties sysProperties = new CommandlineJava.SysProperties(); /** * The list of parameters. */ private List parameters = new ArrayList(); /** * The list of options. */ private List

Set this to false if you want explicit control with nested resource collections.

* * @param useimplicitfileset set to true if you want to use implicit fileset */ public void setUseImplicitFileset(boolean useimplicitfileset) { useImplicitFileset = useimplicitfileset; } /** * Whether to process all files in the included directories as well; * optional, default is true. * * @param b true if files in included directories are processed. */ public void setScanIncludedDirectories(boolean b) { performDirectoryScan = b; } /** * Defines the mapper to map source to destination files. * * @param mapper the mapper to use * @throws BuildException if more than one mapper is defined */ public void addMapper(Mapper mapper) throws BuildException { add(mapper.getImplementation()); } /** * Adds a nested FileNameMapper. * * @param fileNameMapper the mapper to add * @throws BuildException if more than one mapper is defined */ public void add(FileNameMapper fileNameMapper) throws BuildException { if (mapper != null) { handleError("Cannot define more than one mapper"); return; } mapper = fileNameMapper; } /** * Set whether to check dependencies, or always generate; optional, default is false. * * @param force true if always generate. */ public void setForce(boolean force) { this.force = force; } /* * A system property to set during transformation. */ public void addSysproperty(Environment.Variable sysp) { sysProperties.addVariable(sysp); } /* * A set of system properties to set during transformation. */ public void addSyspropertyset(PropertySet sysp) { sysProperties.addSyspropertyset(sysp); } /** * Work with an instance of a <binding> element already configured by Ant. * * @param namespace the configured Namespace */ public void addConfiguredNamespace(Namespace namespace) { if (!namespace.shouldUse()) { log("Skipping namespace '" + namespace.getPrefix() + "=" + namespace.getURI() + "' as it is configured to be unused.", Project.MSG_DEBUG); return; } // prefix and/or uri may have been omitted in build file // without Ant complaining if (namespace.getPrefix() == null) { handleError(" prefix cannot be null"); return; } if (namespace.getURI() == null) { handleError(" URI cannot be null"); return; } try { userArgs.addBinding(namespace.getPrefix(), namespace.getURI()); } catch (Exception e) { handleError(e); } } /** * Work with an instance of a <option> element already configured by Ant. * * @param option the configured Option */ public void addConfiguredOption(Option option) { if (!option.shouldUse()) { log("Skipping option '" + option.getName() + "' as it is configured to be unused.", Project.MSG_DEBUG); return; } options.add(option); } /** * Use an {@code




© 2015 - 2024 Weber Informatics LLC | Privacy Policy