KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > deprecated > taglibs > util > Utils


1 package org.jahia.deprecated.taglibs.util;
2
3 import java.util.Enumeration JavaDoc;
4
5 import javax.servlet.http.HttpServletRequest JavaDoc;
6
7 import org.jahia.exceptions.JahiaException;
8 import org.jahia.gui.GuiBean;
9 import org.jahia.utils.JahiaConsole;
10
11 /**
12  * Class Utils : provides miscellaneous methods used by the taglibs
13  *
14  * @author Jerome Tamiotti
15  *
16  */

17 public class Utils {
18
19     private static String JavaDoc SEPARATOR = "_";
20
21
22     /**
23      * Method buildUniqueName : builds a unique name for a field, thanks to the name
24      * of the container which contains it
25      *
26      * @param parentName : the name of the parent tag (a container)
27      * @param tagName : the name of the current tag (a container or a field)
28      *
29      * @return the new name for the current tag, used by Jahia to retrieve him
30      */

31     public static String JavaDoc buildUniqueName (String JavaDoc parentName, String JavaDoc tagName) {
32
33         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(parentName);
34         buffer.append(SEPARATOR);
35         buffer.append(tagName);
36         return buffer.toString();
37     }
38
39
40     /**
41      * Method getParentName : returns the name of the parent, that means the complete name
42      * without the last container name
43      *
44      * @param completeName : the complete path
45      * @param tagName : the name to cut
46      *
47      * @return the name for the parent tag
48      */

49     public static String JavaDoc getParentName (String JavaDoc completeName, String JavaDoc tagName) {
50
51         if (completeName.endsWith(SEPARATOR + tagName)) {
52             // checks if the completeName ends with the tagName
53
int last = completeName.length() - tagName.length() - 1; // -1 for '_'
54
return completeName.substring(0, last);
55
56         } else if (completeName.equals(tagName)) {
57             // or if it IS the same
58
return "";
59         }
60         // nothing to cut
61
return completeName;
62     }
63
64
65     /**
66      * Method replace : replaces a token in a string with another given String
67      *
68      * @param str the String to be parsed
69      * @param token the token to be found and replaced
70      * @param replaceValue the value to insert where token is found
71      * @return the new String
72      */

73     public static String JavaDoc replace(String JavaDoc str, String JavaDoc token,
74                                  String JavaDoc replaceValue) {
75
76         String JavaDoc result = "";
77         int i = str.indexOf(token);
78         while (i != -1) {
79             result += str.substring(0,i) + replaceValue;
80             str = str.substring(i + token.length(), str.length());
81             i = str.indexOf(token);
82         }
83         return result + str;
84     }
85
86
87     /**
88      * Method insertContextPath : insert the URL of the context in the link
89      * Used for accessing images :
90      * E.g. : title = "<img SRC='images/icon.gif'>"
91      * will be changed in : "<img SRC='$contextPath/images/icon.gif'>"
92      * where $contextPath is read in the request
93      *
94      * @param contextPath the String to be parsed
95      * @param link the link to change
96      *
97      * @return the new link
98      */

99     public static String JavaDoc insertContextPath(String JavaDoc contextPath, String JavaDoc link) {
100         
101         int pos = link.indexOf("src=");
102         if (pos != -1) {
103             StringBuffer JavaDoc tmp = new StringBuffer JavaDoc(link);
104             // insert after "src='"
105
tmp = tmp.insert(pos + 5, contextPath + "/");
106             return tmp.toString();
107         }
108         // no image in link : remains the same
109
return link;
110     }
111  
112  
113     /**
114      * Method getShortClassName : from a fully-qualified class name,
115      * returns only the name of the class
116      * E.g. : full class name = "java.lang.String"
117      * returns "String"
118      *
119      * @param theClass the class whose name is wanted
120      *
121      * @return the short name
122      */

123     public static String JavaDoc getShortClassName(Class JavaDoc theClass) {
124      
125         String JavaDoc fullName = theClass.getName();
126         int lastDot = fullName.lastIndexOf(".");
127         if (lastDot == -1) {
128             JahiaConsole.println("Utils: getShortClassName ", "The class name contains no dot.");
129             return fullName;
130         }
131         return fullName.substring(lastDot + 1);
132     }
133
134
135     /**
136      * Method compare : compare two arithmetic values by using the given operator
137      *
138      * @param leftOperand the left operand
139      * @param rightOperand the right operand
140      * @param operator the operator : EQU equals to
141      * NEQ not equals to
142      * GT greater than
143      * GTE greater than or equals
144      * LT less than
145      * LTE less than or equals
146      *
147      * @return the boolan result of the comparison
148      */

149     public static boolean compare(int leftOperand, String JavaDoc operator, int rightOperand) {
150      
151         if (operator.toUpperCase().equals("EQU") || operator.equals("==") || operator.equals("=")) {
152             return leftOperand == rightOperand;
153         }
154         if (operator.toUpperCase().equals("NEQ") || operator.equals("!=")) {
155             return leftOperand != rightOperand;
156         }
157         if (operator.toUpperCase().equals("LT") || operator.equals("<")) {
158             return leftOperand < rightOperand;
159         }
160         if (operator.toUpperCase().equals("LTE") || operator.equals("<=")) {
161             return leftOperand <= rightOperand;
162         }
163         if (operator.toUpperCase().equals("GT") || operator.equals(">")) {
164             return leftOperand > rightOperand;
165         }
166         if (operator.toUpperCase().equals("GTE") || operator.equals(">=")) {
167             return leftOperand >= rightOperand;
168         }
169         JahiaConsole.println("org.jahia.deprecated.taglibs.Utils.compare()", "Invalid operator !");
170         return false;
171     }
172
173
174     /**
175      * Method enumSize : returns the size of the given enumeration
176      *
177      * @param enumeration the enumeration
178      * @return its size
179      */

180     public static int enumSize(Enumeration JavaDoc enumeration) {
181         int i = 0;
182         while(enumeration.hasMoreElements()) {
183             i++;
184             enumeration.nextElement();
185         }
186         return i;
187     }
188     
189     
190     /**
191      * Method isBrowserVersion : tests the version of the browser thanks to the guiBean method
192      *
193      * @param gui the GuiBean used to test
194      * @param version the version to compare to
195      */

196     public static boolean isBrowserVersion(HttpServletRequest JavaDoc req, GuiBean gui, String JavaDoc version)
197     throws JahiaException {
198         
199         version = version.toUpperCase();
200         if (version.equals("NS")) {
201             return gui.isNS(req);
202         }
203         if (version.equals("NS4")) {
204             return gui.isNS4(req);
205         }
206         if (version.equals("NS6")) {
207             return gui.isNS6(req);
208         }
209         if (version.equals("IE")) {
210             return gui.isIE(req);
211         }
212         if (version.equals("IE4")) {
213             return gui.isIE4(req);
214         }
215         if (version.equals("IE5")) {
216             return gui.isIE5(req);
217         }
218         if (version.equals("IE6")) {
219             return gui.isIE6(req);
220         }
221         return false;
222     }
223         
224 }
225
Popular Tags