Repository: commons-lang Updated Branches: refs/heads/master bbd1dc343 -> dd5a0e6e1
[LANG-1227] Add XMLCharacter class. Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/dd5a0e6e Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/dd5a0e6e Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/dd5a0e6e Branch: refs/heads/master Commit: dd5a0e6e1e3edb41afb4b40e4ec2c99e5932e73c Parents: bbd1dc3 Author: ggregory <[email protected]> Authored: Mon May 9 00:31:06 2016 -0700 Committer: ggregory <[email protected]> Committed: Mon May 9 00:31:06 2016 -0700 ---------------------------------------------------------------------- src/changes/changes.xml | 1 + .../org/apache/commons/lang3/XMLCharacter.java | 84 +++++++++++++++ .../apache/commons/lang3/XMLCharacterTest.java | 104 +++++++++++++++++++ 3 files changed, 189 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-lang/blob/dd5a0e6e/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 6e7d230..b3c7b1a 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,7 @@ <body> <release version="3.5" date="tba" description="tba"> + <action issue="LANG-1227" type="new" dev="ggregory" due-to="Gary Gregory">Add XMLCharacter class.</action> <action issue="LANG-1218" type="update" dev="ggregory" due-to="Ruslan Cheremin">EqualsBuilder.append(Object,Object) is too big to be inlined, which prevents whole builder to be scalarized</action> <action issue="LANG-1205" type="fix" dev="chas" due-to="pbrose">NumberUtils.createNumber() behaves inconsistently with NumberUtils.isNumber()</action> <action issue="LANG-1115" type="add" dev="chas" due-to="Jim Lloyd, Joe Ferner">Add support for varargs in ConstructorUtils, MemberUtils, and MethodUtils</action> http://git-wip-us.apache.org/repos/asf/commons-lang/blob/dd5a0e6e/src/main/java/org/apache/commons/lang3/XMLCharacter.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/lang3/XMLCharacter.java b/src/main/java/org/apache/commons/lang3/XMLCharacter.java new file mode 100644 index 0000000..ec42b2a --- /dev/null +++ b/src/main/java/org/apache/commons/lang3/XMLCharacter.java @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +/* + * From Apache Xalan XMLCharacterRecognizer. + */ +package org.apache.commons.lang3; + +/** + * Verifies whether specified primitives and objects conforms to the XML 1.0 definition of whitespace. + * + * <p> + * Copied and tweaked from Apache Xalan {@code XMLCharacterRecognizer} + * </p> + * + * @since 3.5 + */ +public class XMLCharacter { + + /** + * Returns whether the specified {@code ch} conforms to the XML 1.0 definition of whitespace. Refer to + * <a href="http://www.w3.org/TR/1998/REC-xml-19980210#NT-S"> the definition of <CODE>S</CODE></a> for details. + * + * @param ch + * Character to check as XML whitespace. + * @return true if {@code ch} is XML whitespace; otherwise false. + */ + public static boolean isWhitespace(final char ch) { + return ch == 0x20 || ch == 0x09 || ch == 0xD || ch == 0xA; + } + + /** + * Detects if the string is whitespace. + * + * @param ch + * Character array to check as XML whitespace. + * @param start + * Start index of characters in the array + * @param length + * Number of characters in the array + * @return true if the characters in the array are XML whitespace; otherwise, false. + */ + public static boolean isWhitespace(final char ch[], final int start, final int length) { + final int end = start + length; + for (int s = start; s < end; s++) { + if (!isWhitespace(ch[s])) { + return false; + } + } + return length > 0; + } + + /** + * Detects if the string is whitespace. + * + * @param charSequence + * StringBuffer to check as XML whitespace. + * @return True if characters in buffer are XML whitespace, false otherwise + */ + public static boolean isWhitespace(final CharSequence charSequence) { + final int length = charSequence.length(); + for (int i = 0; i < length; i++) { + if (!isWhitespace(charSequence.charAt(i))) { + return false; + } + } + return length > 0; + } + +} http://git-wip-us.apache.org/repos/asf/commons-lang/blob/dd5a0e6e/src/test/java/org/apache/commons/lang3/XMLCharacterTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/lang3/XMLCharacterTest.java b/src/test/java/org/apache/commons/lang3/XMLCharacterTest.java new file mode 100644 index 0000000..4984b88 --- /dev/null +++ b/src/test/java/org/apache/commons/lang3/XMLCharacterTest.java @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.commons.lang3; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests {@link XMLCharacter}. + */ +public class XMLCharacterTest { + + private static char[] XML_WHITESPACE_CHARS = { ' ', '\t', '\r', '\n' }; + + private static char[] JAVA_EXTRA_WHITESPACE_CHARS = { '\u000B', '\u001C', '\u001D', '\u001E', '\u001F' }; + + /** + * @see Character#isWhitespace(char) + */ + @Test + public void testIsWhitespace_char() { + for (final char c : XML_WHITESPACE_CHARS) { + Assert.assertTrue(XMLCharacter.isWhitespace(c)); + Assert.assertTrue(Character.isWhitespace(c)); + } + for (final char c : JAVA_EXTRA_WHITESPACE_CHARS) { + Assert.assertFalse(XMLCharacter.isWhitespace(c)); + Assert.assertTrue(Character.isWhitespace(c)); + } + // + Assert.assertFalse(XMLCharacter.isWhitespace('a')); + } + + @Test + public void testIsWhitespace_char_arrary() { + Assert.assertTrue(XMLCharacter.isWhitespace(XML_WHITESPACE_CHARS, 0, XML_WHITESPACE_CHARS.length)); + Assert.assertFalse( + XMLCharacter.isWhitespace(JAVA_EXTRA_WHITESPACE_CHARS, 0, JAVA_EXTRA_WHITESPACE_CHARS.length)); + } + + @Test + public void testIsWhitespace_CharSequence() { + Assert.assertFalse(XMLCharacter.isWhitespace(StringUtils.EMPTY)); + } + + @Test + public void testIsWhitespace_EmptyArray() { + Assert.assertFalse(XMLCharacter.isWhitespace(new char[] {}, 0, 0)); + } + + @Test + public void testIsWhitespace_String_firstChar() { + for (final char c : XML_WHITESPACE_CHARS) { + Assert.assertTrue(XMLCharacter.isWhitespace(Character.toString(c) + Character.toString(c))); + Assert.assertFalse(XMLCharacter.isWhitespace(Character.toString(c) + "X")); + } + for (final char c : JAVA_EXTRA_WHITESPACE_CHARS) { + Assert.assertFalse(XMLCharacter.isWhitespace(Character.toString(c) + "X")); + } + // + Assert.assertFalse(XMLCharacter.isWhitespace('a')); + } + + @Test + public void testIsWhitespace_String_lastChar() { + for (final char c : XML_WHITESPACE_CHARS) { + Assert.assertTrue(XMLCharacter.isWhitespace(Character.toString(c) + Character.toString(c))); + Assert.assertFalse(XMLCharacter.isWhitespace("X" + Character.toString(c))); + } + for (final char c : JAVA_EXTRA_WHITESPACE_CHARS) { + Assert.assertFalse(XMLCharacter.isWhitespace("X" + Character.toString(c))); + } + // + Assert.assertFalse(XMLCharacter.isWhitespace('a')); + } + + @Test + public void testIsWhitespace_String_singleChar() { + for (final char c : XML_WHITESPACE_CHARS) { + Assert.assertTrue(XMLCharacter.isWhitespace(Character.toString(c))); + } + for (final char c : JAVA_EXTRA_WHITESPACE_CHARS) { + Assert.assertFalse(XMLCharacter.isWhitespace(Character.toString(c))); + } + // + Assert.assertFalse(XMLCharacter.isWhitespace('a')); + } + +}
