KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > tools > ant > foreach > ParamSet


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis.tools.ant.foreach;
17
18 import org.apache.tools.ant.DirectoryScanner;
19 import org.apache.tools.ant.Project;
20 import org.apache.tools.ant.types.EnumeratedAttribute;
21 import org.apache.tools.ant.types.FileSet;
22
23 import java.io.File;
24 import java.util.Enumeration;
25 import java.util.Vector;
26
27 /**
28  * Inner class stores sets of <param>s.
29  * It can hold <fileset>s or <item>s or both.
30  *
31  * @author <a HREF="mailto:tpv@spamcop.net">Tim Vernum</a>
32  * @author Davanum Srinivas
33  * @author Richard A. Sitze
34  */

35 public class ParamSet {
36     public static final String TYPE_FILE = "file".intern();
37     public static final String TYPE_DIR = "dir".intern();
38     public static final String TYPE_BOTH = "both".intern();
39
40     /**
41      * Enumerated attribute with the values "file", "dir" and "both"
42      * for the type attribute.
43      */

44     public static class FileDirBoth extends EnumeratedAttribute {
45         public String[] getValues() {
46             return new String[]{
47                 TYPE_FILE, TYPE_DIR, TYPE_BOTH
48             };
49         }
50     }
51
52     /** Defaults to "file". */
53     protected String type = TYPE_FILE;
54     private Vector filesets;
55     private Vector items;
56     private String name;
57
58     public ParamSet() {
59         filesets = new Vector();
60         items = new Vector();
61     }
62
63     public void addFileset(FileSet fileset) {
64         filesets.addElement(fileset);
65     }
66
67     public ParamItem createItem() {
68         ParamItem item = new ParamItem();
69         items.addElement(item);
70         return item;
71     }
72
73     public void setName(String name) {
74         this.name = name;
75     }
76
77     public String getName() {
78         return name;
79     }
80
81     public Enumeration getValues(Project project) {
82         /* As an arbitrary rule, this will return filesets first,
83         and then <item>s. The ordering of the buildfile is
84         not guaranteed. */

85         Vector values = new Vector();
86         Enumeration enum = filesets.elements();
87         while (enum.hasMoreElements()) {
88             FileSet fileSet = (FileSet) enum.nextElement();
89             File base = fileSet.getDir(project);
90             DirectoryScanner scanner = fileSet.getDirectoryScanner(project);
91             if (TYPE_DIR != type) {
92                 String[] files = getFiles(base, scanner);
93                 for (int j = 0; j < files.length; j++) {
94                     File f = new File(base, files[j]);
95                     values.addElement(f.getAbsolutePath());
96                 }
97             }
98             if (TYPE_FILE != type) {
99                 String[] dirs = getDirs(base, scanner);
100                 for (int j = 0; j < dirs.length; j++) {
101                     File f = new File(base, dirs[j]);
102                     values.addElement(f.getAbsolutePath());
103                 }
104             }
105         }
106         enum = items.elements();
107         while (enum.hasMoreElements()) {
108             ParamItem item = (ParamItem) enum.nextElement();
109             values.addElement(item.getValue());
110         }
111         return values.elements();
112     }
113
114     /**
115      * Shall the command work only on files, directories or both?
116      */

117     public void setType(FileDirBoth type) {
118         this.type = type.getValue().intern();
119     }
120
121     /**
122      * Return the list of files from this DirectoryScanner that should
123      * be included on the command line.
124      */

125     protected String[] getFiles(File basedir, DirectoryScanner ds) {
126         return ds.getIncludedFiles();
127     }
128
129     /**
130      * Return the list of Directories from this DirectoryScanner that
131      * should be included on the command line.
132      */

133     protected String[] getDirs(File basedir, DirectoryScanner ds) {
134         return ds.getIncludedDirectories();
135     }
136 }
137
Popular Tags