Source Code Cross Referenced for StringUtils.java in  » Web-Services-apache-cxf-2.0.1 » common » org » apache » cxf » common » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Services apache cxf 2.0.1 » common » org.apache.cxf.common.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.