From dd5a0e6e1e3edb41afb4b40e4ec2c99e5932e73c Mon Sep 17 00:00:00 2001 From: ggregory Date: Mon, 9 May 2016 00:31:06 -0700 Subject: [PATCH] [LANG-1227] Add XMLCharacter class. --- src/changes/changes.xml | 1 + .../apache/commons/lang3/XMLCharacter.java | 84 ++++++++++++++ .../commons/lang3/XMLCharacterTest.java | 104 ++++++++++++++++++ 3 files changed, 189 insertions(+) create mode 100644 src/main/java/org/apache/commons/lang3/XMLCharacter.java create mode 100644 src/test/java/org/apache/commons/lang3/XMLCharacterTest.java diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 6e7d23051..b3c7b1a1f 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,7 @@ + Add XMLCharacter class. EqualsBuilder.append(Object,Object) is too big to be inlined, which prevents whole builder to be scalarized NumberUtils.createNumber() behaves inconsistently with NumberUtils.isNumber() Add support for varargs in ConstructorUtils, MemberUtils, and MethodUtils 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 000000000..ec42b2ada --- /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. + * + *

+ * Copied and tweaked from Apache Xalan {@code XMLCharacterRecognizer} + *

+ * + * @since 3.5 + */ +public class XMLCharacter { + + /** + * Returns whether the specified {@code ch} conforms to the XML 1.0 definition of whitespace. Refer to + * the definition of S 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; + } + +} 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 000000000..4984b88c3 --- /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')); + } + +}