[Practicalxml-commits] SF.net SVN: practicalxml:[77] trunk
Brought to you by:
kdgregory
|
From: Auto-Generated S. C. M. <pra...@li...> - 2009-04-25 13:17:39
|
Revision: 77
http://practicalxml.svn.sourceforge.net/practicalxml/?rev=77&view=rev
Author: kdgregory
Date: 2009-04-25 13:17:38 +0000 (Sat, 25 Apr 2009)
Log Message:
-----------
Add "internal" package
Remove dependency on Jakarta Commons lang
Modified Paths:
--------------
trunk/pom.xml
trunk/src/main/java/net/sf/practicalxml/DomUtil.java
trunk/src/main/java/net/sf/practicalxml/XmlUtil.java
Added Paths:
-----------
trunk/src/main/java/net/sf/practicalxml/internal/
trunk/src/main/java/net/sf/practicalxml/internal/StringUtils.java
trunk/src/main/java/net/sf/practicalxml/internal/package.html
trunk/src/test/java/net/sf/practicalxml/internal/
trunk/src/test/java/net/sf/practicalxml/internal/TestStringUtils.java
Modified: trunk/pom.xml
===================================================================
--- trunk/pom.xml 2009-04-25 12:24:37 UTC (rev 76)
+++ trunk/pom.xml 2009-04-25 13:17:38 UTC (rev 77)
@@ -122,11 +122,6 @@
<dependencies>
<dependency>
- <groupId>commons-lang</groupId>
- <artifactId>commons-lang</artifactId>
- <version>2.3</version>
- </dependency>
- <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.2</version>
Modified: trunk/src/main/java/net/sf/practicalxml/DomUtil.java
===================================================================
--- trunk/src/main/java/net/sf/practicalxml/DomUtil.java 2009-04-25 12:24:37 UTC (rev 76)
+++ trunk/src/main/java/net/sf/practicalxml/DomUtil.java 2009-04-25 13:17:38 UTC (rev 77)
@@ -27,11 +27,10 @@
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
+import net.sf.practicalxml.internal.StringUtils;
import net.sf.practicalxml.xpath.NamespaceResolver;
-import org.apache.commons.lang.StringUtils;
-
/**
* A collection of static utility methods for working with DOM trees.
* Most of these are usability workarounds for the <code>org.w3c.dom</code>
Modified: trunk/src/main/java/net/sf/practicalxml/XmlUtil.java
===================================================================
--- trunk/src/main/java/net/sf/practicalxml/XmlUtil.java 2009-04-25 12:24:37 UTC (rev 76)
+++ trunk/src/main/java/net/sf/practicalxml/XmlUtil.java 2009-04-25 13:17:38 UTC (rev 77)
@@ -21,7 +21,9 @@
import java.util.GregorianCalendar;
import java.util.TimeZone;
+import net.sf.practicalxml.internal.StringUtils;
+
/**
* A collection of static methods for manipulating XML as text.
*/
@@ -399,11 +401,9 @@
// caller has checked &#, so skip them
curPos += 2;
- boolean isHex = false;
int multiplier = 10;
if (s.charAt(curPos) == 'x')
{
- isHex = true;
multiplier = 16;
curPos++;
}
@@ -415,7 +415,7 @@
char c = s.charAt(curPos + ii);
if (c == ';')
break;
- int cVal = convertDigit(c, isHex);
+ int cVal = StringUtils.parseDigit(c, multiplier);
if (cVal < 0)
return '\0';
value = value * multiplier + cVal;
@@ -426,22 +426,4 @@
return (char)value;
}
-
-
- // FIXME - refactor this into a common method
- /**
- * Verifies that the passed character is a digit, and converts it to its
- * numeric value if yes. Returns -1 if not a legal digit.
- */
- private static int convertDigit(char c, boolean allowHex)
- {
- if ((c >= '0') && (c <= '9'))
- return c - '0';
- if (allowHex && (c >= 'a') && (c <= 'f'))
- return c - 'a' + 10;
- if (allowHex && (c >= 'A') && (c <= 'F'))
- return c - 'A' + 10;
- return -1;
- }
-
}
Added: trunk/src/main/java/net/sf/practicalxml/internal/StringUtils.java
===================================================================
--- trunk/src/main/java/net/sf/practicalxml/internal/StringUtils.java (rev 0)
+++ trunk/src/main/java/net/sf/practicalxml/internal/StringUtils.java 2009-04-25 13:17:38 UTC (rev 77)
@@ -0,0 +1,108 @@
+// Copyright 2008-2009 severally by the contributors
+//
+// 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 net.sf.practicalxml.internal;
+
+
+/**
+ * Static methods for working with strings and characters. This class exists
+ * primarily to break dependency on Jakarta Commons.
+ */
+public class StringUtils
+{
+ /**
+ * Returns true if the passed string is null or zero-length; false
+ * otherwise (including a string containing only whitespace). This
+ * is a replacement for the Jakarta Commons method with the same
+ * name.
+ */
+ public static boolean isEmpty(String s)
+ {
+ return (s == null) || (s.length() == 0);
+ }
+
+
+ /**
+ * Returns true if the passed string is null, zero-length, or contains
+ * only whitespace characters as defined by Character.isWhitespace();
+ * false otherwise. This is a replacement for the Jakarta Commons method
+ * with the same name.
+ */
+ public static boolean isBlank(String s)
+ {
+ if ((s == null) || (s.length() == 0))
+ return true;
+
+ for (int ii = 0 ; ii < s.length() ; ii++)
+ {
+ if (!Character.isWhitespace(s.charAt(ii)))
+ return false;
+ }
+
+ return true;
+ }
+
+
+ /**
+ * Trims all whitespace characters (as defined by Character.isWhitespace())
+ * from both ends of the string, returning an empty string if there's
+ * nothing left. Will also return an empty string if passed null. This is a
+ * replacement for the Jakarta Commons method with the same name.
+ */
+ public static String trimToEmpty(String s)
+ {
+ if ((s == null) || (s.length() == 0))
+ return "";
+
+ int i0 = 0;
+ int i1 = s.length() - 1;
+ while (i0 <= i1)
+ {
+ if (Character.isWhitespace(s.charAt(i0)))
+ i0++;
+ else if (Character.isWhitespace(s.charAt(i1)))
+ i1--;
+ else
+ return s.substring(i0, i1 + 1);
+ }
+
+ return "";
+ }
+
+
+ /**
+ * Parses the passed character as a digit in the specified base,
+ * returning its value. Bases > 10 are represented by ASCII letters
+ * in the range A to Z (or a to z). Base 36 is the largest supported.
+ *
+ * @return The value, or -1 if the character is not a valid digit
+ * in the specified base (this method will typically be used
+ * in a loop, so no good reason to force exception checking).
+ */
+ public static int parseDigit(char c, int base)
+ {
+ int value = -1;
+ if ((c >= '0') && (c <= '9'))
+ value = c - '0';
+ else if ((c >= 'a') && (c <= 'z'))
+ value = c - 'a' + 10;
+ else if ((c >= 'A') && (c <= 'Z'))
+ value = c - 'A' + 10;
+
+ if (value >= base)
+ value = -1;
+ return value;
+ }
+
+}
Added: trunk/src/main/java/net/sf/practicalxml/internal/package.html
===================================================================
--- trunk/src/main/java/net/sf/practicalxml/internal/package.html (rev 0)
+++ trunk/src/main/java/net/sf/practicalxml/internal/package.html 2009-04-25 13:17:38 UTC (rev 77)
@@ -0,0 +1,4 @@
+This package contains classes used internally by the PracticalXML library.
+Applications should not rely on the API of these classes, or indeed of their
+continued existence. In most cases, there's a better alternative in a third-
+party library.
\ No newline at end of file
Added: trunk/src/test/java/net/sf/practicalxml/internal/TestStringUtils.java
===================================================================
--- trunk/src/test/java/net/sf/practicalxml/internal/TestStringUtils.java (rev 0)
+++ trunk/src/test/java/net/sf/practicalxml/internal/TestStringUtils.java 2009-04-25 13:17:38 UTC (rev 77)
@@ -0,0 +1,77 @@
+// Copyright 2008-2009 severally by the contributors
+//
+// 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 net.sf.practicalxml.internal;
+
+import junit.framework.TestCase;
+
+
+public class TestStringUtils extends TestCase
+{
+ public void testIsEmpty() throws Exception
+ {
+ assertTrue(StringUtils.isEmpty(null));
+ assertTrue(StringUtils.isEmpty(""));
+
+ assertFalse(StringUtils.isEmpty("A"));
+ assertFalse(StringUtils.isEmpty(" "));
+ }
+
+
+ public void testIsBlank() throws Exception
+ {
+ assertTrue(StringUtils.isBlank(null));
+ assertTrue(StringUtils.isBlank(""));
+ assertTrue(StringUtils.isBlank(" "));
+ assertTrue(StringUtils.isBlank(" \n "));
+
+ assertFalse(StringUtils.isBlank("A"));
+ assertFalse(StringUtils.isBlank(" A "));
+ assertFalse(StringUtils.isBlank("\u00A0"));
+ }
+
+
+ public void testTrimToEmpty() throws Exception
+ {
+ assertEquals("", StringUtils.trimToEmpty(null));
+ assertEquals("", StringUtils.trimToEmpty(""));
+ assertEquals("", StringUtils.trimToEmpty(" \n \t "));
+
+ assertEquals("A", StringUtils.trimToEmpty(" A\n "));
+ assertEquals("AB", StringUtils.trimToEmpty(" AB\n "));
+ assertEquals("\u00A0", StringUtils.trimToEmpty("\u00A0"));
+ }
+
+
+ public void testParseDigit() throws Exception
+ {
+ assertEquals(0, StringUtils.parseDigit('0', 10));
+ assertEquals(9, StringUtils.parseDigit('9', 10));
+ assertEquals(-1, StringUtils.parseDigit('A', 10));
+
+ assertEquals(0, StringUtils.parseDigit('0', 16));
+ assertEquals(9, StringUtils.parseDigit('9', 16));
+ assertEquals(10, StringUtils.parseDigit('A', 16));
+ assertEquals(15, StringUtils.parseDigit('F', 16));
+ assertEquals(-1, StringUtils.parseDigit('G', 16));
+ assertEquals(10, StringUtils.parseDigit('a', 16));
+ assertEquals(15, StringUtils.parseDigit('f', 16));
+ assertEquals(-1, StringUtils.parseDigit('g', 16));
+
+ assertEquals(35, StringUtils.parseDigit('Z', 36));
+ assertEquals(35, StringUtils.parseDigit('z', 36));
+
+ assertEquals(-1, StringUtils.parseDigit('!', 100));
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|