1 package org.jicengine; 2 import org.jicengine.io.Resource; 3 import java.util.Map ; 4 import org.xml.sax.SAXException ; 5 import java.io.IOException ; 6 7 26 27 public class JICEngine { 28 29 private static Builder INSTANCE = new org.jicengine.builder.BuilderImpl(); 30 31 34 public static Builder getBuilder() 35 { 36 return INSTANCE; 37 } 38 39 private JICEngine() 40 { 41 } 42 43 60 public static Object build(Instructions instructions) throws IOException , SAXException , JICException 61 { 62 return getBuilder().build(instructions); 63 } 64 65 68 public static Object build(Resource jicFile, Map buildParameters) throws IOException , SAXException , JICException 69 { 70 return build(new Instructions(jicFile, buildParameters)); 71 } 72 73 public static Object build(Resource jicFile) throws IOException , SAXException , JICException 74 { 75 return build(jicFile, null); 76 } 77 78 86 public static void main(String [] args) throws Exception 87 { 88 if( args.length == 0 ){ 89 throw new IllegalArgumentException ("Usage: java org.jicengine.JICE -jic jic-file [-param name1 value1] [-param name2 value2] .."); 90 } 91 92 String jicFile = null; 93 Map parameters = new java.util.HashMap (); 94 95 96 for (int i = 0; i < args.length; i++) { 97 String arg = args[i]; 98 99 if( arg.startsWith("-") ){ 100 String name = arg.substring(1); 102 103 if( name.equals("jic") ){ 104 i++; 105 jicFile = getArgument(args, i, "expected '-jic' to be followed by a path."); 106 } 107 else if( name.equals("param") ){ 108 i++; 109 String paramName = getArgument(args, i, "expected '-param' to be followed by the param name."); 110 i++; 111 String paramValue = getArgument(args, i, "expected '-param " + paramName + " to be followed by the param value."); 112 parameters.put(paramName, paramValue); 113 } 114 else { 115 throw new IllegalArgumentException ("unknown argument: " + name); 116 } 117 } 118 else { 119 throw new IllegalArgumentException ("expected an argument-name starting with '-', got '" + arg + "'"); 120 } 121 } 122 123 if( jicFile == null ){ 124 throw new IllegalArgumentException ("argument -jic [jic-file] not specified."); 125 } 126 127 long now = System.currentTimeMillis(); 128 Object result = build(new org.jicengine.io.FileResource(new java.io.File (jicFile)), parameters); 129 long elapsed = System.currentTimeMillis() - now; 130 System.out.println("[JICE]: '" + jicFile + "' processed in " + elapsed + " ms. result:"); 131 System.out.println(" " + result); 132 if( result != null ){ 133 System.out.println(" [" + result.getClass().getName() + "]"); 134 } 135 } 136 137 private static String getArgument(String [] args, int index, String errorMessage) 138 { 139 try { 140 return args[index]; 141 } catch (ArrayIndexOutOfBoundsException e){ 142 throw new IllegalArgumentException (errorMessage); 143 } 144 } 145 } 146 | Popular Tags |