1 25 26 package org.objectweb.petals.component.common.util; 27 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.util.ArrayList ; 31 import java.util.List ; 32 import java.util.StringTokenizer ; 33 34 39 public final class StringHelper { 40 41 private StringHelper() { 42 } 43 44 52 public static boolean equal(String a, String b) { 53 if (a != null) { 54 return a.equals(b); 55 } else { 56 return b == null; 57 } 58 } 59 60 67 public static boolean equalIgnoreCase(String a, String b) { 68 if (a != null) { 69 return a.equalsIgnoreCase(b); 70 } else { 71 return b == null; 72 } 73 } 74 75 81 public static boolean isEmpty(String s) { 82 if (s == null) 83 return true; 84 85 if (s.trim().equals("")) 86 return true; 87 88 return false; 89 } 90 91 106 public static String extractValueForAttribute(String string, 107 String attribute, String separator) { 108 String result = null; 109 110 if (!isEmpty(string) && !isEmpty(attribute)) { 112 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 int end = 0; 121 122 if (isEmpty(separator)) { 123 end = string.length(); 125 } else { 126 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 152 public static List <String > splitPathElements(String path) { 153 List <String > pathElements = new ArrayList <String >(); 154 155 if (!isEmpty(path)) { 156 StringTokenizer tokenizer = new StringTokenizer (path, "/"); 157 158 while (tokenizer.hasMoreTokens()) { 159 pathElements.add(tokenizer.nextToken()); 160 } 161 } 162 return pathElements; 163 } 164 165 174 public static String inputStreamToString(InputStream in) throws IOException { 175 StringBuffer out = new StringBuffer (); 176 byte[] b = new byte[4096]; 177 for (int n; (n = in.read(b)) != -1;) { 178 out.append(new String (b, 0, n)); 179 } 180 return out.toString(); 181 } 182 183 } 184 | Popular Tags |