KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > tools > jbicommon > util > StringHelper


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005-2006 EBM WebSourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: SourceHelper.java 94 2006-03-26 17:11:05Z alouis $
20  * -------------------------------------------------------------------------
21  */

22
23 package org.objectweb.petals.tools.jbicommon.util;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 /**
30  * Initial developer(s): Adrien LOUIS
31  * @author alouis, ofabre - EBM Websourcing
32  *
33  */

34 public final class StringHelper {
35
36     private StringHelper() {
37         super();
38     }
39     
40     /**
41      * Test the equality of the specified strings. test is : ( (a==b==null) ||
42      * a.equals(b) )
43      *
44      * @param a
45      * @param b
46      * @return
47      */

48     public static boolean equal(final String JavaDoc a, final String JavaDoc b) {
49         boolean result;
50         if (a == null) {
51             result = b == null;
52         } else {
53             result = a.equals(b);
54         }
55         return result;
56     }
57
58     /**
59      * Same as equal, with ignoreCase
60      *
61      * @param a
62      * @param b
63      * @return
64      */

65     public static boolean equalIgnoreCase(final String JavaDoc a, final String JavaDoc b) {
66         boolean result;
67         if (a == null) {
68             result = b == null;
69         } else {
70             result = a.equalsIgnoreCase(b);
71         }
72         return result;
73     }
74
75     /**
76      * TODO use Tokenizer
77      * Extract in the given String the value corresponding to the given
78      * attribute: ".......<ATTRIBUTE>=VALUE<SEPARATOR>......". - not case
79      * sensitive - return the first value for the first matching attribute - the
80      * separator between ATT and VALUE must be : "="
81      *
82      * @param string
83      * can be null or empty (return null)
84      * @param attribute
85      * can be null or empty (return null)
86      * @param separator
87      * (";"," ","&"...) can be null or empty. in this case, take the
88      * end of the String : "ATT=<VALUE etc etc>"
89      * @return the found attribute value, null otherwise
90      */

91     public static String JavaDoc extractValueForAttribute(final String JavaDoc string,
92             final String JavaDoc attribute, final String JavaDoc separator) {
93         String JavaDoc result = null;
94
95         // check string and att are not empty
96
if (!isNullOrEmpty(string) && !isNullOrEmpty(attribute)) {
97             // find start index
98
int start = string.indexOf(attribute);
99
100             if (start >= 0 && start < string.length()) {
101                 start += attribute.length() + 1;
102
103                 if (start < string.length()) {
104                     // find end index
105
int end = 0;
106
107                     if (isNullOrEmpty(separator)) {
108                         // no separator, end = end of string
109
end = string.length();
110                     } else {
111                         // a separator is specified, find end index
112
end = string.indexOf(separator, start);
113
114                         if (end < 0) {
115                             end = string.length();
116                         }
117                     }
118                     result = string.substring(start, end);
119                 }
120             }
121         }
122         return result;
123     }
124
125     /**
126      * Return true if the String is null or its size is 0.
127      *
128      * @param s
129      * @return
130      */

131     public static boolean isNullOrEmpty(final String JavaDoc s) {
132         return (s == null) || s.trim().equals("");
133     }
134
135     /**
136      * Split the given String path into String path elements. For example, the
137      * following path "/foo/bar" will be split into two parts "foo" and "bar",
138      * returned in List. If the given path includes empty path elements (Ex:
139      * "/foo//bar//"), the returned List doesn't contain these elements (for
140      * this exemple result will be "foo","bar").
141      *
142      * @param path
143      * the String path to split, can be null or empty (return an
144      * empty ArrayList)
145      * @return a List of String path elements, cannot be null, can be empty
146      */

147     public static List JavaDoc<String JavaDoc> splitPathElements(final String JavaDoc path) {
148         List JavaDoc<String JavaDoc> pathElements = new ArrayList JavaDoc<String JavaDoc>();
149
150         if (!isNullOrEmpty(path)) {
151             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(path, "/");
152
153             while (tokenizer.hasMoreTokens()) {
154                 pathElements.add(tokenizer.nextToken());
155             }
156         }
157         return pathElements;
158     }
159 }
160
Popular Tags