harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zhouke...@apache.org
Subject svn commit: r929953 - in /harmony/enhanced/java/trunk/classlib/modules/logging/src: main/java/java/util/logging/XMLFormatter.java test/java/org/apache/harmony/logging/tests/java/util/logging/XMLFormatterTest.java
Date Thu, 01 Apr 2010 12:58:01 GMT
Author: zhoukevin
Date: Thu Apr  1 12:58:01 2010
New Revision: 929953

URL: http://svn.apache.org/viewvc?rev=929953&view=rev
Log:
XMLFormatter should convert special symbol like '<','>','&' into XML file, add a
test case. Apply this patch to the new truck according to Mark's requirement.

Modified:
    harmony/enhanced/java/trunk/classlib/modules/logging/src/main/java/java/util/logging/XMLFormatter.java
    harmony/enhanced/java/trunk/classlib/modules/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/XMLFormatterTest.java

Modified: harmony/enhanced/java/trunk/classlib/modules/logging/src/main/java/java/util/logging/XMLFormatter.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/logging/src/main/java/java/util/logging/XMLFormatter.java?rev=929953&r1=929952&r2=929953&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/logging/src/main/java/java/util/logging/XMLFormatter.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/logging/src/main/java/java/util/logging/XMLFormatter.java
Thu Apr  1 12:58:01 2010
@@ -19,7 +19,7 @@ package java.util.logging;
 
 import java.security.AccessController;
 import java.security.PrivilegedAction;
-import java.text.MessageFormat;
+import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.ResourceBundle;
 
@@ -37,6 +37,9 @@ public class XMLFormatter extends Format
 
     private static final String indent = "    "; //$NON-NLS-1$
 
+    private static final SimpleDateFormat dataFormat = new SimpleDateFormat(
+            "yyyy-MM-dd hh:mm:ss"); //$NON-NLS-1$
+
     /**
      * Constructs a new {@code XMLFormatter}.
      */
@@ -57,45 +60,61 @@ public class XMLFormatter extends Format
         // call a method of LogRecord to ensure not null
         long time = r.getMillis();
         // format to date
-        String date = MessageFormat.format("{0, date} {0, time}",
-                new Object[] { new Date(time) });
+        String date = dataFormat.format(new Date(time)).replace(' ', 'T');
 
         StringBuilder sb = new StringBuilder();
-        sb.append(("<record>")).append(lineSeperator);
-        sb.append(indent).append(("<date>")).append(date).append(("</date>"))
+        sb.append("<record>").append(lineSeperator);
+        sb.append(indent).append("<date>").append(date).append("</date>")
+                .append(lineSeperator);
+
+        sb.append(indent).append("<millis>").append(time).append("</millis>")
                 .append(lineSeperator);
