01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18: package org.apache.tools.ant.types.optional;
19:
20: import org.apache.tools.ant.util.FileNameMapper;
21:
22: import java.util.ArrayList;
23:
24: /**
25: * Script support at map time.
26: * @since Ant1.7
27: */
28: public class ScriptMapper extends AbstractScriptComponent implements
29: FileNameMapper {
30:
31: private ArrayList files;
32: static final String[] EMPTY_STRING_ARRAY = new String[0];
33:
34: /**
35: * Sets the from part of the transformation rule.
36: *
37: * @param from a string.
38: */
39: public void setFrom(String from) {
40:
41: }
42:
43: /**
44: * Sets the to part of the transformation rule.
45: *
46: * @param to a string.
47: */
48: public void setTo(String to) {
49:
50: }
51:
52: /**
53: * Reset the list of files
54: */
55: public void clear() {
56: files = new ArrayList(1);
57: }
58:
59: /**
60: * Add a mapped name
61: * @param mapping the value to use.
62: */
63: public void addMappedName(String mapping) {
64: files.add(mapping);
65: }
66:
67: /**
68: * Returns an array containing the target filename(s) for the given source
69: * file.
70: * <p/>
71: * <p>if the given rule doesn't apply to the source file, implementation
72: * must return null. SourceFileScanner will then omit the source file in
73: * question.</p>
74: *
75: * @param sourceFileName the name of the source file relative to some given
76: * basedirectory.
77: * @return an array of strings if the rule applies to the source file, or
78: * null if it does not.
79: */
80:
81: public String[] mapFileName(String sourceFileName) {
82: initScriptRunner();
83: getRunner().addBean("source", sourceFileName);
84: clear();
85: executeScript("ant_mapper");
86: if (files.size() == 0) {
87: return null;
88: } else {
89: return (String[]) files.toArray(EMPTY_STRING_ARRAY);
90: }
91: }
92: }
|