01: package com.opensymphony.webwork.sitegraph.entities;
02:
03: import com.opensymphony.util.FileUtils;
04: import com.opensymphony.webwork.config.Configuration;
05: import com.opensymphony.webwork.sitegraph.model.Link;
06: import com.opensymphony.webwork.WebWorkConstants;
07:
08: import java.io.File;
09: import java.util.Set;
10: import java.util.TreeSet;
11: import java.util.regex.Matcher;
12: import java.util.regex.Pattern;
13:
14: /**
15: * User: plightbo
16: * Date: Jun 25, 2005
17: * Time: 2:07:43 PM
18: */
19: public abstract class FileBasedView implements View {
20: private String name;
21: private String contents;
22:
23: public FileBasedView(File file) {
24: this .name = file.getName();
25: // get the contents as a single line
26: this .contents = FileUtils.readFile(file).replaceAll("[\r\n ]+",
27: " ");
28: }
29:
30: public String getName() {
31: return name;
32: }
33:
34: public Set getTargets() {
35: TreeSet targets = new TreeSet();
36:
37: // links
38: matchPatterns(getLinkPattern(), targets, Link.TYPE_HREF);
39:
40: // actions
41: matchPatterns(getActionPattern(), targets, Link.TYPE_ACTION);
42:
43: // forms
44: matchPatterns(getFormPattern(), targets, Link.TYPE_FORM);
45:
46: return targets;
47: }
48:
49: protected Pattern getLinkPattern() {
50: Object ext = Configuration
51: .get(WebWorkConstants.WEBWORK_ACTION_EXTENSION);
52: String actionRegex = "([A-Za-z0-9\\._\\-\\!]+\\." + ext + ")";
53: return Pattern.compile(actionRegex);
54: }
55:
56: private void matchPatterns(Pattern pattern, Set targets, int type) {
57: Matcher matcher = pattern.matcher(contents);
58: while (matcher.find()) {
59: String target = matcher.group(1);
60: targets.add(new Target(target, type));
61: }
62: }
63:
64: protected abstract Pattern getActionPattern();
65:
66: protected abstract Pattern getFormPattern();
67: }
|