1 package org.sapia.resource; 2 3 import java.io.BufferedReader ; 4 import java.io.File ; 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.io.InputStreamReader ; 8 import java.io.StringWriter ; 9 import java.net.URI ; 10 import java.net.URISyntaxException ; 11 import java.util.ArrayList ; 12 import java.util.List ; 13 14 31 public class Utils { 32 46 public static String [] split(String toSplit, char splitChar, boolean trim) { 47 List tokens = new ArrayList (); 48 49 StringBuffer token = new StringBuffer (); 50 51 for(int i = 0; i < toSplit.length(); i++) { 52 if(toSplit.charAt(i) == splitChar) { 53 if(trim) { 54 tokens.add(token.toString().trim()); 55 } else { 56 tokens.add(token.toString()); 57 } 58 59 token.delete(0, token.length()); 60 } else { 61 token.append(toSplit.charAt(i)); 62 } 63 } 64 65 if(token.length() > 0) { 66 if(trim) { 67 tokens.add(token.toString().trim()); 68 } else { 69 tokens.add(token.toString()); 70 } 71 } 72 73 return (String []) tokens.toArray(new String [tokens.size()]); 74 } 75 76 83 public static boolean hasScheme(String path) { 84 if(path == null) { 85 return false; 86 } 87 88 return path.indexOf(":") >= 0; 89 } 90 91 100 public static String chopScheme(String path) { 101 int idx = path.indexOf(":"); 102 103 if(idx >= 0) { 104 String toReturn = path.substring(idx + 1); 105 if(toReturn.startsWith("//")){ 106 toReturn = toReturn.substring(1); 107 } 108 return toReturn; 109 } 110 111 return path; 112 } 113 114 122 public static String getScheme(String path) { 123 int idx = path.indexOf(":"); 124 125 if(idx >= 0) { 126 return path.substring(0, idx); 127 } else { 128 return null; 129 } 130 } 131 132 154 public static String getRelativePath(String base, String relative, 155 boolean isBaseFile) { 156 String toReturn; 157 String compared = base.replace('\\', '/'); 158 relative = Utils.chopScheme(relative); 159 if(isBaseFile) { 160 int idx; 161 162 if((idx = compared.lastIndexOf('/')) >= 0) { 163 toReturn = base.substring(0, idx) + File.separator + relative; 164 } else { 165 toReturn = base + File.separator + relative; 166 } 167 } else { 168 if(compared.endsWith("//")) { 169 toReturn = base + relative; 170 } else { 171 toReturn = base + File.separator + relative; 172 } 173 } 174 175 return toReturn; 176 } 177 178 public static URI toURIObject(String uri) throws IOException { 179 try{ 180 return new URI (uri); 181 }catch(URISyntaxException e){ 182 throw new IOException ("Could not parse uri: " + uri + " - " + e.getMessage()); 183 } 184 } 185 186 public static boolean isAbsolute(URI uri){ 187 return (uri.isAbsolute() && (uri.getSchemeSpecificPart().charAt(0) == '/' || 188 uri.getSchemeSpecificPart().charAt(0) == '\\')); 189 } 190 191 202 public static String textStreamToString(InputStream is) throws IOException { 203 BufferedReader reader = new BufferedReader (new InputStreamReader (is), 1024); 204 String line; 205 StringWriter writer = new StringWriter (); 206 207 while((line = reader.readLine()) != null) { 208 writer.write(line); 209 writer.write(System.getProperty("line.separator")); 210 } 211 212 return writer.getBuffer().toString(); 213 } 214 } 215 | Popular Tags |