Author: ndbeyer
Date: Sat Aug 26 11:44:09 2006
New Revision: 437203
URL: http://svn.apache.org/viewvc?rev=437203&view=rev
Log:
Apply path for HARMONY-1280: [classlib][logging] java.util.logging.XMLFormatter.getHead(null)
throws unexpected NPE.
Modified:
incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/XMLFormatter.java
incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/XMLFormatterTest.java
Modified: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/XMLFormatter.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/XMLFormatter.java?rev=437203&r1=437202&r2=437203&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/XMLFormatter.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/main/java/java/util/logging/XMLFormatter.java
Sat Aug 26 11:44:09 2006
@@ -1,225 +1,228 @@
-/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-
-package java.util.logging;
-
-import java.security.AccessController;
-import java.security.PrivilegedAction;
-import java.text.MessageFormat;
-import java.util.Date;
-import java.util.ResourceBundle;
-
-/**
- * Format a given <code>LogRecord</code> into string represents XML. The DTD
specified in
- * Appendix A to Java Logging APIs specification is used.
- *
- * <code>XMLFormatter</code> uses given <code>Handler</code>'s encoding
if has, otherwise
- * uses default platform encoding instead. However, the UTF-8 is recommended encoding.
- *
- */
-public class XMLFormatter extends Formatter {
-
- /*
- * ---------------------------------------
- * Constants
- * ---------------------------------------
- */
-
- private static final String lineSeperator = LogManager
- .getSystemLineSeparator();
-
- private static final String indent = " "; //$NON-NLS-1$
-
- /*
- * ---------------------------------------
- * Constructor
- * ---------------------------------------
- */
-
- /**
- * Default constructor
- */
- public XMLFormatter() {
- super();
- }
-
- /*
- * ---------------------------------------
- * Abstract method implementation of Formatter
- * ---------------------------------------
- */
- /**
- * Format a <code>LogRecord</code> into string which represents XML
- *
- * @param r the given LogRecord instance to be formatted
- * @return string which represents XML
- */
- public String format(LogRecord r) {
- //call a method of LogRecord to ensure not null
- long time = r.getMillis();
- //format to date
- String date = MessageFormat.format("{0, date} {0, time}", //$NON-NLS-1$
- new Object[] { new Date(time) });
-
- StringBuffer sb = new StringBuffer();
- sb.append(("<record>")).append(lineSeperator); //$NON-NLS-1$
- sb.append(indent).append(("<date>")).append(date).append(("</date>"))
//$NON-NLS-1$ //$NON-NLS-2$
- .append(lineSeperator);
- sb.append(indent).append(("<millis>")).append(time).append( //$NON-NLS-1$
- ("</millis>")).append(lineSeperator); //$NON-NLS-1$
- sb.append(indent).append(("<sequence>")).append(r.getSequenceNumber()) //$NON-NLS-1$
- .append(("</sequence>")).append(lineSeperator); //$NON-NLS-1$
- if (null != r.getLoggerName()) {
- sb.append(indent).append(("<logger>")).append(r.getLoggerName()) //$NON-NLS-1$
- .append(("</logger>")).append(lineSeperator); //$NON-NLS-1$
- }
- sb.append(indent).append(("<level>")).append(r.getLevel().getName()) //$NON-NLS-1$
- .append(("</level>")).append(lineSeperator); //$NON-NLS-1$
- if (null != r.getSourceClassName()) {
- sb.append(indent).append(("<class>")) //$NON-NLS-1$
- .append(r.getSourceClassName()).append(("</class>")) //$NON-NLS-1$
- .append(lineSeperator);
- }
- if (null != r.getSourceMethodName()) {
- sb.append(indent).append(("<method>")).append( //$NON-NLS-1$
- r.getSourceMethodName()).append(("</method>")).append( //$NON-NLS-1$
- lineSeperator);
- }
- sb.append(indent).append(("<thread>")).append(r.getThreadID()).append( //$NON-NLS-1$
- ("</thread>")).append(lineSeperator); //$NON-NLS-1$
- formatMessages(r, sb);
- Object[] params;
- if ((params = r.getParameters()) != null) {
- for (int i = 0; i < params.length; i++) {
- sb.append(indent).append(("<param>")).append(params[i]).append( //$NON-NLS-1$
- ("</param>")).append(lineSeperator); //$NON-NLS-1$
- }
- }
- formatThrowable(r, sb);
- sb.append(("</record>")).append(lineSeperator); //$NON-NLS-1$
- return sb.toString();
- }
-
- /*
- * ---------------------------------------
- * Methods override Formatter
- * ---------------------------------------
- */
-
- private void formatMessages(LogRecord r, StringBuffer sb) {
- //get localized message if has, but don't call Formatter.formatMessage to parse pattern
string
- ResourceBundle rb = r.getResourceBundle();
- String pattern = r.getMessage();
- if (null != rb && null != pattern) {
- String message;
- try {
- message = rb.getString(pattern);
- } catch (Exception e) {
- message = null;
- }
-
- if (message == null) {
- message = pattern;
- sb.append(indent).append(("<message>")).append(message).append( //$NON-NLS-1$
- ("</message>")).append(lineSeperator); //$NON-NLS-1$
- } else {
- sb.append(indent).append(("<message>")).append(message).append( //$NON-NLS-1$
- ("</message>")).append(lineSeperator); //$NON-NLS-1$
- sb.append(indent).append(("<key>")).append(pattern).append( //$NON-NLS-1$
- ("</key>")).append(lineSeperator); //$NON-NLS-1$
- sb.append(indent).append(("<catalog>")).append( //$NON-NLS-1$
- r.getResourceBundleName()).append(("</catalog>")) //$NON-NLS-1$
- .append(lineSeperator);
- }
- } else if(null != pattern){
- sb.append(indent).append(("<message>")).append(pattern).append( //$NON-NLS-1$
- ("</message>")).append(lineSeperator); //$NON-NLS-1$
- } else{
- sb.append(indent).append(("<message/>")); //$NON-NLS-1$
- }
- }
-
- private void formatThrowable(LogRecord r, StringBuffer sb) {
- Throwable t;
- if ((t = r.getThrown()) != null) {
- sb.append(indent).append("<exception>").append(lineSeperator); //$NON-NLS-1$
- sb.append(indent).append(indent).append("<message>").append( //$NON-NLS-1$
- t.toString()).append("</message>").append(lineSeperator); //$NON-NLS-1$
- //format throwable's stack trace
- StackTraceElement[] elements = t.getStackTrace();
- for (int i = 0; i < elements.length; i++) {
- StackTraceElement e = elements[i];
- sb.append(indent).append(indent).append("<frame>").append( //$NON-NLS-1$
- lineSeperator);
- sb.append(indent).append(indent).append(indent).append(
- "<class>").append(e.getClassName()).append("</class>")
//$NON-NLS-1$//$NON-NLS-2$
- .append(lineSeperator);
- sb.append(indent).append(indent).append(indent).append(
- "<method>").append(e.getMethodName()).append( //$NON-NLS-1$
- "</method>").append(lineSeperator); //$NON-NLS-1$
- sb.append(indent).append(indent).append(indent)
- .append("<line>").append(e.getLineNumber()).append( //$NON-NLS-1$
- "</line>").append(lineSeperator); //$NON-NLS-1$
- sb.append(indent).append(indent).append("</frame>").append( //$NON-NLS-1$
- lineSeperator);
- }
- sb.append(indent).append("</exception>").append(lineSeperator); //$NON-NLS-1$
- }
- }
-
- /**
- * Return the header string for XML, use given handler's encoding if has,
- * other wise use default platform encoding
- *
- * @param h the given handler
- * @return the header string for XML
- */
- public String getHead(Handler h) {
- String encoding = h.getEncoding();
- if (null == encoding) {
- encoding = getSystemProperty("file.encoding"); //$NON-NLS-1$
- }
- StringBuffer sb = new StringBuffer();
- sb.append("<?xml version=\"1.0\" encoding=\"").append(encoding).append( //$NON-NLS-1$
- "\" standalone=\"no\"?>").append(lineSeperator); //$NON-NLS-1$
- sb.append("<!DOCTYPE log SYSTEM \"logger.dtd\">").append(lineSeperator); //$NON-NLS-1$
- sb.append(("<log>")); //$NON-NLS-1$
- return sb.toString();
- }
-
- /**
- * Return the tail string for XML
- *
- * @param h the given handler
- * @return the tail string for XML
- */
- public String getTail(Handler h) {
- return "</log>"; //$NON-NLS-1$
- }
-
- //use privilege code to get system property
- private static String getSystemProperty(final String key) {
- return (String) AccessController.doPrivileged(
- new PrivilegedAction<String>() {
- public String run() {
- return System.getProperty(key);
- }
- });
- }
-
-}
-
-
+/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package java.util.logging;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.text.MessageFormat;
+import java.util.Date;
+import java.util.ResourceBundle;
+
+/**
+ * Format a given <code>LogRecord</code> into string represents XML. The DTD
specified in
+ * Appendix A to Java Logging APIs specification is used.
+ *
+ * <code>XMLFormatter</code> uses given <code>Handler</code>'s encoding
if has, otherwise
+ * uses default platform encoding instead. However, the UTF-8 is recommended encoding.
+ *
+ */
+public class XMLFormatter extends Formatter {
+
+ /*
+ * ---------------------------------------
+ * Constants
+ * ---------------------------------------
+ */
+
+ private static final String lineSeperator = LogManager
+ .getSystemLineSeparator();
+
+ private static final String indent = " "; //$NON-NLS-1$
+
+ /*
+ * ---------------------------------------
+ * Constructor
+ * ---------------------------------------
+ */
+
+ /**
+ * Default constructor
+ */
+ public XMLFormatter() {
+ super();
+ }
+
+ /*
+ * ---------------------------------------
+ * Abstract method implementation of Formatter
+ * ---------------------------------------
+ */
+ /**
+ * Format a <code>LogRecord</code> into string which represents XML
+ *
+ * @param r the given LogRecord instance to be formatted
+ * @return string which represents XML
+ */
+ public String format(LogRecord r) {
+ //call a method of LogRecord to ensure not null
+ long time = r.getMillis();
+ //format to date
+ String date = MessageFormat.format("{0, date} {0, time}", //$NON-NLS-1$
+ new Object[] { new Date(time) });
+
+ StringBuffer sb = new StringBuffer();
+ sb.append(("<record>")).append(lineSeperator); //$NON-NLS-1$
+ sb.append(indent).append(("<date>")).append(date).append(("</date>"))
//$NON-NLS-1$ //$NON-NLS-2$
+ .append(lineSeperator);
+ sb.append(indent).append(("<millis>")).append(time).append( //$NON-NLS-1$
+ ("</millis>")).append(lineSeperator); //$NON-NLS-1$
+ sb.append(indent).append(("<sequence>")).append(r.getSequenceNumber()) //$NON-NLS-1$
+ .append(("</sequence>")).append(lineSeperator); //$NON-NLS-1$
+ if (null != r.getLoggerName()) {
+ sb.append(indent).append(("<logger>")).append(r.getLoggerName()) //$NON-NLS-1$
+ .append(("</logger>")).append(lineSeperator); //$NON-NLS-1$
+ }
+ sb.append(indent).append(("<level>")).append(r.getLevel().getName()) //$NON-NLS-1$
+ .append(("</level>")).append(lineSeperator); //$NON-NLS-1$
+ if (null != r.getSourceClassName()) {
+ sb.append(indent).append(("<class>")) //$NON-NLS-1$
+ .append(r.getSourceClassName()).append(("</class>")) //$NON-NLS-1$
+ .append(lineSeperator);
+ }
+ if (null != r.getSourceMethodName()) {
+ sb.append(indent).append(("<method>")).append( //$NON-NLS-1$
+ r.getSourceMethodName()).append(("</method>")).append( //$NON-NLS-1$
+ lineSeperator);
+ }
+ sb.append(indent).append(("<thread>")).append(r.getThreadID()).append( //$NON-NLS-1$
+ ("</thread>")).append(lineSeperator); //$NON-NLS-1$
+ formatMessages(r, sb);
+ Object[] params;
+ if ((params = r.getParameters()) != null) {
+ for (int i = 0; i < params.length; i++) {
+ sb.append(indent).append(("<param>")).append(params[i]).append( //$NON-NLS-1$
+ ("</param>")).append(lineSeperator); //$NON-NLS-1$
+ }
+ }
+ formatThrowable(r, sb);
+ sb.append(("</record>")).append(lineSeperator); //$NON-NLS-1$
+ return sb.toString();
+ }
+
+ /*
+ * ---------------------------------------
+ * Methods override Formatter
+ * ---------------------------------------
+ */
+
+ private void formatMessages(LogRecord r, StringBuffer sb) {
+ //get localized message if has, but don't call Formatter.formatMessage to parse pattern
string
+ ResourceBundle rb = r.getResourceBundle();
+ String pattern = r.getMessage();
+ if (null != rb && null != pattern) {
+ String message;
+ try {
+ message = rb.getString(pattern);
+ } catch (Exception e) {
+ message = null;
+ }
+
+ if (message == null) {
+ message = pattern;
+ sb.append(indent).append(("<message>")).append(message).append( //$NON-NLS-1$
+ ("</message>")).append(lineSeperator); //$NON-NLS-1$
+ } else {
+ sb.append(indent).append(("<message>")).append(message).append( //$NON-NLS-1$
+ ("</message>")).append(lineSeperator); //$NON-NLS-1$
+ sb.append(indent).append(("<key>")).append(pattern).append( //$NON-NLS-1$
+ ("</key>")).append(lineSeperator); //$NON-NLS-1$
+ sb.append(indent).append(("<catalog>")).append( //$NON-NLS-1$
+ r.getResourceBundleName()).append(("</catalog>")) //$NON-NLS-1$
+ .append(lineSeperator);
+ }
+ } else if(null != pattern){
+ sb.append(indent).append(("<message>")).append(pattern).append( //$NON-NLS-1$
+ ("</message>")).append(lineSeperator); //$NON-NLS-1$
+ } else{
+ sb.append(indent).append(("<message/>")); //$NON-NLS-1$
+ }
+ }
+
+ private void formatThrowable(LogRecord r, StringBuffer sb) {
+ Throwable t;
+ if ((t = r.getThrown()) != null) {
+ sb.append(indent).append("<exception>").append(lineSeperator); //$NON-NLS-1$
+ sb.append(indent).append(indent).append("<message>").append( //$NON-NLS-1$
+ t.toString()).append("</message>").append(lineSeperator); //$NON-NLS-1$
+ //format throwable's stack trace
+ StackTraceElement[] elements = t.getStackTrace();
+ for (int i = 0; i < elements.length; i++) {
+ StackTraceElement e = elements[i];
+ sb.append(indent).append(indent).append("<frame>").append( //$NON-NLS-1$
+ lineSeperator);
+ sb.append(indent).append(indent).append(indent).append(
+ "<class>").append(e.getClassName()).append("</class>")
//$NON-NLS-1$//$NON-NLS-2$
+ .append(lineSeperator);
+ sb.append(indent).append(indent).append(indent).append(
+ "<method>").append(e.getMethodName()).append( //$NON-NLS-1$
+ "</method>").append(lineSeperator); //$NON-NLS-1$
+ sb.append(indent).append(indent).append(indent)
+ .append("<line>").append(e.getLineNumber()).append( //$NON-NLS-1$
+ "</line>").append(lineSeperator); //$NON-NLS-1$
+ sb.append(indent).append(indent).append("</frame>").append( //$NON-NLS-1$
+ lineSeperator);
+ }
+ sb.append(indent).append("</exception>").append(lineSeperator); //$NON-NLS-1$
+ }
+ }
+
+ /**
+ * Return the header string for XML, use given handler's encoding if has,
+ * other wise use default platform encoding
+ *
+ * @param h the given handler
+ * @return the header string for XML
+ */
+ public String getHead(Handler h) {
+ String encoding = null;
+ if(null != h) {
+ encoding = h.getEncoding();
+ }
+ if (null == encoding) {
+ encoding = getSystemProperty("file.encoding"); //$NON-NLS-1$
+ }
+ StringBuffer sb = new StringBuffer();
+ sb.append("<?xml version=\"1.0\" encoding=\"").append(encoding).append( //$NON-NLS-1$
+ "\" standalone=\"no\"?>").append(lineSeperator); //$NON-NLS-1$
+ sb.append("<!DOCTYPE log SYSTEM \"logger.dtd\">").append(lineSeperator); //$NON-NLS-1$
+ sb.append(("<log>")); //$NON-NLS-1$
+ return sb.toString();
+ }
+
+ /**
+ * Return the tail string for XML
+ *
+ * @param h the given handler
+ * @return the tail string for XML
+ */
+ public String getTail(Handler h) {
+ return "</log>"; //$NON-NLS-1$
+ }
+
+ //use privilege code to get system property
+ private static String getSystemProperty(final String key) {
+ return (String) AccessController.doPrivileged(
+ new PrivilegedAction<String>() {
+ public String run() {
+ return System.getProperty(key);
+ }
+ });
+ }
+
+}
+
+
Modified: incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/XMLFormatterTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/XMLFormatterTest.java?rev=437203&r1=437202&r2=437203&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/XMLFormatterTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/logging/src/test/java/org/apache/harmony/logging/tests/java/util/logging/XMLFormatterTest.java
Sat Aug 26 11:44:09 2006
@@ -144,6 +144,10 @@
result = formatter.getHead(handler);
assertNull(handler.getEncoding());
// assertTrue(result.indexOf(defaultEncoding)>0);
+
+ // regression test for Harmony-1280
+ // make sure no NPE is thrown
+ formatter.getHead(null);
}
@@ -153,11 +157,6 @@
public void testInvalidParameter() {
formatter.getTail(null);
- try {
- formatter.getHead(null);
- fail();
- } catch (NullPointerException e) {
- }
try {
formatter.format(null);
fail();
|