View Javadoc

1   /***
2    * 
3    */
4   package org.telscenter.pas.ui.browser;
5   
6   import java.util.ArrayList;
7   import java.util.List;
8   import java.util.regex.Matcher;
9   import java.util.regex.Pattern;
10  
11  /***
12   * A policy to restrict navigation
13   * 
14   * @author hiroki
15   * 
16   */
17  public class NavigationPolicy {
18  
19  	List<String> includes = new ArrayList<String>(); // list of URL patterns that are allowed
20  
21  	List<String> excludes = new ArrayList<String>(); // list of URL patterns that are not
22  										// allowed
23  
24  	/***
25  	 * Determines if the given url is compliant with the policy Algorithm:
26  	 * return (URL is "in" inclusion list) && (URL is not "in" exclusion list)
27  	 * 
28  	 * @param url
29  	 *            the url to check for compliancy
30  	 * @return boolean true iff the given url is compliant with this policy
31  	 */
32  	public boolean isCompliant(String url) {
33  		return isIncluded(url) && !isExcluded(url);
34  	}
35  
36  	/***
37  	 * @param url
38  	 * @return boolean true iff the url is in the list to be included
39  	 */
40  	private boolean isIncluded(String url) {
41  		return urlMatches(url, includes);
42  	}
43  
44  	/***
45  	 * @param url
46  	 * @return true iff the url is in the list to be excluded
47  	 */
48  	private boolean isExcluded(String url) {
49  		return urlMatches(url, excludes);
50  	}
51  
52  	/***
53  	 * @param url
54  	 * @param patternList
55  	 *            TODO
56  	 * @return
57  	 */
58  	private static boolean urlMatches(String url, List<String> patternList) {
59  		for (int i = 0; i < patternList.size(); i++) {
60  			Pattern p = Pattern.compile(patternList.get(i));
61  			Matcher m = p.matcher(url);
62  			if (m.matches()) {
63  				return true;
64  			}
65  		}
66  		return false;
67  	}
68  
69  	/***
70  	 * @return the excludes
71  	 */
72  	public List<String> getExcludes() {
73  		return excludes;
74  	}
75  
76  	/***
77  	 * @param excludes
78  	 *            the excludes to set
79  	 */
80  	public void setExcludes(List<String> excludes) {
81  		this.excludes = excludes;
82  	}
83  
84  	/***
85  	 * @return the includes
86  	 */
87  	public List<String> getIncludes() {
88  		return includes;
89  	}
90  
91  	/***
92  	 * @param includes
93  	 *            the includes to set
94  	 */
95  	public void setIncludes(List<String> includes) {
96  		this.includes = includes;
97  	}
98  }