📄 strings.java
字号:
} /** * Converts the string s to a Boolean. See <code>isTrue</code> for valid values of s. * * @param s * The string to convert. * @return Boolean <code>TRUE</code> when <code>isTrue(s)</code>. * @throws StringValueConversionException * when s is not a valid value * @see #isTrue(String) */ public static Boolean toBoolean(final String s) throws StringValueConversionException { return Boolean.valueOf(isTrue(s)); } /** * Converts the 1 character string s to a character. * * @param s * The 1 character string to convert to a char. * @return Character value to convert * @throws StringValueConversionException * when the string is longer or shorter than 1 character, or <code>null</code>. */ public static char toChar(final String s) throws StringValueConversionException { if (s != null) { if (s.length() == 1) { return s.charAt(0); } else { throw new StringValueConversionException("Expected single character, not \"" + s + "\""); } } throw new StringValueConversionException("Character value was null"); } /** * Converts unicodes to encoded \uxxxx. * * @param unicodeString * The unicode string * @return The escaped unicode string, like '\u4F60\u597D'. */ public static String toEscapedUnicode(String unicodeString) { if ((unicodeString == null) || (unicodeString.length() == 0)) { return unicodeString; } int len = unicodeString.length(); int bufLen = len * 2; StringBuffer outBuffer = new StringBuffer(bufLen); for (int x = 0; x < len; x++) { char aChar = unicodeString.charAt(x); // Handle common case first, selecting largest block that // avoids the specials below if ((aChar > 61) && (aChar < 127)) { if (aChar == '\\') { outBuffer.append('\\'); outBuffer.append('\\'); continue; } outBuffer.append(aChar); continue; } switch (aChar) { case ' ' : if (x == 0) { outBuffer.append('\\'); } outBuffer.append(' '); break; case '\t' : outBuffer.append('\\'); outBuffer.append('t'); break; case '\n' : outBuffer.append('\\'); outBuffer.append('n'); break; case '\r' : outBuffer.append('\\'); outBuffer.append('r'); break; case '\f' : outBuffer.append('\\'); outBuffer.append('f'); break; case '=' : // Fall through case ':' : // Fall through case '#' : // Fall through case '!' : outBuffer.append('\\'); outBuffer.append(aChar); break; default : if ((aChar < 0x0020) || (aChar > 0x007e)) { outBuffer.append('\\'); outBuffer.append('u'); outBuffer.append(toHex((aChar >> 12) & 0xF)); outBuffer.append(toHex((aChar >> 8) & 0xF)); outBuffer.append(toHex((aChar >> 4) & 0xF)); outBuffer.append(toHex(aChar & 0xF)); } else { outBuffer.append(aChar); } } } return outBuffer.toString(); } /** * Converts a String to multiline HTML markup by replacing newlines with line break entities * (<br/>) and multiple occurrences of newline with paragraph break entities (<p>). * * @param s * String to transform * @return String with all single occurrences of newline replaced with <br/> and all * multiple occurrences of newline replaced with <p>. */ public static CharSequence toMultilineMarkup(final CharSequence s) { if (s == null) { return null; } final AppendingStringBuffer buffer = new AppendingStringBuffer(); int newlineCount = 0; buffer.append("<p>"); for (int i = 0; i < s.length(); i++) { final char c = s.charAt(i); switch (c) { case '\n' : newlineCount++; break; case '\r' : break; default : if (newlineCount == 1) { buffer.append("<br/>"); } else if (newlineCount > 1) { buffer.append("</p><p>"); } buffer.append(c); newlineCount = 0; break; } } if (newlineCount == 1) { buffer.append("<br/>"); } else if (newlineCount > 1) { buffer.append("</p><p>"); } buffer.append("</p>"); return buffer; } /** * Converts the given object to a string. Does special conversion for * {@link Throwable throwables} and String arrays of length 1 (in which case it just returns to * string in that array, as this is a common thing to have in the Servlet API). * * @param object * The object * @return The string */ public static String toString(final Object object) { if (object == null) { return null; } if (object instanceof Throwable) { return toString((Throwable)object); } if (object instanceof String) { return (String)object; } if (object instanceof String[] && ((String[])object).length == 1) { return ((String[])object)[0]; } return object.toString(); } /** * Creates a location stacktrace string representation for the component for reference when the * render check fails. This method filters out most of the unnecessary parts of the stack trace. * The message of the <code>location</code> is used as a verb in the rendered string. Use * "added", "constructed" or similar verbs as values. * * @param component * the component that was constructed or added and failed to render * @param location * the location where the component was created or added in the java code. * @return a string giving the line precise location where the component was added or created. */ public static String toString(final Component component, final Throwable location) { Class componentClass = component.getClass(); // try to find the component type, if it is an inner element, then get // the parent component. String componentType = componentClass.getName(); if (componentType.indexOf('$') >= 0) { componentType = componentClass.getSuperclass().getName(); } componentType = componentType.substring(componentType.lastIndexOf('.') + 1); // create a user friendly message, using the location's message as a // differentiator for the message (e.g. "component foo was ***added***" // or "component foo was ***created***") AppendingStringBuffer sb = new AppendingStringBuffer("The " + componentType.toLowerCase() + " with id '" + component.getId() + "' that failed to render was " + location.getMessage() + "\n"); // a list of stacktrace elements that need to be skipped in the location // stack trace String[] skippedElements = new String[] { "org.apache.wicket.MarkupContainer", "org.apache.wicket.Component", "org.apache.wicket.markup" }; // a list of stack trace elements that stop the traversal of the stack // trace String[] breakingElements = new String[] { "org.apache.wicket.protocol.http.WicketServlet", "org.apache.wicket.protocol.http.WicketFilter", "java.lang.reflect" }; StackTraceElement[] trace = location.getStackTrace(); for (int i = 0; i < trace.length; i++) { String traceString = trace[i].toString(); if (shouldSkip(traceString, skippedElements)) { // don't print this line, is wicket internal continue; } if (!(traceString.startsWith("sun.reflect.") && i > 1)) { // filter out reflection API calls from the stack trace if (traceString.indexOf("java.lang.reflect") < 0) { sb.append(" at "); sb.append(traceString); sb.append("\n"); } if (shouldSkip(traceString, breakingElements)) { break; } } } sb.append("\n"); return sb.toString(); } private static boolean shouldSkip(String text, String[] filters) { for (int i = 0; i < filters.length; i++) { if (text.indexOf(filters[i]) >= 0) { return true; } } return false; } /** * Converts a Throwable to a string. * * @param throwable * The throwable * @return The string */ public static String toString(final Throwable throwable) { if (throwable != null) { ArrayList al = new ArrayList(); Throwable cause = throwable; al.add(cause); while (cause.getCause() != null && cause != cause.getCause()) { cause = cause.getCause(); al.add(cause); } AppendingStringBuffer sb = new AppendingStringBuffer(256); // first print the last cause int length = al.size() - 1; cause = (Throwable)al.get(length); if (throwable instanceof WicketRuntimeException) { sb.append("WicketMessage: "); sb.append(throwable.getMessage()); sb.append("\n\n"); } sb.append("Root cause:\n\n"); outputThrowable(cause, sb, false); if (length > 0) { sb.append("\n\nComplete stack:\n\n"); for (int i = 0; i < length; i++) { outputThrowable((Throwable)al.get(i), sb, true); sb.append("\n"); } } return sb.toString(); } else { return "<Null Throwable>"; } } private static void append(AppendingStringBuffer buffer, CharSequence s, int from, int to) { if (s instanceof AppendingStringBuffer) { AppendingStringBuffer asb = (AppendingStringBuffer)s; buffer.append(asb.getValue(), from, to - from); } else if (s instanceof StringBuffer) { buffer.append((StringBuffer)s, from, to - from); } else { buffer.append(s.subSequence(from, to)); } } /** * Outputs the throwable and its stacktrace to the stringbuffer. If stopAtWicketSerlvet is true * then the output will stop when the org.apache.wicket servlet is reached. sun.reflect. * packages are filtered out. * * @param cause * @param sb * @param stopAtWicketServlet */ private static void outputThrowable(Throwable cause, AppendingStringBuffer sb, boolean stopAtWicketServlet) { sb.append(cause); sb.append("\n"); StackTraceElement[] trace = cause.getStackTrace(); for (int i = 0; i < trace.length; i++) { String traceString = trace[i].toString(); if (!(traceString.startsWith("sun.reflect.") && i > 1)) { sb.append(" at "); sb.append(traceString); sb.append("\n"); if (stopAtWicketServlet && (traceString.startsWith("org.apache.wicket.protocol.http.WicketServlet") || traceString .startsWith("org.apache.wicket.protocol.http.WicketFilter"))) { return; } } } } private static int search(final CharSequence s, String searchString, int pos) { int matchIndex = -1; if (s instanceof String) { matchIndex = ((String)s).indexOf(searchString, pos); } else if (s instanceof StringBuffer) { matchIndex = ((StringBuffer)s).indexOf(searchString, pos); } else if (s instanceof AppendingStringBuffer) { matchIndex = ((AppendingStringBuffer)s).indexOf(searchString, pos); } else { matchIndex = s.toString().indexOf(searchString, pos); } return matchIndex; } /** * Convert a nibble to a hex character * * @param nibble * the nibble to convert. * @return hex character */ private static char toHex(int nibble) { return hexDigit[(nibble & 0xF)]; } /** * Private constructor prevents construction. */ private Strings() { }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -