00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package dochelper;
00011
00012 import java.io.File;
00013 import java.io.IOException;
00014 import java.util.ArrayList;
00015 import java.util.HashMap;
00016
00017 import dochelper.exceptions.IllegalParamterTypeException;
00018 import dochelper.exceptions.InitializationException;
00019 import dochelper.exceptions.InvalidValueException;
00020 import dochelper.exceptions.MissingArgumentException;
00021 import dochelper.exceptions.ResourceUnavailableException;
00022 import dochelper.util.FileUtils;
00023
00035 public class FileRecordSet implements RecordSet {
00036
00040 protected static final String PATH = "path";
00041
00045 protected static final String RECUR = "recursive";
00046
00050 protected static final String GLOB = "glob";
00051
00055 private String name;
00056
00060 protected HashMap<String, Object> params = new HashMap<String, Object>();
00061
00065 protected ArrayList<Record> rsfiles;
00066
00067
00068 private ArrayList<DocPattern> patterns;
00069
00083 public void setParam(String paramName, Object value) throws IllegalParamterTypeException {
00084
00085 if (value instanceof String || value instanceof DocumentSection)
00086 params.put(paramName, value);
00087 else
00088 throw new IllegalParamterTypeException(paramName);
00089 }
00090
00102 public void checkParams() throws InvalidValueException, MissingArgumentException, ResourceUnavailableException {
00103
00104
00105 if (!params.containsKey(PATH))
00106 throw new MissingArgumentException(PATH);
00107
00108
00109 if (!params.containsKey(GLOB))
00110 throw new MissingArgumentException(GLOB);
00111
00112
00113 if (!params.containsKey(RECUR))
00114 throw new MissingArgumentException(RECUR);
00115
00116
00117 }
00118
00126 public void init() throws InitializationException, ResourceUnavailableException {
00127
00128
00129 setRsfiles(new ArrayList<Record>());
00130
00131
00132 String filter = FileUtils.globToRE((String)params.get(GLOB));
00133
00134 try {
00135
00136 String canonicalPath = new File((String)params.get(PATH)).getCanonicalPath();
00137
00138
00139
00140 _listDirectory(new ArrayList<String>(), rsfiles, canonicalPath, filter, "true".equals(((String)params.get(RECUR)).toLowerCase()));
00141
00142 }
00143 catch (Exception e) {
00144 InitializationException ie = new InitializationException(e.getMessage());
00145 e.printStackTrace();
00146 throw ie;
00147 }
00148 }
00149
00168 public ResultNode execute(ResultNode doc) throws ResourceUnavailableException {
00169
00170
00171 ResultNode result = new ResultNode(null, params, this.getName());
00172
00173
00174 for (Record record : rsfiles) {
00175
00176 result.addChild(record.execute(patterns));
00177 }
00178
00179 doc.addChild(result);
00180
00181 return doc;
00182 }
00183
00184
00185
00195 protected void _listDirectory(ArrayList stack, ArrayList files, String directory, String glob, boolean recursive) throws IOException {
00196
00197 if (stack.contains(directory)) {
00198
00199 return;
00200 }
00201 else
00202 stack.add(directory);
00203
00204 File[] _files = FileUtils._listDirectory(directory);
00205
00206 if (_files == null || _files.length == 0)
00207 return;
00208
00209 for (int i = 0;i < _files.length;i++) {
00210 File file = _files[i];
00211
00212 if (file.isDirectory()) {
00213
00214 if (recursive) {
00215
00216 String canonPath = file.getCanonicalPath();
00217
00218 _listDirectory(stack, files, canonPath, glob, recursive);
00219 }
00220 }
00221 else {
00222
00223 if (!java.util.regex.Pattern.matches(glob, file.getName()))
00224 continue;
00225
00226
00227
00228 files.add(new FileRecord(file, this.getName() + "Record"));
00229 }
00230 }
00231
00232 }
00233
00239 public ArrayList<Record> getRsfiles() {
00240 return rsfiles;
00241 }
00242
00247 public void setRsfiles(ArrayList<Record> rsfiles) {
00248 this.rsfiles = rsfiles;
00249 }
00250
00256 public ArrayList<DocPattern> getPatterns() {
00257 return patterns;
00258 }
00259
00264 public void setPatterns(ArrayList<DocPattern> patterns) {
00265 this.patterns = patterns;
00266 }
00267
00273 public String getName() {
00274 return name;
00275 }
00276
00277 public void setName(String name) {
00278 this.name = name;
00279 }
00280
00287 public void addPattern(DocPattern pattern) {
00288
00289 if (this.patterns == null)
00290 this.patterns = new ArrayList<DocPattern>();
00291
00292 this.patterns.add(pattern);
00293 }
00294
00299 public void clearPatterns() {
00300 this.patterns = new ArrayList<DocPattern>();
00301 }
00302
00303 public String toString() {
00304
00305 String out = "";
00306
00307 out += "File Record Set: [" + this.getName() + ", Files Count : " + rsfiles.size() + "]";
00308
00309
00310 return out;
00311 }
00312 }