一、前言
关于apache的fediz-core源码包中的org.apache.cxf.fediz.core.util.StringUtils字符串工具类,通过抽取extract指定开始结束字符标记Token的中间字符串数据、包装自定字符token范围的字符串数据、字符串判空及差异相当比较diff并去除、获取第一不为空字符串getFirstNotEmpty数据及大小写处理capitalize等。
二、源码说明
package org.apache.cxf.fediz.core.util;@b@@b@import java.io.File;@b@import java.net.MalformedURLException;@b@import java.net.URI;@b@import java.net.URL;@b@import java.util.ArrayList;@b@import java.util.Arrays;@b@import java.util.List;@b@import java.util.regex.Matcher;@b@import java.util.regex.Pattern;@b@@b@public final class StringUtils@b@{@b@ public static String extract(String string, String startToken, String endToken)@b@ {@b@ int start = string.indexOf(startToken) + startToken.length();@b@ int end = string.lastIndexOf(endToken);@b@@b@ if ((start == -1) || (end == -1)) {@b@ return null;@b@ }@b@@b@ return string.substring(start, end);@b@ }@b@@b@ public static String wrapper(String string, String startToken, String endToken)@b@ {@b@ StringBuilder sb = new StringBuilder();@b@ sb.append(startToken);@b@ sb.append(string);@b@ sb.append(endToken);@b@ return sb.toString();@b@ }@b@@b@ public static boolean isFileExist(String file) {@b@ return ((new File(file).exists()) && (new File(file).isFile()));@b@ }@b@@b@ public static boolean isFileAbsolute(String file) {@b@ return ((isFileExist(file)) && (new File(file).isAbsolute()));@b@ }@b@@b@ public static URL getURL(String spec) throws MalformedURLException {@b@ try {@b@ return new URL(spec); } catch (MalformedURLException e) {@b@ }@b@ return new File(spec).toURI().toURL();@b@ }@b@@b@ public static boolean isEmpty(String str)@b@ {@b@ int x;@b@ if (str != null) {@b@ int len = str.length();@b@ for (x = 0; x < len; ++x)@b@ if (str.charAt(x) > ' ')@b@ return false;@b@@b@ }@b@@b@ return true;@b@ }@b@@b@ public static boolean isEmpty(List<String> list) {@b@ if ((list == null) || (list.size() == 0)) {@b@ return true;@b@ }@b@@b@ return ((list.size() == 1) && (isEmpty((String)list.get(0))));@b@ }@b@@b@ public static boolean isEqualUri(String uri1, String uri2)@b@ {@b@ if ((uri1.substring(uri1.length() - 1).equals("/")) && (!(uri2.substring(uri2.length() - 1).equals("/"))))@b@ {@b@ return uri1.substring(0, uri1.length() - 1).equals(uri2); }@b@ if ((uri2.substring(uri2.length() - 1).equals("/")) && (!(uri1.substring(uri1.length() - 1).equals("/"))))@b@ {@b@ return uri2.substring(0, uri2.length() - 1).equals(uri1);@b@ }@b@ return uri1.equals(uri2);@b@ }@b@@b@ public static String diff(String str1, String str2)@b@ {@b@ int index = str1.lastIndexOf(str2);@b@ if (index > -1)@b@ return str1.substring(str2.length());@b@@b@ return str1;@b@ }@b@@b@ public static List<String> getParts(String str, String sperator) {@b@ List ret = new ArrayList();@b@ List parts = Arrays.asList(str.split("/"));@b@ for (String part : parts)@b@ if (!(isEmpty(part)))@b@ ret.add(part);@b@@b@@b@ return ret;@b@ }@b@@b@ public static String getFirstNotEmpty(String str, String sperator) {@b@ List parts = Arrays.asList(str.split("/"));@b@ for (String part : parts)@b@ if (!(isEmpty(part)))@b@ return part;@b@@b@@b@ return str;@b@ }@b@@b@ public static String getFirstNotEmpty(List<String> list) {@b@ if (isEmpty(list))@b@ return null;@b@@b@ for (String item : list)@b@ if (!(isEmpty(item)))@b@ return item;@b@@b@@b@ return null;@b@ }@b@@b@ public static List<String> getFound(String contents, String regex) {@b@ if ((isEmpty(regex)) || (isEmpty(contents)))@b@ return null;@b@@b@ List results = new ArrayList();@b@ Pattern pattern = Pattern.compile(regex, 64);@b@ Matcher matcher = pattern.matcher(contents);@b@ while (true) {@b@ while (true) { if (!(matcher.find())) break label85;@b@ if (matcher.groupCount() <= 0) break;@b@ results.add(matcher.group(1));@b@ }@b@ results.add(matcher.group());@b@ }@b@@b@ label85: return results;@b@ }@b@@b@ public static String getFirstFound(String contents, String regex) {@b@ List founds = getFound(contents, regex);@b@ if (isEmpty(founds))@b@ return null;@b@@b@ return ((String)founds.get(0));@b@ }@b@@b@ public static String formatVersionNumber(String target) {@b@ List found = getFound(target, "\\d+\\.\\d+\\.?\\d*");@b@@b@ if (isEmpty(found))@b@ return target;@b@@b@ return getFirstNotEmpty(found);@b@ }@b@@b@ public static String addDefaultPortIfMissing(String urlString) {@b@ return addDefaultPortIfMissing(urlString, "80");@b@ }@b@@b@ public static String addDefaultPortIfMissing(String urlString, String defaultPort)@b@ {@b@ URL url = null;@b@ try {@b@ url = new URL(urlString);@b@ } catch (MalformedURLException e) {@b@ return urlString;@b@ }@b@ if (url.getPort() != -1)@b@ return urlString;@b@@b@ String regex = "http://([^/]+)";@b@ String found = getFirstFound(urlString, regex);@b@ String replacer = "http://" + found + ":" + defaultPort;@b@@b@ if (!(isEmpty(found)))@b@ urlString = urlString.replaceFirst(regex, replacer);@b@@b@ return urlString;@b@ }@b@@b@ public static String capitalize(String name)@b@ {@b@ if ((name == null) || (name.length() == 0))@b@ return name;@b@@b@ char[] chars = name.toCharArray();@b@ chars[0] = Character.toUpperCase(chars[0]);@b@ return new String(chars);@b@ }@b@@b@ public static String uncapitalize(String str) {@b@ if ((str == null) || (str.length() == 0))@b@ return str;@b@@b@ return str.length() + Character.toLowerCase(str.charAt(0)) + str.substring(1);@b@ }@b@}