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