[LANG-1227] Add XMLCharacter class.

This commit is contained in:
ggregory 2016-05-09 00:31:06 -07:00
parent bbd1dc3439
commit dd5a0e6e1e
3 changed files with 189 additions and 0 deletions

View File

@ -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>

View File

@ -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;
}
}

View File

@ -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'));
}
}