01: /*
02: * Created on 9 Aug 2007
03: */
04: package uk.org.ponder.beanutil.support;
05:
06: import uk.org.ponder.beanutil.BeanPredicateModel;
07: import uk.org.ponder.beanutil.PathUtil;
08: import uk.org.ponder.stringutil.StringList;
09:
10: public class ListBeanPredicateModel implements BeanPredicateModel {
11: private StringList paths = new StringList();
12:
13: public void clear() {
14: paths.clear();
15: }
16:
17: public static String matchPath(String spec, String path) {
18: String togo = "";
19: while (true) {
20: if (spec == null || spec.equals(""))
21: break;
22: if (path == null || path.equals(""))
23: return null;
24: String spechead = PathUtil.getHeadPath(spec);
25: String pathhead = PathUtil.getHeadPath(path);
26: // if we fail to match on a specific component, fail.
27: if (!(spechead.equals("*") || spechead.equals(pathhead)))
28: return null;
29: togo = PathUtil.composePath(togo, pathhead);
30: spec = PathUtil.getFromHeadPath(spec);
31: path = PathUtil.getFromHeadPath(path);
32: }
33: return togo;
34: }
35:
36: public String findMatch(String spec, boolean isspec) {
37: for (int i = 0; i < paths.size(); ++i) {
38: String tocheck = paths.stringAt(i);
39: String match = isspec ? matchPath(spec, tocheck)
40: : matchPath(tocheck, spec);
41: if (match != null)
42: return match;
43: }
44: return null;
45: }
46:
47: public void setPaths(StringList paths) {
48: for (int i = 0; i < paths.size(); ++i) {
49: addPath(paths.stringAt(i));
50: }
51: }
52:
53: public void addPath(String path) {
54: paths.add(path);
55: }
56:
57: public boolean isMatch(String path) {
58: return findMatch(path, false) != null;
59: }
60: }
|