1 16 17 package org.apache.log4j.helpers; 18 19 import org.apache.log4j.spi.LoggingEvent; 20 import org.apache.log4j.spi.LocationInfo; 21 22 28 public class Transform { 29 30 private static final String CDATA_START = "<![CDATA["; 31 private static final String CDATA_END = "]]>"; 32 private static final String CDATA_PSEUDO_END = "]]>"; 33 private static final String CDATA_EMBEDED_END = CDATA_END + CDATA_PSEUDO_END + CDATA_START; 34 private static final int CDATA_END_LEN = CDATA_END.length(); 35 36 45 static public String escapeTags(String input) { 46 49 if( input == null || input.length() == 0 ) { 50 return input; 51 } 52 53 56 StringBuffer buf = new StringBuffer (input.length() + 6); 57 char ch = ' '; 58 59 int len = input.length(); 60 for(int i=0; i < len; i++) { 61 ch = input.charAt(i); 62 if(ch == '<') { 63 buf.append("<"); 64 } else if(ch == '>') { 65 buf.append(">"); 66 } else { 67 buf.append(ch); 68 } 69 } 70 return buf.toString(); 71 } 72 73 82 static public void appendEscapingCDATA(StringBuffer buf, String str) { 83 if(str == null) { 84 buf.append(""); 85 return; 86 } 87 88 int end = str.indexOf(CDATA_END); 89 if (end < 0) { 90 buf.append(str); 91 return; 92 } 93 94 int start = 0; 95 while (end > -1) { 96 buf.append(str.substring(start,end)); 97 buf.append(CDATA_EMBEDED_END); 98 start = end + CDATA_END_LEN; 99 if (start < str.length()) { 100 end = str.indexOf(CDATA_END, start); 101 } else { 102 return; 103 } 104 } 105 106 buf.append(str.substring(start)); 107 } 108 } 109 | Popular Tags |