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