> >
> >You don't need to use a CDATA section for the message, you could use 
> >Transform.escapeTags for the message itself. In fact the best idea 
> >would be to use a version of Transform.escapeTags that tried 
> to escape 
> >tags, but if there were too many escapes (i.e. more than 4 
> escapes) it 
> >aborts escaping and wraps the text in a CDATA section.
> 
> That's an original idea. Do you have an implementation in mind?
> 

The trivial implementation is:

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

    if( input == null || input.length() == 0 ) {
      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 replacementCount = 0;

    int len = input.length();
    for(int i=0; i < len; i++) {

      if (replacementCount > 4) {
        //Abandon escaping
        buf.setLength(0);
        buf.append("<![CDATA[");
        appendEscapingCDATA(buf, input);
        buf.append("]]>");
        return buf.toString();
      }

      ch = input.charAt(i);
      if(ch == '<') {
        buf.append("&lt;");
        replacementCount++;
      } else if(ch == '>') {
        buf.append("&gt;");
        replacementCount++;
      } else {
        buf.append(ch);
      }
    }
    return buf.toString();
  }

(lack of compiler == lack of syntax checking == lack of testing ;)

Shouldn't this method also escape ampersands (&) ? I know that it is called
escapeTags so it does what it says. Also the current escapeTags method
contains a mixture of tabs and spaces!

Nicko


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to