001: /*
002: * Created on Feb 12, 2004
003: *
004: * To change the template for this generated file go to
005: * Window>Preferences>Java>Code Generation>Code and Comments
006: */
007: package org.xdev.base.xssl.io;
008:
009: import java.io.File;
010: import java.io.FilenameFilter;
011: import java.util.HashMap;
012: import java.util.StringTokenizer;
013:
014: import java.util.regex.Pattern;
015: import java.util.regex.Matcher;
016:
017: import org.apache.log4j.Level;
018: import org.xdev.base.core.BASE;
019: import org.xdev.base.log.LoggerWriter;
020:
021: /**
022: * @author AYegorov
023: *
024: * To change the template for this generated type comment go to
025: * Window>Preferences>Java>Code Generation>Code and Comments
026: */
027: public class FileList extends AbstractFile {
028: /**
029: * @param id
030: */
031: public FileList(String id) {
032: super (id);
033: // XXX Auto-generated constructor stub
034: }
035:
036: /**
037: * @param id
038: * @param properties
039: */
040: public FileList(String id, HashMap properties) {
041: super (id, properties);
042: // XXX Auto-generated constructor stub
043: }
044:
045: /* (non-Javadoc)
046: * @see org.xdev.base.xssl.io.AbstractFile#processFile(java.io.File)
047: */
048: protected Object processFile(File f) throws Exception {
049:
050: String[] props = new String[] { "filter", "find", "match" };
051:
052: final String fileNameFilter = this .getProperty(props);
053:
054: final int code = this .hasProperty(props);
055:
056: return f.listFiles(new FilenameFilter() {
057: public boolean accept(File dir, String name) {
058: boolean flag = true;
059:
060: try {
061: switch (code) {
062:
063: case 0:
064:
065: LoggerWriter.log("Evaluating file " + name
066: + " against filter " + fileNameFilter,
067: Level.DEBUG_INT, this .getClass());
068:
069: StringTokenizer tokens = new StringTokenizer(
070: fileNameFilter, "*");
071:
072: String token = null;
073:
074: int size = tokens.countTokens();
075:
076: for (int i = 0; i < size; i++) {
077:
078: token = tokens.nextToken();
079:
080: if (!"".equals(token)) {
081:
082: LoggerWriter.log("At index " + i
083: + " evaluating file " + name
084: + " against filter token "
085: + token, Level.DEBUG_INT, this
086: .getClass());
087:
088: if (i == 0 && !name.startsWith(token)) {
089: LoggerWriter.log(name
090: + " does not start with "
091: + token, Level.DEBUG_INT,
092: this .getClass());
093:
094: flag = false;
095: break;
096: } else if (i > 0 && i == size - 1
097: && !name.endsWith(token)) {
098: LoggerWriter.log(name
099: + " does not end with "
100: + token, Level.DEBUG_INT,
101: this .getClass());
102:
103: flag = false;
104: break;
105: } else if (i > 0 && i < size
106: && name.indexOf(token) == -1) {
107: LoggerWriter.log(name
108: + " does not contain "
109: + token, Level.DEBUG_INT,
110: this .getClass());
111:
112: flag = false;
113: break;
114: }
115:
116: LoggerWriter
117: .log(
118: "Token match found, finding next token...",
119: Level.DEBUG_INT, this
120: .getClass());
121:
122: name = name
123: .substring(name.indexOf(token)
124: + token.length(), name
125: .length());
126: }
127: }
128: break;
129:
130: case 1:
131: flag = this .getMatcher(fileNameFilter,
132: dir.getAbsolutePath() + "/" + name)
133: .find();
134: break;
135:
136: case 2:
137: flag = this .getMatcher(fileNameFilter,
138: dir.getAbsolutePath() + "/" + name)
139: .matches();
140: break;
141: }
142: } catch (Exception ex) {
143: flag = false;
144:
145: LoggerWriter.log(BASE.getValueFromObject(ex),
146: Level.ERROR_INT, this .getClass());
147: }
148:
149: return flag;
150: }
151:
152: private Matcher getMatcher(String fileNameFilter,
153: String name) throws Exception {
154: Pattern p = Pattern.compile(fileNameFilter);
155:
156: Matcher m = p.matcher(name);
157:
158: return m;
159: }
160: });
161: }
162:
163: }
|