1 18 19 package org.apache.tools.ant.taskdefs.optional; 20 21 import java.io.BufferedInputStream ; 22 import java.io.BufferedOutputStream ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.util.Hashtable ; 30 import java.util.Vector ; 31 import java.util.Enumeration ; 32 import java.net.URL ; 33 import javax.xml.parsers.ParserConfigurationException ; 34 import javax.xml.parsers.SAXParserFactory ; 35 import javax.xml.transform.ErrorListener ; 36 import javax.xml.transform.Source ; 37 import javax.xml.transform.SourceLocator ; 38 import javax.xml.transform.Templates ; 39 import javax.xml.transform.Transformer ; 40 import javax.xml.transform.TransformerException ; 41 import javax.xml.transform.TransformerFactory ; 42 import javax.xml.transform.URIResolver ; 43 import javax.xml.transform.sax.SAXSource ; 44 import javax.xml.transform.stream.StreamResult ; 45 import javax.xml.transform.stream.StreamSource ; 46 import javax.xml.transform.TransformerConfigurationException ; 47 import org.apache.tools.ant.BuildException; 48 import org.apache.tools.ant.Project; 49 import org.apache.tools.ant.taskdefs.XSLTLiaison3; 50 import org.apache.tools.ant.taskdefs.XSLTLogger; 51 import org.apache.tools.ant.taskdefs.XSLTLoggerAware; 52 import org.apache.tools.ant.taskdefs.XSLTProcess; 53 import org.apache.tools.ant.types.XMLCatalog; 54 import org.apache.tools.ant.types.Resource; 55 import org.apache.tools.ant.types.resources.FileResource; 56 import org.apache.tools.ant.types.resources.URLResource; 57 import org.apache.tools.ant.util.FileUtils; 58 import org.apache.tools.ant.util.JAXPUtils; 59 import org.xml.sax.EntityResolver ; 60 import org.xml.sax.InputSource ; 61 import org.xml.sax.SAXException ; 62 import org.xml.sax.XMLReader ; 63 64 69 public class TraXLiaison implements XSLTLiaison3, ErrorListener , XSLTLoggerAware { 70 71 76 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 77 78 81 private Project project; 82 83 87 private String factoryName = null; 88 89 90 private TransformerFactory tfactory = null; 91 92 93 private Resource stylesheet; 94 95 private XSLTLogger logger; 96 97 98 private EntityResolver entityResolver; 99 100 101 private Transformer transformer; 102 103 104 private Templates templates; 105 106 110 private long templatesModTime; 111 112 113 private URIResolver uriResolver; 114 115 116 private Vector outputProperties = new Vector (); 117 118 119 private Hashtable params = new Hashtable (); 120 121 122 private Vector attributes = new Vector (); 123 124 128 public TraXLiaison() throws Exception { 129 } 130 131 136 public void setStylesheet(File stylesheet) throws Exception { 137 FileResource fr = new FileResource(); 138 fr.setProject(project); 139 fr.setFile(stylesheet); 140 setStylesheet(fr); 141 } 142 143 148 public void setStylesheet(Resource stylesheet) throws Exception { 149 if (this.stylesheet != null) { 150 transformer = null; 152 153 if (!this.stylesheet.equals(stylesheet) 155 || (stylesheet.getLastModified() != templatesModTime)) { 156 templates = null; 157 } 158 } 159 this.stylesheet = stylesheet; 160 } 161 162 168 public void transform(File infile, File outfile) throws Exception { 169 if (transformer == null) { 170 createTransformer(); 171 } 172 173 InputStream fis = null; 174 OutputStream fos = null; 175 try { 176 fis = new BufferedInputStream (new FileInputStream (infile)); 177 fos = new BufferedOutputStream (new FileOutputStream (outfile)); 178 StreamResult res = new StreamResult (fos); 179 res.setSystemId(JAXPUtils.getSystemId(outfile)); 181 Source src = getSource(fis, infile); 182 183 setTransformationParameters(); 186 187 transformer.transform(src, res); 188 } finally { 189 try { 193 if (fis != null) { 194 fis.close(); 195 } 196 } catch (IOException ignored) { 197 } 199 try { 200 if (fos != null) { 201 fos.close(); 202 } 203 } catch (IOException ignored) { 204 } 206 } 207 } 208 209 218 private Source getSource(InputStream is, File infile) 219 throws ParserConfigurationException , SAXException { 220 Source src = null; 224 if (entityResolver != null) { 225 if (getFactory().getFeature(SAXSource.FEATURE)) { 226 SAXParserFactory spFactory = SAXParserFactory.newInstance(); 227 spFactory.setNamespaceAware(true); 228 XMLReader reader = spFactory.newSAXParser().getXMLReader(); 229 reader.setEntityResolver(entityResolver); 230 src = new SAXSource (reader, new InputSource (is)); 231 } else { 232 throw new IllegalStateException ("xcatalog specified, but " 233 + "parser doesn't support SAX"); 234 } 235 } else { 236 src = new StreamSource (is); 239 } 240 src.setSystemId(JAXPUtils.getSystemId(infile)); 241 return src; 242 } 243 244 private Source getSource(InputStream is, Resource resource) 245 throws ParserConfigurationException , SAXException { 246 Source src = null; 250 if (entityResolver != null) { 251 if (getFactory().getFeature(SAXSource.FEATURE)) { 252 SAXParserFactory spFactory = SAXParserFactory.newInstance(); 253 spFactory.setNamespaceAware(true); 254 XMLReader reader = spFactory.newSAXParser().getXMLReader(); 255 reader.setEntityResolver(entityResolver); 256 src = new SAXSource (reader, new InputSource (is)); 257 } else { 258 throw new IllegalStateException ("xcatalog specified, but " 259 + "parser doesn't support SAX"); 260 } 261 } else { 262 src = new StreamSource (is); 265 } 266 src.setSystemId(resourceToURI(resource)); 270 return src; 271 } 272 273 private String resourceToURI(Resource resource) { 274 if (resource instanceof FileResource) { 275 File f = ((FileResource) resource).getFile(); 276 return FILE_UTILS.toURI(f.getAbsolutePath()); 277 } 278 if (resource instanceof URLResource) { 279 URL u = ((URLResource) resource).getURL(); 280 return String.valueOf(u); 281 } else { 282 return resource.getName(); 283 } 284 } 285 286 289 private void readTemplates() 290 throws IOException , TransformerConfigurationException , 291 ParserConfigurationException , SAXException { 292 293 InputStream xslStream = null; 298 try { 299 xslStream 300 = new BufferedInputStream (stylesheet.getInputStream()); 301 templatesModTime = stylesheet.getLastModified(); 302 Source src = getSource(xslStream, stylesheet); 303 templates = getFactory().newTemplates(src); 304 } finally { 305 if (xslStream != null) { 306 xslStream.close(); 307 } 308 } 309 } 310 311 318 private void createTransformer() throws Exception { 319 if (templates == null) { 320 readTemplates(); 321 } 322 323 transformer = templates.newTransformer(); 324 325 transformer.setErrorListener(this); 327 if (uriResolver != null) { 328 transformer.setURIResolver(uriResolver); 329 } 330 for (int i = 0; i < outputProperties.size(); i++) { 331 final String [] pair = (String []) outputProperties.elementAt(i); 332 transformer.setOutputProperty(pair[0], pair[1]); 333 } 334 } 335 336 339 private void setTransformationParameters() { 340 for (final Enumeration enumeration = params.keys(); 341 enumeration.hasMoreElements();) { 342 final String name = (String ) enumeration.nextElement(); 343 final String value = (String ) params.get(name); 344 transformer.setParameter(name, value); 345 } 346 } 347 348 356 private TransformerFactory getFactory() throws BuildException { 357 if (tfactory != null) { 358 return tfactory; 359 } 360 if (factoryName == null) { 362 tfactory = TransformerFactory.newInstance(); 363 } else { 364 try { 365 Class clazz = Class.forName(factoryName); 366 tfactory = (TransformerFactory ) clazz.newInstance(); 367 } catch (Exception e) { 368 throw new BuildException(e); 369 } 370 } 371 tfactory.setErrorListener(this); 372 373 for (int i = 0; i < attributes.size(); i++) { 375 final Object [] pair = (Object []) attributes.elementAt(i); 376 tfactory.setAttribute((String ) pair[0], pair[1]); 377 } 378 379 if (uriResolver != null) { 380 tfactory.setURIResolver(uriResolver); 381 } 382 return tfactory; 383 } 384 385 386 392 public void setFactory(String name) { 393 factoryName = name; 394 } 395 396 403 public void setAttribute(String name, Object value) { 404 final Object [] pair = new Object []{name, value}; 405 attributes.addElement(pair); 406 } 407 408 417 public void setOutputProperty(String name, String value) { 418 final String [] pair = new String []{name, value}; 419 outputProperties.addElement(pair); 420 } 421 422 426 public void setEntityResolver(EntityResolver aResolver) { 427 entityResolver = aResolver; 428 } 429 430 434 public void setURIResolver(URIResolver aResolver) { 435 uriResolver = aResolver; 436 } 437 438 443 public void addParam(String name, String value) { 444 params.put(name, value); 445 } 446 447 451 public void setLogger(XSLTLogger l) { 452 logger = l; 453 } 454 455 459 public void error(TransformerException e) { 460 logError(e, "Error"); 461 } 462 463 467 public void fatalError(TransformerException e) { 468 logError(e, "Fatal Error"); 469 throw new BuildException("Fatal error during transformation", e); 470 } 471 472 476 public void warning(TransformerException e) { 477 logError(e, "Warning"); 478 } 479 480 private void logError(TransformerException e, String type) { 481 if (logger == null) { 482 return; 483 } 484 485 StringBuffer msg = new StringBuffer (); 486 SourceLocator locator = e.getLocator(); 487 if (locator != null) { 488 String systemid = locator.getSystemId(); 489 if (systemid != null) { 490 String url = systemid; 491 if (url.startsWith("file:")) { 492 url = FileUtils.getFileUtils().fromURI(url); 493 } 494 msg.append(url); 495 } else { 496 msg.append("Unknown file"); 497 } 498 int line = locator.getLineNumber(); 499 if (line != -1) { 500 msg.append(":"); 501 msg.append(line); 502 int column = locator.getColumnNumber(); 503 if (column != -1) { 504 msg.append(":"); 505 msg.append(column); 506 } 507 } 508 } 509 msg.append(": "); 510 msg.append(type); 511 msg.append("! "); 512 msg.append(e.getMessage()); 513 if (e.getCause() != null) { 514 msg.append(" Cause: "); 515 msg.append(e.getCause()); 516 } 517 518 logger.log(msg.toString()); 519 } 520 521 528 protected String getSystemId(File file) { 529 return JAXPUtils.getSystemId(file); 530 } 531 532 533 538 public void configure(XSLTProcess xsltTask) { 539 project = xsltTask.getProject(); 540 XSLTProcess.Factory factory = xsltTask.getFactory(); 541 if (factory != null) { 542 setFactory(factory.getName()); 543 544 for (Enumeration attrs = factory.getAttributes(); 546 attrs.hasMoreElements();) { 547 XSLTProcess.Factory.Attribute attr = 548 (XSLTProcess.Factory.Attribute) attrs.nextElement(); 549 setAttribute(attr.getName(), attr.getValue()); 550 } 551 } 552 553 XMLCatalog xmlCatalog = xsltTask.getXMLCatalog(); 554 if (xmlCatalog != null) { 556 setEntityResolver(xmlCatalog); 557 setURIResolver(xmlCatalog); 558 } 559 560 561 for (Enumeration props = xsltTask.getOutputProperties(); 563 props.hasMoreElements();) { 564 XSLTProcess.OutputProperty prop 565 = (XSLTProcess.OutputProperty) props.nextElement(); 566 setOutputProperty(prop.getName(), prop.getValue()); 567 } 568 } 569 } 570 | Popular Tags |