📄 utils.java
字号:
* added is "file:". */ public static String ensureProtocol (String url) { url = url.trim(); final int colon = url.indexOf(":"); if (colon < 0 || colon == 1) // probably something like "D:\nada\" { return getFileProtocolPrefix() + url; } return url; } /* public static String toFlatString (final Object o) { if (o == null) return ("<null/>"); StringBuffer result = new StringBuffer(); Class clazz = o.getClass(); result.append("<"); result.append(clazz.getName()); java.lang.reflect.Method[] methods = clazz.getMethods(); for (int i=0; i<methods.length; i++) { java.lang.reflect.Method m = methods[i]; if (m.getName().startsWith("get") && m.getParameterTypes().length == 0) { String attributeName = m.getName().substring(3, 4).toLowerCase(); attributeName += m.getName().substring(4); Object value = null; try { value = m.invoke(o, new Object[] {}); } catch (Exception e) { value = e.toString(); } result.append(" "); result.append(attributeName); result.append("=\""); result.append(value.toString()); result.append("\""); } } result.append(" />"); return result.toString(); } */ /* * turns a jdom element into a String * public static String toString (org.jdom.Element elt, String encoding) { return toString(new org.jdom.Document(elt), encoding); } */ /** * builds a list of all the files (their path) that are under * the given rootPath */ public static java.util.Set buildFileSet (String rootPath) { java.util.Set result = new java.util.HashSet(); buildFileSet(result, new java.io.File(rootPath)); return result; } private static void buildFileSet (java.util.Set result, java.io.File rootPath) { java.io.File[] files = rootPath.listFiles(); for (int i=0; i<files.length; i++) { java.io.File f = files[i]; if (f.isDirectory()) { buildFileSet(result, f); continue; } result.add(f.getPath()); } } /** * Given a List l, locates the elt thisObject in it and replaces it with * thatObject. */ public static boolean replace (final java.util.List l, final Object thisObject, final Object thatObject) { synchronized (l) { int index = -1; for (int i=0; i<l.size(); i++) { final Object o = l.get(i); if (o.equals(thisObject)) { index = i; break; } } if (index > -1) { l.remove(index); l.add(index, thatObject); return true; } return false; } } /** * Turns a real path into a URL, given the realRootPath.<br> * <br> * changeToWebPath("/home/jmettraux/", "http://localhost", "/home/jmettraux/nada/file.htm") * <br> * will return "http://localhost/nada/file.htm".<br> * <br> * Should also work on windows. */ public static String changeToWebPath (String realRootPath, String webRootPath, String pathToChange) { pathToChange = pathToChange.substring(realRootPath.length()); pathToChange = pathToChange.replace('\\', '/'); if ( ! webRootPath.endsWith("/") && ! pathToChange.startsWith("/")) { webRootPath += "/"; } return webRootPath + pathToChange; } /* * Returns true if the input string is a regular expression, erh, * well if it contains '*'or '$' or '^'... * public static boolean isRegex (String s) { if (s.indexOf("*") > -1) return true; if (s.indexOf("$") > -1) return true; if (s.indexOf("^") > -1) return true; if (s.indexOf("?") > -1) return true; if (s.indexOf("+") > -1) return true; return false; } */ /** * given a map of string <-> integers, adds one to the integer * corresponding to the given key */ public static void inc (java.util.Map map, String key) { Integer ii = (Integer)map.get(key); if (ii == null) ii = new Integer(0); ii = new Integer(ii.intValue() + 1); map.put(key, ii); } /** * precondition : the 2 values are not null */ public static int compareValues (final Object value, final Object otherValue) { // // try numeric comparison first try { double dVal = Double.parseDouble(value.toString()); double dOtherVal = Double.parseDouble(otherValue.toString()); return (int)(dVal - dOtherVal); } catch (NumberFormatException nfe) { // forget it //log.debug("non numeric values, proceeding with string comparison"); } // // string comparison else return (value.toString().compareTo(otherValue.toString())); } /** * Returns true if the o.toString() equals "true" or "false" * (case is not relevant). * If the object is null, will return false. */ public static boolean toBoolean (Object o) { if (o == null) return false; String s = o.toString(); return ("yes".equalsIgnoreCase(s) || "ok".equalsIgnoreCase(s) || "true".equalsIgnoreCase(s)); } /** * Returns the encoding for the system * by default, it's ISO-8859-1. * Can be set by changing the ENCODING system property. */ public static String getEncoding () { return System.getProperty("openwfe.xml.encoding", "ISO-8859-1"); } /** * As getEncoding() returns the String value of the encoding to user, * this method returns the corresponding charset. */ public static java.nio.charset.Charset getCharset () { return java.nio.charset.Charset.forName(getEncoding()); } /** * Returns a copy of a String (or null if the in string is null) */ public static String copyString (String in) { if (in == null) return null; return new String(in); } /** * returns true if the two strings are equal, * returns false as soon as one of them is null, or * if they are not equals */ public static boolean stringEquals (String s1, String s2) { if (s1 == null && s2 == null) return true; if (s1 == null || s2 == null) return false; return s1.equals(s2); } /** * This method is useful for printing stack traces from a JSP : it accepts * the 'out' writer has parameter. */ public static void printStackTrace (final Throwable t, final java.io.Writer w) { java.io.PrintWriter pw = new java.io.PrintWriter(w); t.printStackTrace(pw); pw.flush(); } /* * * Normalizes a filePath, ie turns something like "root/././nada" into * "root/nada" * / public static String normalizeFilePath (final String filePath) { String s = filePath; s = s.replaceAll("\\.\\/", ""); s = s.replaceAll("\\.\\\\", ""); return s; } */ /** * Outputs at DEBUG level the content of a map */ public static void debugMap (final org.apache.log4j.Logger log, final java.util.Map map) { final StringBuffer sb = new StringBuffer(); sb.append("\n*** debugMap ***\n"); final java.util.Iterator it = map.keySet().iterator(); while (it.hasNext()) { final Object key = it.next(); final Object value = map.get(key); sb.append(" * "); sb.append(key); sb.append(" --> "); sb.append(value); sb.append("\n"); } log.debug(sb.toString()); } /** * Simply returning the platform dependent temp directory. */ public static String getTempDir () { return System.getProperty("java.io.tmpdir"); } /** * A method for dumping data in a temp file */ public static void dump (final String prefix, final byte[] data) { try { java.io.FileOutputStream fos = new java.io.FileOutputStream (java.io.File.createTempFile(prefix, ".dmp")); fos.write(data); fos.flush(); fos.close(); } catch (Exception e) { log.debug("dump() failure "+e); } } /** * Ensures (return a sure copy) a string for use in a fileName. * I.e. 'toto nada' --> 'toto_nada' */ public static String ensureForFileName (final String in) { return in.replaceAll(" ", "_"); } /** * Logs the current stack trace. */ public static void logStackTrace (final org.apache.log4j.Logger log) { try { throw new Exception("dump"); } catch (final Exception e) { log.info("logStackTrace()", e); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -