View Javadoc

1   /*
2    * $Id: FileBasedView.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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          // get the contents as a single line
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          // links
61          matchPatterns(getLinkPattern(), targets, Link.TYPE_HREF);
62  
63          // actions
64          matchPatterns(getActionPattern(), targets, Link.TYPE_ACTION);
65  
66          // forms
67          matchPatterns(getFormPattern(), targets, Link.TYPE_FORM);
68  
69          return targets;
70      }
71  
72      protected Pattern getLinkPattern() {
73          // FIXME: work with new configuration style
74          //Object ext = Settings.get(StrutsConstants.STRUTS_ACTION_EXTENSION);
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 }