1 29 30 package com.caucho.xsl; 31 32 import com.caucho.java.JavaCompiler; 33 import com.caucho.loader.DynamicClassLoader; 34 import com.caucho.loader.EnvironmentLocal; 35 import com.caucho.loader.SimpleLoader; 36 import com.caucho.log.Log; 37 import com.caucho.server.util.CauchoSystem; 38 import com.caucho.util.Base64; 39 import com.caucho.util.CharBuffer; 40 import com.caucho.util.L10N; 41 import com.caucho.util.LruCache; 42 import com.caucho.vfs.Crc64Stream; 43 import com.caucho.vfs.Path; 44 import com.caucho.vfs.ReadStream; 45 import com.caucho.vfs.Vfs; 46 import com.caucho.vfs.WriteStream; 47 import com.caucho.xml.*; 48 import com.caucho.xpath.Expr; 49 import com.caucho.xpath.XPath; 50 51 import org.w3c.dom.Attr ; 52 import org.w3c.dom.Document ; 53 import org.w3c.dom.DocumentType ; 54 import org.w3c.dom.Node ; 55 import org.xml.sax.ContentHandler ; 56 import org.xml.sax.InputSource ; 57 import org.xml.sax.XMLFilter ; 58 import org.xml.sax.XMLReader ; 59 60 import javax.xml.transform.ErrorListener ; 61 import javax.xml.transform.Source ; 62 import javax.xml.transform.Templates ; 63 import javax.xml.transform.TransformerConfigurationException ; 64 import javax.xml.transform.TransformerException ; 65 import javax.xml.transform.URIResolver ; 66 import javax.xml.transform.dom.DOMResult ; 67 import javax.xml.transform.dom.DOMSource ; 68 import javax.xml.transform.sax.SAXResult ; 69 import javax.xml.transform.sax.SAXSource ; 70 import javax.xml.transform.sax.SAXTransformerFactory ; 71 import javax.xml.transform.sax.TemplatesHandler ; 72 import javax.xml.transform.sax.TransformerHandler ; 73 import javax.xml.transform.stream.StreamResult ; 74 import javax.xml.transform.stream.StreamSource ; 75 import java.io.IOException ; 76 import java.io.InputStream ; 77 import java.io.OutputStream ; 78 import java.io.Reader ; 79 import java.lang.ref.SoftReference ; 80 import java.util.Iterator ; 81 import java.util.Random ; 82 import java.util.logging.Level ; 83 import java.util.logging.Logger ; 84 85 88 abstract public class AbstractStylesheetFactory 89 extends SAXTransformerFactory { 90 static final Logger log = Log.open(AbstractStylesheetFactory.class); 91 static final L10N L = new L10N(AbstractStylesheetFactory.class); 92 93 private static 94 EnvironmentLocal<LruCache<String ,SoftReference <StylesheetImpl>>> _stylesheetCache = 95 new EnvironmentLocal<LruCache<String ,SoftReference <StylesheetImpl>>>(); 96 97 private URIResolver _uriResolver; 98 private ErrorListener _errorListener; 99 100 private String _systemId; 101 102 private Path _workPath; 103 private Path _stylePath; 104 private ClassLoader _loader; 105 private String _className; 106 private boolean _isAutoCompile = true; 107 private boolean _loadPrecompiledStylesheet = true; 108 109 protected AbstractStylesheetFactory() 110 { 111 } 112 113 118 public Object getAttribute(String name) 119 { 120 return null; 121 } 122 123 129 public void setAttribute(String name, Object value) 130 { 131 } 132 133 138 public boolean getFeature(String name) 139 { 140 if (name.equals(SAXTransformerFactory.FEATURE) || 141 name.equals(SAXTransformerFactory.FEATURE_XMLFILTER) || 142 name.equals(DOMResult.FEATURE) || 143 name.equals(DOMSource.FEATURE) || 144 name.equals(SAXResult.FEATURE) || 145 name.equals(SAXSource.FEATURE) || 146 name.equals(StreamResult.FEATURE) || 147 name.equals(StreamSource.FEATURE)) 148 return true; 149 else 150 return false; 151 } 152 153 159 public void setFeature(String name, boolean value) 160 { 161 } 162 163 166 public URIResolver getURIResolver() 167 { 168 return _uriResolver; 169 } 170 171 174 public void setURIResolver(URIResolver uriResolver) 175 { 176 _uriResolver = uriResolver; 177 } 178 179 182 public ErrorListener getErrorListener() 183 { 184 return _errorListener; 185 } 186 187 190 public void setErrorListener(ErrorListener errorListener) 191 { 192 _errorListener = errorListener; 193 } 194 195 public String getSystemId() 196 { 197 return _systemId; 198 } 199 200 public void setSystemId(String systemId) 201 { 202 _systemId = systemId; 203 } 204 205 211 public void setStylePath(Path path) 212 { 213 _stylePath = path; 214 } 215 216 219 public Path getStylePath() 220 { 221 if (_stylePath != null) 222 return _stylePath; 223 else 224 return getSearchPath(); 225 } 226 227 233 public void setSearchPath(Path path) 234 { 235 _stylePath = path; 236 } 237 238 241 public Path getSearchPath() 242 { 243 if (_stylePath != null) 244 return _stylePath; 245 else 246 return Vfs.getPwd(); 247 } 248 249 252 public void setWorkPath(Path path) 253 { 254 _workPath = path; 255 } 256 257 260 public Path getWorkPath() 261 { 262 if (_workPath != null) 263 return _workPath; 264 else 265 return CauchoSystem.getWorkPath(); 266 } 267 268 public void setClassName(String className) 269 { 270 _className = className; 271 } 272 273 public String getClassName() 274 { 275 return _className; 276 } 277 278 283 public void setClassLoader(ClassLoader loader) 284 { 285 _loader = loader; 286 } 287 288 291 public ClassLoader getClassLoader() 292 { 293 return _loader; 294 } 295 296 299 public boolean getLoadPrecompiledStylesheet() 300 { 301 return _loadPrecompiledStylesheet; 302 } 303 304 307 public void setLoadPrecompiledStylesheet(boolean preload) 308 { 309 _loadPrecompiledStylesheet = preload; 310 } 311 312 315 public boolean isAutoCompile() 316 { 317 return _isAutoCompile; 318 } 319 320 323 public void setAutoCompile(boolean autoCompile) 324 { 325 _isAutoCompile = autoCompile; 326 } 327 328 337 public Source getAssociatedStylesheet(Source source, 338 String media, 339 String title, 340 String charset) 341 throws TransformerConfigurationException 342 { 343 try { 344 XmlStylesheetReader reader = new XmlStylesheetReader(); 345 346 parseSAX(source, reader); 347 348 String href = reader.getAssociatedStylesheet(media, title, charset); 349 350 if (href == null) 351 return null; 352 353 String base = source.getSystemId(); 354 355 return getSource(href, base); 356 } catch (Exception e) { 357 throw new TransformerConfigurationException (e); 358 } 359 } 360 361 364 private Source getSource(String href, String base) 365 throws Exception 366 { 367 Path subpath; 368 369 if (href == null) 370 href = ""; 371 if (base == null) 372 base = "/"; 373 374 if (_uriResolver != null) { 375 if (href.startsWith("/") || base.equals("/")) 376 subpath = getSearchPath().lookup(href); 377 else { 378 subpath = getSearchPath().lookup(base).getParent().lookup(href); 379 } 380 381 Source source = _uriResolver.resolve(href, base); 382 383 if (source != null) { 384 if (source.getSystemId() == null) 385 source.setSystemId(subpath.getURL()); 386 387 return source; 388 } 389 } 390 391 if (href.startsWith("/") || base.equals("/")) 392 subpath = getSearchPath().lookup(href); 393 else { 394 if (base.startsWith("file:")) 395 base = base.substring(5); 396 397 subpath = getSearchPath().lookup(base).getParent().lookup(href); 398 } 399 400 return new StreamSource (subpath.getURL()); 401 } 402 403 private void parseSAX(Source source, ContentHandler handler) 404 throws TransformerConfigurationException 405 { 406 try { 407 if (source instanceof SAXSource ) { 408 SAXSource saxSource = (SAXSource ) source; 409 410 XMLReader reader = saxSource.getXMLReader(); 411 InputSource inputSource = saxSource.getInputSource(); 412 413 reader.setContentHandler(handler); 414 415 reader.parse(inputSource); 416 } 417 else if (source instanceof StreamSource ) { 418 419 XmlParser parser = new Xml(); 420 421 parser.setContentHandler(handler); 422 423 ReadStream rs = openPath(source); 424 try { 425 parser.parse(rs); 426 } finally { 427 rs.close(); 428 } 429 } 430 else if (source instanceof DOMSource ) { 431 DOMSource domSource = (DOMSource ) source; 432 433 Node node = domSource.getNode(); 434 435 XmlUtil.toSAX(node, handler); 436 } 437 } catch (Exception e) { 438 throw new TransformerConfigurationException (e); 439 } 440 } 441 442 449 public javax.xml.transform.Transformer newTransformer(Source source) 450 throws TransformerConfigurationException 451 { 452 Templates templates = newTemplates(source); 453 454 return templates.newTransformer(); 455 } 456 457 462 public javax.xml.transform.Transformer newTransformer() 463 throws TransformerConfigurationException 464 { 465 return new TransformerImpl(new IdentityStylesheet()); 466 } 467 468 471 public StylesheetImpl newStylesheet(Document xsl) 472 throws Exception 473 { 474 return (StylesheetImpl) generate(xsl, null); 475 } 476 477 480 public StylesheetImpl newStylesheet(Reader reader) 481 throws Exception 482 { 483 ReadStream rs = Vfs.openRead(reader); 484 485 return (StylesheetImpl) generate(parseXSL(rs), rs.getPath()); 486 } 487 488 491 public StylesheetImpl newStylesheet(InputStream is) 492 throws Exception 493 { 494 ReadStream rs = Vfs.openRead(is); 495 496 return (StylesheetImpl) generate(parseXSL(rs), rs.getPath()); 497 } 498 499 504 public StylesheetImpl newStylesheet(String systemId) 505 throws Exception 506 { 507 StylesheetImpl stylesheet = loadPrecompiledStylesheet(systemId, systemId); 508 509 if (stylesheet != null) 510 return stylesheet; 511 512 synchronized (AbstractStylesheetFactory.class) { 513 stylesheet = loadPrecompiledStylesheet(systemId, systemId); 514 515 if (stylesheet != null) 516 return stylesheet; 517 518 ReadStream is; 519 520 if (_stylePath != null) 521 is = _stylePath.lookup(systemId).openRead(); 522 else 523 is = Vfs.lookup(systemId).openRead(); 524 525 try { 526 return newStylesheet(is); 527 } finally { 528 if (is != null) 529 is.close(); 530 } 531 } 532 } 533 534 public StylesheetImpl newStylesheet(Path path) 535 throws Exception 536 { 537 StylesheetImpl stylesheet = loadPrecompiledStylesheet(path.getFullPath(), 538 path.getUserPath()); 539 540 if (stylesheet != null) 541 return stylesheet; 542 543 synchronized (AbstractStylesheetFactory.class) { 544 stylesheet = loadPrecompiledStylesheet(path.getFullPath(), 545 path.getUserPath()); 546 547 if (stylesheet != null) 548 return stylesheet; 549 550 Path oldStylePath = _stylePath; 551 552 if (_stylePath == null) 553 _stylePath = path.getParent(); 554 555 InputStream is = null; 556 557 try { 558 is = path.openRead(); 559 return newStylesheet(is); 560 } finally { 561 _stylePath = oldStylePath; 562 if (is != null) 563 is.close(); 564 } 565 } 566 } 567 568 575 public Templates newTemplates(Source source) 576 throws TransformerConfigurationException 577 { 578 String systemId = source.getSystemId(); 579 580 try { 581 if (systemId != null) { 582 StylesheetImpl stylesheet = loadPrecompiledStylesheet(systemId, 583 systemId); 584 585 if (stylesheet != null) 586 return stylesheet; 587 } 588 589 if (source instanceof DOMSource ) { 590 Node node = ((DOMSource ) source).getNode(); 591 592 return generateFromNode(node, systemId); 593 } 594 else if (source instanceof SAXSource ) { 595 SAXSource saxSource = (SAXSource ) source; 596 XMLReader reader = saxSource.getXMLReader(); 597 InputSource inputSource = saxSource.getInputSource(); 598 599 Document doc = new QDocument(); 600 DOMBuilder builder = new DOMBuilder(); 601 builder.init(doc); 602 reader.setContentHandler(builder); 603 604 reader.parse(inputSource); 605 606 return generateFromNode(doc, systemId); 607 } 608 609 ReadStream rs = openPath(source); 610 try { 611 Path path = rs.getPath(); 612 613 Document doc = parseXSL(rs); 614 615 if (systemId != null) { 616 String mangledName = getMangledName(systemId); 617 Path genPath = getWorkPath().lookup(mangledName); 618 619 genPath.setUserPath(systemId); 620 621 return generate(doc, genPath); 622 } 623 else 624 return generateFromNode(doc, null); 625 } finally { 626 if (rs != null) 627 rs.close(); 628 } 629 } catch (TransformerConfigurationException e) { 630 throw e; 631 } catch (Exception e) { 632 throw new XslParseException(e); 633 } 634 } 635 636 private Templates generateFromNode(Node node, String systemId) 637 throws IOException , TransformerConfigurationException 638 { 639 Path tempPath = writeTempFile(node); 640 641 String tempId = tempPath.getTail(); 642 643 StylesheetImpl stylesheet = loadPrecompiledStylesheet(tempId, 644 tempId, 645 false); 646 647 if (systemId != null) 648 tempPath.setUserPath(systemId); 649 650 if (stylesheet != null) 651 return stylesheet; 652 653 return generate(node, tempPath); 654 } 655 656 private Path writeTempFile(Node node) 657 throws IOException 658 { 659 Path workDir = CauchoSystem.getWorkPath().lookup("_xsl"); 660 workDir.mkdirs(); 661 662 664 WriteStream os = Vfs.lookup("null:").openWrite(); 665 Crc64Stream crcStream = new Crc64Stream(os.getSource()); 666 os.init(crcStream); 667 try { 668 XmlPrinter printer = new XmlPrinter(os); 669 670 printer.printNode(node); 671 } finally { 672 os.close(); 673 } 674 675 long crc = crcStream.getCRC(); 676 CharBuffer cb = new CharBuffer(); 677 Base64.encode(cb, crc); 678 679 String crcValue = cb.toString().replace('/', '-'); 680 681 Path xslPath = workDir.lookup(crcValue + ".xsl"); 682 683 685 return xslPath; 686 } 687 688 691 public TransformerHandler newTransformerHandler() 692 throws TransformerConfigurationException 693 { 694 return newTransformerHandler(new StylesheetImpl()); 695 } 696 697 700 public TransformerHandler newTransformerHandler(Source source) 701 throws TransformerConfigurationException 702 { 703 return newTransformerHandler(newTemplates(source)); 704 } 705 706 709 public TransformerHandler newTransformerHandler(Templates templates) 710 throws TransformerConfigurationException 711 { 712 return new TransformerHandlerImpl(templates.newTransformer()); 713 } 714 715 720 public TemplatesHandler newTemplatesHandler() 721 throws TransformerConfigurationException 722 { 723 return new TemplatesHandlerImpl(this); 724 } 725 726 731 public XMLFilter newXMLFilter(Source source) 732 throws TransformerConfigurationException 733 { 734 Templates templates = newTemplates(source); 735 736 return newXMLFilter(templates); 737 } 738 739 744 public XMLFilter newXMLFilter(Templates templates) 745 throws TransformerConfigurationException 746 { 747 return new SAXFilterImpl((TransformerImpl) templates.newTransformer()); 748 } 749 750 753 protected Node parseStylesheet(Source source) 754 throws TransformerConfigurationException 755 { 756 if (source instanceof DOMSource ) 757 return ((DOMSource ) source).getNode(); 758 else if (source instanceof StreamSource ) { 759 InputStream is = ((StreamSource ) source).getInputStream(); 760 ReadStream rs = null; 761 762 try { 763 rs = Vfs.openRead(is); 764 return parseXSL(rs); 765 } catch (Exception e) { 766 throw new TransformerConfigurationException (e); 767 } finally { 768 if (rs != null) 769 rs.close(); 770 } 771 } 772 else 773 return null; 774 } 775 776 783 public javax.xml.transform.Templates newTemplates(Node node) 784 throws TransformerConfigurationException 785 { 786 Document doc = node.getOwnerDocument(); 787 if (node instanceof Document ) 788 doc = (Document ) node; 789 790 DocumentType dtd = doc.getDoctype(); 791 792 if (dtd != null && dtd.getSystemId() != null) 793 return generate(node, getSearchPath().lookup(dtd.getSystemId())); 794 else if (doc instanceof CauchoDocument) { 795 String systemId = ((CauchoDocument) doc).getFilename(); 796 797 return generate(node, getSearchPath().lookup(systemId)); 798 } 799 else 800 return generate(node, null); 801 } 802 803 810 public javax.xml.transform.Templates newTemplates(String systemId) 811 throws TransformerConfigurationException 812 { 813 StylesheetImpl stylesheet = loadPrecompiledStylesheet(systemId, systemId); 814 815 if (stylesheet != null) 816 return stylesheet; 817 818 else if (systemId == null) 819 return generate(new QDocument(), null); 820 821 Path path = getSearchPath().lookup(systemId); 822 823 try { 824 ReadStream is = path.openRead(); 825 Document doc; 826 try { 827 doc = parseXSL(is); 828 } finally { 829 is.close(); 830 } 831 832 return generate(doc, path); 833 } catch (TransformerConfigurationException e) { 834 throw e; 835 } catch (Exception e) { 836 throw new TransformerConfigurationExceptionWrapper(e); 837 } 838 } 839 840 841 842 845 ReadStream openPath(String href, String base) 846 throws TransformerException , IOException 847 { 848 if (_uriResolver != null) { 849 Source source = _uriResolver.resolve(href, base); 850 851 if (source != null) 852 return openPath(source); 853 } 854 855 if (href.startsWith("/") || base.equals("/")) 856 return getSearchPath().lookup(href).openRead(); 857 else { 858 Path path = getSearchPath().lookup(base).getParent().lookup(href); 859 860 if (path.exists()) 861 return path.openRead(); 862 else 863 return getSearchPath().lookup(href).openRead(); 864 } 865 } 866 867 870 ReadStream openPath(Source source) 871 throws TransformerException , IOException 872 { 873 String systemId = source.getSystemId(); 874 875 Path path; 876 if (systemId != null) 877 path = getSearchPath().lookup(systemId); 878 else 879 path = getSearchPath().lookup("anonymous.xsl"); 880 881 if (source instanceof StreamSource ) { 882 StreamSource stream = (StreamSource ) source; 883 884 InputStream is = stream.getInputStream(); 885 886 if (is instanceof ReadStream) { 887 ReadStream rs = (ReadStream) is; 888 889 rs.setPath(path); 890 891 return rs; 892 } 893 else if (is != null) { 894 ReadStream rs = Vfs.openRead(is); 895 rs.setPath(path); 896 897 return rs; 898 } 899 900 Reader reader = stream.getReader(); 901 902 if (reader != null) { 903 ReadStream rs = Vfs.openRead(reader); 904 rs.setPath(path); 905 906 return rs; 907 } 908 } 909 910 if (systemId != null) 911 return getSearchPath().lookup(systemId).openRead(); 912 913 throw new TransformerException ("bad source " + source); 914 } 915 916 Path lookupPath(String base, String href) 917 throws TransformerException 918 { 919 if (_uriResolver != null) { 920 Source source = _uriResolver.resolve(href, base); 921 922 if (source != null) { 923 String systemId = source.getSystemId(); 924 925 if (systemId != null) 926 return getSearchPath().lookup(systemId); 927 } 928 } 929 930 return getSearchPath().lookup(base).lookup(href); 931 } 932 933 940 public javax.xml.transform.Transformer newTransformer(Document xsl) 941 throws TransformerConfigurationException 942 { 943 return newTemplates(xsl).newTransformer(); 944 } 945 946 953 public void transform(Document xsl, Node xml, OutputStream out) 954 throws Exception 955 { 956 TransformerImpl transformer = (TransformerImpl) newTransformer(xsl); 957 958 transformer.transform(xml, out); 959 } 960 961 968 public void transform(String xsl, Node xml, OutputStream out) 969 throws Exception 970 { 971 TransformerImpl transformer; 972 973 transformer = (TransformerImpl) newTemplates(xsl).newTransformer(); 974 975 transformer.transform(xml, out); 976 } 977 978 983 abstract protected Document parseXSL(ReadStream rs) 984 throws TransformerConfigurationException ; 985 986 992 Templates generate(Node xsl, Path path) 993 throws TransformerConfigurationException 994 { 995 log.fine("Generating XSL from " + path); 996 997 synchronized (AbstractStylesheetFactory.class) { 1002 Generator gen = null; 1003 1004 try { 1005 if (path == null && xsl != null) { 1006 Document doc = xsl.getOwnerDocument(); 1007 if (doc == null && xsl instanceof Document ) 1008 doc = (Document ) xsl; 1009 1010 DocumentType dtd = doc.getDoctype(); 1011 String systemId = null; 1012 if (dtd != null) 1013 systemId = dtd.getSystemId(); 1014 1015 if (systemId != null) 1016 path = getStylePath().lookup(systemId); 1017 } 1018 1019 if (path == null && xsl instanceof CauchoNode) { 1020 String filename = ((CauchoNode) xsl).getFilename(); 1021 if (filename != null) 1022 path = getStylePath().lookup(filename); 1023 } 1024 1025 if (path == null) 1026 path = getStylePath().lookup("anonymous.xsl"); 1027 1028 Path stylePath = path.getParent(); 1029 1030 Expr expr = XPath.parseExpr("//xtp:directive.page/@language"); 1031 String language = expr.evalString(xsl); 1032 1033 String userName = path.getUserPath(); 1034 String mangledName = getMangledName(userName); 1035 1036 String encoding = XPath.evalString("//xsl:output/@encoding", xsl); 1037 if (encoding != null && encoding.equals("")) 1038 encoding = null; 1039 1040 if (language == null || language.equals("") || language.equals("java")) { 1041 language = "java"; 1042 gen = new JavaGenerator(this, mangledName, encoding); 1043 } 1044 else 1045 throw new XslParseException(L.l("unsupported language `{0}'", 1046 language)); 1047 1048 gen.setPath(path); 1049 1050 Iterator iter = XPath.select("//xtp:directive.page/@*", xsl); 1051 while (iter.hasNext()) { 1052 Attr attr = (Attr ) iter.next(); 1053 String name = attr.getNodeName(); 1054 String value = attr.getNodeValue(); 1055 1056 if (name.equals("errorPage")) 1057 gen.setErrorPage(value); 1058 else if (name.equals("import")) 1059 gen.addImport(value); 1060 else if (name.equals("contentType")) 1061 gen.setContentType(value); 1062 else if (name.equals("language")) { 1063 if (! language.equalsIgnoreCase(value)) 1064 throw new XslParseException(L.l("mismatched language `{0}'", 1065 value)); 1066 } 1067 else if (name.equals("xml:space")) { 1068 } 1069 else 1070 throw new XslParseException(L.l("unknown directive `{0}'", 1071 name)); 1072 } 1073 1074 StylesheetImpl stylesheet = gen.generate(xsl); 1075 gen = null; 1076 stylesheet.init(path); 1077 stylesheet.setURIResolver(_uriResolver); 1079 1080 return stylesheet; 1081 } catch (TransformerConfigurationException e) { 1082 throw e; 1083 } catch (Exception e) { 1084 throw new XslParseException(e); 1085 } finally { 1086 try { 1087 if (gen != null) 1088 gen.close(); 1089 } catch (IOException e) { 1090 } 1091 } 1092 } 1093 } 1094 1095 1103 private String getMangledName(String userName) 1104 { 1105 String name = null; 1106 1107 if (userName == null || userName.equals("anonymous.xsl") || 1108 userName.equals("string") || userName.equals("stream")) { 1109 userName = "x" + (new Random ().nextInt() & 0x3ff) + ".xsl"; 1110 } 1111 1112 if (getClassName() == null) 1113 name = userName; 1114 else 1115 name = getClassName(); 1116 1117 if (name.startsWith("/")) 1118 name = "xsl" + name; 1119 else 1120 name = "xsl/" + name; 1121 1122 return JavaCompiler.mangleName(name); 1123 } 1124 1125 1132 StylesheetImpl loadPrecompiledStylesheet(String systemId, 1133 String userId) 1134 { 1135 return loadPrecompiledStylesheet(systemId, userId, _isAutoCompile); 1136 } 1137 1138 1145 StylesheetImpl loadPrecompiledStylesheet(String systemId, 1146 String userId, 1147 boolean checkModified) 1148 { 1149 if (! _loadPrecompiledStylesheet) 1150 return null; 1151 1152 try { 1153 StylesheetImpl stylesheet = loadStylesheet(systemId, 1155 getMangledName(userId)); 1156 1157 if (stylesheet == null) 1158 return null; 1159 1160 stylesheet.setURIResolver(_uriResolver); 1161 if (! checkModified || ! stylesheet.isModified()) { 1163 stylesheet.setURIResolver(_uriResolver); 1164 return stylesheet; 1165 } 1166 } catch (Throwable e) { 1167 log.log(Level.FINER, e.toString(), e); 1168 } 1169 1170 return null; 1171 } 1172 1173 1178 protected StylesheetImpl loadStylesheet(String systemId, String className) 1179 throws Exception 1180 { 1181 LruCache<String ,SoftReference <StylesheetImpl>> cache; 1182 1183 ClassLoader parentLoader = Thread.currentThread().getContextClassLoader(); 1184 1185 cache = _stylesheetCache.getLevel(parentLoader); 1186 1187 if (cache == null) { 1188 cache = new LruCache<String ,SoftReference <StylesheetImpl>>(256); 1189 _stylesheetCache.set(cache, parentLoader); 1190 } 1191 1192 SoftReference <StylesheetImpl> stylesheetRef; 1193 1194 stylesheetRef = cache.get(className); 1195 1196 StylesheetImpl stylesheet = null; 1197 1198 if (stylesheetRef != null) 1199 stylesheet = stylesheetRef.get(); 1200 1201 try { 1202 if (stylesheet != null && ! stylesheet.isModified()) 1203 return stylesheet; 1204 } catch (Throwable e) { 1205 log.log(Level.FINER, e.toString(), e); 1206 } 1207 1208 Path classPath = getWorkPath().lookup(className.replace('.', '/') + ".class"); 1209 if (! classPath.canRead()) 1210 throw new ClassNotFoundException ("can't find compiled XSL `" + className + "'"); 1211 1212 DynamicClassLoader loader; 1213 loader = SimpleLoader.create(parentLoader, getWorkPath(), className); 1214 1215 Class cl = null; 1216 1217 try { 1219 cl = CauchoSystem.loadClass(className, false, loader); 1220 } catch (Error e) { 1221 try { 1222 classPath.remove(); 1223 } catch (IOException e1) { 1224 log.log(Level.FINE, e1.toString(), e1); 1225 } 1226 1227 throw e; 1228 } 1229 1230 stylesheet = (StylesheetImpl) cl.newInstance(); 1231 Path path; 1232 1233 path = getSearchPath().lookup("").lookup(systemId); 1234 1242 1243 stylesheet.init(getStylePath()); 1245 stylesheet.setURIResolver(_uriResolver); 1246 1247 stylesheetRef = new SoftReference <StylesheetImpl>(stylesheet); 1248 cache.put(className, stylesheetRef); 1249 1250 return stylesheet; 1251 } 1252} 1253 1254 | Popular Tags |