1 16 17 20 21 package com.sun.org.apache.xalan.internal.xsltc.trax; 22 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileNotFoundException ; 26 import java.io.FilenameFilter ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 import java.util.Enumeration ; 32 import java.util.Hashtable ; 33 import java.util.Properties ; 34 import java.util.Vector ; 35 import java.util.zip.ZipEntry ; 36 import java.util.zip.ZipFile ; 37 38 import javax.xml.XMLConstants ; 39 40 import javax.xml.parsers.SAXParserFactory ; 41 import javax.xml.parsers.SAXParser ; 42 import javax.xml.parsers.ParserConfigurationException ; 43 44 import javax.xml.transform.ErrorListener ; 45 import javax.xml.transform.Source ; 46 import javax.xml.transform.Templates ; 47 import javax.xml.transform.Transformer ; 48 import javax.xml.transform.TransformerConfigurationException ; 49 import javax.xml.transform.TransformerException ; 50 import javax.xml.transform.URIResolver ; 51 import javax.xml.transform.dom.DOMResult ; 52 import javax.xml.transform.dom.DOMSource ; 53 import javax.xml.transform.sax.SAXResult ; 54 import javax.xml.transform.sax.SAXSource ; 55 import javax.xml.transform.sax.SAXTransformerFactory ; 56 import javax.xml.transform.sax.TemplatesHandler ; 57 import javax.xml.transform.sax.TransformerHandler ; 58 import javax.xml.transform.stream.StreamResult ; 59 import javax.xml.transform.stream.StreamSource ; 60 61 import com.sun.org.apache.xml.internal.utils.StylesheetPIHandler; 62 import com.sun.org.apache.xml.internal.utils.StopParseException; 63 64 import com.sun.org.apache.xalan.internal.xsltc.compiler.SourceLoader; 65 import com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC; 66 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg; 67 import com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager; 68 69 import org.xml.sax.InputSource ; 70 import org.xml.sax.XMLFilter ; 71 import org.xml.sax.XMLReader ; 72 import org.xml.sax.helpers.XMLReaderFactory ; 73 74 77 public class TransformerFactoryImpl 78 extends SAXTransformerFactory implements SourceLoader, ErrorListener 79 { 80 83 private static final String CLASS_NAME = "TransformerFactoryImpl"; 84 85 public final static String TRANSLET_NAME = "translet-name"; 87 public final static String DESTINATION_DIRECTORY = "destination-directory"; 88 public final static String PACKAGE_NAME = "package-name"; 89 public final static String JAR_NAME = "jar-name"; 90 public final static String GENERATE_TRANSLET = "generate-translet"; 91 public final static String AUTO_TRANSLET = "auto-translet"; 92 public final static String USE_CLASSPATH = "use-classpath"; 93 public final static String DEBUG = "debug"; 94 public final static String ENABLE_INLINING = "enable-inlining"; 95 public final static String INDENT_NUMBER = "indent-number"; 96 97 101 private ErrorListener _errorListener = this; 102 103 106 private URIResolver _uriResolver = null; 107 108 118 protected static String DEFAULT_TRANSLET_NAME = "GregorSamsa"; 119 120 123 private String _transletName = DEFAULT_TRANSLET_NAME; 124 125 128 private String _destinationDirectory = null; 129 130 133 private String _packageName = null; 134 135 138 private String _jarFileName = null; 139 140 144 private Hashtable _piParams = null; 145 146 147 150 static ThreadLocal _xmlReader = new ThreadLocal (); 151 152 155 private static class PIParamWrapper { 156 public String _media = null; 157 public String _title = null; 158 public String _charset = null; 159 160 public PIParamWrapper(String media, String title, String charset) { 161 _media = media; 162 _title = title; 163 _charset = charset; 164 } 165 } 166 167 170 private boolean _debug = false; 171 172 175 private boolean _enableInlining = false; 176 177 181 private boolean _generateTranslet = false; 182 183 189 private boolean _autoTranslet = false; 190 191 195 private boolean _useClasspath = false; 196 197 200 private int _indentNumber = -1; 201 202 208 private Class m_DTMManagerClass; 209 210 213 private boolean _isSecureProcessing = false; 214 215 218 public TransformerFactoryImpl() { 219 m_DTMManagerClass = XSLTCDTMManager.getDTMManagerClass(); 220 } 221 222 231 public void setErrorListener(ErrorListener listener) 232 throws IllegalArgumentException 233 { 234 if (listener == null) { 235 ErrorMsg err = new ErrorMsg(ErrorMsg.ERROR_LISTENER_NULL_ERR, 236 "TransformerFactory"); 237 throw new IllegalArgumentException (err.toString()); 238 } 239 _errorListener = listener; 240 } 241 242 248 public ErrorListener getErrorListener() { 249 return _errorListener; 250 } 251 252 260 public Object getAttribute(String name) 261 throws IllegalArgumentException 262 { 263 if (name.equals(TRANSLET_NAME)) { 265 return _transletName; 266 } 267 else if (name.equals(GENERATE_TRANSLET)) { 268 return new Boolean (_generateTranslet); 269 } 270 else if (name.equals(AUTO_TRANSLET)) { 271 return new Boolean (_autoTranslet); 272 } 273 274 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_INVALID_ATTR_ERR, name); 276 throw new IllegalArgumentException (err.toString()); 277 } 278 279 287 public void setAttribute(String name, Object value) 288 throws IllegalArgumentException 289 { 290 if (name.equals(TRANSLET_NAME) && value instanceof String ) { 293 _transletName = (String ) value; 294 return; 295 } 296 else if (name.equals(DESTINATION_DIRECTORY) && value instanceof String ) { 297 _destinationDirectory = (String ) value; 298 return; 299 } 300 else if (name.equals(PACKAGE_NAME) && value instanceof String ) { 301 _packageName = (String ) value; 302 return; 303 } 304 else if (name.equals(JAR_NAME) && value instanceof String ) { 305 _jarFileName = (String ) value; 306 return; 307 } 308 else if (name.equals(GENERATE_TRANSLET)) { 309 if (value instanceof Boolean ) { 310 _generateTranslet = ((Boolean ) value).booleanValue(); 311 return; 312 } 313 else if (value instanceof String ) { 314 _generateTranslet = ((String ) value).equalsIgnoreCase("true"); 315 return; 316 } 317 } 318 else if (name.equals(AUTO_TRANSLET)) { 319 if (value instanceof Boolean ) { 320 _autoTranslet = ((Boolean ) value).booleanValue(); 321 return; 322 } 323 else if (value instanceof String ) { 324 _autoTranslet = ((String ) value).equalsIgnoreCase("true"); 325 return; 326 } 327 } 328 else if (name.equals(USE_CLASSPATH)) { 329 if (value instanceof Boolean ) { 330 _useClasspath = ((Boolean ) value).booleanValue(); 331 return; 332 } 333 else if (value instanceof String ) { 334 _useClasspath = ((String ) value).equalsIgnoreCase("true"); 335 return; 336 } 337 } 338 else if (name.equals(DEBUG)) { 339 if (value instanceof Boolean ) { 340 _debug = ((Boolean ) value).booleanValue(); 341 return; 342 } 343 else if (value instanceof String ) { 344 _debug = ((String ) value).equalsIgnoreCase("true"); 345 return; 346 } 347 } 348 else if (name.equals(ENABLE_INLINING)) { 349 if (value instanceof Boolean ) { 350 _enableInlining = ((Boolean ) value).booleanValue(); 351 return; 352 } 353 else if (value instanceof String ) { 354 _enableInlining = ((String ) value).equalsIgnoreCase("true"); 355 return; 356 } 357 } 358 else if (name.equals(INDENT_NUMBER)) { 359 if (value instanceof String ) { 360 try { 361 _indentNumber = Integer.parseInt((String ) value); 362 return; 363 } 364 catch (NumberFormatException e) { 365 } 367 } 368 else if (value instanceof Integer ) { 369 _indentNumber = ((Integer ) value).intValue(); 370 return; 371 } 372 } 373 374 final ErrorMsg err 376 = new ErrorMsg(ErrorMsg.JAXP_INVALID_ATTR_ERR, name); 377 throw new IllegalArgumentException (err.toString()); 378 } 379 380 401 public void setFeature(String name, boolean value) 402 throws TransformerConfigurationException { 403 404 if (name == null) { 406 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_SET_FEATURE_NULL_NAME); 407 throw new NullPointerException (err.toString()); 408 } 409 else if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { 411 _isSecureProcessing = value; 412 return; 414 } 415 else { 416 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNSUPPORTED_FEATURE, name); 418 throw new TransformerConfigurationException (err.toString()); 419 } 420 } 421 422 431 public boolean getFeature(String name) { 432 String [] features = { 434 DOMSource.FEATURE, 435 DOMResult.FEATURE, 436 SAXSource.FEATURE, 437 SAXResult.FEATURE, 438 StreamSource.FEATURE, 439 StreamResult.FEATURE, 440 SAXTransformerFactory.FEATURE, 441 SAXTransformerFactory.FEATURE_XMLFILTER 442 }; 443 444 if (name == null) { 446 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_GET_FEATURE_NULL_NAME); 447 throw new NullPointerException (err.toString()); 448 } 449 450 for (int i =0; i < features.length; i++) { 452 if (name.equals(features[i])) { 453 return true; 454 } 455 } 456 if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { 458 return _isSecureProcessing; 459 } 460 461 return false; 463 } 464 465 473 public URIResolver getURIResolver() { 474 return _uriResolver; 475 } 476 477 487 public void setURIResolver(URIResolver resolver) { 488 _uriResolver = resolver; 489 } 490 491 506 public Source getAssociatedStylesheet(Source source, String media, 507 String title, String charset) 508 throws TransformerConfigurationException { 509 510 String baseId; 511 XMLReader reader = null; 512 InputSource isource = null; 513 514 515 518 StylesheetPIHandler _stylesheetPIHandler = new StylesheetPIHandler(null,media,title,charset); 519 520 try { 521 522 if (source instanceof DOMSource ) { 523 final DOMSource domsrc = (DOMSource ) source; 524 baseId = domsrc.getSystemId(); 525 final org.w3c.dom.Node node = domsrc.getNode(); 526 final DOM2SAX dom2sax = new DOM2SAX(node); 527 528 _stylesheetPIHandler.setBaseId(baseId); 529 530 dom2sax.setContentHandler( _stylesheetPIHandler); 531 dom2sax.parse(); 532 } else { 533 isource = SAXSource.sourceToInputSource(source); 534 baseId = isource.getSystemId(); 535 536 SAXParserFactory factory = SAXParserFactory.newInstance(); 537 factory.setNamespaceAware(true); 538 539 if (_isSecureProcessing) { 540 try { 541 factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); 542 } 543 catch (org.xml.sax.SAXException e) {} 544 } 545 546 SAXParser jaxpParser = factory.newSAXParser(); 547 548 reader = jaxpParser.getXMLReader(); 549 if (reader == null) { 550 reader = XMLReaderFactory.createXMLReader(); 551 } 552 553 _stylesheetPIHandler.setBaseId(baseId); 554 reader.setContentHandler(_stylesheetPIHandler); 555 reader.parse(isource); 556 557 } 558 559 if (_uriResolver != null ) { 560 _stylesheetPIHandler.setURIResolver(_uriResolver); 561 } 562 563 } catch (StopParseException e ) { 564 566 } catch (javax.xml.parsers.ParserConfigurationException e) { 567 568 throw new TransformerConfigurationException ( 569 "getAssociatedStylesheets failed", e); 570 571 } catch (org.xml.sax.SAXException se) { 572 573 throw new TransformerConfigurationException ( 574 "getAssociatedStylesheets failed", se); 575 576 577 } catch (IOException ioe ) { 578 throw new TransformerConfigurationException ( 579 "getAssociatedStylesheets failed", ioe); 580 581 } 582 583 return _stylesheetPIHandler.getAssociatedStylesheet(); 584 585 } 586 587 594 public Transformer newTransformer() 595 throws TransformerConfigurationException 596 { 597 TransformerImpl result = new TransformerImpl(new Properties (), 598 _indentNumber, this); 599 if (_uriResolver != null) { 600 result.setURIResolver(_uriResolver); 601 } 602 603 if (_isSecureProcessing) { 604 result.setSecureProcessing(true); 605 } 606 607 return result; 608 } 609 610 620 public Transformer newTransformer(Source source) throws 621 TransformerConfigurationException 622 { 623 final Templates templates = newTemplates(source); 624 final Transformer transformer = templates.newTransformer(); 625 if (_uriResolver != null) { 626 transformer.setURIResolver(_uriResolver); 627 } 628 return(transformer); 629 } 630 631 634 private void passWarningsToListener(Vector messages) 635 throws TransformerException 636 { 637 if (_errorListener == null || messages == null) { 638 return; 639 } 640 final int count = messages.size(); 642 for (int pos = 0; pos < count; pos++) { 643 String message = messages.elementAt(pos).toString(); 644 _errorListener.error( 645 new TransformerConfigurationException (message)); 646 } 647 } 648 649 652 private void passErrorsToListener(Vector messages) { 653 try { 654 if (_errorListener == null || messages == null) { 655 return; 656 } 657 final int count = messages.size(); 659 for (int pos = 0; pos < count; pos++) { 660 String message = messages.elementAt(pos).toString(); 661 _errorListener.error(new TransformerException (message)); 662 } 663 } 664 catch (TransformerException e) { 665 } 667 } 668 669 678 public Templates newTemplates(Source source) 679 throws TransformerConfigurationException 680 { 681 if (_useClasspath) { 685 String transletName = getTransletBaseName(source); 686 687 if (_packageName != null) 688 transletName = _packageName + "." + transletName; 689 690 try { 691 final Class clazz = ObjectFactory.findProviderClass( 692 transletName, ObjectFactory.findClassLoader(), true); 693 resetTransientAttributes(); 694 695 return new TemplatesImpl(new Class []{clazz}, transletName, null, _indentNumber, this); 696 } 697 catch (ClassNotFoundException cnfe) { 698 ErrorMsg err = new ErrorMsg(ErrorMsg.CLASS_NOT_FOUND_ERR, transletName); 699 throw new TransformerConfigurationException (err.toString()); 700 } 701 catch (Exception e) { 702 ErrorMsg err = new ErrorMsg( 703 new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY) 704 + e.getMessage()); 705 throw new TransformerConfigurationException (err.toString()); 706 } 707 } 708 709 if (_autoTranslet) { 712 byte[][] bytecodes = null; 713 String transletClassName = getTransletBaseName(source); 714 715 if (_packageName != null) 716 transletClassName = _packageName + "." + transletClassName; 717 718 if (_jarFileName != null) 719 bytecodes = getBytecodesFromJar(source, transletClassName); 720 else 721 bytecodes = getBytecodesFromClasses(source, transletClassName); 722 723 if (bytecodes != null) { 724 if (_debug) { 725 if (_jarFileName != null) 726 System.err.println(new ErrorMsg( 727 ErrorMsg.TRANSFORM_WITH_JAR_STR, transletClassName, _jarFileName)); 728 else 729 System.err.println(new ErrorMsg( 730 ErrorMsg.TRANSFORM_WITH_TRANSLET_STR, transletClassName)); 731 } 732 733 resetTransientAttributes(); 736 737 return new TemplatesImpl(bytecodes, transletClassName, null, _indentNumber, this); 738 } 739 } 740 741 final XSLTC xsltc = new XSLTC(); 743 if (_debug) xsltc.setDebug(true); 744 if (_enableInlining) xsltc.setTemplateInlining(true); 745 if (_isSecureProcessing) xsltc.setSecureProcessing(true); 746 xsltc.init(); 747 748 if (_uriResolver != null) { 750 xsltc.setSourceLoader(this); 751 } 752 753 if ((_piParams != null) && (_piParams.get(source) != null)) { 756 PIParamWrapper p = (PIParamWrapper)_piParams.get(source); 758 if (p != null) { 760 xsltc.setPIParameters(p._media, p._title, p._charset); 761 } 762 } 763 764 int outputType = XSLTC.BYTEARRAY_OUTPUT; 766 if (_generateTranslet || _autoTranslet) { 767 xsltc.setClassName(getTransletBaseName(source)); 769 770 if (_destinationDirectory != null) 771 xsltc.setDestDirectory(_destinationDirectory); 772 else { 773 String xslName = getStylesheetFileName(source); 774 if (xslName != null) { 775 File xslFile = new File (xslName); 776 String xslDir = xslFile.getParent(); 777 778 if (xslDir != null) 779 xsltc.setDestDirectory(xslDir); 780 } 781 } 782 783 if (_packageName != null) 784 xsltc.setPackageName(_packageName); 785 786 if (_jarFileName != null) { 787 xsltc.setJarFileName(_jarFileName); 788 outputType = XSLTC.BYTEARRAY_AND_JAR_OUTPUT; 789 } 790 else 791 outputType = XSLTC.BYTEARRAY_AND_FILE_OUTPUT; 792 } 793 794 final InputSource input = Util.getInputSource(xsltc, source); 796 byte[][] bytecodes = xsltc.compile(null, input, outputType); 797 final String transletName = xsltc.getClassName(); 798 799 if ((_generateTranslet || _autoTranslet) 801 && bytecodes != null && _jarFileName != null) { 802 try { 803 xsltc.outputToJar(); 804 } 805 catch (java.io.IOException e) { } 806 } 807 808 resetTransientAttributes(); 811 812 if (_errorListener != this) { 814 try { 815 passWarningsToListener(xsltc.getWarnings()); 816 } 817 catch (TransformerException e) { 818 throw new TransformerConfigurationException (e); 819 } 820 } 821 else { 822 xsltc.printWarnings(); 823 } 824 825 if (bytecodes == null) { 827 828 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR); 829 TransformerConfigurationException exc = new TransformerConfigurationException (err.toString()); 830 831 if (_errorListener != null) { 833 passErrorsToListener(xsltc.getErrors()); 834 835 try { 839 _errorListener.fatalError(exc); 840 } catch (TransformerException te) { 841 } 843 } 844 else { 845 xsltc.printErrors(); 846 } 847 throw exc; 848 } 849 850 return new TemplatesImpl(bytecodes, transletName, 851 xsltc.getOutputProperties(), _indentNumber, this); 852 } 853 854 862 public TemplatesHandler newTemplatesHandler() 863 throws TransformerConfigurationException 864 { 865 final TemplatesHandlerImpl handler = 866 new TemplatesHandlerImpl(_indentNumber, this); 867 if (_uriResolver != null) { 868 handler.setURIResolver(_uriResolver); 869 } 870 return handler; 871 } 872 873 881 public TransformerHandler newTransformerHandler() 882 throws TransformerConfigurationException 883 { 884 final Transformer transformer = newTransformer(); 885 if (_uriResolver != null) { 886 transformer.setURIResolver(_uriResolver); 887 } 888 return new TransformerHandlerImpl((TransformerImpl) transformer); 889 } 890 891 901 public TransformerHandler newTransformerHandler(Source src) 902 throws TransformerConfigurationException 903 { 904 final Transformer transformer = newTransformer(src); 905 if (_uriResolver != null) { 906 transformer.setURIResolver(_uriResolver); 907 } 908 return new TransformerHandlerImpl((TransformerImpl) transformer); 909 } 910 911 921 public TransformerHandler newTransformerHandler(Templates templates) 922 throws TransformerConfigurationException 923 { 924 final Transformer transformer = templates.newTransformer(); 925 final TransformerImpl internal = (TransformerImpl)transformer; 926 return new TransformerHandlerImpl(internal); 927 } 928 929 938 public XMLFilter newXMLFilter(Source src) 939 throws TransformerConfigurationException 940 { 941 Templates templates = newTemplates(src); 942 if (templates == null) return null; 943 return newXMLFilter(templates); 944 } 945 946 955 public XMLFilter newXMLFilter(Templates templates) 956 throws TransformerConfigurationException 957 { 958 try { 959 return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates); 960 } 961 catch (TransformerConfigurationException e1) { 962 if (_errorListener != null) { 963 try { 964 _errorListener.fatalError(e1); 965 return null; 966 } 967 catch (TransformerException e2) { 968 new TransformerConfigurationException (e2); 969 } 970 } 971 throw e1; 972 } 973 } 974 975 986 public void error(TransformerException e) 987 throws TransformerException 988 { 989 Throwable wrapped = e.getException(); 990 if (wrapped != null) { 991 System.err.println(new ErrorMsg(ErrorMsg.ERROR_PLUS_WRAPPED_MSG, 992 e.getMessageAndLocation(), 993 wrapped.getMessage())); 994 } else { 995 System.err.println(new ErrorMsg(ErrorMsg.ERROR_MSG, 996 e.getMessageAndLocation())); 997 } 998 throw e; 999 } 1000 1001 1014 public void fatalError(TransformerException e) 1015 throws TransformerException 1016 { 1017 Throwable wrapped = e.getException(); 1018 if (wrapped != null) { 1019 System.err.println(new ErrorMsg(ErrorMsg.FATAL_ERR_PLUS_WRAPPED_MSG, 1020 e.getMessageAndLocation(), 1021 wrapped.getMessage())); 1022 } else { 1023 System.err.println(new ErrorMsg(ErrorMsg.FATAL_ERR_MSG, 1024 e.getMessageAndLocation())); 1025 } 1026 throw e; 1027 } 1028 1029 1042 public void warning(TransformerException e) 1043 throws TransformerException 1044 { 1045 Throwable wrapped = e.getException(); 1046 if (wrapped != null) { 1047 System.err.println(new ErrorMsg(ErrorMsg.WARNING_PLUS_WRAPPED_MSG, 1048 e.getMessageAndLocation(), 1049 wrapped.getMessage())); 1050 } else { 1051 System.err.println(new ErrorMsg(ErrorMsg.WARNING_MSG, 1052 e.getMessageAndLocation())); 1053 } 1054 } 1055 1056 1065 public InputSource loadSource(String href, String context, XSLTC xsltc) { 1066 try { 1067 if (_uriResolver != null) { 1068 final Source source = _uriResolver.resolve(href, context); 1069 if (source != null) { 1070 return Util.getInputSource(xsltc, source); 1071 } 1072 } 1073 } 1074 catch (TransformerException e) { 1075 } 1077 return null; 1078 } 1079 1080 1083 private void resetTransientAttributes() { 1084 _transletName = DEFAULT_TRANSLET_NAME; 1085 _destinationDirectory = null; 1086 _packageName = null; 1087 _jarFileName = null; 1088 } 1089 1090 1098 private byte[][] getBytecodesFromClasses(Source source, String fullClassName) 1099 { 1100 if (fullClassName == null) 1101 return null; 1102 1103 String xslFileName = getStylesheetFileName(source); 1104 File xslFile = null; 1105 if (xslFileName != null) 1106 xslFile = new File (xslFileName); 1107 1108 final String transletName; 1110 int lastDotIndex = fullClassName.lastIndexOf('.'); 1111 if (lastDotIndex > 0) 1112 transletName = fullClassName.substring(lastDotIndex+1); 1113 else 1114 transletName = fullClassName; 1115 1116 String transletPath = fullClassName.replace('.', '/'); 1118 if (_destinationDirectory != null) { 1119 transletPath = _destinationDirectory + "/" + transletPath + ".class"; 1120 } 1121 else { 1122 if (xslFile != null && xslFile.getParent() != null) 1123 transletPath = xslFile.getParent() + "/" + transletPath + ".class"; 1124 else 1125 transletPath = transletPath + ".class"; 1126 } 1127 1128 File transletFile = new File (transletPath); 1130 if (!transletFile.exists()) 1131 return null; 1132 1133 if (xslFile != null && xslFile.exists()) { 1138 long xslTimestamp = xslFile.lastModified(); 1139 long transletTimestamp = transletFile.lastModified(); 1140 if (transletTimestamp < xslTimestamp) 1141 return null; 1142 } 1143 1144 Vector bytecodes = new Vector (); 1146 int fileLength = (int)transletFile.length(); 1147 if (fileLength > 0) { 1148 FileInputStream input = null; 1149 try { 1150 input = new FileInputStream (transletFile); 1151 } 1152 catch (FileNotFoundException e) { 1153 return null; 1154 } 1155 1156 byte[] bytes = new byte[fileLength]; 1157 try { 1158 readFromInputStream(bytes, input, fileLength); 1159 input.close(); 1160 } 1161 catch (IOException e) { 1162 return null; 1163 } 1164 1165 bytecodes.addElement(bytes); 1166 } 1167 else 1168 return null; 1169 1170 String transletParentDir = transletFile.getParent(); 1172 if (transletParentDir == null) 1173 transletParentDir = System.getProperty("user.dir"); 1174 1175 File transletParentFile = new File (transletParentDir); 1176 1177 final String transletAuxPrefix = transletName + "$"; 1179 File [] auxfiles = transletParentFile.listFiles(new FilenameFilter () { 1180 public boolean accept(File dir, String name) 1181 { 1182 return (name.endsWith(".class") && name.startsWith(transletAuxPrefix)); 1183 } 1184 }); 1185 1186 for (int i = 0; i < auxfiles.length; i++) 1188 { 1189 File auxfile = auxfiles[i]; 1190 int auxlength = (int)auxfile.length(); 1191 if (auxlength > 0) { 1192 FileInputStream auxinput = null; 1193 try { 1194 auxinput = new FileInputStream (auxfile); 1195 } 1196 catch (FileNotFoundException e) { 1197 continue; 1198 } 1199 1200 byte[] bytes = new byte[auxlength]; 1201 1202 try { 1203 readFromInputStream(bytes, auxinput, auxlength); 1204 auxinput.close(); 1205 } 1206 catch (IOException e) { 1207 continue; 1208 } 1209 1210 bytecodes.addElement(bytes); 1211 } 1212 } 1213 1214 final int count = bytecodes.size(); 1216 if ( count > 0) { 1217 final byte[][] result = new byte[count][1]; 1218 for (int i = 0; i < count; i++) { 1219 result[i] = (byte[])bytecodes.elementAt(i); 1220 } 1221 1222 return result; 1223 } 1224 else 1225 return null; 1226 } 1227 1228 1235 private byte[][] getBytecodesFromJar(Source source, String fullClassName) 1236 { 1237 String xslFileName = getStylesheetFileName(source); 1238 File xslFile = null; 1239 if (xslFileName != null) 1240 xslFile = new File (xslFileName); 1241 1242 String jarPath = null; 1244 if (_destinationDirectory != null) 1245 jarPath = _destinationDirectory + "/" + _jarFileName; 1246 else { 1247 if (xslFile != null && xslFile.getParent() != null) 1248 jarPath = xslFile.getParent() + "/" + _jarFileName; 1249 else 1250 jarPath = _jarFileName; 1251 } 1252 1253 File file = new File (jarPath); 1255 if (!file.exists()) 1256 return null; 1257 1258 if (xslFile != null && xslFile.exists()) { 1261 long xslTimestamp = xslFile.lastModified(); 1262 long transletTimestamp = file.lastModified(); 1263 if (transletTimestamp < xslTimestamp) 1264 return null; 1265 } 1266 1267 ZipFile jarFile = null; 1269 try { 1270 jarFile = new ZipFile (file); 1271 } 1272 catch (IOException e) { 1273 return null; 1274 } 1275 1276 String transletPath = fullClassName.replace('.', '/'); 1277 String transletAuxPrefix = transletPath + "$"; 1278 String transletFullName = transletPath + ".class"; 1279 1280 Vector bytecodes = new Vector (); 1281 1282 Enumeration entries = jarFile.entries(); 1285 while (entries.hasMoreElements()) 1286 { 1287 ZipEntry entry = (ZipEntry )entries.nextElement(); 1288 String entryName = entry.getName(); 1289 if (entry.getSize() > 0 && 1290 (entryName.equals(transletFullName) || 1291 (entryName.endsWith(".class") && 1292 entryName.startsWith(transletAuxPrefix)))) 1293 { 1294 try { 1295 InputStream input = jarFile.getInputStream(entry); 1296 int size = (int)entry.getSize(); 1297 byte[] bytes = new byte[size]; 1298 readFromInputStream(bytes, input, size); 1299 input.close(); 1300 bytecodes.addElement(bytes); 1301 } 1302 catch (IOException e) { 1303 return null; 1304 } 1305 } 1306 } 1307 1308 final int count = bytecodes.size(); 1310 if (count > 0) { 1311 final byte[][] result = new byte[count][1]; 1312 for (int i = 0; i < count; i++) { 1313 result[i] = (byte[])bytecodes.elementAt(i); 1314 } 1315 1316 return result; 1317 } 1318 else 1319 return null; 1320 } 1321 1322 1329 private void readFromInputStream(byte[] bytes, InputStream input, int size) 1330 throws IOException 1331 { 1332 int n = 0; 1333 int offset = 0; 1334 int length = size; 1335 while (length > 0 && (n = input.read(bytes, offset, length)) > 0) { 1336 offset = offset + n; 1337 length = length - n; 1338 } 1339 } 1340 1341 1352 private String getTransletBaseName(Source source) 1353 { 1354 String transletBaseName = null; 1355 if (!_transletName.equals(DEFAULT_TRANSLET_NAME)) 1356 return _transletName; 1357 else { 1358 String systemId = source.getSystemId(); 1359 if (systemId != null) { 1360 String baseName = Util.baseName(systemId); 1361 if (baseName != null) { 1362 baseName = Util.noExtName(baseName); 1363 transletBaseName = Util.toJavaName(baseName); 1364 } 1365 } 1366 } 1367 1368 return (transletBaseName != null) ? transletBaseName : DEFAULT_TRANSLET_NAME; 1369 } 1370 1371 1378 private String getStylesheetFileName(Source source) 1379 { 1380 String systemId = source.getSystemId(); 1381 if (systemId != null) { 1382 File file = new File (systemId); 1383 if (file.exists()) 1384 return systemId; 1385 else { 1386 URL url = null; 1387 try { 1388 url = new URL (systemId); 1389 } 1390 catch (MalformedURLException e) { 1391 return null; 1392 } 1393 1394 if ("file".equals(url.getProtocol())) 1395 return url.getFile(); 1396 else 1397 return null; 1398 } 1399 } 1400 else 1401 return null; 1402 } 1403 1404 1407 protected Class getDTMManagerClass() { 1408 return m_DTMManagerClass; 1409 } 1410} 1411 | Popular Tags |