From 5ee66cf257ad0b06158cfbad75fca23485b781c4 Mon Sep 17 00:00:00 2001 From: Steve Loughran Date: Mon, 16 May 2005 22:40:23 +0000 Subject: [PATCH] , with tests and docs. At this point we have near total script coverage of all the core ant extension points. Note the refactoring of scriptcondition to give us a better base for these things. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@278273 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 5 + docs/manual/CoreTypes/mapper.html | 100 ++++++++++++++++++ .../types/mappers/define.mapperresult.xml | 3 + .../testcases/types/mappers/scriptmapper.xml | 41 +++++++ .../tools/ant/types/defaults.properties | 1 + .../optional/AbstractScriptComponent.java | 87 +++++++++++++++ .../ant/types/optional/ScriptCondition.java | 40 +------ .../ant/types/optional/ScriptMapper.java | 98 +++++++++++++++++ .../tools/ant/types/mappers/MapperResult.java | 7 +- .../ant/types/optional/ScriptMapperTest.java | 43 ++++++++ 10 files changed, 388 insertions(+), 37 deletions(-) create mode 100644 src/etc/testcases/types/mappers/scriptmapper.xml create mode 100644 src/main/org/apache/tools/ant/types/optional/AbstractScriptComponent.java create mode 100644 src/main/org/apache/tools/ant/types/optional/ScriptMapper.java create mode 100644 src/testcases/org/apache/tools/ant/types/optional/ScriptMapperTest.java diff --git a/WHATSNEW b/WHATSNEW index fcd3c4a26..925349653 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -98,6 +98,9 @@ Fixed bugs: * and create a new parser for every file in a fileset, and so validate multiple files properly. Bugzilla Report 32791 +* New mapper, , supports scripted mapping of source files/strings to + destination strings. + Other changes: -------------- @@ -203,6 +206,8 @@ Other changes: * project name is now used for *all* targets so one can write consistent import build file. bugzilla report 28444. + + Changes from Ant 1.6.3 to current Ant 1.6 CVS version ===================================================== diff --git a/docs/manual/CoreTypes/mapper.html b/docs/manual/CoreTypes/mapper.html index cddcee66c..b1f40e92c 100644 --- a/docs/manual/CoreTypes/mapper.html +++ b/docs/manual/CoreTypes/mapper.html @@ -683,6 +683,106 @@ with <uptodate> and <junit> output.

The filtermapper has no corresponding <mapper type> attribute.

+ + + + + +

scriptmapper (since ant 1.7)

+

+This mapper executes a script written in Apache BSF +supported language, once per file to map.

+The script can be declared inline or in a specified file. +

+

+See the Script task for +an explanation of scripts and dependencies. +

+ + + + + + + + + + + + + + + + + +
AttributeDescriptionRequired
language + Scripting language + Yes
src + File containing the script + No
+ +

+Example +

+
+<scriptmapper language="javascript">
+  self.addMappedName(source.toUpperCase());
+  self.addMappedName(source.toLowerCase());
+</scriptmapper>
+
+ + + + + + + + + + +
Source file nameTarget file names
foo\bar\A.javaFOO\BAR\A.JAVA foo\bar\a.java
+ +

+To use this mapper, the scripts need access to the source file, +and the ability to return multiple mappings. Here are the relevant beans and +their methods. The script is called once for every source file, with the +list of mapped names reset after every invocation. + + + + + + + + + + + + + + + + + + + + + + +
Script beanDescription
source: String + The file/path to map +
self + the scriptmapper itself +
self.addMappedName(String name) + Add a new mapping +
self.clear() + Reset the list of files. +
+ +

The scriptmapper has no corresponding + <mapper type> attribute. +

+

Copyright © 2000-2005 The Apache Software Foundation. All rights Reserved.

