📄 localutil.java
字号:
/* * Copyright 2005 Joe Walker * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.directwebremoting.util;import java.io.IOException;import java.io.InputStream;import java.io.RandomAccessFile;import java.io.Reader;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.net.URLDecoder;import java.util.ArrayList;import java.util.Arrays;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Locale;import java.util.Map;import java.util.Set;import javax.servlet.ServletConfig;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import javax.xml.transform.TransformerFactoryConfigurationError;/** * Various utilities, mostly to make up for JDK 1.4 functionallity that is not * in JDK 1.3 * @author Joe Walker [joe at getahead dot ltd dot uk] */public final class LocalUtil{ /** * splitInbound() returns the type info in this parameter */ public static final int INBOUND_INDEX_TYPE = 0; /** * splitInbound() returns the value info in this parameter */ public static final int INBOUND_INDEX_VALUE = 1; /** * Prevent instansiation */ private LocalUtil() { } /** * Replacement for String#replaceAll(String, String) in JDK 1.4+ * @param text source text * @param repl the stuff to get rid of * @param with the stuff to replace it with * @return replaced text or null if any args are null */ public static String replace(String text, String repl, String with) { if (text == null || repl == null || with == null || repl.length() == 0) { return text; } StringBuffer buf = new StringBuffer(text.length()); int searchFrom = 0; while (true) { int foundAt = text.indexOf(repl, searchFrom); if (foundAt == -1) { break; } buf.append(text.substring(searchFrom, foundAt)).append(with); searchFrom = foundAt + repl.length(); } buf.append(text.substring(searchFrom)); return buf.toString(); } /** * Determines if the specified string is permissible as a Java identifier. * Returns true if the string is non-null, non-zero length with a Java * identifier start as the first character and Java identifier parts in all * remaining characters. * @param test the string to be tested. * @return true if the string is a Java identifier, false otherwise. * @see java.lang.Character#isJavaIdentifierPart(char) * @see java.lang.Character#isJavaIdentifierStart(char) */ public static boolean isJavaIdentifier(String test) { if (test == null || test.length() == 0) { return false; } if (!Character.isJavaIdentifierStart(test.charAt(0)) && test.charAt(0) != '_') { return false; } for (int i = 1; i < test.length(); i++) { if (!Character.isJavaIdentifierPart(test.charAt(i)) && test.charAt(i) != '_') { return false; } } return true; } /** * Determines if the specified string contains only Unicode letters or * digits as defined by {@link Character#isLetterOrDigit(char)} * @param test The string to test * @return true if the string is non-null, non-empty and contains only * characters that are unicode letters or digits * @see Character#isLetterOrDigit(char) */ public static boolean isLetterOrDigitOrUnderline(String test) { if (test == null || test.length() == 0) { return false; } for (int i = 0; i < test.length(); i++) { if (!Character.isLetterOrDigit(test.charAt(i)) && test.charAt(i) != '_') { return false; } } return true; } /** * True if c1 is java.lang.Boolean and c2 is boolean, etc. * @param c1 the first class to test * @param c2 the second class to test * @return true if the classes are equivalent */ public static boolean isEquivalent(Class c1, Class c2) { if (c1 == Boolean.class || c1 == Boolean.TYPE) { return c2 == Boolean.class || c2 == Boolean.TYPE; } else if (c1 == Byte.class || c1 == Byte.TYPE) { return c2 == Byte.class || c2 == Byte.TYPE; } else if (c1 == Character.class || c1 == Character.TYPE) { return c2 == Character.class || c2 == Character.TYPE; } else if (c1 == Short.class || c1 == Short.TYPE) { return c2 == Short.class || c2 == Short.TYPE; } else if (c1 == Integer.class || c1 == Integer.TYPE) { return c2 == Integer.class || c2 == Integer.TYPE; } else if (c1 == Long.class || c1 == Long.TYPE) { return c2 == Long.class || c2 == Long.TYPE; } else if (c1 == Float.class || c1 == Float.TYPE) { return c2 == Float.class || c2 == Float.TYPE; } else if (c1 == Double.class || c1 == Double.TYPE) { return c2 == Double.class || c2 == Double.TYPE; } else if (c1 == Void.class || c1 == Void.TYPE) { return c2 == Void.class || c2 == Void.TYPE; } return false; } /** * @param type The class to de-primitivize * @return The non-privitive version of the class */ public static Class getNonPrimitiveType(Class type) { if (!type.isPrimitive()) { return type; } else if (type == Boolean.TYPE) { return Boolean.class; } else if (type == Byte.TYPE) { return Byte.class; } else if (type == Character.TYPE) { return Character.class; } else if (type == Short.TYPE) { return Short.class; } else if (type == Integer.TYPE) { return Integer.class; } else if (type == Long.TYPE) { return Long.class; } else if (type == Float.TYPE) { return Float.class; } else if (type == Double.TYPE) { return Double.class; } else if (type == Void.TYPE) { return Void.class; } return null; } /** * Add headers to prevent browers and proxies from caching this reply. * @param resp The response to add headers to */ public static void addNoCacheHeaders(HttpServletResponse resp) { // Set standard HTTP/1.1 no-cache headers. resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate"); // Set IE extended HTTP/1.1 no-cache headers (use addHeader). resp.addHeader("Cache-Control", "post-check=0, pre-check=0"); // Set standard HTTP/1.0 no-cache header. resp.setHeader("Pragma", "no-cache"); // Set to expire far in the past. Prevents caching at the proxy server resp.setHeader("Expires", "Sat, 6 May 1995 12:00:00 GMT"); } /** * Is this class one that we auto fill, so the user can ignore? * @param paramType The type to test * @return true if the type is a Servlet type */ public static boolean isServletClass(Class paramType) { return paramType == HttpServletRequest.class || paramType == HttpServletResponse.class || paramType == ServletConfig.class || paramType == ServletContext.class || paramType == HttpSession.class; } /** * URL decode a value. This method gets around the lack of a * decode(String, String) method in JDK 1.3. * @param value The string to decode * @return The decoded string */ public static String decode(String value) { if (!testedDecoder) { try { decode14 = URLDecoder.class.getMethod("decode", new Class[] { String.class, String.class }); } catch (Exception ex) { if (!warn13) { log.warn("URLDecoder.decode(String, String) is not available. Falling back to 1.3 variant."); warn13 = true; } } testedDecoder = true; } if (decode14 != null) { try { return (String) decode14.invoke(null, new Object[] { value, "UTF-8" }); } catch (Exception ex) { log.warn("Failed to use JDK 1.4 decoder", ex); } } return URLDecoder.decode(value); } /** * Set use reflection to set the setters on the object called by the keys * in the params map with the corresponding values * @param object The object to setup * @param params The settings to use * @param ignore List of keys to not warn about if they are not properties * Note only the warning is skipped, we still try the setter */ public static void setParams(Object object, Map params, List ignore) { for (Iterator it = params.entrySet().iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); String key = (String) entry.getKey();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -