diff options
author | Neil Fuller <nfuller@google.com> | 2014-09-04 13:25:27 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2014-09-04 13:25:28 +0000 |
commit | fc5a9bde9565f9d6d5aaf1155743a669c37f7f08 (patch) | |
tree | 624efc4e0d138825530695f96fe4bde8d0db4e65 /luni/src | |
parent | e19e61898aa401d2a2803df6da6ac843f4822f98 (diff) | |
parent | c46b8c831e3c77c83aeaab54c83f914b6b471b0a (diff) | |
download | libcore-fc5a9bde9565f9d6d5aaf1155743a669c37f7f08.zip libcore-fc5a9bde9565f9d6d5aaf1155743a669c37f7f08.tar.gz libcore-fc5a9bde9565f9d6d5aaf1155743a669c37f7f08.tar.bz2 |
Merge "Fix apache-harmony logging tests"
Diffstat (limited to 'luni/src')
-rw-r--r-- | luni/src/main/java/java/util/logging/FileHandler.java | 14 | ||||
-rw-r--r-- | luni/src/main/java/java/util/logging/SocketHandler.java | 6 | ||||
-rw-r--r-- | luni/src/main/java/java/util/logging/XMLFormatter.java | 66 |
3 files changed, 67 insertions, 19 deletions
diff --git a/luni/src/main/java/java/util/logging/FileHandler.java b/luni/src/main/java/java/util/logging/FileHandler.java index 6ffef87..1fd1dbf 100644 --- a/luni/src/main/java/java/util/logging/FileHandler.java +++ b/luni/src/main/java/java/util/logging/FileHandler.java @@ -261,15 +261,15 @@ public class FileHandler extends StreamHandler { boolean hasUniqueID = false; boolean hasGeneration = false; - // TODO privilege code? + String homePath = System.getProperty("user.home"); + if (homePath == null) { + throw new NullPointerException("System property \"user.home\" is null"); + } + boolean homePathHasSepEnd = homePath.endsWith(File.separator); String tempPath = System.getProperty("java.io.tmpdir"); - boolean tempPathHasSepEnd = (tempPath == null ? false : tempPath - .endsWith(File.separator)); - - String homePath = System.getProperty("user.home"); - boolean homePathHasSepEnd = (homePath == null ? false : homePath - .endsWith(File.separator)); + tempPath = tempPath == null ? homePath : tempPath; + boolean tempPathHasSepEnd = tempPath.endsWith(File.separator); StringBuilder sb = new StringBuilder(); pattern = pattern.replace('/', File.separatorChar); diff --git a/luni/src/main/java/java/util/logging/SocketHandler.java b/luni/src/main/java/java/util/logging/SocketHandler.java index 48bfc0e..c6d4be0 100644 --- a/luni/src/main/java/java/util/logging/SocketHandler.java +++ b/luni/src/main/java/java/util/logging/SocketHandler.java @@ -106,9 +106,13 @@ public class SocketHandler extends StreamHandler { throw new IllegalArgumentException("host == null || host.isEmpty()"); } // check the validity of the port number - int p = 0; + int p; try { p = Integer.parsePositiveInt(port); + // Must be >= 0 to get this far. 0 is invalid too. + if (p == 0) { + throw new IllegalArgumentException("Illegal port argument " + port); + } } catch (NumberFormatException e) { throw new IllegalArgumentException("Illegal port argument " + port); } diff --git a/luni/src/main/java/java/util/logging/XMLFormatter.java b/luni/src/main/java/java/util/logging/XMLFormatter.java index 0d80b3e..3952596 100644 --- a/luni/src/main/java/java/util/logging/XMLFormatter.java +++ b/luni/src/main/java/java/util/logging/XMLFormatter.java @@ -17,6 +17,7 @@ package java.util.logging; +import java.io.IOException; import java.text.MessageFormat; import java.util.Date; import java.util.ResourceBundle; @@ -50,7 +51,7 @@ public class XMLFormatter extends Formatter { // 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 = MessageFormat.format("{0, date} {0, time}", new Date(time)); String nl = System.lineSeparator(); StringBuilder sb = new StringBuilder(); @@ -59,21 +60,21 @@ public class XMLFormatter extends Formatter { append(sb, 1, "millis", time); append(sb, 1, "sequence", r.getSequenceNumber()); if (r.getLoggerName() != null) { - append(sb, 1, "logger", r.getLoggerName()); + escapeAndAppend(sb, 1, "logger", r.getLoggerName()); } append(sb, 1, "level", r.getLevel().getName()); if (r.getSourceClassName() != null) { append(sb, 1, "class", r.getSourceClassName()); } if (r.getSourceMethodName() != null) { - append(sb, 1, "method", r.getSourceMethodName()); + escapeAndAppend(sb, 1, "method", r.getSourceMethodName()); } append(sb, 1, "thread", r.getThreadID()); formatMessages(r, sb); Object[] params = r.getParameters(); if (params != null) { for (Object element : params) { - append(sb, 1, "param", element); + escapeAndAppend(sb, 1, "param", element); } } formatThrowable(r, sb); @@ -96,14 +97,14 @@ public class XMLFormatter extends Formatter { if (message == null) { message = pattern; - append(sb, 1, "message", message); + escapeAndAppend(sb, 1, "message", message); } else { - append(sb, 1, "message", message); - append(sb, 1, "key", pattern); - append(sb, 1, "catalog", r.getResourceBundleName()); + escapeAndAppend(sb, 1, "message", message); + escapeAndAppend(sb, 1, "key", pattern); + escapeAndAppend(sb, 1, "catalog", r.getResourceBundleName()); } } else if (pattern != null) { - append(sb, 1, "message", pattern); + escapeAndAppend(sb, 1, "message", pattern); } else { sb.append(indent).append("<message/>"); } @@ -114,13 +115,13 @@ public class XMLFormatter extends Formatter { if ((t = r.getThrown()) != null) { String nl = System.lineSeparator(); sb.append(indent).append("<exception>").append(nl); - append(sb, 2, "message", t.toString()); + escapeAndAppend(sb, 2, "message", t.toString()); // format throwable's stack trace StackTraceElement[] elements = t.getStackTrace(); for (StackTraceElement e : elements) { sb.append(indent).append(indent).append("<frame>").append(nl); append(sb, 3, "class", e.getClassName()); - append(sb, 3, "method", e.getMethodName()); + escapeAndAppend(sb, 3, "method", e.getMethodName()); append(sb, 3, "line", e.getLineNumber()); sb.append(indent).append(indent).append("</frame>").append(nl); } @@ -138,6 +139,49 @@ public class XMLFormatter extends Formatter { sb.append(System.lineSeparator()); } + private static void escapeAndAppend(StringBuilder sb, int indentCount, String tag, Object value) { + if (value == null) { + append(sb, indentCount, tag, value); + } else { + for (int i = 0; i < indentCount; ++i) { + sb.append(indent); + } + sb.append("<").append(tag).append(">"); + try { + escapeXml(sb, value.toString()); + } catch (IOException e) { + throw new AssertionError(); + } + sb.append("</").append(tag).append(">"); + sb.append(System.lineSeparator()); + } + } + + private static void escapeXml(Appendable valueBuilder, String value) throws IOException { + for (int i = 0; i < value.length(); i++) { + char c = value.charAt(i); + switch (c) { + case '\"': + valueBuilder.append("""); + break; + case '>': + valueBuilder.append(">"); + break; + case '<': + valueBuilder.append("<"); + break; + case '&': + valueBuilder.append("&"); + break; + case '\'': + valueBuilder.append("'"); + break; + default: + valueBuilder.append(c); + } + } + } + /** * Returns the header string for a set of log records formatted as XML * strings, using the output handler's encoding if it is defined, otherwise |