diff --git a/src/etc/testcases/types/mappers/define.mapperresult.xml b/src/etc/testcases/types/mappers/define.mapperresult.xml index 9ea447f14..72a2697d1 100644 --- a/src/etc/testcases/types/mappers/define.mapperresult.xml +++ b/src/etc/testcases/types/mappers/define.mapperresult.xml @@ -2,4 +2,7 @@ + + + diff --git a/src/etc/testcases/types/mappers/scriptmapper.xml b/src/etc/testcases/types/mappers/scriptmapper.xml new file mode 100644 index 000000000..51f0aa61c --- /dev/null +++ b/src/etc/testcases/types/mappers/scriptmapper.xml @@ -0,0 +1,41 @@ + + + + + + + + self.addMappedName("a"); + + + + + + + + self.addMappedName("a"); + self.clear(); + + + + + + + + self.addMappedName("a"); + self.addMappedName("b"); + + + + + + + + //relying on "a" to map to "A" on all locales. + self.addMappedName(source.toUpperCase()); + self.addMappedName(source.toLowerCase()); + + + + + diff --git a/src/main/org/apache/tools/ant/types/defaults.properties b/src/main/org/apache/tools/ant/types/defaults.properties index 0838aecbb..c1bbf5219 100644 --- a/src/main/org/apache/tools/ant/types/defaults.properties +++ b/src/main/org/apache/tools/ant/types/defaults.properties @@ -43,3 +43,4 @@ scriptselector=org.apache.tools.ant.types.optional.ScriptSelector scriptcondition=org.apache.tools.ant.types.optional.ScriptCondition xor=org.apache.tools.ant.taskdefs.condition.Xor parsersupports=org.apache.tools.ant.taskdefs.condition.ParserSupports +scriptmapper=org.apache.tools.ant.types.optional.ScriptMapper diff --git a/src/main/org/apache/tools/ant/types/optional/AbstractScriptComponent.java b/src/main/org/apache/tools/ant/types/optional/AbstractScriptComponent.java new file mode 100644 index 000000000..388ab7f05 --- /dev/null +++ b/src/main/org/apache/tools/ant/types/optional/AbstractScriptComponent.java @@ -0,0 +1,87 @@ +/** (C) Copyright 2005 Hewlett-Packard Development Company, LP + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + For more information: www.smartfrog.org + + */ + +package org.apache.tools.ant.types.optional; + +import org.apache.tools.ant.ProjectComponent; +import org.apache.tools.ant.util.ScriptRunner; + +import java.io.File; + +/** + * This is a {@link ProjectComponent} that has script support built in + * Use it as a foundation for scriptable things. + */ +public abstract class AbstractScriptComponent extends ProjectComponent { + /** + * script runner + */ + private ScriptRunner runner = new ScriptRunner(); + + /** + * Get our script runner + * @return + */ + public ScriptRunner getRunner() { + return runner; + } + + /** + * Load the script from an external file ; optional. + * + * @param file the file containing the script source. + */ + public void setSrc(File file) { + runner.setSrc(file); + } + + /** + * The script text. + * + * @param text a component of the script text to be added. + */ + public void addText(String text) { + runner.addText(text); + } + + /** + * Defines the language (required). + * + * @param language the scripting language name for the script. + */ + public void setLanguage(String language) { + runner.setLanguage(language); + } + + /** + * Initialize the script runner. Calls this before running the system + */ + protected void initScriptRunner() { + getRunner().bindToComponent(this); + } + + /** + * Run a script + * @param execName name of the script + */ + protected void executeScript(String execName) { + getRunner().executeScript(execName); + } +} diff --git a/src/main/org/apache/tools/ant/types/optional/ScriptCondition.java b/src/main/org/apache/tools/ant/types/optional/ScriptCondition.java index 8ea9f0599..e9f8823e9 100644 --- a/src/main/org/apache/tools/ant/types/optional/ScriptCondition.java +++ b/src/main/org/apache/tools/ant/types/optional/ScriptCondition.java @@ -25,49 +25,17 @@ import java.io.File; /** * A condition that lets you include script. - * The condition component sets a bean "self", whose attribute "result" + * The condition component sets a bean "self", whose attribute "value" * must be set to true for the condition to succeed, false to fail. * The default is 'false' */ -public class ScriptCondition extends ProjectComponent implements Condition { - - /** - * script runner - */ - private ScriptRunner runner = new ScriptRunner(); +public class ScriptCondition extends AbstractScriptComponent implements Condition { /** * result field */ private boolean value = false; - /** - * Load the script from an external file ; optional. - * - * @param file the file containing the script source. - */ - public void setSrc(File file) { - runner.setSrc(file); - } - - /** - * The script text. - * - * @param text a component of the script text to be added. - */ - public void addText(String text) { - runner.addText(text); - } - - /** - * Defines the language (required). - * - * @param language the scripting language name for the script. - */ - public void setLanguage(String language) { - runner.setLanguage(language); - } - /** * Is this condition true? @@ -78,8 +46,8 @@ public class ScriptCondition extends ProjectComponent implements Condition { * if an error occurs */ public boolean eval() throws BuildException { - runner.bindToComponent(this); - runner.executeScript("ant_condition"); + initScriptRunner(); + executeScript("ant_condition"); return getValue(); } diff --git a/src/main/org/apache/tools/ant/types/optional/ScriptMapper.java b/src/main/org/apache/tools/ant/types/optional/ScriptMapper.java new file mode 100644 index 000000000..d4ed26ffd --- /dev/null +++ b/src/main/org/apache/tools/ant/types/optional/ScriptMapper.java @@ -0,0 +1,98 @@ +/** (C) Copyright 2005 Hewlett-Packard Development Company, LP + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + For more information: www.smartfrog.org + + */ + +package org.apache.tools.ant.types.optional; + +import org.apache.tools.ant.util.FileNameMapper; +import org.apache.tools.ant.util.ScriptRunner; +import org.apache.tools.ant.ProjectComponent; + +import java.util.ArrayList; + +/** + * Script support at map time. + * @since Ant1.7 + */ +public class ScriptMapper extends AbstractScriptComponent implements FileNameMapper { + + + ArrayList files; + static final String[] EMPTY_STRING_ARRAY = new String[0]; + + + /** + * Sets the from part of the transformation rule. + * + * @param from a string. + */ + public void setFrom(String from) { + + } + + /** + * Sets the to part of the transformation rule. + * + * @param to a string. + */ + public void setTo(String to) { + + } + + /** + * Reset the list of files + */ + public void clear() { + files=new ArrayList(1); + } + + /** + * Add a mapped name + * @param mapping + */ + public void addMappedName(String mapping) { + files.add(mapping); + } + + /** + * Returns an array containing the target filename(s) for the given source + * file. + *

+ *

if the given rule doesn't apply to the source file, implementation + * must return null. SourceFileScanner will then omit the source file in + * question.

+ * + * @param sourceFileName the name of the source file relative to some given + * basedirectory. + * @return an array of strings if the rule applies to the source file, or + * null if it does not. + */ + + public String[] mapFileName(String sourceFileName) { + initScriptRunner(); + getRunner().addBean("source", sourceFileName); + clear(); + executeScript("ant_mapper"); + if(files.size()==0) { + return null; + } else { + return (String[])files.toArray(EMPTY_STRING_ARRAY); + } + } +} diff --git a/src/testcases/org/apache/tools/ant/types/mappers/MapperResult.java b/src/testcases/org/apache/tools/ant/types/mappers/MapperResult.java index 3a4b94c2c..cdaf94591 100644 --- a/src/testcases/org/apache/tools/ant/types/mappers/MapperResult.java +++ b/src/testcases/org/apache/tools/ant/types/mappers/MapperResult.java @@ -36,6 +36,11 @@ public class MapperResult extends Task { private String output; private FileNameMapper fileNameMapper; + /** + * The output on an empty string array + */ + private static final String NULL_MAPPER_RESULT = ""; + public void setFailMessage(String failMessage) { this.failMessage = failMessage; } @@ -72,7 +77,7 @@ public class MapperResult extends Task { String[] result = fileNameMapper.mapFileName(input); String flattened; if (result == null) { - flattened = ""; + flattened = NULL_MAPPER_RESULT; } else { StringBuffer b = new StringBuffer(); for (int i = 0; i < result.length; ++i) { diff --git a/src/testcases/org/apache/tools/ant/types/optional/ScriptMapperTest.java b/src/testcases/org/apache/tools/ant/types/optional/ScriptMapperTest.java new file mode 100644 index 000000000..dd7457125 --- /dev/null +++ b/src/testcases/org/apache/tools/ant/types/optional/ScriptMapperTest.java @@ -0,0 +1,43 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant.types.optional; + +import org.apache.tools.ant.BuildFileTest; + +/** + * Test our script mapping + */ +public class ScriptMapperTest extends BuildFileTest { + public ScriptMapperTest(String name) { + super(name); + } + + public void setUp() { + configureProject("src/etc/testcases/types/mappers/scriptmapper.xml"); + } + + public void testClear() { + executeTarget("testClear"); + } + public void testSetMultiple() { + executeTarget("testSetMultiple"); + } + public void testPassthrough() { + executeTarget("testPassthrough"); + } +}