util.java
来自「opennms得相关源码 请大家看看」· Java 代码 · 共 662 行 · 第 1/2 页
JAVA
662 行
if ((ignoreType == IgnoreType.REQUEST_ONLY || !ignoreList.contains(name)) && values != null) { for (int i = 0; i < values.length; i++) { buffer.append("<input type=\"hidden\" name=\""); buffer.append(name); buffer.append("\" value=\""); buffer.append(values[i]); buffer.append("\" />"); buffer.append("\n"); } } } return (buffer.toString()); } /** * Creates a query string of the format "key1=value1&key2=value2" for * each parameter in the given <code>HttpServletRequest</code>. * * @see #makeQueryString( HttpServletRequest, Map, String[] ) */ public static String makeQueryString(HttpServletRequest request) { return (makeQueryString(request, new HashMap(), new String[0])); } /** * Creates a query string of the format "key1=value1&key2=value2" for * each parameter in the given <code>HttpServletRequest</code> and key in * given <code>Map</code>. * * @see #makeQueryString( HttpServletRequest, Map, String[] ) */ public static String makeQueryString(HttpServletRequest request, Map additions) { return (makeQueryString(request, additions, new String[0])); } /** * Creates a query string of the format "key1=value1&key2=value2" for * each parameter in the given <code>HttpServletRequest</code> that is not * listed in the ignore list. * * @see #makeQueryString( HttpServletRequest, Map, String[] ) */ public static String makeQueryString(HttpServletRequest request, String[] ignores) { return (makeQueryString(request, new HashMap(), ignores)); } /** * Creates a query string of the format "key1=value1&key2=value2" for * each parameter in the given <code>HttpServletRequest</code> and key in * given <code>Map</code> that is not listed in the ignore list. * * @param request * the <code>HttpServletRequest</code> to read the parameters * from * @param additions * a mapping of strings to strings or string arrays to be * included in the query string * @param ignores * the list of parameters and map entries not to include * @return A string in the <em>x-www-form-urlencoded</em> format that is * suitable for adding to a URL as a query string. */ public static String makeQueryString(HttpServletRequest request, Map additions, String[] ignores) { return (makeQueryString(request, additions, ignores, IgnoreType.BOTH)); } /** * Creates a query string of the format "key1=value1&key2=value2" for * each parameter in the given <code>HttpServletRequest</code> and key in * given <code>Map</code> that is not listed in the ignore list. * * @param request * the <code>HttpServletRequest</code> to read the parameters * from * @param additions * a mapping of strings to strings or string arrays to be * included in the query string * @param ignores * the list of parameters and map entries not to include * @return A string in the <em>x-www-form-urlencoded</em> format that is * suitable for adding to a URL as a query string. */ public static String makeQueryString(HttpServletRequest request, Map additions, String[] ignores, IgnoreType ignoreType) { if (request == null || additions == null || ignores == null || ignoreType == null) { throw new IllegalArgumentException("Cannot take null parameters."); } StringBuffer buffer = new StringBuffer(); ArrayList ignoreList = new ArrayList(); for (int i = 0; i < ignores.length; i++) { ignoreList.add(ignores[i]); } Enumeration names = request.getParameterNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); String[] values = request.getParameterValues(name); if ((ignoreType == IgnoreType.ADDITIONS_ONLY || !ignoreList.contains(name)) && values != null) { for (int i = 0; i < values.length; i++) { buffer.append("&"); buffer.append(name); buffer.append("="); buffer.append(Util.encode(values[i])); } } } Set keySet = additions.keySet(); Iterator keys = keySet.iterator(); while (keys.hasNext()) { String name = (String) keys.next(); // handle both a String value or a String[] value Object tmp = additions.get(name); String[] values = (tmp instanceof String[]) ? ((String[]) tmp) : (new String[] { (String) tmp }); if ((ignoreType == IgnoreType.REQUEST_ONLY || !ignoreList.contains(name)) && values != null) { for (int i = 0; i < values.length; i++) { buffer.append("&"); buffer.append(name); buffer.append("="); buffer.append(Util.encode(values[i])); } } } // removes the first & from the buffer buffer.deleteCharAt(0); return (buffer.toString()); } public static class IgnoreType extends Object { public static final int _REQUEST_ONLY = 0; public static final int _ADDITIONS_ONLY = 0; public static final int _BOTH = 0; public static final IgnoreType REQUEST_ONLY = new IgnoreType(_REQUEST_ONLY); public static final IgnoreType ADDITIONS_ONLY = new IgnoreType(_ADDITIONS_ONLY); public static final IgnoreType BOTH = new IgnoreType(_BOTH); protected int value; private IgnoreType(int value) { this.value = value; } public int getValue() { return this.value; } } public static Map getOrderedMap(String names[][]) { TreeMap orderedMap = new TreeMap(); for (int i = 0; i < names.length; i++) { orderedMap.put(names[i][1], names[i][0]); } return orderedMap; } /** * Convenience method for reading data from a <code>Reader</code> and then * immediately writing that data to a <code>Writer</code> with a default * buffer size of one kilobyte (1,024 chars). * * @param in * a data source * @param out * a data sink */ public static void streamToStream(Reader in, Writer out) throws IOException { streamToStream(in, out, 1024); } /** * Convenience method for reading data from a <code>Reader</code> and then * immediately writing that data to a <code>Writer</code>. * * @param in * a data source * @param out * a data sink * @param bufferSize * the size of the <code>char</code> buffer to use for each * read/write */ public static void streamToStream(Reader in, Writer out, int bufferSize) throws IOException { if (in == null || out == null) { throw new IllegalArgumentException("Cannot take null parameters."); } if (bufferSize < 1) { throw new IllegalArgumentException("Cannot take negative buffer size."); } char[] b = new char[bufferSize]; int length; while ((length = in.read(b)) != -1) { out.write(b, 0, length); } } /** * Convenience method for reading data from an <code>InputStream</code> * and then immediately writing that data to an <code>OutputStream</code> * with a default buffer size of one kilobyte (1,024 bytes). * * @param in * a data source * @param out * a data sink */ public static void streamToStream(InputStream in, OutputStream out) throws IOException { streamToStream(in, out, 1024); } /** * Convenience method for reading data from an <code>InputStream</code> * and then immediately writing that data to an <code>OutputStream</code>. * * @param in * a data source * @param out * a data sink * @param bufferSize * the size of the <code>byte</code> buffer to use for each * read/write */ public static void streamToStream(InputStream in, OutputStream out, int bufferSize) throws IOException { byte[] b = new byte[bufferSize]; int length; while ((length = in.read(b)) != -1) { out.write(b, 0, length); } } /** * Convenience method for creating arrays of strings suitable for use as * command-line parameters when executing an external process. * * <p> * The default {@link Runtime#exec Runtime.exec}method will split a single * string based on spaces, but it does not respect spaces within quotation * marks, and it will leave the quotation marks in the resulting substrings. * This method solves those problems by replacing all in-quote spaces with * the given delimiter, removes the quotes, and then splits the resulting * string by the remaining out-of-quote spaces. It then goes through each * substring and replaces the delimiters with spaces. * </p> * * <p> * <em>Caveat:</em> This method does not respect escaped quotes! It will * simply remove them and leave the stray escape characters. * </p> * * @param s * the string to split * @param delim * a char that does not already exist in <code>s</code> * @return An array of strings split by spaces outside of quotes. * @throws IllegalArgumentException * If <code>s</code> is null or if <code>delim</code> * already exists in <code>s</code>. */ public static String[] createCommandArray(String s, char delim) { if (s == null) { throw new IllegalArgumentException("Cannot take null parameters."); } if (s.indexOf(delim) != -1) { throw new IllegalArgumentException("String parameter cannot already contain delimiter character: " + delim); } char[] chars = s.toCharArray(); boolean inquote = false; StringBuffer buffer = new StringBuffer(); // append each char to a StringBuffer, but // leave out quote chars and replace spaces // inside quotes with the delim char for (int i = 0; i < chars.length; i++) { if (chars[i] == '"') { inquote = (inquote) ? false : true; } else if (inquote && chars[i] == ' ') { buffer.append(delim); } else { buffer.append(chars[i]); } } s = buffer.toString(); // split the new string by the whitespaces that were not in quotes ArrayList arrayList = new ArrayList(); StringTokenizer tokenizer = new StringTokenizer(s); while (tokenizer.hasMoreTokens()) { arrayList.add(tokenizer.nextElement()); } // put the strings in the arraylist into a string[] String[] list = (String[]) arrayList.toArray(new String[arrayList.size()]); // change all the delim characters back to spaces for (int i = 0; i < list.length; i++) { list[i] = list[i].replace(delim, ' '); } return list; } public static String htmlify(String input) { return (input == null ? null : input.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">")); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?