1 package org.sapia.soto.util; 2 3 import org.sapia.util.text.TemplateContextIF; 4 import org.sapia.util.text.TemplateElementIF; 5 import org.sapia.util.text.TemplateException; 6 import org.sapia.util.text.TemplateFactory; 7 8 import java.io.BufferedReader ; 9 import java.io.ByteArrayInputStream ; 10 import java.io.File ; 11 import java.io.IOException ; 12 import java.io.InputStream ; 13 import java.io.InputStreamReader ; 14 import java.io.StringWriter ; 15 16 import java.lang.reflect.Field ; 17 18 import java.util.ArrayList ; 19 import java.util.HashSet ; 20 import java.util.List ; 21 import java.util.Set ; 22 23 24 34 public class Utils { 35 44 public static String [] split(String toSplit, char splitChar, boolean trim) { 45 List tokens = new ArrayList (); 46 47 StringBuffer token = new StringBuffer (); 48 49 for (int i = 0; i < toSplit.length(); i++) { 50 if (toSplit.charAt(i) == splitChar) { 51 if (trim) { 52 tokens.add(token.toString().trim()); 53 } else { 54 tokens.add(token.toString()); 55 } 56 57 token.delete(0, token.length()); 58 } else { 59 token.append(toSplit.charAt(i)); 60 } 61 } 62 63 if (token.length() > 0) { 64 if (trim) { 65 tokens.add(token.toString().trim()); 66 } else { 67 tokens.add(token.toString()); 68 } 69 } 70 71 return (String []) tokens.toArray(new String [tokens.size()]); 72 } 73 74 84 public static void copyFields(Object src, Object trg) 85 throws Exception { 86 copyFields(src.getClass(), src, trg); 87 } 88 89 94 public static boolean isRelativePath(String path) { 95 path = path.trim(); 96 97 if (path.length() > 0) { 98 return !hasScheme(path) && 99 ((path.charAt(0) != '/') && (path.charAt(0) != '\\') && 100 (path.charAt(0) != '.')); 101 } 102 103 return false; 104 } 105 106 112 public static boolean hasScheme(String path) { 113 if (path == null) { 114 return false; 115 } 116 117 return path.indexOf(":/") >= 0; 118 } 119 120 127 public static String chopScheme(String path) { 128 int idx = path.indexOf(":/"); 129 130 if (idx >= 0) { 131 String toReturn = path.substring(idx + 2); 132 133 if (File.separator.equals("/") && (toReturn.charAt(0) != '/')) { 134 return File.separator + toReturn; 135 } else { 136 return toReturn; 137 } 138 } 139 140 return path; 141 } 142 143 149 public static String getScheme(String path) { 150 int idx = path.indexOf(":/"); 151 152 if (idx >= 0) { 153 return path.substring(0, idx); 154 } else { 155 return null; 156 } 157 } 158 159 171 public static InputStream replaceVars(TemplateContextIF ctx, InputStream is, 172 String resourceName) throws IOException { 173 String txt = textStreamToString(is); 174 175 try { 176 TemplateFactory fac = new TemplateFactory(); 177 TemplateElementIF elem = fac.parse(txt); 178 179 return new ByteArrayInputStream (elem.render(ctx).getBytes()); 180 } catch (TemplateException e) { 181 e.printStackTrace(); 182 throw new IOException ("Could not replace variables in content: " + 183 resourceName + " - " + e.getMessage()); 184 } finally { 185 is.close(); 186 } 187 } 188 189 198 public static String textStreamToString(InputStream is) 199 throws IOException { 200 BufferedReader reader = new BufferedReader (new InputStreamReader (is), 1024); 201 String line; 202 StringWriter writer = new StringWriter (); 203 204 while ((line = reader.readLine()) != null) { 205 writer.write(line); 206 writer.write(System.getProperty("line.separator")); 207 } 208 209 return writer.getBuffer().toString(); 210 } 211 212 229 public static String getRelativePath(String base, String relative, 230 boolean isBaseFile) { 231 String toReturn; 232 String compared = base.replace('\\', '/'); 233 234 if (isBaseFile) { 235 int idx; 236 237 if ((idx = compared.lastIndexOf('/')) >= 0) { 238 toReturn = base.substring(0, idx) + File.separator + relative; 239 } else { 240 toReturn = base + File.separator + relative; 241 } 242 } else { 243 if (compared.endsWith("//")) { 244 toReturn = base + relative; 245 } else { 246 toReturn = base + File.separator + relative; 247 } 248 } 249 250 return toReturn; 251 } 252 253 259 public static Class [] getClasses(Object src) { 260 Set classes = new HashSet (); 261 getClasses(classes, src.getClass()); 262 263 return (Class []) classes.toArray(new Class [classes.size()]); 264 } 265 266 private static void getClasses(Set classes, Class current) { 267 Class [] classArr = current.getInterfaces(); 268 269 for (int i = 0; i < classArr.length; i++) { 270 classes.add(classArr[i]); 271 getClasses(classes, classArr[i]); 272 } 273 274 current = current.getSuperclass(); 275 276 if (current != null) { 277 getClasses(classes, current); 278 } 279 } 280 281 private static void copyFields(Class current, Object src, Object trg) 282 throws Exception { 283 Field [] fields = current.getDeclaredFields(); 284 Object value; 285 286 for (int i = 0; i < fields.length; i++) { 287 if (!fields[i].isAccessible()) { 288 fields[i].setAccessible(true); 289 } 290 291 value = fields[i].get(src); 292 fields[i].set(trg, value); 293 } 294 295 current = current.getSuperclass(); 296 297 if (current != null) { 298 copyFields(current, src, trg); 299 } 300 } 301 } 302 | Popular Tags |