code: static public void appendEscapingCDATA(final StringBuffer buf,
                                             final String str) {
          if (str != null) {
              int end = str.indexOf(CDATA_END);
              if (end < 0) {
                  buf.append(str);
              } else {
                  int start = 0;
                  while (end > -1) {
                      buf.append(str.substring(start, end));
                      buf.append(CDATA_EMBEDED_END);
                      start = end + CDATA_END_LEN;
                      if (start < str.length()) {
                          end = str.indexOf(CDATA_END, start);
                      } else {
                          return;
                      }
                  }
                  buf.append(str.substring(start));
              }
          }
      }
docs: Ensures that embeded CDEnd strings (]]>) are handled properly
  within message, NDC and throwable tag text.
  
  @param buf StringBuffer holding the XML data to this point.  The
  initial CDStart (<![CDATA[) and final CDEnd (]]>) of the CDATA
  section are the responsibility of the calling method.
  @param str The String that is inserted into an existing CDATA Section within buf.