001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1999 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution, if
020: * any, must include the following acknowlegement:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowlegement may appear in the software itself,
024: * if and wherever such third-party acknowlegements normally appear.
025: *
026: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
027: * Foundation" must not be used to endorse or promote products derived
028: * from this software without prior written permission. For written
029: * permission, please contact apache@apache.org.
030: *
031: * 5. Products derived from this software may not be called "Apache"
032: * nor may "Apache" appear in their names without prior written
033: * permission of the Apache Group.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of the Apache Software Foundation. For more
051: * information on the Apache Software Foundation, please see
052: * <http://www.apache.org/>.
053: *
054: * Modified for us in XuiEditor
055: */
056:
057: package net.xoetrope.builder.editor.ant.taskdefs;
058:
059: import java.io.File;
060: import java.util.Enumeration;
061: import java.util.Hashtable;
062: import java.util.StringTokenizer;
063: import java.util.Vector;
064:
065: import net.xoetrope.builder.editor.ant.DirectoryScanner;
066: import net.xoetrope.debug.DebugLogger;
067:
068: /**
069: * This is an abstract task that should be used by all those tasks that
070: * require to include or exclude files based on pattern matching.
071: *
072: * @author Arnout J. Kuiper <a href="mailto:ajkuiper@wxs.nl">ajkuiper@wxs.nl</a>
073: * @author Stefano Mazzocchi <a href="mailto:stefano@apache.org">stefano@apache.org</a>
074: * @author Sam Ruby <a href="mailto:rubys@us.ibm.com">rubys@us.ibm.com</a>
075: * @author Jon S. Stevens <a href="mailto:jon@clearink.com">jon@clearink.com</a>
076: */
077:
078: public abstract class MatchingTask /*extends Task*/
079: {
080:
081: protected Vector includeList = new Vector();
082: protected Vector excludeList = new Vector();
083: protected boolean useDefaultExcludes = true;
084: private Hashtable properties = new Hashtable();
085:
086: /**
087: * provide access to properties from within the inner class
088: */
089: public String getProperty(String name) {
090: if (name == null)
091: return null;
092: String property = (String) properties.get(name);
093: return property;
094: }
095:
096: /**
097: * inner class to hold a name on list. "If" and "Unless" attributes
098: * may be used to invalidate the entry based on the existence of a
099: * property (typically set thru the use of the Available task).
100: */
101: public class NameEntry {
102: private boolean valid = true;
103: private String name;
104:
105: public String getName() {
106: return valid ? name : null;
107: }
108:
109: public void setName(String name) {
110: this .name = name;
111: }
112:
113: public void setIf(String name) {
114: if (getProperty(name) == null)
115: valid = false;
116: }
117:
118: public void setUnless(String name) {
119: if (getProperty(name) != null)
120: valid = false;
121: }
122: }
123:
124: /**
125: * add a name entry on the include list
126: */
127: public NameEntry createInclude() {
128: NameEntry result = new NameEntry();
129: includeList.addElement(result);
130: return result;
131: }
132:
133: /**
134: * add a name entry on the exclude list
135: */
136: public NameEntry createExclude() {
137: NameEntry result = new NameEntry();
138: excludeList.addElement(result);
139: return result;
140: }
141:
142: /**
143: * Sets the set of include patterns. Patterns may be separated by a comma
144: * or a space.
145: *
146: * @param includes the string containing the include patterns
147: */
148: public void setIncludes(String includes) {
149: if (includes != null && includes.length() > 0) {
150: createInclude().setName(includes);
151: }
152: }
153:
154: /**
155: * Set this to be the items in the base directory that you want to be
156: * included. You can also specify "*" for the items (ie: items="*")
157: * and it will include all the items in the base directory.
158: *
159: * @param itemString the string containing the files to include.
160: */
161: public void setItems(String itemString) {
162: DebugLogger.trace("The items attribute is deprecated. "
163: + "Please use the includes attribute.");
164: if (itemString == null || itemString.equals("*")
165: || itemString.equals(".")) {
166: createInclude().setName("**");
167: } else {
168: StringTokenizer tok = new StringTokenizer(itemString, ", ");
169: while (tok.hasMoreTokens()) {
170: String pattern = tok.nextToken().trim();
171: if (pattern.length() > 0) {
172: createInclude().setName(pattern + "/**");
173: }
174: }
175: }
176: }
177:
178: /**
179: * Sets the set of exclude patterns. Patterns may be separated by a comma
180: * or a space.
181: *
182: * @param excludes the string containing the exclude patterns
183: */
184: public void setExcludes(String excludes) {
185: if (excludes != null && excludes.length() > 0) {
186: createExclude().setName(excludes);
187: }
188: }
189:
190: /**
191: * List of filenames and directory names to not include. They should be
192: * either , or " " (space) separated. The ignored files will be logged.
193: *
194: * @param ignoreString the string containing the files to ignore.
195: */
196: public void setIgnore(String ignoreString) {
197: DebugLogger.trace("The ignore attribute is deprecated."
198: + "Please use the excludes attribute.");
199: if (ignoreString != null && ignoreString.length() > 0) {
200: Vector tmpExcludes = new Vector();
201: StringTokenizer tok = new StringTokenizer(ignoreString,
202: ", ", false);
203: while (tok.hasMoreTokens()) {
204: createExclude().setName(
205: "**/" + tok.nextToken().trim() + "/**");
206: }
207: }
208: }
209:
210: /**
211: * Sets whether default exclusions should be used or not.
212: *
213: * @param useDefaultExcludes "true"|"on"|"yes" when default exclusions
214: * should be used, "false"|"off"|"no" when they
215: * shouldn't be used.
216: */
217: public void setDefaultexcludes(boolean useDefaultExcludes) {
218: this .useDefaultExcludes = useDefaultExcludes;
219: }
220:
221: /**
222: * Convert a vector of NameEntry elements into an array of Strings.
223: */
224: private String[] makeArray(Vector list) {
225: if (list.size() == 0)
226: return null;
227:
228: Vector tmpNames = new Vector();
229: for (Enumeration e = list.elements(); e.hasMoreElements();) {
230: String includes = ((NameEntry) e.nextElement()).getName();
231: if (includes == null)
232: continue;
233: StringTokenizer tok = new StringTokenizer(includes, ", ",
234: false);
235: while (tok.hasMoreTokens()) {
236: String pattern = tok.nextToken().trim();
237: if (pattern.length() > 0) {
238: tmpNames.addElement(pattern);
239: }
240: }
241: }
242:
243: String result[] = new String[tmpNames.size()];
244: for (int i = 0; i < tmpNames.size(); i++) {
245: result[i] = (String) tmpNames.elementAt(i);
246: }
247:
248: return result;
249: }
250:
251: /**
252: * Returns the directory scanner needed to access the files to process.
253: */
254: protected DirectoryScanner getDirectoryScanner(File baseDir) {
255: DirectoryScanner ds = new DirectoryScanner();
256: ds.setBasedir(baseDir);
257: ds.setIncludes(makeArray(includeList));
258: ds.setExcludes(makeArray(excludeList));
259: if (useDefaultExcludes)
260: ds.addDefaultExcludes();
261: ds.scan();
262: return ds;
263: }
264: }
|