1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.sitegraph.entities;
19
20 import java.io.BufferedReader;
21 import java.io.File;
22 import java.io.FileNotFoundException;
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.util.Set;
26 import java.util.TreeSet;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.struts2.StrutsConstants;
33 import org.apache.struts2.config.Settings;
34 import org.apache.struts2.sitegraph.model.Link;
35
36 /***
37 */
38 public abstract class FileBasedView implements View {
39 private String name;
40 private String contents;
41
42 private static final Log log = LogFactory.getLog(FileBasedView.class);
43
44 public FileBasedView(File file) {
45 this.name = file.getName();
46
47 this.contents = readFile(file).replaceAll("[\r\n ]+", " ");
48 }
49
50 public String getName() {
51 return name;
52 }
53
54 public Set getTargets() {
55 TreeSet targets = new TreeSet();
56
57
58 matchPatterns(getLinkPattern(), targets, Link.TYPE_HREF);
59
60
61 matchPatterns(getActionPattern(), targets, Link.TYPE_ACTION);
62
63
64 matchPatterns(getFormPattern(), targets, Link.TYPE_FORM);
65
66 return targets;
67 }
68
69 protected Pattern getLinkPattern() {
70 Object ext = Settings.get(StrutsConstants.STRUTS_ACTION_EXTENSION);
71 String actionRegex = "([A-Za-z0-9//._//-//!]+//." + ext + ")";
72 return Pattern.compile(actionRegex);
73 }
74
75 private void matchPatterns(Pattern pattern, Set targets, int type) {
76 Matcher matcher = pattern.matcher(contents);
77 while (matcher.find()) {
78 String target = matcher.group(1);
79 targets.add(new Target(target, type));
80 }
81 }
82
83 protected abstract Pattern getActionPattern();
84
85 protected abstract Pattern getFormPattern();
86
87 protected String readFile(File file) {
88 try {
89 BufferedReader in = new BufferedReader(new FileReader(file));
90
91 String s = new String();
92 StringBuffer buffer = new StringBuffer();
93
94 while ((s = in.readLine()) != null) {
95 buffer.append(s + "\n");
96 }
97
98 in.close();
99
100 return buffer.toString();
101 } catch (FileNotFoundException e) {
102 log.warn("File not found");
103 } catch (IOException e) {
104 log.error(e);
105 }
106
107 return null;
108 }
109 }