abstractstylesheetfactory.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,258 行 · 第 1/3 页
JAVA
1,258 行
} } /** * Opens a relative path. */ ReadStream openPath(String href, String base) throws TransformerException, IOException { if (_uriResolver != null) { Source source = _uriResolver.resolve(href, base); if (source != null) return openPath(source); } if (href.startsWith("/") || base.equals("/")) return getSearchPath().lookup(href).openRead(); else { Path path = getSearchPath().lookup(base).getParent().lookup(href); if (path.exists()) return path.openRead(); else return getSearchPath().lookup(href).openRead(); } } /** * Opens a path based on a Source. */ ReadStream openPath(Source source) throws TransformerException, IOException { String systemId = source.getSystemId(); Path path; if (systemId != null) path = getSearchPath().lookup(systemId); else path = getSearchPath().lookup("anonymous.xsl"); if (source instanceof StreamSource) { StreamSource stream = (StreamSource) source; InputStream is = stream.getInputStream(); if (is instanceof ReadStream) { ReadStream rs = (ReadStream) is; rs.setPath(path); return rs; } else if (is != null) { ReadStream rs = Vfs.openRead(is); rs.setPath(path); return rs; } Reader reader = stream.getReader(); if (reader != null) { ReadStream rs = Vfs.openRead(reader); rs.setPath(path); return rs; } } if (systemId != null) return getSearchPath().lookup(systemId).openRead(); throw new TransformerException("bad source " + source); } Path lookupPath(String base, String href) throws TransformerException { if (_uriResolver != null) { Source source = _uriResolver.resolve(href, base); if (source != null) { String systemId = source.getSystemId(); if (systemId != null) return getSearchPath().lookup(systemId); } } return getSearchPath().lookup(base).lookup(href); } /** * Convenience class to create a transformer instance. * * @param xsl DOM source for the stylesheet. * * @return a transformer instance. */ public javax.xml.transform.Transformer newTransformer(Document xsl) throws TransformerConfigurationException { return newTemplates(xsl).newTransformer(); } /** * Convenience class to transform a node. * * @param xsl DOM containing the parsed xsl. * @param xml DOM document node. * @param out output stream destination. */ public void transform(Document xsl, Node xml, OutputStream out) throws Exception { TransformerImpl transformer = (TransformerImpl) newTransformer(xsl); transformer.transform(xml, out); } /** * Convenience class to transform a node. * * @param xsl path name to the xsl file. * @param xml dom source document. * @param out output stream destination. */ public void transform(String xsl, Node xml, OutputStream out) throws Exception { TransformerImpl transformer; transformer = (TransformerImpl) newTemplates(xsl).newTransformer(); transformer.transform(xml, out); } /** * Parses the XSL into a DOM document. * * @param rs the input stream. */ abstract protected Document parseXSL(ReadStream rs) throws TransformerConfigurationException; /** * Generates a compiled stylesheet from a parsed XSL document. * * @param xsl the parsed xsl document. * @param path the path to the document. */ Templates generate(Node xsl, Path path) throws TransformerConfigurationException { log.fine("Generating XSL from " + path); // The code generation needs a static lock because the // application might have a separate factory object // for each thread. The static lock protects the code generation // from overwriting its own code. synchronized (AbstractStylesheetFactory.class) { Generator gen = null; try { if (path == null && xsl != null) { Document doc = xsl.getOwnerDocument(); if (doc == null && xsl instanceof Document) doc = (Document) xsl; DocumentType dtd = doc.getDoctype(); String systemId = null; if (dtd != null) systemId = dtd.getSystemId(); if (systemId != null) path = getStylePath().lookup(systemId); } if (path == null && xsl instanceof CauchoNode) { String filename = ((CauchoNode) xsl).getFilename(); if (filename != null) path = getStylePath().lookup(filename); } if (path == null) path = getStylePath().lookup("anonymous.xsl"); Path stylePath = path.getParent(); Expr expr = XPath.parseExpr("//xtp:directive.page/@language"); String language = expr.evalString(xsl); String userName = path.getUserPath(); String mangledName = getMangledName(userName); String encoding = XPath.evalString("//xsl:output/@encoding", xsl); if (encoding != null && encoding.equals("")) encoding = null; if (language == null || language.equals("") || language.equals("java")) { language = "java"; gen = new JavaGenerator(this, mangledName, encoding); } else throw new XslParseException(L.l("unsupported language `{0}'", language)); gen.setPath(path); Iterator iter = XPath.select("//xtp:directive.page/@*", xsl); while (iter.hasNext()) { Attr attr = (Attr) iter.next(); String name = attr.getNodeName(); String value = attr.getNodeValue(); if (name.equals("errorPage")) gen.setErrorPage(value); else if (name.equals("import")) gen.addImport(value); else if (name.equals("contentType")) gen.setContentType(value); else if (name.equals("language")) { if (! language.equalsIgnoreCase(value)) throw new XslParseException(L.l("mismatched language `{0}'", value)); } else if (name.equals("xml:space")) { } else throw new XslParseException(L.l("unknown directive `{0}'", name)); } StylesheetImpl stylesheet = gen.generate(xsl); gen = null; stylesheet.init(path); // XXX: why was this here? stylesheet.init(getStylePath()); stylesheet.setURIResolver(_uriResolver); return stylesheet; } catch (TransformerConfigurationException e) { throw e; } catch (Exception e) { throw new XslParseException(e); } finally { try { if (gen != null) gen.close(); } catch (IOException e) { } } } } /** * Returns the mangled classname for the stylesheet. If getClassName() * is not null, it will be used as the mangled name. * * @param userName the user specified name for the stylesheet. * * @return a valid Java classname for the generated stylesheet. */ private String getMangledName(String userName) { String name = null; if (userName == null || userName.equals("anonymous.xsl") || userName.equals("string") || userName.equals("stream")) { userName = "x" + (new Random().nextInt() & 0x3ff) + ".xsl"; } if (getClassName() == null) name = userName; else name = getClassName(); if (name.startsWith("/")) name = "xsl" + name; else name = "xsl/" + name; return JavaCompiler.mangleName(name); } /** * Returns existing compiled Templates if it exists. * * @param systemId source path for the stylesheet. * * @return a compiled stylesheet */ StylesheetImpl loadPrecompiledStylesheet(String systemId, String userId) { return loadPrecompiledStylesheet(systemId, userId, _isAutoCompile); } /** * Returns existing compiled Templates if it exists. * * @param systemId source path for the stylesheet. * * @return a compiled stylesheet */ StylesheetImpl loadPrecompiledStylesheet(String systemId, String userId, boolean checkModified) { if (! _loadPrecompiledStylesheet) return null; try { // look for compiled template base on SystemID StylesheetImpl stylesheet = loadStylesheet(systemId, getMangledName(userId)); if (stylesheet == null) return null; stylesheet.setURIResolver(_uriResolver); // and check if it's modified or not if (! checkModified || ! stylesheet.isModified()) { stylesheet.setURIResolver(_uriResolver); return stylesheet; } } catch (Throwable e) { log.log(Level.FINER, e.toString(), e); } return null; } /** * Loads the compiled stylesheet .class file * * @param className the mangled classname for the stylesheet */ protected StylesheetImpl loadStylesheet(String systemId, String className) throws Exception { LruCache<String,SoftReference<StylesheetImpl>> cache; ClassLoader parentLoader = Thread.currentThread().getContextClassLoader(); cache = _stylesheetCache.getLevel(parentLoader); if (cache == null) { cache = new LruCache<String,SoftReference<StylesheetImpl>>(256); _stylesheetCache.set(cache, parentLoader); } SoftReference<StylesheetImpl> stylesheetRef; stylesheetRef = cache.get(className); StylesheetImpl stylesheet = null; if (stylesheetRef != null) stylesheet = stylesheetRef.get(); try { if (stylesheet != null && ! stylesheet.isModified()) return stylesheet; } catch (Throwable e) { log.log(Level.FINER, e.toString(), e); } Path classPath = getWorkPath().lookup(className.replace('.', '/') + ".class"); if (! classPath.canRead()) throw new ClassNotFoundException("can't find compiled XSL `" + className + "'"); DynamicClassLoader loader; loader = SimpleLoader.create(parentLoader, getWorkPath(), className); Class cl = null; // If the loading fails, remove the class because it may be corrupted try { cl = CauchoSystem.loadClass(className, false, loader); } catch (Error e) { try { classPath.remove(); } catch (IOException e1) { log.log(Level.FINE, e1.toString(), e1); } throw e; } stylesheet = (StylesheetImpl) cl.newInstance(); Path path; path = getSearchPath().lookup("").lookup(systemId); /* try { } catch (TransformerException e) { log.log(Level.FINE, e.toString(), e); path = Vfs.lookup(systemId); } */ // stylesheet.init(path); stylesheet.init(getStylePath()); stylesheet.setURIResolver(_uriResolver); stylesheetRef = new SoftReference<StylesheetImpl>(stylesheet); cache.put(className, stylesheetRef); return stylesheet; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?