KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > component > common > util > StringHelper


1 /**
2  * PETALS: PETALS Services Platform
3  * Copyright (C) 2005 EBM WebSourcing
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 any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): Adrien LOUIS
21  * --------------------------------------------------------------------------
22  * $Id: SourceHelper.java 94 2006-03-26 17:11:05Z alouis $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.petals.component.common.util;
27
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.StringTokenizer JavaDoc;
33
34 /**
35  *
36  * @author alouis, ofabre - EBM Websourcing
37  *
38  */

39 public final class StringHelper {
40
41     private StringHelper() {
42     }
43
44     /**
45      * Test the equality of the specified strings. test is : ( (a==b==null) ||
46      * a.equals(b) )
47      *
48      * @param a
49      * @param b
50      * @return
51      */

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

67     public static boolean equalIgnoreCase(String JavaDoc a, String JavaDoc b) {
68         if (a != null) {
69             return a.equalsIgnoreCase(b);
70         } else {
71             return b == null;
72         }
73     }
74
75     /**
76      * Return true if the String is null or its size is 0.
77      *
78      * @param s
79      * @return
80      */

81     public static boolean isEmpty(String JavaDoc s) {
82         if (s == null)
83             return true;
84
85         if (s.trim().equals(""))
86             return true;
87
88         return false;
89     }
90
91     /**
92      * Extract in the given String the value corresponding to the given
93      * attribute: ".......<ATTRIBUTE>=VALUE<SEPARATOR>......". - not case
94      * sensitive - return the first value for the first matching attribute - the
95      * separator between ATT and VALUE must be : "="
96      *
97      * @param string
98      * can be null or empty (return null)
99      * @param attribute
100      * can be null or empty (return null)
101      * @param separator
102      * (";"," ","&"...) can be null or empty. in this case, take the
103      * end of the String : "ATT=<VALUE etc etc>"
104      * @return the found attribute value, null otherwise
105      */

106     public static String JavaDoc extractValueForAttribute(String JavaDoc string,
107             String JavaDoc attribute, String JavaDoc separator) {
108         String JavaDoc result = null;
109
110         // check string and att are not empty
111
if (!isEmpty(string) && !isEmpty(attribute)) {
112             // find start index
113
int start = string.indexOf(attribute);
114
115             if (start >= 0 && start < string.length()) {
116                 start += attribute.length() + 1;
117
118                 if (start < string.length()) {
119                     // find end index
120
int end = 0;
121
122                     if (isEmpty(separator)) {
123                         // no separator, end = end of string
124
end = string.length();
125                     } else {
126                         // a separator is specified, find end index
127
end = string.indexOf(separator, start);
128
129                         if (end < 0) {
130                             end = string.length();
131                         }
132                     }
133                     result = string.substring(start, end);
134                 }
135             }
136         }
137         return result;
138     }
139
140     /**
141      * Split the given String path into String path elements. For example, the
142      * following path "/foo/bar" will be split into two parts "foo" and "bar",
143      * returned in List. If the given path includes empty path elements (Ex:
144      * "/foo//bar//"), the returned List doesn't contain these elements (for
145      * this exemple result will be "foo","bar").
146      *
147      * @param path
148      * the String path to split, can be null or empty (return an
149      * empty ArrayList)
150      * @return a List of String path elements, cannot be null, can be empty
151      */

152     public static List JavaDoc<String JavaDoc> splitPathElements(String JavaDoc path) {
153         List JavaDoc<String JavaDoc> pathElements = new ArrayList JavaDoc<String JavaDoc>();
154
155         if (!isEmpty(path)) {
156             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(path, "/");
157
158             while (tokenizer.hasMoreTokens()) {
159                 pathElements.add(tokenizer.nextToken());
160             }
161         }
162         return pathElements;
163     }
164
165     /**
166      * Convert an {@link InputStream} into a String.
167      *
168      * @param in
169      * the {@link InputStream} to convert
170      * @return the resulting String
171      * @throws IOException
172      * if error occurs
173      */

174     public static String JavaDoc inputStreamToString(InputStream JavaDoc in) throws IOException JavaDoc {
175         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
176         byte[] b = new byte[4096];
177         for (int n; (n = in.read(b)) != -1;) {
178             out.append(new String JavaDoc(b, 0, n));
179         }
180         return out.toString();
181     }
182
183 }
184
Popular Tags