View Javadoc

1   /*
2    * $Id: FileBasedView.java 454251 2006-10-09 02:10:57Z husted $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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          // get the contents as a single line
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          // links
58          matchPatterns(getLinkPattern(), targets, Link.TYPE_HREF);
59  
60          // actions
61          matchPatterns(getActionPattern(), targets, Link.TYPE_ACTION);
62  
63          // forms
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 }