1 28 29 30 package org.jibx.binding; 31 32 import java.io.BufferedReader ; 33 import java.io.FileInputStream ; 34 import java.io.IOException ; 35 import java.io.InputStream ; 36 import java.io.InputStreamReader ; 37 import java.lang.reflect.InvocationTargetException ; 38 import java.lang.reflect.Method ; 39 import java.util.ArrayList ; 40 41 import org.jibx.runtime.JiBXException; 42 43 51 52 public class Run 53 { 54 private static final String BINDING_LIST_RESOURCE = "jibx_bindings.txt"; 55 private static final String DEFAULT_BINDING_RESOURCE = "jibx_binding.xml"; 56 57 private Run() {} 58 59 65 private static void addBindings(InputStream is, ArrayList bindings) 66 throws IOException { 67 BufferedReader rdr = new BufferedReader (new InputStreamReader (is)); 68 String line; 69 while ((line = rdr.readLine()) != null) { 70 if (line.length() > 0) { 71 bindings.add(line); 72 } 73 } 74 } 75 76 81 public static void main(String [] args) { 82 if (args.length >= 1) { 83 try { 84 85 ArrayList files = new ArrayList (); 87 ArrayList resources = new ArrayList (); 88 int index = 0; 89 String target = null; 90 while (index < args.length) { 91 String arg = args[index++]; 92 if ("-b".equals(arg)) { 93 if (index < args.length) { 94 files.add(args[index++]); 95 } else { 96 System.err.println("Missing binding file and " + 97 "target class following '-b'"); 98 } 99 } else if ("-l".equals(arg)) { 100 if (index < args.length) { 101 FileInputStream is = 102 new FileInputStream (args[index++]); 103 addBindings(is, files); 104 is.close(); 105 } else { 106 System.err.println("Missing binding list file " + 107 "and target class following '-l'"); 108 } 109 } else if ("-r".equals(arg)) { 110 if (index < args.length) { 111 resources.add(args[index++]); 112 } else { 113 System.err.println("Missing binding resource and " + 114 "target class following '-r'"); 115 } 116 } else { 117 target = arg; 118 break; 119 } 120 } 121 122 if (target != null) { 124 125 Loader loader = new Loader(); 127 128 if (files.size() == 0 && resources.size() == 0) { 130 InputStream is = 131 loader.getResourceAsStream(BINDING_LIST_RESOURCE); 132 if (is == null) { 133 String name = target.replace('.', '/') + "_" + 134 BINDING_LIST_RESOURCE; 135 is = loader.getResourceAsStream(name); 136 } 137 if (is != null) { 138 addBindings(is, resources); 139 is.close(); 140 } else { 141 String name = DEFAULT_BINDING_RESOURCE; 142 is = loader.getResourceAsStream(name); 143 if (is == null) { 144 name = target.replace('.', '/') + "_" + 145 DEFAULT_BINDING_RESOURCE; 146 is = loader.getResourceAsStream(name); 147 } 148 if (is != null) { 149 resources.add(name); 150 is.close(); 151 } 152 153 } 154 } 155 156 if (files.size() == 0 && resources.size() == 0) { 158 System.err.println("No bindings found"); 159 } else { 160 161 for (int i = 0; i < files.size(); i++) { 163 loader.loadFileBinding((String )files.get(i)); 164 } 165 for (int i = 0; i < resources.size(); i++) { 166 String path = (String )resources.get(i); 167 String fname = path; 168 int split = fname.lastIndexOf('/'); 169 if (split >= 0) { 170 fname = fname.substring(split+1); 171 } 172 String bname = fname; 173 split = bname.lastIndexOf('.'); 174 if (split >= 0) { 175 bname = bname.substring(0, split); 176 } 177 InputStream is = loader.getResourceAsStream(path); 178 if (is == null) { 179 throw new IOException ("Resource " + path + 180 " not found on classpath"); 181 } 182 loader.loadBinding(fname, 183 Utility.convertName(bname), is, null); 184 } 185 loader.processBindings(); 186 187 Class clas = loader.loadClass(target); 189 190 Class [] ptypes = new Class [] { args.getClass() }; 192 Method main = clas.getDeclaredMethod("main", ptypes); 193 String [] pargs = new String [args.length-index]; 194 System.arraycopy(args, index, pargs, 0, pargs.length); 195 Thread.currentThread().setContextClassLoader(loader); 196 main.invoke(null, new Object [] { pargs }); 197 198 } 199 } 200 201 } catch (ClassNotFoundException e) { 202 e.printStackTrace(); 203 } catch (NoSuchMethodException e) { 204 e.printStackTrace(); 205 } catch (IllegalArgumentException e) { 206 e.printStackTrace(); 207 } catch (IllegalAccessException e) { 208 e.printStackTrace(); 209 } catch (InvocationTargetException e) { 210 e.printStackTrace(); 211 } catch (JiBXException e) { 212 e.printStackTrace(); 213 } catch (IOException e) { 214 e.printStackTrace(); 215 } 216 217 } else { 218 System.out.println 219 ("Usage: org.jibx.binding.Run [-b binding-file][-l list-file]" + 220 "[-r binding-resource] main-class args..."); 221 } 222 } 223 } | Popular Tags |