harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r380635 - in /incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src: main/java/java/nio/charset/Charset.java test/java/org/apache/harmony/tests/java/nio/charset/CharsetTest.java
Date Fri, 24 Feb 2006 09:57:31 GMT
Author: tellison
Date: Fri Feb 24 01:57:28 2006
New Revision: 380635

URL: http://svn.apache.org/viewcvs?rev=380635&view=rev
Log:
Fix for HARMONY-113 (java.nio.charset.Charset should regard empty charset name properly as
Illegal CharsetName)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java
    incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java?rev=380635&r1=380634&r2=380635&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/main/java/java/nio/charset/Charset.java
Fri Feb 24 01:57:28 2006
@@ -15,8 +15,6 @@
 
 package java.nio.charset;
 
-
-
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
@@ -104,9 +102,6 @@
 	 * --------------------------------------------------------------------
 	 */
 
-	// the shared pattern used to check charset names
-	// private static Pattern _charsetNamePattern = Pattern
-	// .compile("[0-9A-Za-z][0-9A-Za-z\\x2e\\x3a\\x2d\\x5f]*"); //$NON-NLS-1$
 	// built in provider instance, assuming thread-safe
 	private static CharsetProviderICU _builtInProvider = null;
 
@@ -184,6 +179,10 @@
 	 */
 	protected Charset(String canonicalName, String[] aliases)
 			throws IllegalCharsetNameException {
+		// throw IllegalArgumentException if name is null
+		if (null == canonicalName) {
+			throw new NullPointerException();
+		}
 		// check whether the given canonical name is legal
 		checkCharsetName(canonicalName);
 		this.canonicalName = canonicalName;
@@ -211,23 +210,44 @@
 		return ('-' == c || '.' == c || ':' == c || '_' == c);
 	}
 
+    /*
+     * Checks whether a character is a letter (ascii) which are defined in 
+     * Java Spec.
+     */
+	private static boolean isLetter(char c) {
+		return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
+	}
+
+    /*
+     * Checks whether a character is a digit (ascii) which are defined in 
+     * Java Spec.
+     */
+	private static boolean isDigit(char c) {
+		return ('0' <= c && c <= '9');
+	}
+
 	/*
-	 * Checks whether a given string is a legal charset name. To be compatible
-	 * with JDK's behavior, allow empty strings and strings not starting with a
-	 * letter or digit. These are not allowed according to the 1.4.2 javadoc.
+	 * Checks whether a given string is a legal charset name. The argument name
+	 * should not be null.
 	 */
 	private static void checkCharsetName(String name) {
-		boolean isDigit;
-		boolean isLetter;
+		// An empty string is illegal charset name
+		if (name.length() == 0) {
+			throw new IllegalCharsetNameException(name);
+		}
+		// The first character must be a letter or a digit
+        // This is related to HARMONY-68 (won't fix)
+		//char first = name.charAt(0);
+		//if (!isLetter(first) && !isDigit(first)) {
+		//	throw new IllegalCharsetNameException(name);
+		//}
+		// Check the remaining characters
 		int length = name.length();
 		for (int i = 0; i < length; i++) {
 			char c = name.charAt(i);
-			isLetter = ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z');
-			isDigit = ('0' <= c && c <= '9');
-			if (isDigit || isLetter || isSpecial(c)) {
-				continue;
+			if (!isLetter(c) && !isDigit(c) && !isSpecial(c)) {
+				throw new IllegalCharsetNameException(name);
 			}
-			throw new IllegalCharsetNameException(name);
 		}
 	}
 
@@ -259,6 +279,19 @@
 	}
 
 	/*
+	 * Trim comment string, and then trim white spaces.
+	 */
+	private static String trimClassName(String name) {
+		String trimedName = name;
+		int index = name.indexOf(PROVIDER_CONFIGURATION_FILE_COMMENT);
+		// Trim comments
+		if (index != -1) {
+			trimedName = name.substring(0, index);
+		}
+		return trimedName.trim();
+	}
+
+	/*
 	 * Read a configuration file and add the charsets supported by the providers
 	 * specified by this configuration file to the map.
 	 */
