一、前言
基于apache的cayenne源码包(2.0.4)中的org.apache.commons.lang.WordUtils字母单词通用工具类处理,如对字母只允许小写uncapitalize控制、只允许大写capitalize控制、转大写处理swapCase等
二、源码说明
package org.apache.commons.lang;@b@@b@public class WordUtils@b@{@b@ public static String capitalize(String str)@b@ {@b@ return capitalize(str, null);@b@ }@b@@b@ public static String capitalize(String str, char[] delimiters)@b@ {@b@ if ((str == null) || (str.length() == 0))@b@ return str;@b@@b@ int strLen = str.length();@b@ StringBuffer buffer = new StringBuffer(strLen);@b@@b@ int delimitersLen = 0;@b@ if (delimiters != null) {@b@ delimitersLen = delimiters.length;@b@ }@b@@b@ boolean capitalizeNext = true;@b@ for (int i = 0; i < strLen; ++i) {@b@ int j;@b@ char ch = str.charAt(i);@b@@b@ boolean isDelimiter = false;@b@ if (delimiters == null)@b@ isDelimiter = Character.isWhitespace(ch);@b@ else@b@ for (j = 0; j < delimitersLen; ++j)@b@ if (ch == delimiters[j]) {@b@ isDelimiter = true;@b@ break;@b@ }@b@@b@@b@@b@ if (isDelimiter) {@b@ buffer.append(ch);@b@ capitalizeNext = true;@b@ } else if (capitalizeNext) {@b@ buffer.append(Character.toTitleCase(ch));@b@ capitalizeNext = false;@b@ } else {@b@ buffer.append(ch);@b@ }@b@ }@b@ return buffer.toString();@b@ }@b@@b@ public static String capitalizeFully(String str)@b@ {@b@ return capitalizeFully(str, null);@b@ }@b@@b@ public static String capitalizeFully(String str, char[] delimiters)@b@ {@b@ if ((str == null) || (str.length() == 0))@b@ return str;@b@@b@ str = str.toLowerCase();@b@ return capitalize(str, delimiters);@b@ }@b@@b@ public static String swapCase(String str)@b@ {@b@ int strLen;@b@ if (str != null) if ((strLen = str.length()) != 0) break label15;@b@ return str;@b@@b@ label15: StringBuffer buffer = new StringBuffer(strLen);@b@@b@ boolean whitespace = true;@b@ char ch = ';@b@ char tmp = ';@b@@b@ for (int i = 0; i < strLen; ++i) {@b@ ch = str.charAt(i);@b@ if (Character.isUpperCase(ch))@b@ tmp = Character.toLowerCase(ch);@b@ else if (Character.isTitleCase(ch))@b@ tmp = Character.toLowerCase(ch);@b@ else if (Character.isLowerCase(ch))@b@ if (whitespace)@b@ tmp = Character.toTitleCase(ch);@b@ else@b@ tmp = Character.toUpperCase(ch);@b@@b@ else@b@ tmp = ch;@b@@b@ buffer.append(tmp);@b@ whitespace = Character.isWhitespace(ch);@b@ }@b@ return buffer.toString();@b@ }@b@@b@ public static String uncapitalize(String str)@b@ {@b@ return uncapitalize(str, null);@b@ }@b@@b@ public static String uncapitalize(String str, char[] delimiters)@b@ {@b@ if ((str == null) || (str.length() == 0))@b@ return str;@b@@b@ int strLen = str.length();@b@@b@ int delimitersLen = 0;@b@ if (delimiters != null) {@b@ delimitersLen = delimiters.length;@b@ }@b@@b@ StringBuffer buffer = new StringBuffer(strLen);@b@ boolean uncapitalizeNext = true;@b@ for (int i = 0; i < strLen; ++i) {@b@ int j;@b@ char ch = str.charAt(i);@b@@b@ boolean isDelimiter = false;@b@ if (delimiters == null)@b@ isDelimiter = Character.isWhitespace(ch);@b@ else@b@ for (j = 0; j < delimitersLen; ++j)@b@ if (ch == delimiters[j]) {@b@ isDelimiter = true;@b@ break;@b@ }@b@@b@@b@@b@ if (isDelimiter) {@b@ buffer.append(ch);@b@ uncapitalizeNext = true;@b@ } else if (uncapitalizeNext) {@b@ buffer.append(Character.toLowerCase(ch));@b@ uncapitalizeNext = false;@b@ } else {@b@ buffer.append(ch);@b@ }@b@ }@b@ return buffer.toString();@b@ }@b@@b@ public static String wrap(String str, int wrapLength)@b@ {@b@ return wrap(str, wrapLength, null, false);@b@ }@b@@b@ public static String wrap(String str, int wrapLength, String newLineStr, boolean wrapLongWords)@b@ {@b@ if (str == null)@b@ return null;@b@@b@ if (newLineStr == null)@b@ newLineStr = SystemUtils.LINE_SEPARATOR;@b@@b@ if (wrapLength < 1)@b@ wrapLength = 1;@b@@b@ int inputLineLength = str.length();@b@ int offset = 0;@b@ StringBuffer wrappedLine = new StringBuffer(inputLineLength + 32);@b@@b@ while (inputLineLength - offset > wrapLength)@b@ if (str.charAt(offset) == ' ') {@b@ ++offset;@b@ }@b@ else {@b@ int spaceToWrapAt = str.lastIndexOf(32, wrapLength + offset);@b@@b@ if (spaceToWrapAt >= offset)@b@ {@b@ wrappedLine.append(str.substring(offset, spaceToWrapAt));@b@ wrappedLine.append(newLineStr);@b@ offset = spaceToWrapAt + 1;@b@ }@b@ else if (wrapLongWords)@b@ {@b@ wrappedLine.append(str.substring(offset, wrapLength + offset));@b@ wrappedLine.append(newLineStr);@b@ offset += wrapLength;@b@ }@b@ else {@b@ spaceToWrapAt = str.indexOf(32, wrapLength + offset);@b@ if (spaceToWrapAt >= 0) {@b@ wrappedLine.append(str.substring(offset, spaceToWrapAt));@b@ wrappedLine.append(newLineStr);@b@ offset = spaceToWrapAt + 1;@b@ } else {@b@ wrappedLine.append(str.substring(offset));@b@ offset = inputLineLength;@b@ }@b@ }@b@@b@ }@b@@b@@b@ wrappedLine.append(str.substring(offset));@b@@b@ return wrappedLine.toString();@b@ }@b@}