KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jahia.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      * Method enumSize : returns the size of the given enumeration
136      *
137      * @param enumeration the enumeration
138      * @return its size
139      */

140     public static int enumSize(Enumeration JavaDoc enumeration) {
141         int i = 0;
142         while(enumeration.hasMoreElements()) {
143             i++;
144             enumeration.nextElement();
145         }
146         return i;
147     }
148
149
150     /**
151      * Method isBrowserVersion : tests the version of the browser thanks to the guiBean method
152      *
153      * @param gui the GuiBean used to test
154      * @param version the version to compare to
155      */

156     public static boolean isBrowserVersion(HttpServletRequest JavaDoc req, GuiBean gui, String JavaDoc version)
157     throws JahiaException {
158
159         version = version.toUpperCase();
160         if (version.equals("NS")) {
161             return gui.isNS(req);
162         }
163         if (version.equals("NS4")) {
164             return gui.isNS4(req);
165         }
166         if (version.equals("NS6")) {
167             return gui.isNS6(req);
168         }
169         if (version.equals("IE")) {
170             return gui.isIE(req);
171         }
172         if (version.equals("IE4")) {
173             return gui.isIE4(req);
174         }
175         if (version.equals("IE5")) {
176             return gui.isIE5(req);
177         }
178         if (version.equals("IE6")) {
179             return gui.isIE6(req);
180         }
181         return false;
182     }
183
184 }
185
Popular Tags