Expand CalabashHelper to support parameter values which are instances of String, File, URI, or URL (previously only String was supported)

This commit is contained in:
Sam Harwell
2013-08-23 08:41:31 -05:00
parent 101e754369
commit d6f3b64982
6 changed files with 55 additions and 21 deletions

View File

@@ -68,7 +68,7 @@ public abstract class ApiRefMojo extends AbstractHtmlMojo {
String pathToPipelineFile = "classpath:/wadl2html.xpl"; //use "classpath:/path" for this to work
Source source = super.createSource(inputFilename, sourceFile, filter);
Map map=new HashMap<String, String>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("security", security);
map.put("canonicalUrlBase", canonicalUrlBase);

View File

@@ -10,16 +10,17 @@ import javax.xml.transform.Source;
import javax.xml.transform.sax.SAXSource;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class CalabashHelper {
private static Source run(final String pipelineURI, final InputSource inputSource, final Map<String, String>map) throws FileNotFoundException {
private static Source run(final String pipelineURI, final InputSource inputSource, final Map<String, Object> map) throws FileNotFoundException {
Pipeline pipeline = new CalabashPipelineBuilder(false, true).build(pipelineURI);
// <c:param-set xmlns:c="http://www.w3.org/ns/xproc-step">
@@ -33,17 +34,30 @@ public class CalabashHelper {
if(null!=map){
Set<String>keys=map.keySet();
if(null!=keys){
for (Map.Entry<String, Object> entry : map.entrySet()) {
String rawValue;
if (entry.getValue() instanceof File) {
rawValue = ((File)entry.getValue()).toURI().toString();
} else if (entry.getValue() instanceof URI || entry.getValue() instanceof String) {
rawValue = entry.getValue().toString();
} else if (entry.getValue() instanceof URL) {
rawValue = ((URL)entry.getValue()).toExternalForm();
} else if (entry.getValue() != null) {
throw new UnsupportedOperationException(String.format("The map cannot contain values of type %s.", entry.getValue().getClass()));
} else {
// ignore nulls
continue;
}
for(Iterator<String>iter=keys.iterator();iter.hasNext();){
String aKey=iter.next();
if(null!=map.get(aKey)){
strBuff.append("<c:param name=\""+aKey+ "\" namespace=\"\" value=\""+map.get(aKey)+"\"/>");
}
}
strBuff
.append("<c:param name=\"")
.append(entry.getKey())
.append("\" namespace=\"\" value=\"")
.append(rawValue)
.append("\"/>");
}
}
strBuff.append("</c:param-set>");
String params=strBuff.toString();
final InputStream paramsStream = new ByteArrayInputStream(params.getBytes());
@@ -60,7 +74,27 @@ public class CalabashHelper {
return sources.get(0);
}
public static Source createSource(Source source, String pipelineURI, Map<String, String> map)
/**
* Creates a {@link Source} for use in a Calabash pipeline.
*
* <p>The {@code map} values may instances of {@link String}, {@link File},
* {@link URI}, or {@link URL}.</p>
*
* <ul>
* <li>{@link String}: These are passed to the pipeline unchanged.</li>
* <li>{@link URI}: The results of {@link URI#toString()} is passed to the pipeline.</li>
* <li>{@link File}: These are treated as URIs by calling {@link File#toURI()}.</li>
* <li>{@link URL}: The result of {@link URL#toExternalForm()} is passed to the pipeline.</li>
* <li>{@code null}: This is passed to the pipeline as an empty string.</li>
* </ul>
*
* @param source
* @param pipelineURI
* @param map
* @return
* @throws MojoExecutionException
*/
public static Source createSource(Source source, String pipelineURI, Map<String, Object> map)
throws MojoExecutionException {
try {

View File

@@ -533,7 +533,7 @@ public abstract class PDFMojo extends AbstractFoMojo {
Source source = new SAXSource(filter, inputSource);
//Source source = super.createSource(inputFilename, sourceFile, filter);
Map map=new HashMap<String, String>();
Map<String, Object> map=new HashMap<String, Object>();
String sysSecurity=System.getProperty("security");
getLog().info("adjustTransformer():sysSecurity="+sysSecurity);
if(null!=sysSecurity && !sysSecurity.isEmpty()){

View File

@@ -754,7 +754,7 @@ public abstract class WebHelpMojo extends AbstractWebhelpMojo {
Source source = new SAXSource(filter, inputSource);
//Source source = super.createSource(inputFilename, sourceFile, filter);
Map<String, String> map=new HashMap<String, String>();
Map<String, Object> map = new HashMap<String, Object>();
String sysWebhelpWar=System.getProperty("webhelp.war");

View File

@@ -292,7 +292,7 @@ public abstract class XhtmlMojo extends AbstractHtmlMojo {
String pathToPipelineFile = "classpath:/war.xpl"; //use "classpath:/path" for this to work
Source source = super.createSource(inputFilename, sourceFile, filter);
Map map=new HashMap<String, String>();
Map<String, Object> map=new HashMap<String, Object>();
map.put("failOnValidationError", failOnValidationError);
map.put("transform.dir", transformDir);

View File

@@ -167,7 +167,7 @@ public class PDFBuilder {
FileUtils.extractJaredDirectory("fonts",PDFBuilder.class,imageParentDirectory);
}
public File processSources(Map<String,String> map) throws MojoExecutionException{
public File processSources(Map<String, Object> map) throws MojoExecutionException {
final String[] included = scanIncludedFiles();
// configure a resolver for catalog files
final CatalogManager catalogManager = createCatalogManager();
@@ -1063,7 +1063,7 @@ public class PDFBuilder {
return filter;
}
protected Source createSource(String inputFilename, File sourceFile, PreprocessingFilter filter, Map<String,String> map)
protected Source createSource(String inputFilename, File sourceFile, PreprocessingFilter filter, Map<String, Object> map)
throws MojoExecutionException {
String pathToPipelineFile = "classpath:/pdf.xpl"; //use "classpath:/path" for this to work
@@ -1072,7 +1072,7 @@ public class PDFBuilder {
final InputSource inputSource = new InputSource(sourceFileNameNormalized);
Source source = new SAXSource(filter, inputSource);
Map<String,String> localMap = new HashMap<String,String>(map);
Map<String, Object> localMap = new HashMap<String, Object>(map);
localMap.put("outputType", "pdf");
//removing webhelp specific settings from map