You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-dev@jakarta.apache.org by vm...@apache.org on 2002/05/19 19:16:01 UTC
cvs commit: jakarta-cactus/framework/src/java/share/org/apache/cactus/server/runner XMLFormatter.java
vmassol 02/05/19 10:16:01
Modified: framework/src/java/share/org/apache/cactus/server/runner
XMLFormatter.java
Log:
fixed xml encoding bug ("<", ">" and "&" were not escaped)
Revision Changes Path
1.2 +56 -5 jakarta-cactus/framework/src/java/share/org/apache/cactus/server/runner/XMLFormatter.java
Index: XMLFormatter.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/server/runner/XMLFormatter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- XMLFormatter.java 12 May 2002 20:38:11 -0000 1.1
+++ XMLFormatter.java 19 May 2002 17:16:01 -0000 1.2
@@ -70,7 +70,7 @@
*
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
*
- * @version $Id: XMLFormatter.java,v 1.1 2002/05/12 20:38:11 vmassol Exp $
+ * @version $Id: XMLFormatter.java,v 1.2 2002/05/19 17:16:01 vmassol Exp $
*/
public class XMLFormatter implements XMLConstants, TestListener
{
@@ -222,9 +222,9 @@
StringBuffer xml = new StringBuffer();
xml.append("<" + ERROR + " " + ATTR_MESSAGE + "=\"" +
- failure.exceptionMessage() + "\" " + ATTR_TYPE + "=\"" +
+ xmlEncode(failure.exceptionMessage()) + "\" " + ATTR_TYPE + "=\"" +
failure.thrownException().getClass().getName() + "\">");
- xml.append("<![CDATA[" + failure.trace() + "]]>");
+ xml.append(xmlEncode(failure.trace()));
xml.append("</" + ERROR + ">");
this.currentTestFailure = xml.toString();
@@ -242,9 +242,9 @@
StringBuffer xml = new StringBuffer();
xml.append("<" + FAILURE + " " + ATTR_MESSAGE + "=\"" +
- failure.exceptionMessage() + "\" " + ATTR_TYPE + "=\"" +
+ xmlEncode(failure.exceptionMessage()) + "\" " + ATTR_TYPE + "=\"" +
failure.thrownException().getClass().getName() + "\">");
- xml.append("<![CDATA[" + failure.trace() + "]]>");
+ xml.append(xmlEncode(failure.trace()));
xml.append("</" + FAILURE + ">");
this.currentTestFailure = xml.toString();
@@ -272,6 +272,57 @@
xml.append("</" + TESTCASE + ">");
this.currentTestCaseResults.append(xml.toString());
+ }
+
+ /**
+ * Escapes reserved XML characters.
+ *
+ * @param theString the string to escape
+ * @return the escaped string
+ */
+ private String xmlEncode(String theString)
+ {
+ String newString;
+
+ // It is important to replace the "&" first as the other replacements
+ // also introduces "&" chars ...
+ newString = XMLFormatter.replace(theString, '&', "&");
+
+ newString = XMLFormatter.replace(newString, '<', "<");
+ newString = XMLFormatter.replace(newString, '>', ">");
+
+ return newString;
+ }
+
+ /**
+ * Replaces a character in a string by a substring.
+ *
+ * @param theBaseString the base string in which to perform replacements
+ * @param theChar the char to look for
+ * @param theNewString the string with which to replace the char
+ * @return the string with replacements done
+ */
+ public static String replace(String theBaseString, char theChar,
+ String theNewString)
+ {
+ final int len = theBaseString.length() - 1;
+ int pos = -1;
+
+ while ((pos = theBaseString.indexOf(theChar, pos + 1)) > -1) {
+ if (pos == 0) {
+ final String after = theBaseString.substring(1);
+ theBaseString = theNewString + after;
+ } else if (pos == len) {
+ final String before = theBaseString.substring(0, pos);
+ theBaseString = before + theNewString;
+ } else {
+ final String before = theBaseString.substring(0, pos);
+ final String after = theBaseString.substring(pos + 1);
+ theBaseString = before + theNewString + after;
+ }
+ }
+ return theBaseString;
+
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>