1 16 19 20 package org.apache.xalan.xsltc.trax; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileNotFoundException ; 25 import java.io.FilenameFilter ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.net.MalformedURLException ; 29 import java.net.URL ; 30 import java.util.Enumeration ; 31 import java.util.Hashtable ; 32 import java.util.Properties ; 33 import java.util.Vector ; 34 import java.util.zip.ZipEntry ; 35 import java.util.zip.ZipFile ; 36 37 import javax.xml.parsers.SAXParserFactory ; 38 import javax.xml.parsers.SAXParser ; 39 import javax.xml.parsers.ParserConfigurationException ; 40 41 import javax.xml.transform.ErrorListener ; 42 import javax.xml.transform.Source ; 43 import javax.xml.transform.Templates ; 44 import javax.xml.transform.Transformer ; 45 import javax.xml.transform.TransformerConfigurationException ; 46 import javax.xml.transform.TransformerException ; 47 import javax.xml.transform.URIResolver ; 48 import javax.xml.transform.dom.DOMResult ; 49 import javax.xml.transform.dom.DOMSource ; 50 import javax.xml.transform.sax.SAXResult ; 51 import javax.xml.transform.sax.SAXSource ; 52 import javax.xml.transform.sax.SAXTransformerFactory ; 53 import javax.xml.transform.sax.TemplatesHandler ; 54 import javax.xml.transform.sax.TransformerHandler ; 55 import javax.xml.transform.stream.StreamResult ; 56 import javax.xml.transform.stream.StreamSource ; 57 58 import org.apache.xml.utils.StylesheetPIHandler; 59 import org.apache.xml.utils.StopParseException; 60 61 import org.apache.xalan.xsltc.compiler.SourceLoader; 62 import org.apache.xalan.xsltc.compiler.XSLTC; 63 import org.apache.xalan.xsltc.compiler.util.ErrorMsg; 64 import org.apache.xalan.xsltc.dom.XSLTCDTMManager; 65 66 67 import org.xml.sax.InputSource ; 68 import org.xml.sax.XMLFilter ; 69 import org.xml.sax.XMLReader ; 70 import org.xml.sax.helpers.XMLReaderFactory ; 71 72 78 public class TransformerFactoryImpl 79 extends SAXTransformerFactory implements SourceLoader, ErrorListener 80 { 81 public final static String TRANSLET_NAME = "translet-name"; 83 public final static String DESTINATION_DIRECTORY = "destination-directory"; 84 public final static String PACKAGE_NAME = "package-name"; 85 public final static String JAR_NAME = "jar-name"; 86 public final static String GENERATE_TRANSLET = "generate-translet"; 87 public final static String AUTO_TRANSLET = "auto-translet"; 88 public final static String USE_CLASSPATH = "use-classpath"; 89 public final static String DEBUG = "debug"; 90 public final static String ENABLE_INLINING = "enable-inlining"; 91 public final static String INDENT_NUMBER = "indent-number"; 92 93 97 private ErrorListener _errorListener = this; 98 99 102 private URIResolver _uriResolver = null; 103 104 114 protected static String DEFAULT_TRANSLET_NAME = "GregorSamsa"; 115 116 119 private String _transletName = DEFAULT_TRANSLET_NAME; 120 121 124 private String _destinationDirectory = null; 125 126 129 private String _packageName = null; 130 131 134 private String _jarFileName = null; 135 136 140 private Hashtable _piParams = null; 141 142 143 146 static ThreadLocal _xmlReader = new ThreadLocal (); 147 148 151 private static class PIParamWrapper { 152 public String _media = null; 153 public String _title = null; 154 public String _charset = null; 155 156 public PIParamWrapper(String media, String title, String charset) { 157 _media = media; 158 _title = title; 159 _charset = charset; 160 } 161 } 162 163 166 private boolean _debug = false; 167 168 171 private boolean _enableInlining = false; 172 173 177 private boolean _generateTranslet = false; 178 179 185 private boolean _autoTranslet = false; 186 187 191 private boolean _useClasspath = false; 192 193 196 private int _indentNumber = -1; 197 198 204 private Class m_DTMManagerClass; 205 206 209 public TransformerFactoryImpl() { 210 m_DTMManagerClass = XSLTCDTMManager.getDTMManagerClass(); 211 } 212 213 222 public void setErrorListener(ErrorListener listener) 223 throws IllegalArgumentException 224 { 225 if (listener == null) { 226 ErrorMsg err = new ErrorMsg(ErrorMsg.ERROR_LISTENER_NULL_ERR, 227 "TransformerFactory"); 228 throw new IllegalArgumentException (err.toString()); 229 } 230 _errorListener = listener; 231 } 232 233 239 public ErrorListener getErrorListener() { 240 return _errorListener; 241 } 242 243 251 public Object getAttribute(String name) 252 throws IllegalArgumentException 253 { 254 if (name.equals(TRANSLET_NAME)) { 256 return _transletName; 257 } 258 else if (name.equals(GENERATE_TRANSLET)) { 259 return new Boolean (_generateTranslet); 260 } 261 else if (name.equals(AUTO_TRANSLET)) { 262 return new Boolean (_autoTranslet); 263 } 264 265 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_INVALID_ATTR_ERR, name); 267 throw new IllegalArgumentException (err.toString()); 268 } 269 270 278 public void setAttribute(String name, Object value) 279 throws IllegalArgumentException 280 { 281 if (name.equals(TRANSLET_NAME) && value instanceof String ) { 284 _transletName = (String ) value; 285 return; 286 } 287 else if (name.equals(DESTINATION_DIRECTORY) && value instanceof String ) { 288 _destinationDirectory = (String ) value; 289 return; 290 } 291 else if (name.equals(PACKAGE_NAME) && value instanceof String ) { 292 _packageName = (String ) value; 293 return; 294 } 295 else if (name.equals(JAR_NAME) && value instanceof String ) { 296 _jarFileName = (String ) value; 297 return; 298 } 299 else if (name.equals(GENERATE_TRANSLET)) { 300 if (value instanceof Boolean ) { 301 _generateTranslet = ((Boolean ) value).booleanValue(); 302 return; 303 } 304 else if (value instanceof String ) { 305 _generateTranslet = ((String ) value).equalsIgnoreCase("true"); 306 return; 307 } 308 } 309 else if (name.equals(AUTO_TRANSLET)) { 310 if (value instanceof Boolean ) { 311 _autoTranslet = ((Boolean ) value).booleanValue(); 312 return; 313 } 314 else if (value instanceof String ) { 315 _autoTranslet = ((String ) value).equalsIgnoreCase("true"); 316 return; 317 } 318 } 319 else if (name.equals(USE_CLASSPATH)) { 320 if (value instanceof Boolean ) { 321 _useClasspath = ((Boolean ) value).booleanValue(); 322 return; 323 } 324 else if (value instanceof String ) { 325 _useClasspath = ((String ) value).equalsIgnoreCase("true"); 326 return; 327 } 328 } 329 else if (name.equals(DEBUG)) { 330 if (value instanceof Boolean ) { 331 _debug = ((Boolean ) value).booleanValue(); 332 return; 333 } 334 else if (value instanceof String ) { 335 _debug = ((String ) value).equalsIgnoreCase("true"); 336 return; 337 } 338 } 339 else if (name.equals(ENABLE_INLINING)) { 340 if (value instanceof Boolean ) { 341 _enableInlining = ((Boolean ) value).booleanValue(); 342 return; 343 } 344 else if (value instanceof String ) { 345 _enableInlining = ((String ) value).equalsIgnoreCase("true"); 346 return; 347 } 348 } 349 else if (name.equals(INDENT_NUMBER)) { 350 if (value instanceof String ) { 351 try { 352 _indentNumber = Integer.parseInt((String ) value); 353 return; 354 } 355 catch (NumberFormatException e) { 356 } 358 } 359 else if (value instanceof Integer ) { 360 _indentNumber = ((Integer ) value).intValue(); 361 return; 362 } 363 } 364 365 final ErrorMsg err 367 = new ErrorMsg(ErrorMsg.JAXP_INVALID_ATTR_ERR, name); 368 throw new IllegalArgumentException (err.toString()); 369 } 370 371 380 public boolean getFeature(String name) { 381 String [] features = { 383 DOMSource.FEATURE, 384 DOMResult.FEATURE, 385 SAXSource.FEATURE, 386 SAXResult.FEATURE, 387 StreamSource.FEATURE, 388 StreamResult.FEATURE, 389 SAXTransformerFactory.FEATURE, 390 SAXTransformerFactory.FEATURE_XMLFILTER 391 }; 392 393 for (int i =0; i < features.length; i++) { 395 if (name.equals(features[i])) { 396 return true; 397 } 398 } 399 return false; 401 } 402 403 411 public URIResolver getURIResolver() { 412 return _uriResolver; 413 } 414 415 425 public void setURIResolver(URIResolver resolver) { 426 _uriResolver = resolver; 427 } 428 429 444 public Source getAssociatedStylesheet(Source source, String media, 445 String title, String charset) 446 throws TransformerConfigurationException { 447 448 String baseId; 449 XMLReader reader = null; 450 InputSource isource = null; 451 452 453 456 StylesheetPIHandler _stylesheetPIHandler = new StylesheetPIHandler(null,media,title,charset); 457 458 try { 459 460 if (source instanceof DOMSource ) { 461 final DOMSource domsrc = (DOMSource ) source; 462 baseId = domsrc.getSystemId(); 463 final org.w3c.dom.Node node = domsrc.getNode(); 464 final DOM2SAX dom2sax = new DOM2SAX(node); 465 466 _stylesheetPIHandler.setBaseId(baseId); 467 468 dom2sax.setContentHandler( _stylesheetPIHandler); 469 dom2sax.parse(); 470 } else { 471 isource = SAXSource.sourceToInputSource(source); 472 baseId = isource.getSystemId(); 473 474 SAXParserFactory factory = SAXParserFactory.newInstance(); 475 factory.setNamespaceAware(true); 476 SAXParser jaxpParser = factory.newSAXParser(); 477 478 reader = jaxpParser.getXMLReader(); 479 if (reader == null) { 480 reader = XMLReaderFactory.createXMLReader(); 481 } 482 483 _stylesheetPIHandler.setBaseId(baseId); 484 reader.setContentHandler(_stylesheetPIHandler); 485 reader.parse(isource); 486 487 } 488 489 if (_uriResolver != null ) { 490 _stylesheetPIHandler.setURIResolver(_uriResolver); 491 } 492 493 } catch (StopParseException e ) { 494 496 } catch (javax.xml.parsers.ParserConfigurationException e) { 497 498 throw new TransformerConfigurationException ( 499 "getAssociatedStylesheets failed", e); 500 501 } catch (org.xml.sax.SAXException se) { 502 503 throw new TransformerConfigurationException ( 504 "getAssociatedStylesheets failed", se); 505 506 507 } catch (IOException ioe ) { 508 throw new TransformerConfigurationException ( 509 "getAssociatedStylesheets failed", ioe); 510 511 } 512 513 return _stylesheetPIHandler.getAssociatedStylesheet(); 514 515 } 516 517 524 public Transformer newTransformer() 525 throws TransformerConfigurationException 526 { 527 TransformerImpl result = new TransformerImpl(new Properties (), 528 _indentNumber, this); 529 if (_uriResolver != null) { 530 result.setURIResolver(_uriResolver); 531 } 532 return result; 533 } 534 535 545 public Transformer newTransformer(Source source) throws 546 TransformerConfigurationException 547 { 548 final Templates templates = newTemplates(source); 549 final Transformer transformer = templates.newTransformer(); 550 if (_uriResolver != null) { 551 transformer.setURIResolver(_uriResolver); 552 } 553 return(transformer); 554 } 555 556 559 private void passWarningsToListener(Vector messages) 560 throws TransformerException 561 { 562 if (_errorListener == null || messages == null) { 563 return; 564 } 565 final int count = messages.size(); 567 for (int pos = 0; pos < count; pos++) { 568 String message = messages.elementAt(pos).toString(); 569 _errorListener.error( 570 new TransformerConfigurationException (message)); 571 } 572 } 573 574 577 private void passErrorsToListener(Vector messages) { 578 try { 579 if (_errorListener == null || messages == null) { 580 return; 581 } 582 final int count = messages.size(); 584 for (int pos = 0; pos < count; pos++) { 585 String message = messages.elementAt(pos).toString(); 586 _errorListener.error(new TransformerException (message)); 587 } 588 } 589 catch (TransformerException e) { 590 } 592 } 593 594 603 public Templates newTemplates(Source source) 604 throws TransformerConfigurationException 605 { 606 if (_useClasspath) { 610 String transletName = getTransletBaseName(source); 611 612 if (_packageName != null) 613 transletName = _packageName + "." + transletName; 614 615 try { 616 final Class clazz = ObjectFactory.findProviderClass( 617 transletName, ObjectFactory.findClassLoader(), true); 618 resetTransientAttributes(); 619 620 return new TemplatesImpl(new Class []{clazz}, transletName, null, _indentNumber, this); 621 } 622 catch (ClassNotFoundException cnfe) { 623 ErrorMsg err = new ErrorMsg(ErrorMsg.CLASS_NOT_FOUND_ERR, transletName); 624 throw new TransformerConfigurationException (err.toString()); 625 } 626 catch (Exception e) { 627 ErrorMsg err = new ErrorMsg( 628 new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY) 629 + e.getMessage()); 630 throw new TransformerConfigurationException (err.toString()); 631 } 632 } 633 634 if (_autoTranslet) { 637 byte[][] bytecodes = null; 638 String transletClassName = getTransletBaseName(source); 639 640 if (_packageName != null) 641 transletClassName = _packageName + "." + transletClassName; 642 643 if (_jarFileName != null) 644 bytecodes = getBytecodesFromJar(source, transletClassName); 645 else 646 bytecodes = getBytecodesFromClasses(source, transletClassName); 647 648 if (bytecodes != null) { 649 if (_debug) { 650 if (_jarFileName != null) 651 System.err.println(new ErrorMsg( 652 ErrorMsg.TRANSFORM_WITH_JAR_STR, transletClassName, _jarFileName)); 653 else 654 System.err.println(new ErrorMsg( 655 ErrorMsg.TRANSFORM_WITH_TRANSLET_STR, transletClassName)); 656 } 657 658 resetTransientAttributes(); 661 662 return new TemplatesImpl(bytecodes, transletClassName, null, _indentNumber, this); 663 } 664 } 665 666 final XSLTC xsltc = new XSLTC(); 668 if (_debug) xsltc.setDebug(true); 669 if (_enableInlining) xsltc.setTemplateInlining(true); 670 xsltc.init(); 671 672 if (_uriResolver != null) { 674 xsltc.setSourceLoader(this); 675 } 676 677 if ((_piParams != null) && (_piParams.get(source) != null)) { 680 PIParamWrapper p = (PIParamWrapper)_piParams.get(source); 682 if (p != null) { 684 xsltc.setPIParameters(p._media, p._title, p._charset); 685 } 686 } 687 688 int outputType = XSLTC.BYTEARRAY_OUTPUT; 690 if (_generateTranslet || _autoTranslet) { 691 xsltc.setClassName(getTransletBaseName(source)); 693 694 if (_destinationDirectory != null) 695 xsltc.setDestDirectory(_destinationDirectory); 696 else { 697 String xslName = getStylesheetFileName(source); 698 if (xslName != null) { 699 File xslFile = new File (xslName); 700 String xslDir = xslFile.getParent(); 701 702 if (xslDir != null) 703 xsltc.setDestDirectory(xslDir); 704 } 705 } 706 707 if (_packageName != null) 708 xsltc.setPackageName(_packageName); 709 710 if (_jarFileName != null) { 711 xsltc.setJarFileName(_jarFileName); 712 outputType = XSLTC.BYTEARRAY_AND_JAR_OUTPUT; 713 } 714 else 715 outputType = XSLTC.BYTEARRAY_AND_FILE_OUTPUT; 716 } 717 718 final InputSource input = Util.getInputSource(xsltc, source); 720 byte[][] bytecodes = xsltc.compile(null, input, outputType); 721 final String transletName = xsltc.getClassName(); 722 723 if ((_generateTranslet || _autoTranslet) 725 && bytecodes != null && _jarFileName != null) { 726 try { 727 xsltc.outputToJar(); 728 } 729 catch (java.io.IOException e) { } 730 } 731 732 resetTransientAttributes(); 735 736 if (_errorListener != this) { 738 try { 739 passWarningsToListener(xsltc.getWarnings()); 740 } 741 catch (TransformerException e) { 742 throw new TransformerConfigurationException (e); 743 } 744 } 745 else { 746 xsltc.printWarnings(); 747 } 748 749 if (bytecodes == null) { 751 752 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR); 753 TransformerConfigurationException exc = new TransformerConfigurationException (err.toString()); 754 755 if (_errorListener != null) { 757 passErrorsToListener(xsltc.getErrors()); 758 759 try { 763 _errorListener.fatalError(exc); 764 } catch (TransformerException te) { 765 } 767 } 768 else { 769 xsltc.printErrors(); 770 } 771 throw exc; 772 } 773 774 return new TemplatesImpl(bytecodes, transletName, 775 xsltc.getOutputProperties(), _indentNumber, this); 776 } 777 778 786 public TemplatesHandler newTemplatesHandler() 787 throws TransformerConfigurationException 788 { 789 final TemplatesHandlerImpl handler = 790 new TemplatesHandlerImpl(_indentNumber, this); 791 if (_uriResolver != null) { 792 handler.setURIResolver(_uriResolver); 793 } 794 return handler; 795 } 796 797 805 public TransformerHandler newTransformerHandler() 806 throws TransformerConfigurationException 807 { 808 final Transformer transformer = newTransformer(); 809 if (_uriResolver != null) { 810 transformer.setURIResolver(_uriResolver); 811 } 812 return new TransformerHandlerImpl((TransformerImpl) transformer); 813 } 814 815 825 public TransformerHandler newTransformerHandler(Source src) 826 throws TransformerConfigurationException 827 { 828 final Transformer transformer = newTransformer(src); 829 if (_uriResolver != null) { 830 transformer.setURIResolver(_uriResolver); 831 } 832 return new TransformerHandlerImpl((TransformerImpl) transformer); 833 } 834 835 845 public TransformerHandler newTransformerHandler(Templates templates) 846 throws TransformerConfigurationException 847 { 848 final Transformer transformer = templates.newTransformer(); 849 final TransformerImpl internal = (TransformerImpl)transformer; 850 return new TransformerHandlerImpl(internal); 851 } 852 853 862 public XMLFilter newXMLFilter(Source src) 863 throws TransformerConfigurationException 864 { 865 Templates templates = newTemplates(src); 866 if (templates == null) return null; 867 return newXMLFilter(templates); 868 } 869 870 879 public XMLFilter newXMLFilter(Templates templates) 880 throws TransformerConfigurationException 881 { 882 try { 883 return new org.apache.xalan.xsltc.trax.TrAXFilter(templates); 884 } 885 catch (TransformerConfigurationException e1) { 886 if (_errorListener != null) { 887 try { 888 _errorListener.fatalError(e1); 889 return null; 890 } 891 catch (TransformerException e2) { 892 new TransformerConfigurationException (e2); 893 } 894 } 895 throw e1; 896 } 897 } 898 899 910 public void error(TransformerException e) 911 throws TransformerException 912 { 913 Throwable wrapped = e.getException(); 914 if (wrapped != null) { 915 System.err.println(new ErrorMsg(ErrorMsg.ERROR_PLUS_WRAPPED_MSG, 916 e.getMessageAndLocation(), 917 wrapped.getMessage())); 918 } else { 919 System.err.println(new ErrorMsg(ErrorMsg.ERROR_MSG, 920 e.getMessageAndLocation())); 921 } 922 throw e; 923 } 924 925 938 public void fatalError(TransformerException e) 939 throws TransformerException 940 { 941 Throwable wrapped = e.getException(); 942 if (wrapped != null) { 943 System.err.println(new ErrorMsg(ErrorMsg.FATAL_ERR_PLUS_WRAPPED_MSG, 944 e.getMessageAndLocation(), 945 wrapped.getMessage())); 946 } else { 947 System.err.println(new ErrorMsg(ErrorMsg.FATAL_ERR_MSG, 948 e.getMessageAndLocation())); 949 } 950 throw e; 951 } 952 953 966 public void warning(TransformerException e) 967 throws TransformerException 968 { 969 Throwable wrapped = e.getException(); 970 if (wrapped != null) { 971 System.err.println(new ErrorMsg(ErrorMsg.WARNING_PLUS_WRAPPED_MSG, 972 e.getMessageAndLocation(), 973 wrapped.getMessage())); 974 } else { 975 System.err.println(new ErrorMsg(ErrorMsg.WARNING_MSG, 976 e.getMessageAndLocation())); 977 } 978 } 979 980 989 public InputSource loadSource(String href, String context, XSLTC xsltc) { 990 try { 991 if (_uriResolver != null) { 992 final Source source = _uriResolver.resolve(href, context); 993 if (source != null) { 994 return Util.getInputSource(xsltc, source); 995 } 996 } 997 } 998 catch (TransformerException e) { 999 } 1001 return null; 1002 } 1003 1004 1007 private void resetTransientAttributes() { 1008 _transletName = DEFAULT_TRANSLET_NAME; 1009 _destinationDirectory = null; 1010 _packageName = null; 1011 _jarFileName = null; 1012 } 1013 1014 1022 private byte[][] getBytecodesFromClasses(Source source, String fullClassName) 1023 { 1024 if (fullClassName == null) 1025 return null; 1026 1027 String xslFileName = getStylesheetFileName(source); 1028 File xslFile = null; 1029 if (xslFileName != null) 1030 xslFile = new File (xslFileName); 1031 1032 final String transletName; 1034 int lastDotIndex = fullClassName.lastIndexOf('.'); 1035 if (lastDotIndex > 0) 1036 transletName = fullClassName.substring(lastDotIndex+1); 1037 else 1038 transletName = fullClassName; 1039 1040 String transletPath = fullClassName.replace('.', '/'); 1042 if (_destinationDirectory != null) { 1043 transletPath = _destinationDirectory + "/" + transletPath + ".class"; 1044 } 1045 else { 1046 if (xslFile != null && xslFile.getParent() != null) 1047 transletPath = xslFile.getParent() + "/" + transletPath + ".class"; 1048 else 1049 transletPath = transletPath + ".class"; 1050 } 1051 1052 File transletFile = new File (transletPath); 1054 if (!transletFile.exists()) 1055 return null; 1056 1057 if (xslFile != null && xslFile.exists()) { 1062 long xslTimestamp = xslFile.lastModified(); 1063 long transletTimestamp = transletFile.lastModified(); 1064 if (transletTimestamp < xslTimestamp) 1065 return null; 1066 } 1067 1068 Vector bytecodes = new Vector (); 1070 int fileLength = (int)transletFile.length(); 1071 if (fileLength > 0) { 1072 FileInputStream input = null; 1073 try { 1074 input = new FileInputStream (transletFile); 1075 } 1076 catch (FileNotFoundException e) { 1077 return null; 1078 } 1079 1080 byte[] bytes = new byte[fileLength]; 1081 try { 1082 readFromInputStream(bytes, input, fileLength); 1083 input.close(); 1084 } 1085 catch (IOException e) { 1086 return null; 1087 } 1088 1089 bytecodes.addElement(bytes); 1090 } 1091 else 1092 return null; 1093 1094 String transletParentDir = transletFile.getParent(); 1096 if (transletParentDir == null) 1097 transletParentDir = System.getProperty("user.dir"); 1098 1099 File transletParentFile = new File (transletParentDir); 1100 1101 final String transletAuxPrefix = transletName + "$"; 1103 File [] auxfiles = transletParentFile.listFiles(new FilenameFilter () { 1104 public boolean accept(File dir, String name) 1105 { 1106 return (name.endsWith(".class") && name.startsWith(transletAuxPrefix)); 1107 } 1108 }); 1109 1110 for (int i = 0; i < auxfiles.length; i++) 1112 { 1113 File auxfile = auxfiles[i]; 1114 int auxlength = (int)auxfile.length(); 1115 if (auxlength > 0) { 1116 FileInputStream auxinput = null; 1117 try { 1118 auxinput = new FileInputStream (auxfile); 1119 } 1120 catch (FileNotFoundException e) { 1121 continue; 1122 } 1123 1124 byte[] bytes = new byte[auxlength]; 1125 1126 try { 1127 readFromInputStream(bytes, auxinput, auxlength); 1128 auxinput.close(); 1129 } 1130 catch (IOException e) { 1131 continue; 1132 } 1133 1134 bytecodes.addElement(bytes); 1135 } 1136 } 1137 1138 final int count = bytecodes.size(); 1140 if ( count > 0) { 1141 final byte[][] result = new byte[count][1]; 1142 for (int i = 0; i < count; i++) { 1143 result[i] = (byte[])bytecodes.elementAt(i); 1144 } 1145 1146 return result; 1147 } 1148 else 1149 return null; 1150 } 1151 1152 1159 private byte[][] getBytecodesFromJar(Source source, String fullClassName) 1160 { 1161 String xslFileName = getStylesheetFileName(source); 1162 File xslFile = null; 1163 if (xslFileName != null) 1164 xslFile = new File (xslFileName); 1165 1166 String jarPath = null; 1168 if (_destinationDirectory != null) 1169 jarPath = _destinationDirectory + "/" + _jarFileName; 1170 else { 1171 if (xslFile != null && xslFile.getParent() != null) 1172 jarPath = xslFile.getParent() + "/" + _jarFileName; 1173 else 1174 jarPath = _jarFileName; 1175 } 1176 1177 File file = new File (jarPath); 1179 if (!file.exists()) 1180 return null; 1181 1182 if (xslFile != null && xslFile.exists()) { 1185 long xslTimestamp = xslFile.lastModified(); 1186 long transletTimestamp = file.lastModified(); 1187 if (transletTimestamp < xslTimestamp) 1188 return null; 1189 } 1190 1191 ZipFile jarFile = null; 1193 try { 1194 jarFile = new ZipFile (file); 1195 } 1196 catch (IOException e) { 1197 return null; 1198 } 1199 1200 String transletPath = fullClassName.replace('.', '/'); 1201 String transletAuxPrefix = transletPath + "$"; 1202 String transletFullName = transletPath + ".class"; 1203 1204 Vector bytecodes = new Vector (); 1205 1206 Enumeration entries = jarFile.entries(); 1209 while (entries.hasMoreElements()) 1210 { 1211 ZipEntry entry = (ZipEntry )entries.nextElement(); 1212 String entryName = entry.getName(); 1213 if (entry.getSize() > 0 && 1214 (entryName.equals(transletFullName) || 1215 (entryName.endsWith(".class") && 1216 entryName.startsWith(transletAuxPrefix)))) 1217 { 1218 try { 1219 InputStream input = jarFile.getInputStream(entry); 1220 int size = (int)entry.getSize(); 1221 byte[] bytes = new byte[size]; 1222 readFromInputStream(bytes, input, size); 1223 input.close(); 1224 bytecodes.addElement(bytes); 1225 } 1226 catch (IOException e) { 1227 return null; 1228 } 1229 } 1230 } 1231 1232 final int count = bytecodes.size(); 1234 if (count > 0) { 1235 final byte[][] result = new byte[count][1]; 1236 for (int i = 0; i < count; i++) { 1237 result[i] = (byte[])bytecodes.elementAt(i); 1238 } 1239 1240 return result; 1241 } 1242 else 1243 return null; 1244 } 1245 1246 1253 private void readFromInputStream(byte[] bytes, InputStream input, int size) 1254 throws IOException 1255 { 1256 int n = 0; 1257 int offset = 0; 1258 int length = size; 1259 while (length > 0 && (n = input.read(bytes, offset, length)) > 0) { 1260 offset = offset + n; 1261 length = length - n; 1262 } 1263 } 1264 1265 1276 private String getTransletBaseName(Source source) 1277 { 1278 String transletBaseName = null; 1279 if (!_transletName.equals(DEFAULT_TRANSLET_NAME)) 1280 return _transletName; 1281 else { 1282 String systemId = source.getSystemId(); 1283 if (systemId != null) { 1284 String baseName = Util.baseName(systemId); 1285 if (baseName != null) { 1286 baseName = Util.noExtName(baseName); 1287 transletBaseName = Util.toJavaName(baseName); 1288 } 1289 } 1290 } 1291 1292 return (transletBaseName != null) ? transletBaseName : DEFAULT_TRANSLET_NAME; 1293 } 1294 1295 1302 private String getStylesheetFileName(Source source) 1303 { 1304 String systemId = source.getSystemId(); 1305 if (systemId != null) { 1306 File file = new File (systemId); 1307 if (file.exists()) 1308 return systemId; 1309 else { 1310 URL url = null; 1311 try { 1312 url = new URL (systemId); 1313 } 1314 catch (MalformedURLException e) { 1315 return null; 1316 } 1317 1318 if ("file".equals(url.getProtocol())) 1319 return url.getFile(); 1320 else 1321 return null; 1322 } 1323 } 1324 else 1325 return null; 1326 } 1327 1328 1331 protected Class getDTMManagerClass() { 1332 return m_DTMManagerClass; 1333 } 1334} 1335 | Popular Tags |