-        sb.append(indent).append(("<millis>")).append(time).append(
-                ("</millis>")).append(lineSeperator);
-        sb.append(indent).append(("<sequence>")).append(r.getSequenceNumber())
-                .append(("</sequence>")).append(lineSeperator);
-        if (null != r.getLoggerName()) {
-            sb.append(indent).append(("<logger>")).append(r.getLoggerName())
-                    .append(("</logger>")).append(lineSeperator);
-        }
-        sb.append(indent).append(("<level>")).append(r.getLevel().getName())
-                .append(("</level>")).append(lineSeperator);
-        if (null != r.getSourceClassName()) {
-            sb.append(indent).append(("<class>"))
-                    .append(r.getSourceClassName()).append(("</class>"))
-                    .append(lineSeperator);
-        }
-        if (null != r.getSourceMethodName()) {
-            sb.append(indent).append(("<method>")).append(
-                    r.getSourceMethodName()).append(("</method>")).append(
+
+        sb.append(indent).append("<sequence>").append(r.getSequenceNumber())
+                .append("</sequence>").append(lineSeperator);
+
+        String content = r.getLoggerName();
+        if (null != content) {
+            sb.append(indent).append("<logger>").append(
+                    convertSpecialSymbol(content)).append("</logger>").append(
+                    lineSeperator);
+        }
+
+        sb.append(indent).append("<level>").append(r.getLevel().getName())
+                .append("</level>").append(lineSeperator);
+
+        content = r.getSourceClassName();
+        if (null != content) {
+            sb.append(indent).append("<class>").append(content).append(
+                    "</class>").append(lineSeperator);
+        }
+
+        content = r.getSourceMethodName();
+        if (null != content) {
+            sb.append(indent).append("<method>").append(
+                    convertSpecialSymbol(content)).append("</method>").append(
                     lineSeperator);
         }
-        sb.append(indent).append(("<thread>")).append(r.getThreadID()).append(
-                ("</thread>")).append(lineSeperator);
+
+        sb.append(indent).append("<thread>").append(r.getThreadID()).append(
+                "</thread>").append(lineSeperator);
+
         formatMessages(r, sb);
+
         Object[] params;
         if ((params = r.getParameters()) != null) {
+            String elementString = null;
             for (Object element : params) {
-                sb.append(indent).append(("<param>")).append(element).append(
-                        ("</param>")).append(lineSeperator);
+                elementString = element.toString();
+                sb.append(indent).append("<param>").append(
+                        convertSpecialSymbol(elementString)).append("</param>")
+                        .append(lineSeperator);
             }
         }
+
         formatThrowable(r, sb);
-        sb.append(("</record>")).append(lineSeperator);
+
+        sb.append("</record>").append(lineSeperator);
         return sb.toString();
     }
 
@@ -105,6 +124,7 @@ public class XMLFormatter extends Format
         // to parse pattern string
         ResourceBundle rb = r.getResourceBundle();
         String pattern = r.getMessage();
+
         if (null != rb && null != pattern) {
             String message;
             try {
@@ -115,20 +135,26 @@ public class XMLFormatter extends Format
 
             if (message == null) {
                 message = pattern;
-                sb.append(indent).append(("<message>")).append(message).append(
-                        ("</message>")).append(lineSeperator);
+                sb.append(indent).append("<message>").append(
+                        convertSpecialSymbol(message)).append("</message>")
+                        .append(lineSeperator);
             } else {
-                sb.append(indent).append(("<message>")).append(message).append(
-                        ("</message>")).append(lineSeperator);
-                sb.append(indent).append(("<key>")).append(pattern).append(
-                        ("</key>")).append(lineSeperator);
-                sb.append(indent).append(("<catalog>")).append(
-                        r.getResourceBundleName()).append(("</catalog>"))
+                sb.append(indent).append("<message>").append(
+                        convertSpecialSymbol(message)).append("</message>")
                         .append(lineSeperator);
+
+                sb.append(indent).append("<key>").append(
+                        convertSpecialSymbol(pattern)).append("</key>").append(
+                        lineSeperator);
+
+                sb.append(indent).append("<catalog>").append(
+                        convertSpecialSymbol(r.getResourceBundleName()))
+                        .append("</catalog>").append(lineSeperator);
             }
         } else if (null != pattern) {
-            sb.append(indent).append(("<message>")).append(pattern).append(
-                    ("</message>")).append(lineSeperator);
+            sb.append(indent).append("<message>").append(
+                    convertSpecialSymbol(pattern)).append("</message>").append(
+                    lineSeperator);
         } else {
             sb.append(indent).append(("<message/>"));
         }
@@ -139,24 +165,33 @@ public class XMLFormatter extends Format
         Throwable t;
         if ((t = r.getThrown()) != null) {
             sb.append(indent).append("<exception>").append(lineSeperator);
-            sb.append(indent).append(indent).append("<message>").append(
-                    t.toString()).append("</message>").append(lineSeperator);
-            // format throwable's stack trace
-            StackTraceElement[] elements = t.getStackTrace();
-            for (StackTraceElement e : elements) {
-                sb.append(indent).append(indent).append("<frame>").append(
-                        lineSeperator);
-                sb.append(indent).append(indent).append(indent).append(
-                        "<class>").append(e.getClassName()).append("</class>")
-                        .append(lineSeperator);
-                sb.append(indent).append(indent).append(indent).append(
-                        "<method>").append(e.getMethodName()).append(
-                        "</method>").append(lineSeperator);
-                sb.append(indent).append(indent).append(indent)
-                        .append("<line>").append(e.getLineNumber()).append(
+            {
+                sb.append(indent).append(indent).append("<message>").append(
+                        convertSpecialSymbol(t.toString()))
+                        .append("</message>").append(lineSeperator);
+
+                // format throwable's stack trace
+                StackTraceElement[] elements = t.getStackTrace();
+                for (StackTraceElement e : elements) {
+                    sb.append(indent).append(indent).append("<frame>").append(
+                            lineSeperator);
+                    {
+                        sb.append(indent).append(indent).append(indent).append(
+                                "<class>").append(e.getClassName()).append(
+                                "</class>").append(lineSeperator);
+
+                        sb.append(indent).append(indent).append(indent).append(
+                                "<method>").append(
+                                convertSpecialSymbol(e.getMethodName()))
+                                .append("</method>").append(lineSeperator);
+
+                        sb.append(indent).append(indent).append(indent).append(
+                                "<line>").append(e.getLineNumber()).append(
                                 "</line>").append(lineSeperator);
-                sb.append(indent).append(indent).append("</frame>").append(
-                        lineSeperator);
+                    }
+                    sb.append(indent).append(indent).append("</frame>").append(
+                            lineSeperator);
+                }
             }
             sb.append(indent).append("</exception>").append(lineSeperator);
         }
@@ -185,14 +220,14 @@ public class XMLFormatter extends Format
         sb.append("<?xml version=\"1.0\" encoding=\"").append(encoding).append(
                 "\" standalone=\"no\"?>").append(lineSeperator);
         sb.append("<!DOCTYPE log SYSTEM \"logger.dtd\">").append(lineSeperator);
-        sb.append(("<log>"));
+        sb.append("<log>").append(lineSeperator);
         return sb.toString();
     }
 
     /**
      * Returns the tail string for a set of log records formatted as XML
      * strings.
-     *
+     * 
      * @param h
      *            the output handler, may be {@code null}.
      * @return the tail string for log records formatted as XML strings.
@@ -202,6 +237,26 @@ public class XMLFormatter extends Format
         return "</log>"; //$NON-NLS-1$
     }
 
+    private String convertSpecialSymbol(String str) {
+        if (str == null) {
+            return null;
+        }
+        StringBuilder sb = new StringBuilder();
+        char[] chars = str.toCharArray();
+        for (int index = 0; index < chars.length; index++) {
+            if ('<' == chars[index]) {
+                sb.append("&lt;"); //$NON-NLS-1$
+            } else if ('>' == chars[index]) {
+                sb.append("&gt;"); //$NON-NLS-1$
+            } else if ('&' == chars[index]) {
+                sb.append("&amp;"); //$NON-NLS-1$
+            } else {
+                sb.append(chars[index]);
+            }
+        }
+        return sb.toString();
+    }
+
     // use privilege code to get system property
     private static String getSystemProperty(final String key) {
         return AccessController.doPrivileged(new PrivilegedAction<String>() {

Modified: harmony/enhanced/java/trunk/classlib/modules/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/XMLFormatterTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/XMLFormatterTest.java?rev=929953&r1=929952&r2=929953&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/XMLFormatterTest.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/XMLFormatterTest.java
Thu Apr  1 12:58:01 2010
@@ -17,11 +17,18 @@
 
 package org.apache.harmony.logging.tests.java.util.logging;
 
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.PrintStream;
 import java.io.UnsupportedEncodingException;
 import java.util.ResourceBundle;
+import java.util.logging.FileHandler;
 import java.util.logging.Handler;
 import java.util.logging.Level;
 import java.util.logging.LogRecord;
+import java.util.logging.Logger;
 import java.util.logging.XMLFormatter;
 
 import junit.framework.TestCase;
@@ -170,7 +177,47 @@ public class XMLFormatterTest extends Te
 		String output = formatter.format(lr);
 		// System.out.println(formatter.getHead(handler)+output+formatter.getTail(handler));
 		assertTrue(output.indexOf("<message") > 0);
-	}
+    }
+
+    public class TestFileHandlerClass {
+        Logger logger = Logger.getLogger(TestFileHandlerClass.class.getName());
+
+        public TestFileHandlerClass(String logFile) throws SecurityException,
+                IOException {
+            logger.addHandler(new FileHandler(logFile));
+            LogRecord logRecord = new LogRecord(Level.INFO, "message:<init>&");
+            logRecord.setLoggerName("<init>&");
+            logRecord.setThrown(new Exception("<init>&"));
+            logRecord.setParameters(new String[] { "<init>&" });
+            logger.log(logRecord);
+        }
+    }
+
+    public void test_TestFileHandlerClass_constructor() throws Exception {
+        File logFile = new File(System.getProperty("user.home"),
+                "TestFileHandlerClass.log");
+        logFile.deleteOnExit();
+
+        PrintStream out = System.out;
+        PrintStream err = System.err;
+        try {
+            System.setOut(null);
+            System.setErr(null);
+            new TestFileHandlerClass(logFile.getCanonicalPath());
+            BufferedReader br = new BufferedReader(new FileReader(logFile));
+            String line = null;
+
+            while ((line = br.readLine()) != null) {
+                if (line.contains("<init>&")) {
+                    fail("should convert <init> to &lt;init&gt;");
+                    break;
+                }
+            }
+        } finally {
+            System.setOut(out);
+            System.setErr(err);
+        }
+    }
 
 	public static class MockHandler extends Handler {
 		public void close() {



Mime
View raw message