001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.common.util;
019:
020: import java.io.File;
021: import java.net.MalformedURLException;
022: import java.net.URL;
023: import java.util.ArrayList;
024: import java.util.Arrays;
025: import java.util.List;
026: import java.util.regex.Matcher;
027: import java.util.regex.Pattern;
028:
029: public final class StringUtils {
030:
031: private StringUtils() {
032: }
033:
034: public static String extract(String string, String startToken,
035: String endToken) {
036: int start = string.indexOf(startToken) + startToken.length();
037: int end = string.lastIndexOf(endToken);
038:
039: if (start == -1 || end == -1) {
040: return null;
041: }
042:
043: return string.substring(start, end);
044: }
045:
046: public static String wrapper(String string, String startToken,
047: String endToken) {
048: StringBuffer sb = new StringBuffer();
049: sb.append(startToken);
050: sb.append(string);
051: sb.append(endToken);
052: return sb.toString();
053: }
054:
055: public static boolean isFileExist(String file) {
056: return new File(file).exists() && new File(file).isFile();
057: }
058:
059: public static boolean isFileAbsolute(String file) {
060: return isFileExist(file) && new File(file).isAbsolute();
061: }
062:
063: public static URL getURL(String spec) throws MalformedURLException {
064: try {
065: return new URL(spec);
066: } catch (MalformedURLException e) {
067: return new File(spec).toURI().toURL();
068: }
069: }
070:
071: public static boolean isEmpty(String str) {
072: if (str != null && str.trim().length() > 0) {
073: return false;
074: }
075: return true;
076: }
077:
078: public static boolean isEmpty(List<String> list) {
079: if (list == null || list.size() == 0) {
080: return true;
081: }
082: if (list.size() == 1 && isEmpty(list.get(0))) {
083: return true;
084: }
085: return false;
086: }
087:
088: public static String trim(String target, String token) {
089: int tokenLength = token.length();
090: int targetLength = target.length();
091:
092: if (target.startsWith(token)) {
093: return trim(target.substring(tokenLength), token);
094: }
095: if (target.endsWith(token)) {
096: return trim(
097: target.substring(0, targetLength - tokenLength),
098: token);
099: }
100: return target;
101: }
102:
103: public static boolean isEqualUri(String uri1, String uri2) {
104:
105: if (uri1.substring(uri1.length() - 1).equals("/")
106: && !uri2.substring(uri2.length() - 1).equals("/")) {
107: return uri1.substring(0, uri1.length() - 1).equals(uri2);
108: } else if (uri2.substring(uri2.length() - 1).equals("/")
109: && !uri1.substring(uri1.length() - 1).equals("/")) {
110: return uri2.substring(0, uri2.length() - 1).equals(uri1);
111: } else {
112: return uri1.equals(uri2);
113: }
114: }
115:
116: public static String diff(String str1, String str2) {
117: int index = str1.lastIndexOf(str2);
118: if (index > -1) {
119: return str1.substring(str2.length());
120: }
121: return str1;
122: }
123:
124: public static List<String> getParts(String str, String sperator) {
125: List<String> ret = new ArrayList<String>();
126: List<String> parts = Arrays.asList(str.split("/"));
127: for (String part : parts) {
128: if (!isEmpty(part)) {
129: ret.add(part);
130: }
131: }
132: return ret;
133: }
134:
135: public static String getFirstNotEmpty(String str, String sperator) {
136: List<String> parts = Arrays.asList(str.split("/"));
137: for (String part : parts) {
138: if (!isEmpty(part)) {
139: return part;
140: }
141: }
142: return str;
143: }
144:
145: public static String getFirstNotEmpty(List<String> list) {
146: if (isEmpty(list)) {
147: return null;
148: }
149: for (String item : list) {
150: if (!isEmpty(item)) {
151: return item;
152: }
153: }
154: return null;
155: }
156:
157: public static List<String> getFound(String contents, String regex) {
158: if (isEmpty(regex) || isEmpty(contents)) {
159: return null;
160: }
161: List<String> results = new ArrayList<String>();
162: Pattern pattern = Pattern.compile(regex, Pattern.UNICODE_CASE);
163: Matcher matcher = pattern.matcher(contents);
164:
165: while (matcher.find()) {
166: if (matcher.groupCount() > 0) {
167: results.add(matcher.group(1));
168: } else {
169: results.add(matcher.group());
170: }
171: }
172: return results;
173: }
174:
175: public static String getFirstFound(String contents, String regex) {
176: List<String> founds = getFound(contents, regex);
177: if (isEmpty(founds)) {
178: return null;
179: }
180: return founds.get(0);
181: }
182:
183: public static String formatVersionNumber(String target) {
184: List<String> found = StringUtils.getFound(target,
185: "\\d+\\.\\d+\\.?\\d*");
186: if (isEmpty(found)) {
187: return target;
188: }
189: return getFirstNotEmpty(found);
190: }
191:
192: public static String addDefaultPortIfMissing(String urlString) {
193: return addDefaultPortIfMissing(urlString, "80");
194: }
195:
196: public static String addDefaultPortIfMissing(String urlString,
197: String defaultPort) {
198: URL url = null;
199: try {
200: url = new URL(urlString);
201: } catch (MalformedURLException e) {
202: return urlString;
203: }
204: if (url.getPort() != -1) {
205: return urlString;
206: }
207: String regex = "http://([^/]+)";
208: String found = StringUtils.getFirstFound(urlString, regex);
209: String replacer = "http://" + found + ":" + defaultPort;
210:
211: if (!StringUtils.isEmpty(found)) {
212: urlString = urlString.replaceFirst(regex, replacer);
213: }
214: return urlString;
215: }
216: }
|