scolebourne 2003/07/19 13:22:36
Modified: lang/src/test/org/apache/commons/lang
SerializationUtilsTest.java CharSetUtilsTest.java
StringEscapeUtilsTest.java WordWrapUtilsTest.java
lang/src/java/org/apache/commons/lang WordWrapUtils.java
StringEscapeUtils.java CharSetUtils.java
SerializationUtils.java
Log:
Update null behaviour for consistency and clarity
Doument null behaviour
Revision Changes Path
1.3 +5 -5
jakarta-commons/lang/src/test/org/apache/commons/lang/SerializationUtilsTest.java
Index: SerializationUtilsTest.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/SerializationUtilsTest.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SerializationUtilsTest.java 23 Mar 2003 21:50:58 -0000 1.2
+++ SerializationUtilsTest.java 19 Jul 2003 20:22:36 -0000 1.3
@@ -157,7 +157,7 @@
ByteArrayOutputStream streamTest = new ByteArrayOutputStream();
try {
SerializationUtils.serialize(iMap, null);
- } catch (NullPointerException ex) {
+ } catch (IllegalArgumentException ex) {
return;
}
fail();
@@ -167,7 +167,7 @@
ByteArrayOutputStream streamTest = new ByteArrayOutputStream();
try {
SerializationUtils.serialize(null, null);
- } catch (NullPointerException ex) {
+ } catch (IllegalArgumentException ex) {
return;
}
fail();
@@ -210,7 +210,7 @@
public void testDeserializeStreamNull() throws Exception {
try {
SerializationUtils.deserialize((InputStream) null);
- } catch (NullPointerException ex) {
+ } catch (IllegalArgumentException ex) {
return;
}
fail();
@@ -304,7 +304,7 @@
public void testDeserializeBytesNull() throws Exception {
try {
SerializationUtils.deserialize((byte[]) null);
- } catch (NullPointerException ex) {
+ } catch (IllegalArgumentException ex) {
return;
}
fail();
1.8 +60 -66
jakarta-commons/lang/src/test/org/apache/commons/lang/CharSetUtilsTest.java
Index: CharSetUtilsTest.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/CharSetUtilsTest.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- CharSetUtilsTest.java 23 Mar 2003 21:47:30 -0000 1.7
+++ CharSetUtilsTest.java 19 Jul 2003 20:22:36 -0000 1.8
@@ -63,10 +63,11 @@
*
* @author <a href="mailto:[EMAIL PROTECTED]">Henri Yandell</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Ringo De Smet</a>
+ * @author Stephen Colebourne
* @version $Id$
*/
-public class CharSetUtilsTest extends TestCase
-{
+public class CharSetUtilsTest extends TestCase {
+
public CharSetUtilsTest(String name) {
super(name);
}
@@ -76,8 +77,8 @@
}
public static Test suite() {
- TestSuite suite = new TestSuite(CharSetUtilsTest.class);
- suite.setName("CharSetUtils Tests");
+ TestSuite suite = new TestSuite(CharSetUtilsTest.class);
+ suite.setName("CharSetUtils Tests");
return suite;
}
@@ -91,66 +92,59 @@
//-----------------------------------------------------------------------
- public void testSqueeze()
- {
- assertEquals("squeeze(String,String[]) failed",
- "helo", CharSetUtils.squeeze("hello", new String[] {"el"}));
- assertEquals("squeeze(String,String[]) failed",
- "", CharSetUtils.squeeze("", new String[] {"el"}));
- assertEquals("squeeze(String,String[]) failed",
- "hello", CharSetUtils.squeeze("hello", new String[] {"e"}));
- assertEquals("squeeze(String,String[]) failed",
- "fofof", CharSetUtils.squeeze("fooffooff", new String[]
{"of"}));
- assertEquals("squeeze(String,String[]) failed",
- "fof", CharSetUtils.squeeze("fooooff", new String[] {"fo"}));
- }
-
- public void testCount()
- {
- assertEquals("count(String,String[]) failed",
- 3, CharSetUtils.count("hello", new String[] {"el"}));
- assertEquals("count(String,String[]) failed",
- 0, CharSetUtils.count("", new String[] {"el"}));
- assertEquals("count(String,String[]) failed",
- 0, CharSetUtils.count("hello", new String[] {"x"}));
- assertEquals("count(String,String[]) failed",
- 2, CharSetUtils.count("hello", new String[] {"e-i"}));
- assertEquals("count(String,String[]) failed",
- 5, CharSetUtils.count("hello", new String[] {"a-z"}));
- assertEquals("count(String,String[]) failed",
- 0, CharSetUtils.count("hello", new String[] {""}));
- }
-
- public void testKeep()
- {
- assertEquals("keep(String,String[]) failed",
- "ell", CharSetUtils.keep("hello", new String[] {"el"}));
- assertEquals("keep(String,String[]) failed",
- "hello", CharSetUtils.keep("hello", new String[] {"elho"}));
- assertEquals("keep(String,String[]) failed",
- "", CharSetUtils.keep("hello", new String[] {""}));
- assertEquals("keep(String,String[]) failed",
- "hello", CharSetUtils.keep("hello", new String[] {"a-z"}));
- assertEquals("keep(String,String[]) failed",
- "----", CharSetUtils.keep("----", new String[] {"-"}));
- assertEquals("keep(String,String[]) failed",
- "ll", CharSetUtils.keep("hello", new String[] {"l"}));
- }
-
- public void testDelete()
- {
- assertEquals("delete(String,String[]) failed",
- "ho", CharSetUtils.delete("hello", new String[] {"el"}));
- assertEquals("delete(String,String[]) failed",
- "", CharSetUtils.delete("hello", new String[] {"elho"}));
- assertEquals("delete(String,String[]) failed",
- "hello", CharSetUtils.delete("hello", new String[] {""}));
- assertEquals("delete(String,String[]) failed",
- "", CharSetUtils.delete("hello", new String[] {"a-z"}));
- assertEquals("delete(String,String[]) failed",
- "", CharSetUtils.delete("----", new String[] {"-"}));
- assertEquals("delete(String,String[]) failed",
- "heo", CharSetUtils.delete("hello", new String[] {"l"}));
+ public void testSqueeze() {
+ assertEquals(null, CharSetUtils.squeeze(null, (String[]) null));
+ assertEquals(null, CharSetUtils.squeeze(null, new String[] { "el" }));
+ assertEquals("helo", CharSetUtils.squeeze("hello", new String[] { "el" }));
+ assertEquals("", CharSetUtils.squeeze("", new String[] { "el" }));
+ assertEquals("hello", CharSetUtils.squeeze("hello", new String[] { "e" }));
+ assertEquals("fofof", CharSetUtils.squeeze("fooffooff", new String[] { "of"
}));
+ assertEquals("fof", CharSetUtils.squeeze("fooooff", new String[] { "fo" }));
+ try {
+ CharSetUtils.squeeze("hello", (String[]) null);
+ } catch (NullPointerException ex) {}
+ }
+
+ public void testCount() {
+ assertEquals(0, CharSetUtils.count(null, (String[]) null));
+ assertEquals(0, CharSetUtils.count(null, new String[] { "el" }));
+ assertEquals(3, CharSetUtils.count("hello", new String[] { "el" }));
+ assertEquals(0, CharSetUtils.count("", new String[] { "el" }));
+ assertEquals(0, CharSetUtils.count("hello", new String[] { "x" }));
+ assertEquals(2, CharSetUtils.count("hello", new String[] { "e-i" }));
+ assertEquals(5, CharSetUtils.count("hello", new String[] { "a-z" }));
+ assertEquals(0, CharSetUtils.count("hello", new String[] { "" }));
+ try {
+ CharSetUtils.count("hello", (String[]) null);
+ } catch (NullPointerException ex) {}
+ }
+
+ public void testKeep() {
+ assertEquals(null, CharSetUtils.keep(null, (String[]) null));
+ assertEquals(null, CharSetUtils.keep(null, new String[] { "el" }));
+ assertEquals("ell", CharSetUtils.keep("hello", new String[] { "el" }));
+ assertEquals("hello", CharSetUtils.keep("hello", new String[] { "elho" }));
+ assertEquals("", CharSetUtils.keep("hello", new String[] { "" }));
+ assertEquals("hello", CharSetUtils.keep("hello", new String[] { "a-z" }));
+ assertEquals("----", CharSetUtils.keep("----", new String[] { "-" }));
+ assertEquals("ll", CharSetUtils.keep("hello", new String[] { "l" }));
+ try {
+ CharSetUtils.keep("hello", (String[]) null);
+ } catch (NullPointerException ex) {}
+ }
+
+ public void testDelete() {
+ assertEquals(null, CharSetUtils.delete(null, (String[]) null));
+ assertEquals(null, CharSetUtils.delete(null, new String[] { "el" }));
+ assertEquals("ho", CharSetUtils.delete("hello", new String[] { "el" }));
+ assertEquals("", CharSetUtils.delete("hello", new String[] { "elho" }));
+ assertEquals("hello", CharSetUtils.delete("hello", new String[] { "" }));
+ assertEquals("", CharSetUtils.delete("hello", new String[] { "a-z" }));
+ assertEquals("", CharSetUtils.delete("----", new String[] { "-" }));
+ assertEquals("heo", CharSetUtils.delete("hello", new String[] { "l" }));
+ try {
+ CharSetUtils.delete("hello", (String[]) null);
+ } catch (NullPointerException ex) {}
}
+
}
-
1.9 +49 -1
jakarta-commons/lang/src/test/org/apache/commons/lang/StringEscapeUtilsTest.java
Index: StringEscapeUtilsTest.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/StringEscapeUtilsTest.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- StringEscapeUtilsTest.java 29 Jun 2003 03:05:45 -0000 1.8
+++ StringEscapeUtilsTest.java 19 Jul 2003 20:22:36 -0000 1.9
@@ -87,6 +87,22 @@
//-----------------------------------------------------------------------
public void testEscapeJava() throws IOException {
+ assertEquals(null, StringEscapeUtils.escapeJava(null));
+ try {
+ StringEscapeUtils.escapeJava(null, null);
+ fail();
+ } catch (IOException ex) {
+ fail();
+ } catch (IllegalArgumentException ex) {
+ }
+ try {
+ StringEscapeUtils.escapeJava(null, "");
+ fail();
+ } catch (IOException ex) {
+ fail();
+ } catch (IllegalArgumentException ex) {
+ }
+
assertEscapeJava("empty string", "", "");
assertEscapeJava(FOO, FOO);
assertEscapeJava("tab", "\\t", "\t");
@@ -122,6 +138,22 @@
}
public void testUnescapeJava() throws IOException {
+ assertEquals(null, StringEscapeUtils.unescapeJava(null));
+ try {
+ StringEscapeUtils.unescapeJava(null, null);
+ fail();
+ } catch (IOException ex) {
+ fail();
+ } catch (IllegalArgumentException ex) {
+ }
+ try {
+ StringEscapeUtils.unescapeJava(null, "");
+ fail();
+ } catch (IOException ex) {
+ fail();
+ } catch (IllegalArgumentException ex) {
+ }
+
assertUnescapeJava("", "");
assertUnescapeJava("test", "test");
assertUnescapeJava("\ntest\b", "\\ntest\\b");
@@ -154,6 +186,22 @@
}
public void testEscapeJavaScript() {
+ assertEquals(null, StringEscapeUtils.escapeJavaScript(null));
+ try {
+ StringEscapeUtils.escapeJavaScript(null, null);
+ fail();
+ } catch (IOException ex) {
+ fail();
+ } catch (IllegalArgumentException ex) {
+ }
+ try {
+ StringEscapeUtils.escapeJavaScript(null, "");
+ fail();
+ } catch (IOException ex) {
+ fail();
+ } catch (IllegalArgumentException ex) {
+ }
+
assertEquals("He didn\\'t say, \\\"stop!\\\"",
StringEscapeUtils.escapeJavaScript("He didn't say, \"stop!\""));
}
1.2 +6 -1
jakarta-commons/lang/src/test/org/apache/commons/lang/WordWrapUtilsTest.java
Index: WordWrapUtilsTest.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/WordWrapUtilsTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- WordWrapUtilsTest.java 15 Apr 2003 14:28:41 -0000 1.1
+++ WordWrapUtilsTest.java 19 Jul 2003 20:22:36 -0000 1.2
@@ -107,4 +107,9 @@
"Here is one line\nof\ttext that is\ngoing to be wrapped\nafter 20
columns.";
assertEquals("Text with tab at wrapping index didn't wrap correctly, ",
expected, WordWrapUtils.wrapText(input, "\n", 20));
}
+
+ public void testWrapText4() {
+ assertEquals(null, WordWrapUtils.wrapText(null, "\n", 20));
+ assertEquals("", WordWrapUtils.wrapText("", "\n", 20));
+ }
}
1.7 +19 -8
jakarta-commons/lang/src/java/org/apache/commons/lang/WordWrapUtils.java
Index: WordWrapUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/WordWrapUtils.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- WordWrapUtils.java 12 Jul 2003 03:06:23 -0000 1.6
+++ WordWrapUtils.java 19 Jul 2003 20:22:36 -0000 1.7
@@ -57,7 +57,11 @@
import java.util.StringTokenizer;
/**
- * <code>WordWrapUtils</code> is a utility class to assist with word wrapping.
+ * <p><code>WordWrapUtils</code> is a utility class to assist with word
wrapping.</p>
+ *
+ * <p>This class tries to handle <code>null</code> input gracefully.
+ * An exception will not be thrown for a <code>null</code> input.
+ * Each method documents its behaviour in more detail.</p>
*
* @author Henri Yandell
* @author Stephen Colebourne
@@ -91,13 +95,20 @@
* since tabs are a single character but are displayed as 4 or 8
* spaces. Remove the tabs.</p>
*
- * @param str text which is in need of word-wrapping
- * @param newline the characters that define a newline
+ * @param str text which is in need of word-wrapping, may be null
+ * @param newLineChars the characters that define a newline, null treated as \n
* @param wrapColumn the column to wrap the words at
- * @return the text with all the long lines word-wrapped
+ * @return the text with all the long lines word-wrapped,
+ * <code>null</code> if null string input
*/
- public static String wrapText(String str, String newline, int wrapColumn) {
- StringTokenizer lineTokenizer = new StringTokenizer(str, newline, true);
+ public static String wrapText(String str, String newLineChars, int wrapColumn) {
+ if (str == null) {
+ return null;
+ }
+ if (newLineChars == null) {
+ newLineChars = "\n";
+ }
+ StringTokenizer lineTokenizer = new StringTokenizer(str, newLineChars,
true);
StringBuffer stringBuffer = new StringBuffer();
while (lineTokenizer.hasMoreTokens()) {
@@ -106,7 +117,7 @@
if (nextLine.length() > wrapColumn) {
// This line is long enough to be wrapped.
- nextLine = wrapLine(nextLine, newline, wrapColumn);
+ nextLine = wrapLine(nextLine, newLineChars, wrapColumn);
}
stringBuffer.append(nextLine);
1.17 +77 -41
jakarta-commons/lang/src/java/org/apache/commons/lang/StringEscapeUtils.java
Index: StringEscapeUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/StringEscapeUtils.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- StringEscapeUtils.java 8 Jul 2003 05:59:58 -0000 1.16
+++ StringEscapeUtils.java 19 Jul 2003 20:22:36 -0000 1.17
@@ -60,12 +60,9 @@
/**
* <p>Escapes and unescapes <code>String</code>s for Java, Java Script, HTML, XML,
and SQL.
*
- * <p>Originally from
- * <a href="http://jakarta.apache.org/turbine/">Turbine</a> and the
- * GenerationJavaCore library and from
- * <a href="http://www.purpletech.com/code/">Purple Technology</a>
- * </p>
- *
+ * @author Apache Jakarta Turbine
+ * @author GenerationJavaCore library
+ * @author Purple Technology
* @author <a href="mailto:[EMAIL PROTECTED]">Henri Yandell</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Alexander Day Chaffee</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Antony Riley</a>
@@ -115,9 +112,8 @@
* </pre>
* </p>
*
- * @param str String to escape values in
- * @return String with escaped values
- * @throws NullPointerException if str is <code>null</code>
+ * @param str String to escape values in, may be null
+ * @return String with escaped values, <code>null</code> if null string input
*/
public static String escapeJava(String str) {
return escapeJavaStyleString(str, false);
@@ -127,10 +123,12 @@
* <p>Escapes the characters in a <code>String</code> using Java String rules to
* a <code>Writer</code>.</p>
*
+ * <p>A <code>null</code> string input has no effect.</p>
+ *
* @see #escapeJava(java.lang.String)
- * @param out Writer to write escaped string into
- * @param str String to escape values in
- * @throws NullPointerException if str is <code>null</code>
+ * @param out Writer to write escaped string into
+ * @param str String to escape values in, may be null
+ * @throws IllegalArgumentException if the Writer is <code>null</code>
* @throws IOException if error occurs on undelying Writer
*/
public static void escapeJava(Writer out, String str) throws IOException {
@@ -155,9 +153,8 @@
* </pre>
* </p>
*
- * @param str String to escape values in
- * @return String with escaped values
- * @throws NullPointerException if str is <code>null</code>
+ * @param str String to escape values in, may be null
+ * @return String with escaped values, <code>null</code> if null string input
*/
public static String escapeJavaScript(String str) {
return escapeJavaStyleString(str, true);
@@ -167,10 +164,12 @@
* <p>Escapes the characters in a <code>String</code> using JavaScript String
rules
* to a <code>Writer</code>.</p>
*
+ * <p>A <code>null</code> string input has no effect.</p>
+ *
* @see #escapeJavaScript(java.lang.String)
- * @param out Writer to write escaped string into
- * @param str String to escape values in
- * @throws NullPointerException if str is <code>null</code>
+ * @param out Writer to write escaped string into
+ * @param str String to escape values in, may be null
+ * @throws IllegalArgumentException if the Writer is <code>null</code>
* @throws IOException if error occurs on undelying Writer
**/
public static void escapeJavaScript(Writer out, String str) throws IOException {
@@ -178,6 +177,9 @@
}
private static String escapeJavaStyleString(String str, boolean
escapeSingleQuotes) {
+ if (str == null) {
+ return null;
+ }
try {
StringPrintWriter writer = new StringPrintWriter(str.length() * 2);
escapeJavaStyleString(writer, str, escapeSingleQuotes);
@@ -190,6 +192,12 @@
}
private static void escapeJavaStyleString(Writer out, String str, boolean
escapeSingleQuote) throws IOException {
+ if (out == null) {
+ throw new NullArgumentException("Writer");
+ }
+ if (str == null) {
+ return;
+ }
int sz;
sz = str.length();
for (int i = 0; i < sz; i++) {
@@ -271,10 +279,13 @@
* <code>'n'</code> into a newline character, unless the <code>'\'</code>
* is preceded by another <code>'\'</code>.</p>
*
- * @param str The <code>String</code> to unescape.
- * @return A new unescaped <code>String</code>.
+ * @param str the <code>String</code> to unescape, may be null
+ * @return a new unescaped <code>String</code>, <code>null</code> if null
string input
*/
public static String unescapeJava(String str) {
+ if (str == null) {
+ return null;
+ }
try {
StringPrintWriter writer = new StringPrintWriter(str.length());
unescapeJava(writer, str);
@@ -294,10 +305,19 @@
* <code>'n'</code> into a newline character, unless the <code>'\'</code>
* is preceded by another <code>'\'</code>.</p>
*
- * @param out The <code>Writer</code> used to output unescaped characters.
- * @param str The <code>String</code> to unescape.
+ * <p>A <code>null</code> string input has no effect.</p>
+ *
+ * @param out the <code>Writer</code> used to output unescaped characters
+ * @param str the <code>String</code> to unescape, may be null
+ * @throws IllegalArgumentException if the Writer is <code>null</code>
*/
public static void unescapeJava(Writer out, String str) throws IOException {
+ if (out == null) {
+ throw new NullArgumentException("Writer");
+ }
+ if (str == null) {
+ return;
+ }
int sz = str.length();
StringBuffer unicode = new StringBuffer(4);
boolean hadSlash = false;
@@ -382,9 +402,9 @@
* into a newline character, unless the <code>'\'</code> is preceded by another
* <code>'\'</code>.</p>
*
- * @param str The <code>String</code> to unescape.
- * @return A new unescaped <code>String</code>.
* @see #unescapeJava(String)
+ * @param str the <code>String</code> to unescape, may be null
+ * @return A new unescaped <code>String</code>, <code>null</code> if null
string input
*/
public static String unescapeJavaScript(String str) {
return unescapeJava(str);
@@ -398,10 +418,12 @@
* into a newline character, unless the <code>'\'</code> is preceded by another
* <code>'\'</code>.</p>
*
- * @param out The <code>Writer</code> used to output unescaped characters.
- * @param str The <code>String</code> to unescape.
-
+ * <p>A <code>null</code> string input has no effect.</p>
+ *
* @see #unescapeJava(Writer,String)
+ * @param out the <code>Writer</code> used to output unescaped characters
+ * @param str the <code>String</code> to unescape, may be null
+ * @throws IllegalArgumentException if the Writer is <code>null</code>
*/
public static void unescapeJavaScript(Writer out, String str) throws
IOException {
unescapeJava(out, str);
@@ -419,8 +441,8 @@
*
* <p>Supports all known HTML 4.0 entities, including funky accents.</p>
*
- * @param str The <code>String</code> to escape
- * @return A new escaped <code>String</code>.
+ * @param str the <code>String</code> to escape, may be null
+ * @return a new escaped <code>String</code>, <code>null</code> if null string
input
*
* @see Entities
* @see #unescapeHtml(String)
@@ -431,6 +453,9 @@
* @see </br><a
href="http://www.w3.org/TR/html401/charset.html#code-position">HTML 4.01 Code
positions</a>
**/
public static String escapeHtml(String str) {
+ if (str == null) {
+ return null;
+ }
//todo: add a version that takes a Writer
//todo: rewrite underlying method to use a Writer instead of a StringBuffer
return Entities.HTML40.escape(str);
@@ -448,11 +473,14 @@
* verbatim into the result string. e.g. "&gt;&zzzz;x" will
* become ">&zzzz;x".</p>
*
- * @param str The <code>String</code> to unescape
- * @return A new unescaped <code>String</code>.
+ * @param str the <code>String</code> to unescape, may be null
+ * @return a new unescaped <code>String</code>, <code>null</code> if null
string input
* @see #escapeHtml(String)
**/
public static String unescapeHtml(String str) {
+ if (str == null) {
+ return null;
+ }
return Entities.HTML40.unescape(str);
}
@@ -466,11 +494,14 @@
* <p>Supports only the four basic XML entities (gt, lt, quot, amp).
* Does not support DTDs or external entities.</p>
*
- * @param str The <code>String</code> to escape
- * @return A new escaped <code>String</code>.
+ * @param str the <code>String</code> to escape, may be null
+ * @return a new escaped <code>String</code>, <code>null</code> if null string
input
* @see #unescapeXml(java.lang.String)
**/
public static String escapeXml(String str) {
+ if (str == null) {
+ return null;
+ }
return Entities.XML.escape(str);
}
@@ -482,11 +513,14 @@
* <p>Supports only the four basic XML entities (gt, lt, quot, amp).
* Does not support DTDs or external entities.</p>
*
- * @param str The <code>String</code> to unescape
- * @return A new unescaped <code>String</code>.
+ * @param str the <code>String</code> to unescape, may be null
+ * @return a new unescaped <code>String</code>, <code>null</code> if null
string input
* @see #escapeXml(String)
**/
public static String unescapeXml(String str) {
+ if (str == null) {
+ return null;
+ }
return Entities.XML.unescape(str);
}
@@ -505,12 +539,14 @@
* handle the cases of percent (%) or underscore (_) for use in LIKE
clauses.</p>
*
* see http://www.jguru.com/faq/view.jsp?EID=8881
- * @param s the string to escape
- * @return A new String, escaped for SQL
+ * @param str the string to escape, may be null
+ * @return a new String, escaped for SQL, <code>null</code> if null string input
*/
- public static String escapeSql(String s)
- {
- return StringUtils.replace(s, "'", "''");
+ public static String escapeSql(String str) {
+ if (str == null) {
+ return null;
+ }
+ return StringUtils.replace(str, "'", "''");
}
}
1.14 +66 -34
jakarta-commons/lang/src/java/org/apache/commons/lang/CharSetUtils.java
Index: CharSetUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/CharSetUtils.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- CharSetUtils.java 16 Jul 2003 00:39:05 -0000 1.13
+++ CharSetUtils.java 19 Jul 2003 20:22:36 -0000 1.14
@@ -56,6 +56,10 @@
/**
* <p>Numerous routines to manipulate a <code>CharSet</code>.</p>
*
+ * <p>This class tries to handle <code>null</code> input gracefully.
+ * An exception will generally not be thrown for a <code>null</code> input.
+ * Each method documents its behaviour in more detail.</p>
+ *
* @author <a href="[EMAIL PROTECTED]">Henri Yandell</a>
* @author Stephen Colebourne
* @since 1.0
@@ -84,8 +88,8 @@
* <li>"ej-m" implies e,j->m. e,j,k,l,m.</li>
* </ul>
*
- * @param set
- * @return CharSet
+ * @param set the set, must not be null
+ * @return a CharSet instance
* @throws NullPointerException if any of set[i] is null or if set is null
*/
public static CharSet evaluateSet(String[] set) {
@@ -102,12 +106,15 @@
* </ul>
* @see #evaluateSet(java.lang.String[]) for set-syntax.
*
- * @param str the string to work from
- * @param set the character set to use for manipulation
- * @throws NullPointerException if <code>str</code> is <code>null</code>
+ * @param str the string to squeeze, may be null
+ * @param set the character set to use for manipulation, must not be null
+ * @return modified String, <code>null</code> if null string input
* @throws NullPointerException if <code>set</code> is <code>null</code>
*/
public static String squeeze(String str, String set) {
+ if (str == null) {
+ return null;
+ }
String[] strs = new String[1];
strs[0] = set;
return squeeze(str, strs);
@@ -123,13 +130,16 @@
* </ul>
* @see #evaluateSet(java.lang.String[]) for set-syntax.
*
- * @param str the string to work from
- * @param set the character set to use for manipulation
- * @throws NullPointerException if <code>str</code> is <code>null</code>
+ * @param str the string to squeeze, may be null
+ * @param set the character set to use for manipulation, must not be null
+ * @return modified String, <code>null</code> if null string input
* @throws NullPointerException if <code>set</code> is <code>null</code>
* or any element is <code>null</code>
*/
public static String squeeze(String str, String[] set) {
+ if (str == null) {
+ return null;
+ }
CharSet chars = evaluateSet(set);
StringBuffer buffer = new StringBuffer(str.length());
char[] chrs = str.toCharArray();
@@ -158,12 +168,15 @@
* <li>count("hello", {"c-f", "o"}) returns
2.</li>
* </ul>
*
- * @param str String target to count characters in
- * @param set String set of characters to count
- * @throws NullPointerException if <code>str</code> is <code>null</code>
+ * @param str String to count characters in, may be null
+ * @param set String set of characters to count, must not be null
+ * @return character count, zero if null string input
* @throws NullPointerException if <code>set</code> is <code>null</code>
*/
public static int count(String str, String set) {
+ if (str == null) {
+ return 0;
+ }
String[] strs = new String[1];
strs[0] = set;
return count(str, strs);
@@ -178,13 +191,16 @@
* <li>count("hello", {"c-f", "o"}) returns
2.</li>
* </ul>
*
- * @param str String target to count characters in
- * @param set String[] set of characters to count
- * @throws NullPointerException if <code>str</code> is <code>null</code>
+ * @param str String to count characters in, may be null
+ * @param set String[] set of characters to count, must not be null
+ * @return character count, zero if null string input
* @throws NullPointerException if <code>set</code> is <code>null</code>
* or any element is <code>null</code>
*/
public static int count(String str, String[] set) {
+ if (str == null) {
+ return 0;
+ }
CharSet chars = evaluateSet(set);
int count = 0;
char[] chrs = str.toCharArray();
@@ -206,12 +222,15 @@
* <li>keep("hello", {"c-fo"}) returns
"hll"</li>
* </ul>
*
- * @param str String target to keep characters from
- * @param set String set of characters to keep
- * @throws NullPointerException if <code>str</code> is <code>null</code>
+ * @param str String to keep characters from, may be null
+ * @param set String set of characters to keep, must not be null
+ * @return modified String, <code>null</code> if null string input
* @throws NullPointerException if <code>set</code> is <code>null</code>
*/
public static String keep(String str, String set) {
+ if (str == null) {
+ return null;
+ }
String[] strs = new String[1];
strs[0] = set;
return keep(str, strs);
@@ -227,13 +246,16 @@
* returns "hll"</li>
* </ul>
*
- * @param str String target to keep characters from
- * @param set String[] set of characters to keep
- * @throws NullPointerException if <code>str</code> is <code>null</code>
+ * @param str String to keep characters from, may be null
+ * @param set String[] set of characters to keep, must not be null
+ * @return modified String, <code>null</code> if null string input
* @throws NullPointerException if <code>set</code> is <code>null</code>
* or any element is <code>null</code>
*/
public static String keep(String str, String[] set) {
+ if (str == null) {
+ return null;
+ }
return modify(str, set, true);
}
@@ -246,12 +268,15 @@
* <li>delete("hello", {"c-fo"}) returns
"hll"</li>
* </ul>
*
- * @param str String target to delete characters from
- * @param set String set of characters to delete
- * @throws NullPointerException if <code>str</code> is <code>null</code>
+ * @param str String to delete characters from, may be null
+ * @param set String set of characters to delete, must not be null
+ * @return modified String, <code>null</code> if null string input
* @throws NullPointerException if <code>set</code> is <code>null</code>
*/
public static String delete(String str, String set) {
+ if (str == null) {
+ return null;
+ }
String[] strs = new String[1];
strs[0] = set;
return delete(str, strs);
@@ -267,13 +292,16 @@
* "hll"</li>
* </ul>
*
- * @param str String target to delete characters from
- * @param set String[] set of characters to delete
- * @throws NullPointerException if <code>str</code> is <code>null</code>
+ * @param str String to delete characters from, may be null
+ * @param set String[] set of characters to delete, must not be null
+ * @return modified String, <code>null</code> if null string input
* @throws NullPointerException if <code>set</code> is <code>null</code>
* or any element is <code>null</code>
*/
public static String delete(String str, String[] set) {
+ if (str == null) {
+ return null;
+ }
return modify(str, set, false);
}
@@ -304,15 +332,19 @@
* length of characters to replace, then the last character is
* used.</p>
*
- * @param target String to replace characters in
- * @param repl String to find that will be replaced
- * @param with String to put into the target String
- * @throws NullPointerException if <code>target</code>, with
+ * @param str String to replace characters in, may be null
+ * @param repl String to find that will be replaced, must not be null
+ * @param with String to put into the target String, must not be null
+ * @return translated String, <code>null</code> if null string input
+ * @throws NullPointerException if <code>target</code>, <code>with</code>
* or <code>repl</code> is <code>null</code>
*/
- public static String translate(String target, String repl, String with) {
- StringBuffer buffer = new StringBuffer(target.length());
- char[] chrs = target.toCharArray();
+ public static String translate(String str, String repl, String with) {
+ if (str == null) {
+ return null;
+ }
+ StringBuffer buffer = new StringBuffer(str.length());
+ char[] chrs = str.toCharArray();
char[] withChrs = with.toCharArray();
int sz = chrs.length;
int withMax = with.length() - 1;
1.7 +20 -5
jakarta-commons/lang/src/java/org/apache/commons/lang/SerializationUtils.java
Index: SerializationUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/SerializationUtils.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- SerializationUtils.java 23 Jun 2003 22:36:50 -0000 1.6
+++ SerializationUtils.java 19 Jul 2003 20:22:36 -0000 1.7
@@ -72,6 +72,9 @@
* <li>Deserialize managing finally and IOException
* </ul>
*
+ * <p>This class throws exceptions for invalid <code>null</code> inputs.
+ * Each method documents its behaviour in more detail.</p>
+ *
* @author <a href="mailto:[EMAIL PROTECTED]">Nissim Karpenstein</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Janek Bogucki</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Daniel Rall</a>
@@ -120,11 +123,15 @@
* <p>The stream passed in is not buffered internally within this method.
* This is the responsibility of your application if desired.</p>
*
- * @param obj the object to serialize to bytes
- * @param outputStream the stream to write to
+ * @param obj the object to serialize to bytes, may be null
+ * @param outputStream the stream to write to, must not be null
+ * @throws IllegalArgumentException if <code>outputStream</code> is
<code>null</code>
* @throws SerializationException (runtime) if the serialization fails
*/
public static void serialize(Serializable obj, OutputStream outputStream) {
+ if (outputStream == null) {
+ throw new NullArgumentException("OutputStream");
+ }
ObjectOutputStream out = null;
try {
// stream closed in the finally
@@ -168,11 +175,15 @@
* <p>The stream passed in is not buffered internally within this method.
* This is the responsibility of your application if desired.</p>
*
- * @param inputStream the serialized object input stream
+ * @param inputStream the serialized object input stream, must not be null
* @return the deserialized object
+ * @throws IllegalArgumentException if <code>inputStream</code> is
<code>null</code>
* @throws SerializationException (runtime) if the serialization fails
*/
public static Object deserialize(InputStream inputStream) {
+ if (inputStream == null) {
+ throw new NullArgumentException("InputStream");
+ }
ObjectInputStream in = null;
try {
// stream closed in the finally
@@ -197,11 +208,15 @@
/**
* <p>Deserializes a single <code>Object</code> from an array of bytes.</p>
*
- * @param objectData the serialized object
+ * @param objectData the serialized object, must not be null
* @return the deserialized object
+ * @throws IllegalArgumentException if <code>objectData</code> is
<code>null</code>
* @throws SerializationException (runtime) if the serialization fails
*/
public static Object deserialize(byte[] objectData) {
+ if (objectData == null) {
+ throw new NullArgumentException("byte[]");
+ }
ByteArrayInputStream bais = new ByteArrayInputStream(objectData);
return deserialize(bais);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]