xtppage.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 843 行 · 第 1/2 页
JAVA
843 行
Templates stylesheet = null; try { if (ssName == null) ssName = getStylesheetHref(doc, null); stylesheet = _xslManager.get(ssName, req); } catch (XslParseException e) { JspParseException jspE; if (e.getException() != null) jspE = new JspParseException(e.getException()); else jspE = new JspParseException(e); jspE.setErrorPage(_errorPage); throw jspE; } catch (Exception e) { JspParseException jspE; jspE = new JspParseException(e); jspE.setErrorPage(_errorPage); throw jspE; } ArrayList<String> params = null; if (stylesheet instanceof StylesheetImpl) { StylesheetImpl ss = (StylesheetImpl) stylesheet; params = (ArrayList) ss.getProperty(CauchoStylesheet.GLOBAL_PARAM); } for (int i = 0; params != null && i < params.size(); i++) { String param = params.get(i); if (_paramNames == null) _paramNames = new ArrayList<String>(); if (param.equals("xtp:context_path") || param.equals("xtp:servlet_path")) continue; if (! _paramNames.contains(param)) _paramNames.add(param); } return stylesheet; } /** * Mangles the page name to generate a unique page name. * * @param req the servlet request. * @param res the servlet response. * @param stylesheet the stylesheet. * @param varyName the unique query. * * @return the compiled page. */ private Page compileJspPage(CauchoRequest req, CauchoResponse res, CauchoDocument doc, Templates stylesheet, String varyName) throws IOException, ServletException { // changing paramNames changes the varyName varyName = generateVaryName(req); String className = getClassName(varyName); try { return getJspPage(doc, stylesheet, req, res, className); } catch (TransformerConfigurationException e) { throw new ServletException(e); } catch (JspException e) { throw new ServletException(e); } } /** * Mangles the classname */ private String getClassName(String varyName) { if (varyName == null) return _className; else return _className + JavaCompiler.mangleName("?" + varyName); } /** * Generates a unique string for the variable parameters. The parameters * depend on: * <ul> * <li>The value of caucho.xsl.stylesheet selecting the stylesheet. * <li>The top-level xsl:param variables, which use request parameters. * <li>The request's path-info. * </ul> * * @param req the page request. * * @return a unique string encoding the important variations of the request. */ private String generateVaryName(CauchoRequest req) { CharBuffer cb = CharBuffer.allocate(); String ss = (String) req.getAttribute("caucho.xsl.stylesheet"); if (ss == null && (_paramNames == null || _paramNames.size() == 0)) return null; if (ss != null) { cb.append("ss."); cb.append(ss); } for (int i = 0; _paramNames != null && i < _paramNames.size(); i++) { String name = (String) _paramNames.get(i); String value; if (name.equals("xtp:path_info")) value = req.getPathInfo(); else value = req.getParameter(name); cb.append("."); cb.append(name); if (value != null) { cb.append("."); cb.append(value); } } if (cb.length() == 0) return null; if (cb.length() < 64) return cb.close(); long hash = 37; for (int i = 0; i < cb.length(); i++) hash = 65521 * hash + cb.charAt(i); cb.setLength(32); Base64.encode(cb, hash); return cb.close(); } /** * Compile a JSP page. * * @param doc the parsed Serif page. * @param stylesheet the stylesheet * @param req the servlet request * @param res the servlet response * @param className the className of the generated page * * @return the compiled JspPage */ private Page getJspPage(CauchoDocument doc, Templates stylesheet, CauchoRequest req, CauchoResponse res, String className) throws IOException, ServletException, JspException, TransformerConfigurationException { Path workDir = _jspManager.getClassDir(); String fullClassName = className; Path path = workDir.lookup(fullClassName.replace('.', '/') + ".jsp"); path.getParent().mkdirs(); Properties output = stylesheet.getOutputProperties(); String encoding = (String) output.get(OutputKeys.ENCODING); String mimeType = (String) output.get(OutputKeys.MEDIA_TYPE); String method = (String) output.get(OutputKeys.METHOD); if (method == null || encoding != null) { } else if (method.equals("xml")) encoding = "UTF-8"; javax.xml.transform.Transformer transformer; transformer = stylesheet.newTransformer(); for (int i = 0; _paramNames != null && i < _paramNames.size(); i++) { String param = (String) _paramNames.get(i); transformer.setParameter(param, req.getParameter(param)); } String contextPath = req.getContextPath(); if (contextPath != null && ! contextPath.equals("")) transformer.setParameter("xtp:context_path", contextPath); String servletPath = req.getServletPath(); if (servletPath != null && ! servletPath.equals("")) transformer.setParameter("xtp:servlet_path", servletPath); String pathInfo = req.getPathInfo(); if (pathInfo != null && ! pathInfo.equals("")) transformer.setParameter("xtp:path_info", pathInfo); transformer.setOutputProperty("caucho.jsp", "true"); LineMap lineMap = null; WriteStream os = path.openWrite(); try { if (encoding != null) { os.setEncoding(encoding); if (mimeType == null) mimeType = "text/html"; os.print("<%@ page contentType=\"" + mimeType + "; charset=" + encoding + "\" %>"); } else if (mimeType != null) os.print("<%@ page contentType=\"" + mimeType + "\" %>"); lineMap = writeJspDoc(os, doc, transformer, req, res); } finally { os.close(); } StylesheetImpl ss = null; if (stylesheet instanceof StylesheetImpl) ss = (StylesheetImpl) stylesheet; try { path.setUserPath(_sourcePath.getPath()); boolean cacheable = true; // jspDoc.isCacheable(); ArrayList<PersistentDependency> depends = new ArrayList<PersistentDependency>(); ArrayList<Depend> styleDepends = null; if (ss != null) styleDepends = (ArrayList) ss.getProperty(StylesheetImpl.DEPENDS); for (int i = 0; styleDepends != null && i < styleDepends.size(); i++) { Depend depend = styleDepends.get(i); Depend jspDepend = new Depend(depend.getPath(), depend.getLastModified(), depend.getLength()); jspDepend.setRequireSource(true); if (! depends.contains(jspDepend)) depends.add(jspDepend); } // Fill the page dependency information from the document into // the jsp page. ArrayList<Path> docDepends; docDepends = (ArrayList) doc.getProperty(CauchoDocument.DEPENDS); for (int i = 0; docDepends != null && i < docDepends.size(); i++) { Path depend = docDepends.get(i); Depend jspDepend = new Depend(depend); if (! depends.contains(jspDepend)) depends.add(jspDepend); } // stylesheet cache dependencies are normal dependencies for JSP ArrayList<Path> cacheDepends = null; TransformerImpl xform = null; if (transformer instanceof TransformerImpl) xform = (TransformerImpl) transformer; if (xform != null) cacheDepends = (ArrayList) xform.getProperty(TransformerImpl.CACHE_DEPENDS); for (int i = 0; cacheDepends != null && i < cacheDepends.size(); i++) { Path depend = cacheDepends.get(i); Depend jspDepend = new Depend(depend); if (! depends.contains(jspDepend)) depends.add(jspDepend); } ServletConfig config = null; Page page = _jspManager.createGeneratedPage(path, _uri, className, config, depends); return page; } catch (IOException e) { throw e; } catch (ServletException e) { throw e; } catch (Exception e) { throw new QJspException(e); } } /** * Transform XTP page with the stylesheet to JSP source. * * @param os the output stream to the generated JSP. * @param doc the parsed XTP file. * @param transformed the XSL stylesheet with parameters applied. * @param req the servlet request. * @param res the servlet response. * * @return the line map from the JSP to the original source. */ private LineMap writeJspDoc(WriteStream os, Document doc, javax.xml.transform.Transformer transformer, CauchoRequest req, CauchoResponse res) throws IOException, ServletException { PageContext pageContext; JspFactory factory = JspFactory.getDefaultFactory(); TransformerImpl xform = null; if (transformer instanceof TransformerImpl) xform = (TransformerImpl) transformer; String errorPage = null; if (xform != null) errorPage = (String) xform.getProperty("caucho.error.page"); pageContext = factory.getPageContext(this, req, res, errorPage, false, 8192, // bufferSize, false); // autoFlush); try { if (xform != null) { xform.setProperty("caucho.page.context", pageContext); xform.setProperty("caucho.pwd", Vfs.lookup()); } DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(os); xform.setFeature(TransformerImpl.GENERATE_LOCATION, true); transformer.transform(source, result); if (xform != null) return (LineMap) xform.getProperty(TransformerImpl.LINE_MAP); else return null; } catch (Exception e) { pageContext.handlePageException(e); } finally { factory.releasePageContext(pageContext); } return null; } /** * Returns the stylesheet specified by the page. * * The syntax is: * <pre> * <?xml-stylesheet href='...' media='...'?> * </pre> * * @param doc the XTP document * @param media the http request media * * @return the href of the xml-stylesheet processing-instruction or * "default.xsl" if none is found. */ private String getStylesheetHref(Document doc, String media) throws XPathException { Iterator iter = XPath.select("//processing-instruction('xml-stylesheet')", doc); while (iter.hasNext()) { ProcessingInstruction pi = (ProcessingInstruction) iter.next(); String value = pi.getNodeValue(); String piMedia = XmlUtil.getPIAttribute(value, "media"); if (piMedia == null || piMedia.equals(media)) return XmlUtil.getPIAttribute(value, "href"); } return "default.xsl"; // xslManager.getDefaultStylesheet(); } /** * Returns true if the document varies according to the "media". * (Currently unused.) */ private boolean varyMedia(Document doc) throws XPathException { Iterator iter = XPath.select("//processing-instruction('xml-stylesheet')", doc); while (iter.hasNext()) { ProcessingInstruction pi = (ProcessingInstruction) iter.next(); String value = pi.getNodeValue(); String piMedia = XmlUtil.getPIAttribute(value, "media"); if (piMedia != null) return true; } return false; } public boolean disableLog() { return true; } /** * Returns a printable version of the page object */ public String toString() { return "XtpPage[" + _uri + "]"; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?