View Javadoc

1   /*
2    * $Id: FileBasedView.java 474191 2006-11-13 08:30:40Z mrdon $
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  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          // get the contents as a single line
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          // links
60          matchPatterns(getLinkPattern(), targets, Link.TYPE_HREF);
61  
62          // actions
63          matchPatterns(getActionPattern(), targets, Link.TYPE_ACTION);
64  
65          // forms
66          matchPatterns(getFormPattern(), targets, Link.TYPE_FORM);
67  
68          return targets;
69      }
70  
71      protected Pattern getLinkPattern() {
72          // FIXME: work with new configuration style
73          //Object ext = Settings.get(StrutsConstants.STRUTS_ACTION_EXTENSION);
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 }