1 package org.jicengine.builder; 2 import javax.xml.parsers.*; 3 import org.xml.sax.SAXException ; 4 import org.jicengine.*; 5 import java.io.File ; 6 import java.io.IOException ; 7 import java.util.Map ; 8 import java.util.Iterator ; 9 import org.jicengine.element.Element; 10 import org.jicengine.element.ElementException; 11 import org.jicengine.element.VariableElement; 12 import org.jicengine.element.ActionElement; 13 import org.jicengine.io.Resource; 14 import org.jicengine.io.UrlReadable; 15 import org.jicengine.operation.Context; 16 import org.jicengine.operation.SimpleContext; 17 30 31 public class BuilderImpl implements Builder { 32 33 private static boolean log = false; 34 36 public BuilderImpl() { 37 } 38 39 public Object build(Instructions instructions) throws IOException , SAXException , JICException 40 { 41 42 long begin = System.currentTimeMillis(); 44 Element rootElement = parse(instructions); 45 long parseTime = System.currentTimeMillis() - begin; 46 47 begin = System.currentTimeMillis(); 49 Object result = execute(rootElement, instructions); 50 long execTime = System.currentTimeMillis() - begin; 51 52 if( log ){ 53 System.out.println("[" + instructions.getFile() + "]:"); 54 System.out.println(" parse time : " + parseTime); 55 System.out.println(" exec time : " + execTime); 56 System.out.println(" total time : " + (parseTime+execTime)); 57 System.out.println(); 58 } 59 60 return result; 61 } 62 63 private SAXParser getSAXParser() throws JICException 64 { 65 try { 66 SAXParserFactory factory = SAXParserFactory.newInstance(); 67 factory.setNamespaceAware(true); 68 factory.setFeature("http://xml.org/sax/features/namespaces", true); 69 factory.setFeature("http://xml.org/sax/features/namespace-prefixes", true); 70 SAXParser parser = factory.newSAXParser(); 71 return parser; 72 } catch (Exception e){ 73 throw new JICException("Failed to obtain a SAX parser", e); 74 } 75 } 76 77 private Element parse(Instructions instructions) throws IOException , SAXException , JICException 78 { 79 SAXParser parser = getSAXParser(); 80 Handler handler = new Handler(); 81 83 try { 84 parser.parse(instructions.getFile().getInputStream(), handler, getSystemIdentifier(instructions)); 85 return handler.getResult(); 86 } catch (org.xml.sax.SAXException e){ 87 Exception cause = e.getException(); 88 if( cause != null ){ 89 if( cause instanceof JICException ){ 90 throw (JICException) cause; 91 } 92 else { 93 throw new JICException(cause); 94 } 95 } 96 else { 97 throw e; 99 } 100 } 101 } 102 103 private static String getSystemIdentifier(Instructions instructions) 104 { 105 Resource resource = instructions.getFile(); 106 String systemIdentifier; 107 if( resource instanceof UrlReadable){ 108 try { 109 systemIdentifier = ((UrlReadable)resource).getUrl().toString(); 110 } catch (IOException e){ 111 System.out.println(new java.util.Date ()+ " [JICE]: failed to create resource URL, using identifier '" + resource.getIdentifier() + "' as system identifier"); 112 systemIdentifier = resource.getIdentifier(); 113 } 114 } 115 else { 116 systemIdentifier = resource.getIdentifier(); 117 } 118 119 return systemIdentifier; 120 } 121 122 private Object execute(Element rootElement, Instructions buildInstructions) throws ElementException 123 { 124 Context rootContext = new SimpleContext("//"); 125 BuildContext jiceContext = new BuildContextImpl(buildInstructions); 126 rootContext.addObject(BuildContext.VARIABLE_NAME, jiceContext); 127 128 rootContext.addObject("true", Boolean.TRUE); 130 rootContext.addObject("false", Boolean.FALSE); 131 132 Map buildParameters = buildInstructions.getParameters(); 133 for (Iterator it = buildParameters.keySet().iterator() ; it.hasNext();) { 134 String key = it.next().toString(); 135 rootContext.addObject(org.jicengine.expression.BuildParameterParser.PREFIX + key, buildParameters.get(key)); 136 } 137 138 if( rootElement.isExecuted(rootContext,null) ){ 139 140 if (rootElement instanceof VariableElement) { 141 return ((VariableElement) rootElement).getValue(rootContext, null); 142 } else if (rootElement instanceof ActionElement) { 143 ((ActionElement) rootElement).execute(rootContext, null); 144 return null; 145 } else { 146 throw new IllegalStateException ("unknown element type: " + rootElement); 147 } 148 } 149 else { 150 return null; 152 } 153 } 154 155 public static void main(String [] args) throws Exception 156 { 157 if( args.length == 0 ){ 158 System.out.println("give the org.jicengine-file as an argument."); 159 } 160 else { 161 File file = new File (args[0]); 162 System.out.println("-------------------"); 163 System.out.println("Processing file '" + file + "'"); 164 System.out.println("-------------------"); 165 java.util.Map parameters = new java.util.HashMap (); 166 Object result = new BuilderImpl().build(new Instructions(new org.jicengine.io.FileResource(file),parameters)); 168 169 System.out.println("-------------------"); 170 System.out.println("Got object:"); 171 System.out.println(" " + result); 172 System.out.println(" (" + result.getClass().getName() + ")"); 173 System.out.println("-------------------"); 174 } 175 176 182 } 183 } 184 | Popular Tags |