code: public static void filter(String value, AbstractRenderAppender result, boolean markupHTMLSpaceReturn)
      {
          // if the value is null, return
          if (value == null)
              return;
  
          // convert the string
          int numChars = value.length();
          char c;
          char prev = 0;
  
          for (int i = 0; i < numChars; i++) {
              c = value.charAt(i);
              switch (c) {
                  case '<':
                      result.append("&lt;");
                      break;
                  case '>':
                      result.append("&gt;");
                      break;
                  case '&':
                      result.append("&amp;");
                      break;
                  case '"':
                      result.append("&quot;");
                      break;
                  case '\'':
                      result.append("&#39;");
                      break;
                  case ' ':
                      if (markupHTMLSpaceReturn) {
                          if (prev == ' ') {
                              result.append("&nbsp;");
                          }
                          else
                              result.append(c);
                      }
                      else
                          result.append(c);
                      break;
                  case '\n':
                      if (markupHTMLSpaceReturn) {
                          result.append("<br />");
                      }
                      else
                          result.append(c);
                      break;
                  default:
                      result.append(c);
              }
              prev = c;
          }
      }
docs: Filter the specified string for characters that are sensitive to
  HTML interpreters, returning the string with these characters replaced
  by the corresponding character entities.
  @param value                 The <code>String</code> value to be filtered and returned.
  @param markupHTMLSpaceReturn convert space characters and return characters
  to &amp;nbsp; and &lt;br /&gt; marketup for html.
  @param result the {@link AbstractRenderAppender} to which the results should be rendered