Ejemplo n.º 1
0
 private static void append(
     final AppendingStringBuffer buffer, final CharSequence s, final int from, final int to) {
   if (s instanceof AppendingStringBuffer) {
     AppendingStringBuffer asb = (AppendingStringBuffer) s;
     buffer.append(asb.getValue(), from, to - from);
   } else {
     buffer.append(s.subSequence(from, to));
   }
 }
Ejemplo n.º 2
0
  /**
   * Replace all occurrences of one string replaceWith another string.
   *
   * @param s The string to process
   * @param searchFor The value to search for
   * @param replaceWith The value to searchFor replaceWith
   * @return The resulting string with searchFor replaced with replaceWith
   */
  public static CharSequence replaceAll(
      final CharSequence s, final CharSequence searchFor, CharSequence replaceWith) {
    if (s == null) {
      return null;
    }

    // If searchFor is null or the empty string, then there is nothing to
    // replace, so returning s is the only option here.
    if ((searchFor == null) || "".equals(searchFor)) {
      return s;
    }

    // If replaceWith is null, then the searchFor should be replaced with
    // nothing, which can be seen as the empty string.
    if (replaceWith == null) {
      replaceWith = "";
    }

    String searchString = searchFor.toString();
    // Look for first occurrence of searchFor
    int matchIndex = search(s, searchString, 0);
    if (matchIndex == -1) {
      // No replace operation needs to happen
      return s;
    } else {
      // Allocate a AppendingStringBuffer that will hold one replacement
      // with a
      // little extra room.
      int size = s.length();
      final int replaceWithLength = replaceWith.length();
      final int searchForLength = searchFor.length();
      if (replaceWithLength > searchForLength) {
        size += (replaceWithLength - searchForLength);
      }
      final AppendingStringBuffer buffer = new AppendingStringBuffer(size + 16);

      int pos = 0;
      do {
        // Append text up to the match
        append(buffer, s, pos, matchIndex);

        // Add replaceWith text
        buffer.append(replaceWith);

        // Find next occurrence, if any
        pos = matchIndex + searchForLength;
        matchIndex = search(s, searchString, pos);
      } while (matchIndex != -1);

      // Add tail of s
      buffer.append(s.subSequence(pos, s.length()));

      // Return processed buffer
      return buffer;
    }
  }
Ejemplo n.º 3
0
  /**
   * Converts a Throwable to a string.
   *
   * @param throwable The throwable
   * @return The string
   */
  public static String toString(final Throwable throwable) {
    if (throwable != null) {
      List<Throwable> al = new ArrayList<Throwable>();
      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 = al.get(length);
      if (throwable instanceof RuntimeException) {
        sb.append("Message: ");
        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(al.get(i), sb, true);
          sb.append("\n");
        }
      }
      return sb.toString();
    } else {
      return "<Null Throwable>";
    }
  }
Ejemplo n.º 4
0
 /**
  * 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(
     final Throwable cause, final AppendingStringBuffer sb, final 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;
       }
     }
   }
 }
Ejemplo n.º 5
0
  /**
   * Converts a String to multiline HTML markup by replacing newlines with line break entities
   * (&lt;br/&gt;) and multiple occurrences of newline with paragraph break entities (&lt;p&gt;).
   *
   * @param s String to transform
   * @return String with all single occurrences of newline replaced with &lt;br/&gt; and all
   *     multiple occurrences of newline replaced with &lt;p&gt;.
   */
  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;
  }
Ejemplo n.º 6
0
  /**
   * Converts a Java String to an HTML markup String by replacing illegal characters with HTML
   * entities where appropriate. Spaces are converted to non-breaking spaces (&lt;nbsp&gt;) if
   * escapeSpaces is true, tabs are converted to four non-breaking spaces, less than signs are
   * converted to &amp;lt; entities and greater than signs to &amp;gt; entities.
   *
   * @param s The characters to escape
   * @param escapeSpaces True to replace ' ' with nonbreaking space
   * @param convertToHtmlUnicodeEscapes True to convert non-7 bit characters to unicode HTML (&#...)
   * @return The escaped string
   */
  public static CharSequence escapeMarkup(
      final CharSequence s, final boolean escapeSpaces, final boolean convertToHtmlUnicodeEscapes) {
    if (s == null) {
      return null;
    }

    int len = s.length();
    final AppendingStringBuffer buffer = new AppendingStringBuffer((int) (len * 1.1));

    for (int i = 0; i < len; i++) {
      final char c = s.charAt(i);

      switch (c) {
        case '\t':
          if (escapeSpaces) {
            // Assumption is four space tabs (sorry, but that's
            // just how it is!)
            buffer.append("&nbsp;&nbsp;&nbsp;&nbsp;");
          } else {
            buffer.append(c);
          }
          break;

        case ' ':
          if (escapeSpaces) {
            buffer.append("&nbsp;");
          } else {
            buffer.append(c);
          }
          break;

        case '<':
          buffer.append("&lt;");
          break;

        case '>':
          buffer.append("&gt;");
          break;

        case '&':
          buffer.append("&amp;");
          break;

        case '"':
          buffer.append("&quot;");
          break;

        case '\'':
          buffer.append("&#039;");
          break;

        default:
          int ci = 0xffff & c;

          if (
          // if this is non-printable and not whitespace (TAB, LF, CR)
          ((ci < 32) && (ci != 9) && (ci != 10) && (ci != 13))
              ||
              // or non-ASCII (XXX: why 160+ ?!) and need to UNICODE escape it
              (convertToHtmlUnicodeEscapes && (ci > 159))) {
            buffer.append("&#");
            buffer.append(Integer.toString(ci));
            buffer.append(';');
          } else {
            // ASCII or whitespace
            buffer.append(c);
          }
          break;
      }
    }

    return buffer;
  }