1 20 package org.enhydra.barracuda.taskdefs; 21 22 import java.io.*; 23 import java.util.*; 24 25 import org.apache.tools.ant.*; 26 import org.apache.tools.ant.taskdefs.*; 27 import org.apache.tools.ant.util.*; 28 import org.xml.sax.*; 29 import org.xml.sax.helpers.*; 30 31 65 public class EventBuilder extends Task { 66 67 private static final String DEFAULT_PARSER = "org.apache.xerces.parsers.SAXParser"; 69 70 protected String parserClass = null; 72 protected File xmlFile = null; protected File templateFile = null; protected String sourceOutDir = null; 76 79 public void setDescriptor(File xmlFile) { 80 this.xmlFile = xmlFile; 81 } 82 83 86 public void setSourceout(String sourceOutDir) { 87 this.sourceOutDir = sourceOutDir; 88 } 89 90 93 public void setTemplate(File templateFile) { 94 this.templateFile = templateFile; 95 } 96 97 101 public void execute() throws BuildException { 102 LocalHandler handler = new LocalHandler(); 104 LocalEntityResolver resolver = new LocalEntityResolver(); 105 106 try { 108 if (parserClass==null) parserClass = DEFAULT_PARSER; 110 XMLReader parser = getXMLReader(parserClass); 113 parser.setContentHandler(handler); 114 parser.setErrorHandler(handler); 115 parser.setEntityResolver(resolver); 116 117 log("Generating events from "+xmlFile, Project.MSG_DEBUG); 119 InputSource source = new InputSource(new FileInputStream(xmlFile)); 120 121 parser.parse(source); 123 124 } catch (org.xml.sax.SAXParseException spe) { 125 spe.printStackTrace(System.err); 126 throw new BuildException("SAX Error parsing event.xml!", spe); 127 } catch (org.xml.sax.SAXException se) { 128 if (se.getException()!=null) { 129 se.getException().printStackTrace(System.err); 130 throw new BuildException("SAX Error processing event.xml!", se.getException()); 131 } else { 132 se.printStackTrace(System.err); 133 throw new BuildException("SAX Error processing event.xml!", se); 134 } 135 } catch (Exception e) { 136 e.printStackTrace(System.err); 137 throw new BuildException("Error processing event.xml!", e); 138 } finally { 139 if (resolver!=null) resolver.cleanup(); 141 } 142 } 143 144 155 private XMLReader getXMLReader(String preferred) throws SAXException { 156 XMLReader parser = null; 157 try { if (preferred != null) { 159 parser = XMLReaderFactory.createXMLReader(preferred); 160 } else { 161 throw new SAXException("Preferred SAX parser unavailable!"); 162 } 163 } 164 catch (SAXException e1) { 165 try { parser = XMLReaderFactory.createXMLReader(); 171 } 172 catch (SAXException e2) { 173 try { parser = XMLReaderFactory.createXMLReader("com.bluecast.xml.Piccolo"); 175 } 176 catch (SAXException e3) { 177 try { parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser"); 179 } 180 catch (SAXException e4) { 181 try { parser = XMLReaderFactory.createXMLReader("org.apache.crimson.parser.XMLReaderImpl"); 183 } 184 catch (SAXException e5) { 185 try { parser = XMLReaderFactory.createXMLReader("gnu.xml.aelfred2.XmlReader"); 187 } 188 catch (SAXException e6) { 189 try { parser = XMLReaderFactory.createXMLReader("net.sf.saxon.aelfred.SAXDriver"); 191 } 192 catch (SAXException e7) { 193 parser = XMLReaderFactory.createXMLReader("oracle.xml.parser.v2.SAXParser"); 195 } 196 } 197 } 198 } 199 } 200 } 201 } 202 return parser; 203 } 204 205 209 class CurrentSettings { 210 String template = null; 211 String eventPackage = ""; 212 String eventName = null; 213 String baseClass = null; 214 Map templateKeys = new HashMap(); CurrentSettings parent = null; 216 217 public CurrentSettings createChild() { 218 CurrentSettings cs = new CurrentSettings(); 219 cs.template = template; 220 cs.baseClass = baseClass; 221 cs.eventName = eventName; 222 cs.eventPackage = eventPackage; 223 cs.parent = CurrentSettings.this; 224 return cs; 225 } 226 227 public CurrentSettings getRoot() { 228 if (parent==null) return CurrentSettings.this; 229 else return parent.getRoot(); 230 } 231 232 public String getParentClassName() { 233 return (parent.eventName==null ? baseClass : parent.eventPackage+"."+parent.eventName); 234 } 235 236 public String toString() { 237 return "{"+eventName+" extends "+getParentClassName()+"}"; 238 } 239 } 240 241 244 class LocalHandler extends DefaultHandler { 245 246 String BUILD_EVENTS = "build-events"; 248 String CONTROL_EVENTS = "control-events"; 249 String REQ_EVENTS = "req-events"; 250 String RESP_EVENTS = "resp-events"; 251 String EVENT = "event"; 252 253 String PKG = "pkg"; 255 String TEMPLATE = "template"; 256 String BASE_CLASS = "base-class"; 257 String NAME = "name"; 258 259 CurrentSettings cs = null; 261 Map templateCache = new HashMap(); 262 int fileCnt = 0; 263 264 public void startDocument() { 265 cs = new CurrentSettings(); 267 cs.template = null; 268 cs.baseClass = null; 269 cs.eventName = null; 270 cs.eventPackage = "."; 271 } 272 273 public void startElement(String uri, String local, String raw, Attributes attrs) throws SAXException { 274 String curTag = local; 275 276 cs = cs.createChild(); 279 280 for (int i=0, max=attrs.getLength(); i<max; i++) { 282 String name = attrs.getLocalName(i); 283 String value = attrs.getValue(i); 284 if (name.equals(PKG)) { 285 cs.eventPackage = value; 286 } else if (name.equals(TEMPLATE)) { 287 cs.template = value; 288 } else if (name.equals(BASE_CLASS)) { 289 cs.baseClass = value; 290 } else if (name.equals(NAME)) { 291 cs.eventName = value; 292 } else { 295 log("got custom template key: "+name, Project.MSG_DEBUG); 296 cs.templateKeys.put("@"+name+"@", value); 297 } 299 } 300 301 if (curTag.equals(CONTROL_EVENTS)) { 303 if (cs.baseClass==null) cs.baseClass = "org.enhydra.barracuda.core.event.ControlEvent"; 304 305 } else if (curTag.equals(REQ_EVENTS)) { 307 if (cs.baseClass==null) cs.baseClass = "org.enhydra.barracuda.core.event.HttpRequestEvent"; 308 309 } else if (curTag.equals(RESP_EVENTS)) { 311 if (cs.baseClass==null) cs.baseClass = "org.enhydra.barracuda.core.event.HttpResponseEvent"; 312 313 } else if (curTag.equals(EVENT)) { 315 boolean created = buildEventFile(); 316 if (created) fileCnt++; 317 } 318 } 319 320 public void endElement(String uri, String local, String raw) throws SAXException { 321 cs = cs.parent; 322 } 323 324 public void endDocument() { 325 if (fileCnt>0) log("Created "+fileCnt+" event files from: "+xmlFile+((templateFile!=null)? ", using template: "+templateFile : "")); 326 } 327 328 private String getLocationString(SAXParseException ex) { 329 StringBuffer str = new StringBuffer (); 330 String systemId = ex.getSystemId(); 331 if (systemId!=null) { 332 int index = systemId.lastIndexOf(47); 333 if (index!=-1) systemId = systemId.substring(index + 1); 334 str.append(systemId); 335 } 336 str.append(':'); 337 str.append(ex.getLineNumber()); 338 str.append(':'); 339 str.append(ex.getColumnNumber()); 340 return str.toString(); 341 } 342 343 protected byte[] loadTemplate(String targetTemplate) { 344 if (targetTemplate==null) targetTemplate = "~dflt~"; 345 byte[] templateBytes = (byte[]) templateCache.get(targetTemplate); 346 if (templateBytes==null) { 347 BufferedReader br = null; 348 try { 349 if (templateFile != null && templateFile.exists()) { 350 br = new BufferedReader(new FileReader(templateFile)); 351 } else if (cs.template != null) { 352 br = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(cs.template))); 353 } else { 354 br = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream("EventBuilder.template"))); 355 } 356 templateBytes = FileUtils.readFully(br).getBytes(); 357 templateCache.put(targetTemplate, templateBytes); 358 } catch (IOException e) { 359 throw new BuildException("Error reading template", e); 360 } finally { 361 try { 362 if (br != null) { 363 br.close(); 364 } 365 } catch (IOException e) { 366 throw new BuildException("Error closing template", e); 367 } 368 } 369 } 370 return templateBytes; 371 } 372 373 protected boolean buildEventFile() { 374 String targetPackage = cs.eventPackage.replace('.','/'); 376 File targetPath = new File(sourceOutDir+"/"+targetPackage); 377 File targetFile = new File(targetPath.toString(), cs.eventName+".java"); 378 379 if (!targetPath.exists()) { 381 if (!targetPath.mkdirs()) throw new BuildException("Error creating path: "+targetPath, getLocation()); 384 } 385 386 if (targetFile.exists() && 389 targetFile.lastModified()>xmlFile.lastModified()) return false; 390 391 byte[] templateBytes = loadTemplate(cs.template); 393 394 try { 396 targetFile.createNewFile(); 397 FileOutputStream out = new FileOutputStream(targetFile); 398 out.write(templateBytes, 0, templateBytes.length); 399 out.flush(); 400 out.close(); 401 } catch (IOException e) { 402 throw new BuildException("Error writing "+targetFile, e); 403 } 404 405 Replace replace = new Replace(); 407 replace.setProject(getProject()); 410 replace.setFile(targetFile); 411 Replace.Replacefilter rf = null; 412 rf = replace.createReplacefilter(); 414 rf.setToken("@event.package@"); 415 rf.setValue(cs.eventPackage); 416 rf = replace.createReplacefilter(); 418 rf.setToken("@event.name@"); 419 rf.setValue(cs.eventName); 420 rf = replace.createReplacefilter(); 422 rf.setToken("@event.parent@"); 423 rf.setValue(cs.getParentClassName()); 424 Iterator it = cs.templateKeys.entrySet().iterator(); 427 while (it.hasNext()) { 428 Map.Entry me = (Map.Entry) it.next(); 429 rf = replace.createReplacefilter(); 430 rf.setToken((String ) me.getKey()); 431 rf.setValue((String ) me.getValue()); 432 } 433 replace.execute(); 436 return true; 437 } 438 } 439 440 444 class LocalEntityResolver implements EntityResolver { 445 String id = "http://barracudamvc.org/Barracuda"; 446 List resourceList = new ArrayList(); 447 448 public InputSource resolveEntity (String publicId, String systemId) { 449 if (systemId.startsWith(id)) { 450 String altId = "/xlib"+systemId.substring(id.length()); 452 InputStream is = this.getClass().getResourceAsStream(altId); 453 BufferedInputStream in = new BufferedInputStream(is); 454 resourceList.add(in); 455 return new InputSource(in); 456 } else { 457 return null; 459 } 460 } 461 462 public void cleanup() { 463 Iterator it = resourceList.iterator(); 464 while (it.hasNext()) { 465 try { 466 ((InputStream) it.next()).close(); 467 } catch (Exception e) { 468 System.out.println("unexpected error: "+e); 469 } 470 } 471 } 472 } 473 } 474 | Popular Tags |