@@ -272,12 +305,9 @@
 					PROVIDER_CONFIGURATION_FILE_ENCODING));
 			String providerClassName = reader.readLine();
 			while (null != providerClassName) {
-				// Trim leading and trailing whitespaces
-				providerClassName = providerClassName.trim();
+				providerClassName = trimClassName(providerClassName);
 				// Skip comments and blank lines
-				if (!providerClassName
-						.startsWith(PROVIDER_CONFIGURATION_FILE_COMMENT)
-						&& !"".equals(providerClassName)) { //$NON-NLS-1$
+				if (providerClassName.length() > 0) { // Non empty string
 					// Load the charset provider
 					Object cp = null;
 					try {
@@ -368,12 +398,8 @@
 					PROVIDER_CONFIGURATION_FILE_ENCODING));
 			String providerClassName = reader.readLine();
 			while (null != providerClassName) {
-				// Trim leading and trailing whitespaces
-				providerClassName = providerClassName.trim();
-				// Skip comments and blank lines
-				if (!providerClassName
-						.startsWith(PROVIDER_CONFIGURATION_FILE_COMMENT)
-						&& !"".equals(providerClassName)) { //$NON-NLS-1$
+				providerClassName = trimClassName(providerClassName);
+				if (providerClassName.length() > 0) { // Non empty string
 					// Load the charset provider
 					Object cp = null;
 					try {
@@ -417,6 +443,9 @@
 	 */
 	private static Charset forNameInternal(String charsetName)
 			throws IllegalCharsetNameException {
+		if (null == charsetName) {
+			throw new IllegalArgumentException();
+		}
 		checkCharsetName(charsetName);
 		// Try built-in charsets
 		Charset cs = _builtInProvider.charsetForName(charsetName);
@@ -459,24 +488,11 @@
 	 */
 	public static Charset forName(String charsetName)
 			throws IllegalCharsetNameException, UnsupportedCharsetException {
-		/*
-		 * The following ugly checks try to keep compatibility with JDK. It
-		 * rejects empty names and names starting with a non-letter&digit
-		 * character.
-		 */
-		if (null == charsetName || "".equals(charsetName)) { //$NON-NLS-1$
-			throw new IllegalArgumentException();
-		}
-		char initial = charsetName.charAt(0);
-		if (isSpecial(initial)) {
-			throw new IllegalArgumentException();
-		}
-
 		Charset c = forNameInternal(charsetName);
-		if (null != c) {
-			return c;
+		if (null == c) {
+			throw new UnsupportedCharsetException(charsetName);
 		}
-		throw new UnsupportedCharsetException(charsetName);
+		return c;
 	}
 
 	/**
@@ -490,10 +506,8 @@
 	 */
 	public static boolean isSupported(String charsetName)
 			throws IllegalCharsetNameException {
-		if (null == charsetName) {
-			throw new IllegalCharsetNameException(charsetName);
-		}
-		return null != forNameInternal(charsetName);
+		Charset cs = forNameInternal(charsetName);
+		return (null != cs);
 	}
 
 	/**
@@ -523,7 +537,7 @@
 	/**
 	 * Gets the canonical name of this charset.
 	 * 
-	 * @return this charset's name in canonical form. 
+	 * @return this charset's name in canonical form.
 	 */
 	public final String name() {
 		return this.canonicalName;
@@ -559,15 +573,15 @@
 	}
 
 	/**
-	 * Answers whether this charset is known to be registered in the
-	 * IANA Charset Registry.
+	 * Answers whether this charset is known to be registered in the IANA
+	 * Charset Registry.
 	 * 
-	 * @return true if the charset is known to be registered, otherwise
-	 * 	returns false.
+	 * @return true if the charset is known to be registered, otherwise returns
+	 *         false.
 	 */
 	public final boolean isRegistered() {
-		return !canonicalName.startsWith("x-") &&
-			!canonicalName.startsWith("X-");
+		return !canonicalName.startsWith("x-")
+				&& !canonicalName.startsWith("X-");
 	}
 
 	/**
@@ -643,11 +657,11 @@
 	 */
 
 	/**
-	 * Compares this charset with the given charset.
+	 * Compares this charset with the given charset, regardless of case.
 	 * 
-	 * @param obj
-	 *            the given object to be compared with
-	 * @return a negative integer if less than the given object, a positive
+	 * @param charset
+	 *            the given charset to be compared with
+	 * @return a negative integer if less than the given charset, a positive
 	 *         integer if larger than it, or 0 if equal to it
 	 */
 	public final int compareTo(Object obj) {
@@ -724,7 +738,6 @@
 		public int compare(Object obj1, Object obj2) {
 			String s1 = (String) obj1;
 			String s2 = (String) obj2;
-
 			return s1.compareToIgnoreCase(s2);
 		}
 	}

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetTest.java?rev=380635&r1=380634&r2=380635&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio_char/src/test/java/org/apache/harmony/tests/java/nio/charset/CharsetTest.java
Fri Feb 24 01:57:28 2006
@@ -16,6 +16,7 @@
 package org.apache.harmony.tests.java.nio.charset;
 
 import java.nio.charset.Charset;
+import java.nio.charset.IllegalCharsetNameException;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Set;
@@ -61,4 +62,17 @@
 					Charset.forName(name).isRegistered());
 		}
 	}
+	
+	/**
+	 * @tests java.nio.charset.Charset#isSupported(String)
+	 */
+	public void testIsSupported_EmptyString() {
+		// Regression for HARMONY-113
+		try {
+			Charset.isSupported("");
+			fail("Assert 0: Should throw IllegalCharsetNameException");
+		} catch (IllegalCharsetNameException e) {
+			// Expected
+		}
+	} 
 }



Mime
View raw message