| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.taskdefs; |
| 34 | | | |
| 35 | | | |
| 36 | | | import java.io.File; |
| 37 | | | import java.io.FileInputStream; |
| 38 | | | import java.io.FileNotFoundException; |
| 39 | | | import java.io.IOException; |
| 40 | | | import java.io.InputStream; |
| 41 | | | import java.net.URISyntaxException; |
| 42 | | | import java.net.URL; |
| 43 | | | import java.util.Properties; |
| 44 | | | |
| 45 | | | import javax.xml.transform.ErrorListener; |
| 46 | | | import javax.xml.transform.Source; |
| 47 | | | import javax.xml.transform.Transformer; |
| 48 | | | import javax.xml.transform.TransformerException; |
| 49 | | | import javax.xml.transform.TransformerFactory; |
| 50 | | | import javax.xml.transform.sax.SAXSource; |
| 51 | | | import javax.xml.transform.stream.StreamResult; |
| 52 | | | import javax.xml.transform.stream.StreamSource; |
| 53 | | | |
| 54 | | | import org.apache.tools.ant.AntClassLoader; |
| 55 | | | import org.apache.tools.ant.BuildException; |
| 56 | | | import org.apache.tools.ant.Project; |
| 57 | | | import org.apache.tools.ant.Task; |
| 58 | | | import org.apache.xerces.util.XMLCatalogResolver; |
| 59 | | | import org.jcoderz.commons.util.IoUtil; |
| 60 | | | import org.jcoderz.commons.util.StringUtil; |
| 61 | | | import org.jcoderz.commons.util.XmlUtil; |
| 62 | | | import org.xml.sax.EntityResolver; |
| 63 | | | import org.xml.sax.InputSource; |
| 64 | | | import org.xml.sax.SAXException; |
| 65 | | | |
| 66 | | | |
| 67 | | | |
| 68 | | | |
| 69 | | | |
| 70 | | | @author |
| 71 | | | |
| 72 | 100 | | public abstract class XsltBasedTask |
| 73 | | | extends Task |
| 74 | | | { |
| 75 | | | |
| 76 | | | private static final String XML_PARSER_CONFIGURATION_PROPERTY = |
| 77 | | | "org.apache.xerces.xni.parser.XMLParserConfiguration"; |
| 78 | | | |
| 79 | | | |
| 80 | | | private static final String XML_PARSER_CONFIG_WITH_XINCLUDE = |
| 81 | | | "org.apache.xerces.parsers.XIncludeParserConfiguration"; |
| 82 | | | |
| 83 | | | |
| 84 | | | private static final String FAWKEZ_VERSION_FILE = |
| 85 | | | "/org/jcoderz/commons/VERSION"; |
| 86 | | | |
| 87 | | | |
| 88 | 100 | | private File mDestDir = null; |
| 89 | | | |
| 90 | | | |
| 91 | 100 | | private String mXslFile = null; |
| 92 | | | |
| 93 | | | |
| 94 | 100 | | private File mInFile = null; |
| 95 | | | |
| 96 | | | |
| 97 | 100 | | private File mOutFile = null; |
| 98 | | | |
| 99 | | | |
| 100 | 100 | | private boolean mForce = false; |
| 101 | | | |
| 102 | | | |
| 103 | 100 | | private boolean mFailOnError = false; |
| 104 | | | |
| 105 | | | |
| 106 | 100 | | private int mLogLevel = Project.MSG_INFO; |
| 107 | | | |
| 108 | 100 | | private boolean mResolveExternalEntities = true; |
| 109 | | | |
| 110 | | | |
| 111 | | | |
| 112 | | | <p> |
| 113 | | | |
| 114 | | | |
| 115 | | | |
| 116 | | | |
| 117 | | | </p> |
| 118 | | | |
| 119 | | | @since |
| 120 | | | |
| 121 | 100 | | private AntClassLoader mClassLoader = null; |
| 122 | | | |
| 123 | | | |
| 124 | | | |
| 125 | | | |
| 126 | | | |
| 127 | | | @param |
| 128 | | | |
| 129 | | | public void setDestdir (File dir) |
| 130 | | | { |
| 131 | 100 | | mDestDir = dir; |
| 132 | 100 | | } |
| 133 | | | |
| 134 | | | |
| 135 | | | |
| 136 | | | |
| 137 | | | |
| 138 | | | @param |
| 139 | | | |
| 140 | | | public void setXsl (String s) |
| 141 | | | { |
| 142 | 100 | | mXslFile = s; |
| 143 | 100 | | } |
| 144 | | | |
| 145 | | | |
| 146 | | | |
| 147 | | | |
| 148 | | | |
| 149 | | | @param |
| 150 | | | |
| 151 | | | public void setIn (File f) |
| 152 | | | { |
| 153 | 100 | | mInFile = f; |
| 154 | 100 | | } |
| 155 | | | |
| 156 | | | |
| 157 | | | |
| 158 | | | |
| 159 | | | @param |
| 160 | | | |
| 161 | | | public void setOut (File f) |
| 162 | | | { |
| 163 | 100 | | mOutFile = f; |
| 164 | 100 | | } |
| 165 | | | |
| 166 | | | |
| 167 | | | |
| 168 | | | |
| 169 | | | @param |
| 170 | | | |
| 171 | | | public void setForce (boolean b) |
| 172 | | | { |
| 173 | 100 | | mForce = b; |
| 174 | 100 | | } |
| 175 | | | |
| 176 | | | |
| 177 | | | |
| 178 | | | |
| 179 | | | @param |
| 180 | | | |
| 181 | | | public void setFailonerror (boolean b) |
| 182 | | | { |
| 183 | 100 | | mFailOnError = b; |
| 184 | 100 | | } |
| 185 | | | |
| 186 | | | |
| 187 | | | |
| 188 | | | |
| 189 | | | @param |
| 190 | | | |
| 191 | | | public void setLogLevel (int level) |
| 192 | | | { |
| 193 | 0 | | mLogLevel = level; |
| 194 | 0 | | } |
| 195 | | | |
| 196 | | | |
| 197 | | | |
| 198 | | | |
| 199 | | | @throws |
| 200 | | | |
| 201 | | | public void execute () |
| 202 | | | throws BuildException |
| 203 | | | { |
| 204 | | | try |
| 205 | | | { |
| 206 | 100 | | checkAttributes(); |
| 207 | 50 | | if (mForce || mInFile.lastModified() > mOutFile.lastModified()) |
| 208 | | | { |
| 209 | 100 | | if (mDestDir != null) |
| 210 | | | { |
| 211 | 100 | | log("Generating files to directory " + mDestDir, |
| 212 | | | Project.MSG_VERBOSE); |
| 213 | | | } |
| 214 | 100 | | log("Processing " + mInFile + " to " + mOutFile |
| 215 | | | + " using stylesheet " + mXslFile, mLogLevel); |
| 216 | 100 | | transform(); |
| 217 | 100 | | postExecute(); |
| 218 | | | } |
| 219 | | | } |
| 220 | 100 | | catch (BuildException e) |
| 221 | | | { |
| 222 | 100 | | if (mFailOnError) |
| 223 | | | { |
| 224 | 100 | | throw e; |
| 225 | | | } |
| 226 | 0 | | log(e.getMessage(), Project.MSG_ERR); |
| 227 | 100 | | } |
| 228 | 100 | | } |
| 229 | | | |
| 230 | | | |
| 231 | | | @return |
| 232 | | | |
| 233 | | | public String getFawkezVersionAsString () |
| 234 | | | { |
| 235 | 0 | (1) | final StringBuffer version = new StringBuffer(); |
| 236 | | | try |
| 237 | | | { |
| 238 | 0 | | final Properties fawkezProps = getFawkezVersionProperties(); |
| 239 | 0 | | version.append("fawkeZ "); |
| 240 | 0 | | version.append(fawkezProps.getProperty("version")); |
| 241 | 0 | | version.append(", ["); |
| 242 | 0 | | version.append(fawkezProps.getProperty("cvs_name")); |
| 243 | 0 | (2) | version.append(']'); |
| 244 | | | } |
| 245 | 0 | | catch (Exception x) |
| 246 | | | { |
| 247 | | | |
| 248 | 0 | | version.append("unknown"); |
| 249 | 0 | | } |
| 250 | 0 | | return version.toString(); |
| 251 | | | } |
| 252 | | | |
| 253 | | | |
| 254 | | | <tt></tt> |
| 255 | | | |
| 256 | | | |
| 257 | | | @param |
| 258 | | | |
| 259 | | | public void resolveExternalEntities (boolean b) |
| 260 | | | { |
| 261 | 0 | | mResolveExternalEntities = b; |
| 262 | 0 | | } |
| 263 | | | |
| 264 | | | static void checkXercesVersion (Task task) |
| 265 | | | { |
| 266 | 100 | | final String xercesVersion = org.apache.xerces.impl.Version |
| 267 | | | .getVersion(); |
| 268 | 100 | (3) | if (StringUtil.contains(xercesVersion, ("2.6.2"))) |
| 269 | | | { |
| 270 | 0 | | task.log("Found " + xercesVersion + " on classpath.", |
| 271 | | | Project.MSG_WARN); |
| 272 | 0 | | task.log("This Version only supports the outdated 2003 " |
| 273 | | | + "namespace for XInclude ", Project.MSG_WARN); |
| 274 | 0 | | task.log("please put a newer version of xerces on your classpath" |
| 275 | | | + "or use", Project.MSG_WARN); |
| 276 | 0 | | task.log("at least ANT 1.7.0.", Project.MSG_WARN); |
| 277 | | | } |
| 278 | 100 | | } |
| 279 | | | |
| 280 | | | |
| 281 | | | |
| 282 | | | |
| 283 | | | <p> |
| 284 | | | |
| 285 | | | <tt></tt> |
| 286 | | | |
| 287 | | | @return |
| 288 | | | |
| 289 | | | abstract String getDefaultStyleSheet (); |
| 290 | | | |
| 291 | | | |
| 292 | | | |
| 293 | | | |
| 294 | | | |
| 295 | | | @param |
| 296 | | | |
| 297 | | | void setAdditionalTransformerParameters (Transformer transformer) |
| 298 | | | { |
| 299 | | | |
| 300 | 100 | (4) | } |
| 301 | | | |
| 302 | | | File getInFile () |
| 303 | | | { |
| 304 | 0 | | return mInFile; |
| 305 | | | } |
| 306 | | | |
| 307 | | | File getOutFile () |
| 308 | | | { |
| 309 | 0 | | return mOutFile; |
| 310 | | | } |
| 311 | | | |
| 312 | | | File getDestDir () |
| 313 | | | { |
| 314 | 0 | | return mDestDir; |
| 315 | | | } |
| 316 | | | |
| 317 | | | boolean getFailOnError () |
| 318 | | | { |
| 319 | 0 | | return mFailOnError; |
| 320 | | | } |
| 321 | | | |
| 322 | | | |
| 323 | | | |
| 324 | | | |
| 325 | | | @throws |
| 326 | | | |
| 327 | | | void checkAttributes () |
| 328 | | | throws BuildException |
| 329 | | | { |
| 330 | 100 | | checkAttributeInFile(); |
| 331 | 100 | | checkAttributeOutFile(); |
| 332 | 100 | | checkAttributeDestDir(); |
| 333 | 100 | | checkAttributeXslFile(); |
| 334 | 100 | | checkXercesVersion(this); |
| 335 | 100 | | } |
| 336 | | | |
| 337 | | | void checkAttributeXslFile () |
| 338 | | | { |
| 339 | 100 | | if (mXslFile == null || !new File(mXslFile).exists()) |
| 340 | | | { |
| 341 | 100 | | mXslFile = getDefaultStyleSheet(); |
| 342 | | | } |
| 343 | 100 | | } |
| 344 | | | |
| 345 | | | void checkAttributeDestDir () |
| 346 | | | { |
| 347 | 100 | | if (mDestDir == null) |
| 348 | | | { |
| 349 | 100 | | throw new BuildException("Missing mandatory attribute 'outdir'.", |
| 350 | | | getLocation()); |
| 351 | | | } |
| 352 | 100 | | AntTaskUtil.ensureDirectory(mDestDir); |
| 353 | 100 | | } |
| 354 | | | |
| 355 | | | void checkAttributeOutFile () |
| 356 | | | { |
| 357 | 100 | | if (mOutFile == null) |
| 358 | | | { |
| 359 | 100 | | throw new BuildException("Missing mandatory attribute 'out'.", |
| 360 | | | getLocation()); |
| 361 | | | } |
| 362 | 100 | | AntTaskUtil.ensureDirectoryForFile(mOutFile); |
| 363 | 100 | | } |
| 364 | | | |
| 365 | | | void checkAttributeInFile () |
| 366 | | | { |
| 367 | 100 | | if (mInFile == null) |
| 368 | | | { |
| 369 | 100 | | throw new BuildException("Missing mandatory attribute 'in'.", |
| 370 | | | getLocation()); |
| 371 | | | } |
| 372 | 100 | | if (!mInFile.exists()) |
| 373 | | | { |
| 374 | 100 | | throw new BuildException("Input file '" + mInFile + "' not found.", |
| 375 | | | getLocation()); |
| 376 | | | } |
| 377 | 100 | | } |
| 378 | | | |
| 379 | | | |
| 380 | | | |
| 381 | | | |
| 382 | | | |
| 383 | | | void postExecute () |
| 384 | | | { |
| 385 | | | |
| 386 | 100 | (5) | } |
| 387 | | | |
| 388 | | | |
| 389 | | | |
| 390 | | | |
| 391 | | | @throws |
| 392 | | | |
| 393 | | | void transform () |
| 394 | | | throws BuildException |
| 395 | | | { |
| 396 | 100 | | StreamResult out = null; |
| 397 | | | try |
| 398 | | | { |
| 399 | 100 | | final String xmlParserConfig |
| 400 | | | = System.getProperty(XML_PARSER_CONFIGURATION_PROPERTY); |
| 401 | 100 | | if (!XML_PARSER_CONFIG_WITH_XINCLUDE.equals(xmlParserConfig)) |
| 402 | | | { |
| 403 | 100 | | System.setProperty( |
| 404 | | | XML_PARSER_CONFIGURATION_PROPERTY, |
| 405 | | | XML_PARSER_CONFIG_WITH_XINCLUDE); |
| 406 | 100 | | log("Using XML Parser configuration " |
| 407 | | | + XML_PARSER_CONFIG_WITH_XINCLUDE, Project.MSG_VERBOSE); |
| 408 | | | } |
| 409 | | | |
| 410 | | | |
| 411 | 100 | | final TransformerFactory factory |
| 412 | | | = (TransformerFactory) |
| 413 | | | (loadClass( |
| 414 | | | "org.apache.xalan.processor.TransformerFactoryImpl") |
| 415 | | | .newInstance()); |
| 416 | | | |
| 417 | 100 | | factory.setURIResolver(new JarArchiveUriResolver(this)); |
| 418 | 100 | | final StreamSource source = getXslFileAsSource(); |
| 419 | 100 | | final Transformer transformer |
| 420 | | | = factory.newTransformer(source); |
| 421 | 100 | | setAdditionalTransformerParameters(transformer); |
| 422 | 75 | | transformer.setParameter("outdir", mDestDir != null ? mDestDir |
| 423 | | | .getAbsolutePath() : ""); |
| 424 | 100 | | final Source xml = getInAsStreamSource(); |
| 425 | 100 | | out = XmlUtil.createStreamResult(mOutFile); |
| 426 | 100 | | transformer.setErrorListener(new MyErrorListener()); |
| 427 | 100 | | transformer.transform(xml, out); |
| 428 | | | } |
| 429 | 100 | | catch (Exception e) |
| 430 | | | { |
| 431 | 100 | | throw new BuildException("Error during transformation: " + e, e); |
| 432 | | | } |
| 433 | | | finally |
| 434 | | | { |
| 435 | 100 | | if (out != null) |
| 436 | | | { |
| 437 | 100 | | IoUtil.close(out.getOutputStream()); |
| 438 | | | } |
| 439 | 100 | | if (mClassLoader != null) |
| 440 | | | { |
| 441 | 0 | | mClassLoader.resetThreadContextLoader(); |
| 442 | 0 | | mClassLoader.cleanup(); |
| 443 | 33 | | mClassLoader = null; |
| 444 | | | } |
| 445 | | | } |
| 446 | 100 | | } |
| 447 | | | |
| 448 | | | private Class loadClass (String classname) |
| 449 | | | throws ClassNotFoundException |
| 450 | | | { |
| 451 | | | final Class result; |
| 452 | 100 | | if (getClass().getClassLoader() instanceof AntClassLoader) |
| 453 | | | { |
| 454 | 0 | | mClassLoader = (AntClassLoader) getClass().getClassLoader(); |
| 455 | 0 | | mClassLoader.setThreadContextLoader(); |
| 456 | 0 | | result = Class.forName(classname, true, mClassLoader); |
| 457 | 0 | | log("Loading '" + classname + "' via " + mClassLoader, |
| 458 | | | Project.MSG_VERBOSE); |
| 459 | | | } |
| 460 | | | else |
| 461 | | | { |
| 462 | 100 | | result = Class.forName(classname); |
| 463 | 100 | | log("No ant-classloader found to load '" + classname + "'," |
| 464 | | | + "using 'normal' Class.forName(classname).", |
| 465 | | | Project.MSG_VERBOSE); |
| 466 | | | } |
| 467 | 100 | | return result; |
| 468 | | | } |
| 469 | | | |
| 470 | | | private StreamSource getXslFileAsSource () |
| 471 | | | { |
| 472 | | | final StreamSource result; |
| 473 | 100 | | final InputStream xslStream |
| 474 | | | = XsltBasedTask.class.getResourceAsStream(mXslFile); |
| 475 | 100 | | if (xslStream == null) |
| 476 | | | { |
| 477 | | | try |
| 478 | | | { |
| 479 | 0 | | final File file = new File(mXslFile); |
| 480 | 0 | | final InputStream xslFile = new FileInputStream(file); |
| 481 | 0 | | result = new StreamSource(xslFile); |
| 482 | 0 | | result.setSystemId(file.toURI().toASCIIString()); |
| 483 | | | } |
| 484 | 0 | | catch (FileNotFoundException e) |
| 485 | | | { |
| 486 | 0 | | throw new BuildException("Cannot locate stylesheet " |
| 487 | | | + mXslFile, e); |
| 488 | 0 | | } |
| 489 | | | } |
| 490 | | | else |
| 491 | | | { |
| 492 | 100 | | result = new StreamSource(xslStream); |
| 493 | 75 | | final URL url = XsltBasedTask.class.getResource(mXslFile); |
| 494 | 100 | | if (url != null) |
| 495 | | | { |
| 496 | | | try |
| 497 | | | { |
| 498 | 100 | | result.setSystemId(url.toURI().toASCIIString()); |
| 499 | | | } |
| 500 | 0 | | catch (URISyntaxException ex) |
| 501 | | | { |
| 502 | 0 | | log("Failed to set systemId. Got " + ex, |
| 503 | | | Project.MSG_VERBOSE); |
| 504 | 100 | | } |
| 505 | | | } |
| 506 | | | } |
| 507 | 100 | | return result; |
| 508 | | | } |
| 509 | | | |
| 510 | | | |
| 511 | | | |
| 512 | | | |
| 513 | | | |
| 514 | | | |
| 515 | | | |
| 516 | | | |
| 517 | | | @return |
| 518 | | | |
| 519 | | | private EntityResolver getEntityResolver() |
| 520 | | | { |
| 521 | 0 | | EntityResolver resolver = new DummyEntityResolver(this); |
| 522 | | | try |
| 523 | | | { |
| 524 | 0 | (6) | String [] catalogs = {"src/xml/catalog.xml"}; |
| 525 | 0 | | System.getProperties().put("xml.catalog.verbosity", "1000"); |
| 526 | | | |
| 527 | 0 | | log("Instantiating xml catalog resolver .", Project.MSG_INFO); |
| 528 | | | |
| 529 | 0 | | XMLCatalogResolver xmlResolver = new XMLCatalogResolver(); |
| 530 | | | |
| 531 | 0 | | xmlResolver.setPreferPublic(false); |
| 532 | 0 | | xmlResolver.setCatalogList(catalogs); |
| 533 | 0 | | resolver = xmlResolver; |
| 534 | | | } |
| 535 | 0 | | catch (NoClassDefFoundError e) |
| 536 | | | { |
| 537 | | (7) | |
| 538 | | (8) | |
| 539 | 0 | | log("Class CatalogReader (xml-resolver.jar) could not be found " + |
| 540 | | (9) | " within bootstrap classpath. No entity resolver is " + |
| 541 | | (10) | " available, setting dummy resolver.", Project.MSG_WARN); |
| 542 | 0 | | } |
| 543 | | | |
| 544 | 0 | | return resolver; |
| 545 | | | } |
| 546 | | | |
| 547 | | | |
| 548 | | | @return |
| 549 | | | @throws |
| 550 | | | |
| 551 | | | Source getInAsStreamSource () |
| 552 | | | { |
| 553 | | | final Source result; |
| 554 | 100 | | if (!mResolveExternalEntities) |
| 555 | | | { |
| 556 | | | final org.xml.sax.XMLReader reader; |
| 557 | | | try |
| 558 | | | { |
| 559 | 0 | | EntityResolver resolver = getEntityResolver(); |
| 560 | | | |
| 561 | | | |
| 562 | | | |
| 563 | 0 | | reader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); |
| 564 | 0 | | reader.setEntityResolver(resolver); |
| 565 | 0 | | result = new SAXSource(reader, new InputSource( |
| 566 | | | new FileInputStream(mInFile))); |
| 567 | | | } |
| 568 | 0 | | catch (SAXException e) |
| 569 | | | { |
| 570 | 0 | | throw new BuildException("Cannot create SAX XML Reader: " + e, |
| 571 | | | e); |
| 572 | | | } |
| 573 | 0 | | catch (FileNotFoundException e) |
| 574 | | | { |
| 575 | 0 | | throw new BuildException("Ups, cannot open file: " + e, e); |
| 576 | 0 | | } |
| 577 | 0 | | } |
| 578 | | | else |
| 579 | | | { |
| 580 | 100 | | result = new StreamSource(mInFile); |
| 581 | | | } |
| 582 | 100 | | return result; |
| 583 | | | } |
| 584 | | | |
| 585 | | | |
| 586 | | | |
| 587 | | | |
| 588 | | | @return |
| 589 | | | @throws |
| 590 | | | |
| 591 | | | private Properties getFawkezVersionProperties () |
| 592 | | | throws IOException |
| 593 | | | { |
| 594 | 0 | | final Properties props = new Properties(); |
| 595 | 0 | | final InputStream in |
| 596 | | | = XsltBasedTask.class.getResourceAsStream(FAWKEZ_VERSION_FILE); |
| 597 | | | try |
| 598 | | | { |
| 599 | 0 | | props.load(in); |
| 600 | | | } |
| 601 | | | finally |
| 602 | | | { |
| 603 | 0 | | IoUtil.close(in); |
| 604 | 0 | | } |
| 605 | 0 | | return props; |
| 606 | | | } |
| 607 | | | |
| 608 | 100 | | private static class MyErrorListener |
| 609 | | | implements ErrorListener |
| 610 | | | { |
| 611 | | | {@inheritDoc} |
| 612 | | | public void warning (TransformerException arg0) |
| 613 | | | throws TransformerException |
| 614 | | | { |
| 615 | 100 | | throw arg0; |
| 616 | | | } |
| 617 | | | |
| 618 | | | {@inheritDoc} |
| 619 | | | public void error (TransformerException arg0) |
| 620 | | | throws TransformerException |
| 621 | | | { |
| 622 | 0 | | throw arg0; |
| 623 | | | } |
| 624 | | | |
| 625 | | | {@inheritDoc} |
| 626 | | | public void fatalError (TransformerException arg0) |
| 627 | | | throws TransformerException |
| 628 | | | { |
| 629 | 100 | | throw arg0; |
| 630 | | | } |
| 631 | | | } |
| 632 | | | } |