static public String escapeTags()

in src/main/java/org/apache/log4j/helpers/Transform.java [43:80]


  static public String escapeTags(final String input) {
    //Check if the string is null, zero length or devoid of special characters
    // if so, return what was sent in.

    if(input == null
       || input.length() == 0
       || (input.indexOf('"') == -1 &&
           input.indexOf('&') == -1 &&
           input.indexOf('<') == -1 &&
           input.indexOf('>') == -1)) {
      return input;
    }

    //Use a StringBuffer in lieu of String concatenation -- it is
    //much more efficient this way.

    StringBuffer buf = new StringBuffer(input.length() + 6);
    char ch = ' ';

    int len = input.length();
    for(int i=0; i < len; i++) {
      ch = input.charAt(i);
      if (ch > '>') {
          buf.append(ch);
      } else if(ch == '<') {
	      buf.append("&lt;");
      } else if(ch == '>') {
	      buf.append("&gt;");
      } else if(ch == '&') {
	      buf.append("&amp;");
      } else if(ch == '"') {
	      buf.append("&quot;");
      } else {
	      buf.append(ch);
      }
    }
    return buf.toString();
  }