下面列出了怎么用org.apache.commons.lang.CharUtils的API类实例代码及写法,或者点击链接到github查看源代码。
private boolean isAsciiAlphaNumeric(String s) {
if (s == null) {
return true;
}
for (int i = 0; i < s.length(); i++) {
if (!CharUtils.isAsciiAlphanumeric(s.charAt(i))) {
return false;
}
}
return true;
}
/**
* 根据用户提供的json path,寻址Character对象
*
* @return Character对象,如果path不存在或者Character不存在,返回null
*/
public Character getChar(final String path) {
String result = this.getString(path);
if (null == result) {
return null;
}
try {
return CharUtils.toChar(result);
} catch (Exception e) {
throw new DLConfigException(
String.format("the config value [%s] for key [%s] can not be converted to char type.",
result, path));
}
}
@Override
public boolean init(LogFeederProps logFeederProps, String inputDesc, String fieldName, String mapClassCode, MapFieldDescriptor mapFieldDescriptor) {
init(inputDesc, fieldName, mapClassCode);
pattern = ((MapAnonymizeDescriptor)mapFieldDescriptor).getPattern();
if (StringUtils.isEmpty(pattern)) {
logger.fatal("pattern is empty.");
return false;
}
patternParts = Splitter.on("<hide>").omitEmptyStrings().split(pattern);
hideChar = CharUtils.toChar(((MapAnonymizeDescriptor)mapFieldDescriptor).getHideChar(), DEFAULT_HIDE_CHAR);
return true;
}
static private boolean isAsciiAlphaNumeric(String s) {
if (s == null) {
return true;
}
for (int i = 0; i < s.length(); i++) {
if (!CharUtils.isAsciiAlphanumeric(s.charAt(i))) {
return false;
}
}
return true;
}
public static void assertSimilarXml(String expectedXml, String xml) {
XMLUnit.setIgnoreWhitespace(true);
final Diff diff;
try {
diff = XMLUnit.compareXML(xml, expectedXml);
}
catch (SAXException | IOException ex) {
throw new IllegalArgumentException("Could not run XML comparison", ex);
}
final String message = "Diff: " + diff + CharUtils.LF + "XML: " + xml;
assertTrue(message, diff.similar());
}
private boolean isAsciiAlphaNumeric(String s) {
if (s == null) {
return true;
}
for (int i = 0; i < s.length(); i++) {
if (!CharUtils.isAsciiAlphanumeric(s.charAt(i))) {
return false;
}
}
return true;
}
@Test
public void testUnicodeEscapedDelimiter() {
for (int i = 0; i < 128; i++) {
char c = (char) i;
String delimiter = CharUtils.unicodeEscaped(c);
String escapedDelimiter = StringUtils.unicodeEscapedDelimiter(delimiter);
assertEquals(delimiter, escapedDelimiter);
assertEquals(1, StringEscapeUtils.unescapeJava(escapedDelimiter).length());
assertEquals(c, StringEscapeUtils.unescapeJava(escapedDelimiter).charAt(0));
}
}
@Test
public void testUnescapedDelimiter() {
for (int i = 0; i < 128; i++) {
char c = (char) i;
String delimiter = String.valueOf(c);
String escapedDelimiter = StringUtils.unicodeEscapedDelimiter(delimiter);
assertEquals(CharUtils.unicodeEscaped(c), escapedDelimiter);
assertEquals(1, StringEscapeUtils.unescapeJava(escapedDelimiter).length());
assertEquals(c, StringEscapeUtils.unescapeJava(escapedDelimiter).charAt(0));
}
}
void parseDelimiterChar(RequestContext input) {
String userDelim = input.getGreenplumCSV().getDelimiter() != null ?
String.valueOf(input.getGreenplumCSV().getDelimiter()) : null;
if (userDelim == null) {
/* No DELIMITER in URL, try to get it from fragment's user data*/
HiveUserData hiveUserData = HiveUtilities.parseHiveUserData(input);
if (hiveUserData.getDelimiter() == null) {
throw new IllegalArgumentException("DELIMITER is a required option");
}
delimiter = (char) Integer.valueOf(hiveUserData.getDelimiter()).intValue();
} else {
final int VALID_LENGTH = 1;
final int VALID_LENGTH_HEX = 4;
if (userDelim.startsWith("\\x")) { // hexadecimal sequence
if (userDelim.length() != VALID_LENGTH_HEX) {
throw new IllegalArgumentException(
"Invalid hexdecimal value for delimiter (got"
+ userDelim + ")");
}
delimiter = (char) Integer.parseInt(
userDelim.substring(2, VALID_LENGTH_HEX), 16);
if (!CharUtils.isAscii(delimiter)) {
throw new IllegalArgumentException(
"Invalid delimiter value. Must be a single ASCII character, or a hexadecimal sequence (got non ASCII "
+ delimiter + ")");
}
return;
}
if (userDelim.length() != VALID_LENGTH) {
throw new IllegalArgumentException(
"Invalid delimiter value. Must be a single ASCII character, or a hexadecimal sequence (got "
+ userDelim + ")");
}
if (!CharUtils.isAscii(userDelim.charAt(0))) {
throw new IllegalArgumentException(
"Invalid delimiter value. Must be a single ASCII character, or a hexadecimal sequence (got non ASCII "
+ userDelim + ")");
}
delimiter = userDelim.charAt(0);
}
}
public static boolean checkCharacter(String s){
return s.codePoints().allMatch(code -> CharUtils.isAsciiPrintable((char) code));
}
protected String toFormattedString(TableDesc desc) {
StringBuilder sb = new StringBuilder();
sb.append("\ntable name: ").append(desc.getName()).append("\n");
sb.append("table uri: ").append(desc.getUri()).append("\n");
sb.append("store type: ").append(desc.getMeta().getDataFormat()).append("\n");
if (desc.getStats() != null) {
long row = desc.getStats().getNumRows();
String rowText = row == TajoConstants.UNKNOWN_ROW_NUMBER ? "unknown" : row + "";
sb.append("number of rows: ").append(rowText).append("\n");
sb.append("volume: ").append(
FileUtil.humanReadableByteCount(desc.getStats().getNumBytes(),
true)).append("\n");
}
sb.append("Options:\n");
for(Map.Entry<String, String> entry : desc.getMeta().toMap().entrySet()){
/*
* Checks whether the character is ASCII 7 bit printable.
* For example, a printable unicode '\u007c' become the character ‘|’.
*
* Control-chars : ctrl-a(\u0001), tab(\u0009) ..
* Printable-chars : '|'(\u007c), ','(\u002c) ..
* */
String value = entry.getValue();
String unescaped = StringEscapeUtils.unescapeJava(value);
if (unescaped.length() == 1 && CharUtils.isAsciiPrintable(unescaped.charAt(0))) {
value = unescaped;
}
sb.append("\t").append("'").append(entry.getKey()).append("'").append("=")
.append("'").append(value).append("'").append("\n");
}
sb.append("\n");
sb.append("schema: \n");
for(int i = 0; i < desc.getSchema().size(); i++) {
Column col = desc.getSchema().getColumn(i);
sb.append(col.getSimpleName()).append("\t").append(col.getTypeDesc());
sb.append("\n");
}
sb.append("\n");
if (desc.getPartitionMethod() != null) {
PartitionMethodDesc partition = desc.getPartitionMethod();
sb.append("Partitions: \n");
sb.append("type:").append(partition.getPartitionType().name()).append("\n");
sb.append("columns:").append(":");
sb.append(StringUtils.join(partition.getExpressionSchema().toArray()));
}
return sb.toString();
}
public static String unicodeEscapedDelimiter(char c) {
return CharUtils.unicodeEscaped(c);
}