001: package org.apache.mina.util;
002:
003: import java.io.IOException;
004: import java.io.LineNumberReader;
005: import java.io.PrintWriter;
006: import java.io.StringReader;
007: import java.io.StringWriter;
008: import java.util.ArrayList;
009:
010: /**
011: * Utility class for working with xml data
012: *
013: * Implementation is heavily based on org.apache.log4j.helpers.Transform
014: */
015: public class Transform {
016:
017: private static final String CDATA_START = "<![CDATA[";
018: private static final String CDATA_END = "]]>";
019: private static final String CDATA_PSEUDO_END = "]]>";
020: private static final String CDATA_EMBEDED_END = CDATA_END
021: + CDATA_PSEUDO_END + CDATA_START;
022: private static final int CDATA_END_LEN = CDATA_END.length();
023:
024: /**
025: * This method takes a string which may contain HTML tags (ie,
026: * <b>, <table>, etc) and replaces any
027: * '<', '>' , '&' or '"'
028: * characters with respective predefined entity references.
029: *
030: * @param input The text to be converted.
031: * @return The input string with the special characters replaced.
032: * */
033: static public String escapeTags(final String input) {
034: // Check if the string is null, zero length or devoid of special characters
035: // if so, return what was sent in.
036:
037: if (input == null
038: || input.length() == 0
039: || (input.indexOf('"') == -1
040: && input.indexOf('&') == -1
041: && input.indexOf('<') == -1 && input
042: .indexOf('>') == -1)) {
043: return input;
044: }
045:
046: StringBuffer buf = new StringBuffer(input.length() + 6);
047: char ch;
048:
049: int len = input.length();
050: for (int i = 0; i < len; i++) {
051: ch = input.charAt(i);
052: if (ch > '>') {
053: buf.append(ch);
054: } else if (ch == '<') {
055: buf.append("<");
056: } else if (ch == '>') {
057: buf.append(">");
058: } else if (ch == '&') {
059: buf.append("&");
060: } else if (ch == '"') {
061: buf.append(""");
062: } else {
063: buf.append(ch);
064: }
065: }
066: return buf.toString();
067: }
068:
069: /**
070: * Ensures that embeded CDEnd strings (]]>) are handled properly
071: * within message, NDC and throwable tag text.
072: *
073: * @param buf StringBuffer holding the XML data to this point. The
074: * initial CDStart (<![CDATA[) and final CDEnd (]]>) of the CDATA
075: * section are the responsibility of the calling method.
076: * @param str The String that is inserted into an existing CDATA Section within buf.
077: * */
078: static public void appendEscapingCDATA(final StringBuffer buf,
079: final String str) {
080: if (str != null) {
081: int end = str.indexOf(CDATA_END);
082: if (end < 0) {
083: buf.append(str);
084: } else {
085: int start = 0;
086: while (end > -1) {
087: buf.append(str.substring(start, end));
088: buf.append(CDATA_EMBEDED_END);
089: start = end + CDATA_END_LEN;
090: if (start < str.length()) {
091: end = str.indexOf(CDATA_END, start);
092: } else {
093: return;
094: }
095: }
096: buf.append(str.substring(start));
097: }
098: }
099: }
100:
101: /**
102: * convert a Throwable into an array of Strings
103: * @param throwable
104: * @return string representation of the throwable
105: */
106: public static String[] getThrowableStrRep(Throwable throwable) {
107: StringWriter sw = new StringWriter();
108: PrintWriter pw = new PrintWriter(sw);
109: throwable.printStackTrace(pw);
110: pw.flush();
111: LineNumberReader reader = new LineNumberReader(
112: new StringReader(sw.toString()));
113: ArrayList<String> lines = new ArrayList<String>();
114: try {
115: String line = reader.readLine();
116: while (line != null) {
117: lines.add(line);
118: line = reader.readLine();
119: }
120: } catch (IOException ex) {
121: lines.add(ex.toString());
122: }
123: String[] rep = new String[lines.size()];
124: lines.toArray(rep);
125: return rep;
126: }
127:
128: }
|