KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > forrest > UncopiedFileSelector


1 /*
2 * Copyright 2002-2004 The Apache Software Foundation or its licensors,
3 * as applicable.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * 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 package org.apache.forrest;
18
19 import java.io.BufferedReader JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileNotFoundException JavaDoc;
22 import java.io.FileReader JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import org.apache.tools.ant.BuildException;
28 import org.apache.tools.ant.types.Parameter;
29 import org.apache.tools.ant.types.selectors.BaseExtendSelector;
30
31 /**
32  * Ant selector that selects files listed in an external text file.
33  * In the context of Forrest, the external text file lists "uncopied files"
34  * from the Cocoon run, hence the name.
35  */

36 public class UncopiedFileSelector extends BaseExtendSelector {
37
38   public final static String JavaDoc CONFIG_KEY = "config";
39
40   /** File listing files to copy. Typically /tmp/unprocessed-files.txt. */
41   private String JavaDoc configFile = null;
42   private boolean configRead = false;
43   /** Processed list of files read from <code>configFile</code>. */
44   private Set JavaDoc goodFiles = new HashSet JavaDoc();
45
46
47   public UncopiedFileSelector() {
48   }
49
50   public String JavaDoc toString() {
51     StringBuffer JavaDoc buf = new StringBuffer JavaDoc("{uncopiedfileselector config: ");
52     buf.append(configFile);
53     buf.append("}");
54     return buf.toString();
55   }
56
57   public void setConfigFile(String JavaDoc conf) {
58     this.configFile = conf;
59   }
60
61   /** Process the config file, creating a Set of file names */
62   private void readConfig() throws BuildException {
63     if (configRead) return;
64
65     verifySettings();
66     File JavaDoc confFile = new File JavaDoc(this.configFile);
67     try {
68       BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(confFile));
69       String JavaDoc line=null;
70       while ( (line = br.readLine()) != null) {
71         // Ignore odd lines. Xalan has an annoying habit of writing XML
72
// declarations in the middle of the file.
73
if (! (line.charAt(0) == ' ' || line.charAt(0) == '<') ) {
74           if (! goodFiles.contains(line)) {
75             goodFiles.add(line);
76           }
77         }
78       }
79     } catch (FileNotFoundException JavaDoc e) {
80       throw new BuildException("Couldn't find config file "+this.configFile);
81     } catch (IOException JavaDoc ioe) {
82       throw new BuildException("Error reading config file "+this.configFile);
83     }
84     configRead = true;
85   }
86
87   /**
88    * When using this as a custom selector, this method will be called.
89    * It translates each parameter into the appropriate setXXX() call.
90    *
91    * @param parameters the complete set of parameters for this selector
92    */

93   public void setParameters(Parameter[] parameters) {
94     super.setParameters(parameters);
95     if (parameters != null) {
96       for (int i = 0; i < parameters.length; i++) {
97         String JavaDoc paramname = parameters[i].getName();
98         if (CONFIG_KEY.equalsIgnoreCase(paramname)) {
99           setConfigFile(parameters[i].getValue());
100         }
101         else {
102           setError("Invalid parameter " + paramname);
103         }
104       }
105     }
106   }
107
108   /**
109    * Checks to make sure all settings are kosher. In this case, it
110    * means that the name attribute has been set.
111    *
112    */

113   public void verifySettings() {
114     if (configFile == null) {
115       setError("The "+UncopiedFileSelector.CONFIG_KEY+" attribute is required");
116     }
117   }
118
119   /**
120    * The heart of the matter. This is where the selector gets to decide
121    * on the inclusion of a file in a particular fileset. Here we just check if
122    * the file is listed in <code>goodFiles</code>.
123    *
124    * @param basedir the base directory the scan is being done from
125    * @param filename is the name of the file to check
126    * @param file is a java.io.File object the selector can use
127    * @return whether the file should be selected or not
128    */

129   public boolean isSelected(File JavaDoc basedir, String JavaDoc filename, File JavaDoc file) {
130     validate();
131     readConfig();
132     return goodFiles.contains(filename);
133   }
134 }
135
Popular Tags