📄 utils.java
字号:
package org.jahia.taglibs.util;import java.lang.*;import java.util.Enumeration;import javax.servlet.http.HttpServletRequest;import org.jahia.exceptions.JahiaException;import org.jahia.gui.GuiBean;import org.jahia.utils.JahiaConsole;/** * Class Utils : provides miscellaneous methods used by the taglibs * * @author Jerome Tamiotti * */public class Utils { private static String SEPARATOR = "_"; /** * Method buildUniqueName : builds a unique name for a field, thanks to the name * of the container which contains it * * @param parentName : the name of the parent tag (a container) * @param tagName : the name of the current tag (a container or a field) * * @return the new name for the current tag, used by Jahia to retrieve him */ public static String buildUniqueName (String parentName, String tagName) { StringBuffer buffer = new StringBuffer(parentName); buffer.append(SEPARATOR); buffer.append(tagName); return buffer.toString(); } /** * Method getParentName : returns the name of the parent, that means the complete name * without the last container name * * @param completeName : the complete path * @param tagName : the name to cut * * @return the name for the parent tag */ public static String getParentName (String completeName, String tagName) { if (completeName.endsWith(SEPARATOR + tagName)) { // checks if the completeName ends with the tagName int last = completeName.length() - tagName.length() - 1; // -1 for '_' return completeName.substring(0, last); } else if (completeName.equals(tagName)) { // or if it IS the same return ""; } // nothing to cut return completeName; } /** * Method replace : replaces a token in a string with another given String * * @param str the String to be parsed * @param token the token to be found and replaced * @param replaceValue the value to insert where token is found * @return the new String */ public static String replace(String str, String token, String replaceValue) { String result = ""; int i = str.indexOf(token); while (i != -1) { result += str.substring(0,i) + replaceValue; str = str.substring(i + token.length(), str.length()); i = str.indexOf(token); } return result + str; } /** * Method insertContextPath : insert the URL of the context in the link * Used for accessing images : * E.g. : title = "<img src='images/icon.gif'>" * will be changed in : "<img src='$contextPath/images/icon.gif'>" * where $contextPath is read in the request * * @param contextPath the String to be parsed * @param link the link to change * * @return the new link */ public static String insertContextPath(String contextPath, String link) { int pos = link.indexOf("src="); if (pos != -1) { StringBuffer tmp = new StringBuffer(link); // insert after "src='" tmp = tmp.insert(pos + 5, contextPath + "/"); return tmp.toString(); } // no image in link : remains the same return link; } /** * Method getShortClassName : from a fully-qualified class name, * returns only the name of the class * E.g. : full class name = "java.lang.String" * returns "String" * * @param theClass the class whose name is wanted * * @return the short name */ public static String getShortClassName(Class theClass) { String fullName = theClass.getName(); int lastDot = fullName.lastIndexOf("."); if (lastDot == -1) { JahiaConsole.println("Utils: getShortClassName ", "The class name contains no dot."); return fullName; } return fullName.substring(lastDot + 1); } /** * Method compare : compare two arithmetic values by using the given operator * * @param leftOperand the left operand * @param rightOperand the right operand * @param operator the operator : EQU equals to * NEQ not equals to * GT greater than * GTE greater than or equals * LT less than * LTE less than or equals * * @return the boolan result of the comparison */ public static boolean compare(int leftOperand, String operator, int rightOperand) { if (operator.toUpperCase().equals("EQU") || operator.equals("==") || operator.equals("=")) { return leftOperand == rightOperand; } if (operator.toUpperCase().equals("NEQ") || operator.equals("!=")) { return leftOperand != rightOperand; } if (operator.toUpperCase().equals("LT") || operator.equals("<")) { return leftOperand < rightOperand; } if (operator.toUpperCase().equals("LTE") || operator.equals("<=")) { return leftOperand <= rightOperand; } if (operator.toUpperCase().equals("GT") || operator.equals(">")) { return leftOperand > rightOperand; } if (operator.toUpperCase().equals("GTE") || operator.equals(">=")) { return leftOperand >= rightOperand; } JahiaConsole.println("org.jahia.taglibs.Utils.compare()", "Invalid operator !"); return false; } /** * Method enumSize : returns the size of the given enumeration * * @param enum the enumeration * @return its size */ public static int enumSize(Enumeration enum) { int i = 0; while(enum.hasMoreElements()) { i++; enum.nextElement(); } return i; } /** * Method isBrowserVersion : tests the version of the browser thanks to the guiBean method * * @param gui the GuiBean used to test * @param version the version to compare to */ public static boolean isBrowserVersion(HttpServletRequest req, GuiBean gui, String version) throws JahiaException { version = version.toUpperCase(); if (version.equals("NS")) { return gui.isNS(req); } if (version.equals("NS4")) { return gui.isNS4(req); } if (version.equals("NS6")) { return gui.isNS6(req); } if (version.equals("IE")) { return gui.isIE(req); } if (version.equals("IE4")) { return gui.isIE4(req); } if (version.equals("IE5")) { return gui.isIE5(req); } if (version.equals("IE6")) { return gui.isIE6(req); } return false; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -