📄 jahiatools.java
字号:
} //------------------------------------------------------------------------- /** * Return a substitute String value if the source is null otherwise * return the source value * * @param String the data * @param String the subsitute value * @return String * @author NK */ static public String replaceNullString(String data, String newValue){ if ( data != null ){ return data; } return newValue; } //------------------------------------------------------------------------- /** * Return a substitute Integer object if the source is null otherwise * return the source integer object * * @param Integer data * @param Integer newValue * @return String * @author NK */ static public Integer replaceNullInteger(Integer data, Integer newValue){ if ( data != null ){ return data; } return newValue; } //------------------------------------------------------------------------- /** * Guarantee a String not to be null, giving it an empty value if needed. * * @author Mikha雔 Janson * @param inputString A <code>String</code> value, that may be null * * @return a <code>String</code> value, guaranteed not to be null. */ public static String nnString( String inputString ) { String outputString = (inputString != null) ? inputString : ""; return outputString; } // end nnString //------------------------------------------------------------------------- /** * Simple <code>int</code> to <code>boolean</code> converter * * @param value an integer value * @return <code>false</code> if <code>0</code>, <code>1</code> if not <code>0</code> */ static public boolean int2boolean(int value) { return value != 0; } //------------------------------------------------------------------------- /** * Simple <code>boolean</code> to <code>int</code> converter * * @param value a <code>boolean</code> value * @return <code>0</code> if <code>false</code>, <code>1</code> if <code>true</code> */ static public int boolean2int(boolean value) { return value ? 1 : 0; } //------------------------------------------------------------------------- /** * Method inverseVector : inverse the elements contained in a vector * * @param myVector the vector to inverse * @return the inversed vector */ static public Vector inverseVector(Vector myVector) { Vector temp = new Vector(); for(int i = myVector.size()-1; i >= 0; i--) { temp.addElement(myVector.get(i)); } return temp; } //------------------------------------------------------------------------- /** * Update a property's value in a properties file. * * @author Khue N'Guyen * @author Alexandre Kraft * * @param name the property name * @param val the property value * @param path the full filesystem path to the properties file */ public static void updatepropvalue( String propertyName, String propvalue, String path ) { Vector bufferVector = new Vector(); String lineReaded = null; int position = 0; boolean lineFound = false; boolean success = false; try { // parse the file... BufferedReader buffered = new BufferedReader( new FileReader(path) ); while((lineReaded = buffered.readLine()) != null) { if(lineReaded.indexOf(propertyName) >= 0) { position = lineReaded.lastIndexOf("="); if(position >= 0) { bufferVector.add( lineReaded.substring(0,position+1) + " " + propvalue ); lineFound = true; } } else { bufferVector.add(lineReaded); } } buffered.close(); // add property if it don't exists before... if(!lineFound) { bufferVector.add( propertyName + " = " + propvalue ); } // rewrite the file... File thisFile = new File(path); FileWriter fileWriter = new FileWriter( thisFile ); StringBuffer outputBuffer = new StringBuffer(); for(int i=0; i < bufferVector.size(); i++) { outputBuffer.append((String) bufferVector.get(i)); } fileWriter.write( outputBuffer.toString() ); fileWriter.close(); } catch (java.io.IOException ioe) { } } // end updatepropvalue /************************************************************************** * Client Browser Check Tools * **************************************************************************/ //------------------------------------------------------------------------- public static boolean isMSIExplorer(HttpServletRequest req){ return ( req.getHeader("user-agent") != null && req.getHeader("user-agent").indexOf("MSIE") != -1 ); } //------------------------------------------------------------------------- public static boolean isLynx(HttpServletRequest req){ return ( req.getHeader("user-agent") != null && req.getHeader("user-agent").indexOf("Lynx") != -1 ); } /************************************************************************** * Servlet Request Parameters handling * **************************************************************************/ //------------------------------------------------------------------------- /** * return a parameter of String type if not null or return the subsitute value * * @param HttpServletRequest req the request object * @param String the param name * @param String the subsitute value to return if the parameter is null * @return String the parameter value * @author NK */ public static String getStrParameter(HttpServletRequest req, String paramName, String nullVal){ String val = (String)req.getParameter(paramName); if ( val == null ){ return nullVal; } return val; } //------------------------------------------------------------------------- /** * return a parameter of Integer type if not null or return the subsitute value * * @param HttpServletRequest req the request object * @param String the param name * @param Integer the subsitute value to return if the parameter is null * @return Integer the parameter value * @author NK */ public static Integer getIntParameter(HttpServletRequest req, String paramName, Integer nullVal){ String strVal = (String)req.getParameter(paramName); Integer val = null; if ( strVal == null ){ return nullVal; } else { try { val = new Integer(strVal); } catch ( Throwable t ){ val = nullVal; } } return val; } //------------------------------------------------------------------------- /** * Remove context attribute they names start with the string passed in * parameter. In details, this method select which attribute he wants to * remove and compose a temporary vector with it. Next step is to delete * all attributes contains in this vector, via an enumeration. The reason * of this is that you can't remove a context attribute while you use the * getAttributeNames method to get the list of context attribute names. * @author Alexandre Kraft * * @param context The context where do you want to remove attributes * @param startString The string for select the attributes by they names. */ public static void removeContextAttributes( ServletContext context, String startString ) { Enumeration contextAttributeNames = context.getAttributeNames(); Vector attributesToRemove = new Vector(); String attributeName; while(contextAttributeNames.hasMoreElements()) { attributeName = (String) contextAttributeNames.nextElement(); if(attributeName.length() >= 36) { if((attributeName.substring(0,36).equals(startString)) && (attributeName.indexOf("accessGranted") == -1)) { attributesToRemove.add( attributeName ); } } } Enumeration attributesListToRemove = attributesToRemove.elements(); while(attributesListToRemove.hasMoreElements()) { attributeName = (String) attributesListToRemove.nextElement(); context.removeAttribute( attributeName ); } } // end removeContextAttributes /************************************************************************** * HTML Tools * * *************************************************************************/ //------------------------------------------------------------------------- /*** * Return text with HTML entities (text2html) * @author POL * @version 1.0 POL 14/06/2001 * @version 1.1 MAP 23.10.2001 * @param str Input String * @return str HtmlEntities output */ public static String text2html( String str ) { return TextHtml.text2html(str); // Changed by MAP } //------------------------------------------------------------------------- /*** * Return text without HTML entities (html2text) * @author POL * @version 1.0 POL 18/06/2001 * @version 1.1 MAP 23.10.2001 * @param str Input String * @return str Text output */ public static String html2text(String str) { str = replacePattern(str,"X-AND-X","&"); str = replacePattern(str,"&","&"); return TextHtml.html2text(str); // Changed by MAP } //------------------------------------------------------------------------- /*** * text2XMLEntityRef * Parse a text and replace XML specific characters to their XML Entity Reference value. * @author Khue Nguyen * @param str Input String * @param invert int (0/1) 0 : text to XML Entity Reference / 1 : XML Entity Reference to text * @return str Text output */ public static String text2XMLEntityRef(String str, int invert) { if ( str == null || str.trim().equals("") ){ return str; } if (invert == 0) { // Change by MAP str = TextHtml.text2html(str); } else { str = TextHtml.html2text(str); } str = replacePattern(str,"&","&",invert); str = replacePattern(str,"<","<",invert); str = replacePattern(str,">",">",invert); str = replacePattern(str,"\"",""",invert); str = replacePattern(str,"'","'",invert); return str; } //------------------------------------------------------------------------- /** * Tests whether an URL is valid or not. * * @param URLString the String representation of the URL to test * @return <code>true</code> if the URL could be accessed; <code>false</code> * otherwise */ public static boolean isValidURL(String URLString) { try { URL testURL = new URL(URLString); testURL.openConnection(); return true; } catch (Exception e) { return false; } }} // end JahiaTools
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -