public static String insertOptionalWordBreaks(
      final String text,
      final int threshold,
      final int insertStep,
      final boolean encodeHtmlEntities) {
    String html = "";

    final String[] words = text.split(" ");

    boolean first = true;
    for (final String word : words) {
      String w = "";

      if (word.length() > threshold) {
        int pos = 0;
        for (; pos < word.length() - word.length() % insertStep - 1; pos += insertStep) {
          if (encodeHtmlEntities) {
            w += escapeHtml(word.substring(pos, pos + insertStep));
          } else {
            w += word.substring(pos, pos + insertStep);
          }
          w += "<span style=\"display:block;width:1px;float:left;\"> </span>";
        }

        w += word.substring(pos);
      } else {
        w = encodeHtmlEntities ? escapeHtml(word) : word;
      }

      html += (first ? "" : encodeHtmlEntities ? escapeHtml(" ") : " ") + w;

      first = false;
    }

    return html;
  }
Escapes the characters in a <code>String</code> using HTML entities.

@param str the <code>String</code> to escape, may be <code>null</code>
@return a new escaped <code>String</code>, empty if <code>null</code> string input
java/ui/src/main/java/com/google/security/zynamics/bindiff/utils/HtmlUtils.java