KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > htmlcache > UserAgentGroup


1 package org.jahia.services.htmlcache;
2
3 /**
4  * Title: Jahia
5  * Description:
6  * Copyright: Copyright (c) 2002
7  * Company: Jahia Ltd
8  * @author Serge Huber
9  * @version 1.0
10  */

11
12 import java.util.Iterator JavaDoc;
13 import java.util.Set JavaDoc;
14 import java.util.TreeSet JavaDoc;
15
16 import org.apache.regexp.RE;
17 import org.apache.regexp.RESyntaxException;
18 import java.util.ArrayList JavaDoc;
19
20 /**
21  * The purpose of this class is to store all the information relative to a group
22  * of user agents, as well as the regular expressions that are use to evaluate
23  * if a new user agent should fit into this group.
24  * @author Serge Huber
25  */

26 public class UserAgentGroup {
27
28     private static org.apache.log4j.Logger logger =
29             org.apache.log4j.Logger.getLogger (UserAgentGroup.class);
30
31     private String JavaDoc name;
32     private ArrayList JavaDoc regexpList = new ArrayList JavaDoc();
33     private ArrayList JavaDoc regexpStrings = new ArrayList JavaDoc();
34     private Set JavaDoc userAgentSet = new TreeSet JavaDoc();
35
36     /**
37      * This constructor is used notably when de-serializing objects from
38      * persistant storage, in the case of this implementation most probably
39      * from XML files.
40      * @param name the name of the group
41      * @param regexpStrings a vector of Strings that contain the regular expressions
42      * to be evaluated using the matchesInsertCriterias method. This allows for
43      * quick evaluation of wether a newly encountered user agent should be
44      * insert into this group. This is a list because the order of the entries
45      * is crucial to correct evaluation of the criterias
46      * @param userAgentSet a list of Strings containing user agent string to
47      * initialize the group with.
48      */

49     public UserAgentGroup(String JavaDoc name, ArrayList JavaDoc regexpStrings, Set JavaDoc userAgentSet) {
50         this.name = name;
51         Iterator JavaDoc regexpIter = regexpStrings.iterator();
52         while (regexpIter.hasNext()) {
53             Object JavaDoc curRegExpObj = regexpIter.next();
54             if (curRegExpObj instanceof String JavaDoc) {
55                 String JavaDoc curRegExpStr = (String JavaDoc) curRegExpObj;
56                 try {
57                     RE curRE = new RE(curRegExpStr);
58                     this.regexpList.add(curRE);
59                     this.regexpStrings.add(curRegExpStr);
60                 } catch (RESyntaxException res) {
61                     logger.error("Invalid regular expression syntax : " +
62                                          curRegExpStr + ", ignoring it...");
63                 }
64             }
65         }
66         Iterator JavaDoc userAgentIter = userAgentSet.iterator();
67         while (userAgentIter.hasNext()) {
68             Object JavaDoc userAgentObj = userAgentIter.next();
69             if (userAgentObj instanceof String JavaDoc) {
70                 String JavaDoc userAgentStr = (String JavaDoc) userAgentObj;
71                 this.userAgentSet.add(userAgentStr);
72             }
73         }
74     }
75
76     /**
77      * Tests the specified user agent against all the regular expressions
78      * declared for this group.
79      * @param newUserAgent a String containing the user agent to test.
80      * @return true as soon as a regexp matched the specified user agent string.
81      * The evaluation is immediately stopped as soon as the first regexp matches
82      * the user agent string
83      */

84     public boolean matchesInsertCriterias(String JavaDoc newUserAgent) {
85         Iterator JavaDoc regexpIter = regexpList.iterator();
86         while (regexpIter.hasNext()) {
87             RE curRE = (RE) regexpIter.next();
88             if (curRE.match(newUserAgent)) {
89                 return true;
90             }
91         }
92         return false;
93     }
94
95     /**
96      * Returns the name of this user agent group
97      * @return a String object containing the name of this user agent group.
98      */

99     public String JavaDoc getName() {
100         return name;
101     }
102
103     /**
104      * Returns an enumation to be able to view all the elements of the regular
105      * expression list.
106      * @return an enumeration of String objects that contain the regular
107      * expression. The order should normally be the same as at the time of
108      * creation of the UserAgentGroup instance.
109      * @todo FIXME : test the ordering...
110      */

111     public Iterator JavaDoc getRegExpStrings() {
112         return regexpStrings.iterator();
113     }
114
115     /**
116      * Returns an iterator on a set of user agent strings that are contained in
117      * this group.
118      * @return an Iterator object on a Set of String objects that contain the
119      * user agent string inserted when creating this instance of the object.
120      */

121     public Iterator JavaDoc getUserAgentSetIterator() {
122         return userAgentSet.iterator();
123     }
124
125     /**
126      * Adds the specified user agent into the set of this group. Warning : there
127      * is no verification that this user agent complies to the regular expressions
128      * Use matchInsertCriterias first to know if you should insert this agent
129      * in this group.
130      * @param newUserAgent the new user agent String to insert in this group.
131      * @return true if insertion was successful, false otherwise (which means
132      * the user agent already exists in the set).
133      */

134     public boolean setUserAgent(String JavaDoc newUserAgent) {
135         return userAgentSet.add(newUserAgent);
136     }
137
138 }
139
Popular Tags