1 package org.ozoneDB.tools.OPP; 9 10 13 import java.util.regex.Matcher ; 14 import java.util.regex.Pattern ; 15 import java.util.HashMap ; 16 import java.util.Map ; 17 import java.io.File ; 18 19 23 public class OPPHelper { 24 27 public final static String SIGNATURE_DELIMITER = "|"; 28 private static Map primitives; 29 30 static { 31 primitives = new HashMap (); 32 primitives.put("byte", Byte.TYPE); 33 primitives.put("char", Character.TYPE); 34 primitives.put("double", Double.TYPE); 35 primitives.put("float", Float.TYPE); 36 primitives.put("int", Integer.TYPE); 37 primitives.put("long", Long.TYPE); 38 primitives.put("short", Short.TYPE); 39 primitives.put("boolean", Boolean.TYPE); 40 } 41 42 public static String typecodeForPrimitive(char ch) throws Exception { 43 String ret; 44 switch (ch) { 45 case 'B': 46 ret = "byte"; 47 break; 48 case 'C': 49 ret = "char"; 50 break; 51 case 'D': 52 ret = "double"; 53 break; 54 case 'F': 55 ret = "float"; 56 break; 57 case 'I': 58 ret = "int"; 59 break; 60 case 'J': 61 ret = "long"; 62 break; 63 case 'S': 64 ret = "short"; 65 break; 66 case 'Z': 67 ret = "boolean"; 68 break; 69 default: 70 throw new Exception ("Unknown type code '" + ch + "'."); 71 } 72 return ret; 73 } 74 75 public static String wrappercodeForPrimitive(Class cl) { 76 String name = cl.getName(); 77 return wrappercodeForPrimitive(name); 78 } 79 80 public static String wrappercodeForPrimitive(String name) { 81 String ret; 82 if (name.equals("int")) { 83 ret = "Integer"; 84 } else if (name.equals("char")) { 85 ret = "Character"; 86 } else { 87 ret = name.substring(0, 1).toUpperCase() + name.substring(1); 88 } 89 return ret; 90 } 91 92 100 public static boolean isPrimitive(String typeName) { 101 return primitives.keySet().contains(typeName); 102 } 103 104 107 public static String returncodeForPrimitive(Class cl, String varName) { 108 String name = cl.getName(); 109 return returncodeForPrimitive(name, varName); 110 } 111 112 115 public static String returncodeForPrimitive(String name, String varName) { 116 String ret; 117 if (name.equals("int")) { 118 ret = "((Integer)" + varName + ").intValue()"; 119 } else if (name.equals("boolean")) { 120 ret = "((Boolean)" + varName + ").booleanValue()"; 121 } else if (name.equals("char")) { 122 ret = "((Character)" + varName + ").charValue()"; 123 } else if (name.equals("long")) { 124 ret = "((Long)" + varName + ").longValue()"; 125 } else if (name.equals("float")) { 126 ret = "((Float)" + varName + ").floatValue()"; 127 } else if (name.equals("double")) { 128 ret = "((Double)" + varName + ").doubleValue()"; 129 } else if (name.equals("byte")) { 130 ret = "((Byte)" + varName + ").byteValue()"; 131 } else if (name.equals("short")) { 132 ret = "((Short)" + varName + ").shortValue()"; 133 } else { 134 throw new IllegalArgumentException ("unknown type: '" + name + "'"); 135 } 136 return ret; 137 } 138 139 public static String packageName(Class c) { 140 String name = c.getName(); 141 return OPPHelper.packageName(name); 142 } 143 144 public static String packageName(String name) { 145 int index = name.lastIndexOf('.'); 146 return index != -1 ? name.substring(0, index) : ""; 147 } 148 149 public static String simpleClassName(Class c) { 150 return simpleClassName(c.getName()); 151 } 152 153 public static String simpleClassName(String name) { 154 int index = name.lastIndexOf('.'); 155 return index != -1 ? name.substring(index + 1) : name; 156 } 157 158 public static String classFileBasename(Class c) { 159 return classFileBasename(c.getName()); 160 } 161 162 public static String classFileBasename(String name) { 163 return classFileBasename(name, File.separatorChar); 164 } 165 166 public static String classFileBasename(String name, char separator) { 167 return name.replace('.', separator); 168 } 169 170 171 public static Pattern newRE(String s, boolean ignoreCase) { 172 return ignoreCase ? Pattern.compile(s, Pattern.CASE_INSENSITIVE) : Pattern.compile(s); 173 } 174 175 public static boolean reMatch(Object re, String s) { 176 Pattern pattern = (Pattern ) re; 177 Matcher matcher = pattern.matcher(s); 179 return matcher.matches(); 180 } 181 182 public static String reSearch(Object re, String s, int start, int paren) { 183 Pattern pattern = (Pattern ) re; 184 Matcher matcher = pattern.matcher(s); 185 if (matcher.find(start)) { 186 return matcher.group(paren); 187 } 188 return null; 189 } 190 191 public static void main(String [] av) throws Exception { 192 System.out.println(av[0]); 193 Object re = newRE(av[0], false); 194 System.out.println(reSearch(re, av[1], 0, 0)); 195 } 196 197 public static Class classForPrimitive(String type) { 198 return (Class ) primitives.get(type); 199 } 200 } 201 | Popular Tags |