KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > webFlow > entities > FileBasedView


1 package com.opensymphony.webwork.webFlow.entities;
2
3 import com.opensymphony.util.FileUtils;
4 import com.opensymphony.webwork.config.Configuration;
5 import com.opensymphony.webwork.webFlow.model.Link;
6
7 import java.io.File JavaDoc;
8 import java.util.HashSet JavaDoc;
9 import java.util.Set JavaDoc;
10 import java.util.TreeSet JavaDoc;
11 import java.util.regex.Matcher JavaDoc;
12 import java.util.regex.Pattern JavaDoc;
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 JavaDoc name;
21     private String JavaDoc contents;
22
23     public FileBasedView(File JavaDoc 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     public String JavaDoc getName() {
30         return name;
31     }
32
33     public Set JavaDoc getTargets() {
34         TreeSet JavaDoc targets = new TreeSet JavaDoc();
35
36         // links
37
matchPatterns(getLinkPattern(), targets, Link.TYPE_HREF);
38
39         // actions
40
matchPatterns(getActionPattern(), targets, Link.TYPE_ACTION);
41
42         // forms
43
matchPatterns(getFormPattern(), targets, Link.TYPE_FORM);
44
45         return targets;
46     }
47
48     protected Pattern JavaDoc getLinkPattern() {
49         Object JavaDoc ext = Configuration.get("webwork.action.extension");
50         String JavaDoc actionRegex = "([A-Za-z0-9\\._\\-\\!]+\\." + ext + ")";
51         return Pattern.compile(actionRegex);
52     }
53
54     private void matchPatterns(Pattern JavaDoc pattern, Set JavaDoc targets, int type) {
55         Matcher JavaDoc matcher = pattern.matcher(contents);
56         while (matcher.find()) {
57             String JavaDoc target = matcher.group(1);
58             targets.add(new Target(target, type));
59         }
60     }
61
62     protected abstract Pattern JavaDoc getActionPattern();
63
64     protected abstract Pattern JavaDoc getFormPattern();
65 }
66
Popular Tags