001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsp;
030:
031: import com.caucho.log.Log;
032: import com.caucho.server.webapp.WebApp;
033: import com.caucho.vfs.Path;
034: import com.caucho.vfs.PersistentDependency;
035:
036: import javax.servlet.ServletConfig;
037: import javax.servlet.jsp.JspFactory;
038: import java.util.ArrayList;
039: import java.util.logging.Logger;
040:
041: /**
042: * Manages XTP templates. The XtpManager allows for a template style of
043: * XTP. A servlet can use XTP for its output.
044: *
045: * <p>The template API lets servlets assign implicit script variables for
046: * JavaScript. Filling up a HashMap with the variable name will do the
047: * trick.
048: *
049: * <p>An example servlet may look something like the following. If
050: * the stylesheet or the generated JSP file use JavaScript,
051: * testObject will be assigned to the global variable "test".
052: *
053: * <p><pre><code>
054: * void service(ServletRequest req, ServletResponse res)
055: * {
056: * // do some processing here
057: *
058: * // get the XTP template
059: * XtpManager manager = XtpManager.getManager(getServletContext());
060: * Page page = manager.createPage("WEB-INF/xtp/test.xtp");
061: *
062: * // fill in implicit variables (optional)
063: * HashMap vars = new HashMap();
064: * vars.put("test", testObject);
065: *
066: * // execute the template
067: * page.service(req, res, vars);
068: * }
069: * </code></pre>
070: *
071: * @see Page
072: */
073: public class XtpManager extends PageManager {
074: private static final Logger log = Log.open(XtpManager.class);
075:
076: private boolean _strictXml;
077: private boolean _toLower = true;
078: private boolean _entitiesAsText = true;
079: private XslManager _xslManager;
080: private JspManager _jspManager;
081: private String _defaultStylesheet = "default.xsl";
082:
083: XtpManager() {
084: }
085:
086: void initWebApp(WebApp context) {
087: super .initWebApp(context);
088:
089: if (JspFactory.getDefaultFactory() == null)
090: JspFactory.setDefaultFactory(new QJspFactory());
091:
092: _xslManager = new XslManager(context);
093: _jspManager = new JspManager();
094: _jspManager.initWebApp(context);
095: }
096:
097: /**
098: * Returns the default stylesheet.
099: */
100: public String getDefaultStylesheet() {
101: return _defaultStylesheet;
102: }
103:
104: /**
105: * Sets the default stylesheet.
106: */
107: public void setDefaultStylesheet(String stylesheet) {
108: _defaultStylesheet = stylesheet;
109: }
110:
111: /**
112: * Requires XTP documents conform to strict XML. If false (the
113: * default), XTP files are loose HTML.
114: */
115: public void setStrictXml(boolean strictXml) {
116: _strictXml = strictXml;
117: }
118:
119: /**
120: * Requires XTL stylesheets to conform to strict XSL. If false (the
121: * default), XTP files follow the XSLT-lite syntax.
122: */
123: public void setStrictXsl(boolean strictXsl) {
124: _xslManager.setStrictXsl(strictXsl);
125: }
126:
127: /**
128: * If true, HTML tags are normalized to lower-case.
129: */
130: public void setToLower(boolean toLower) {
131: _toLower = toLower;
132: }
133:
134: /**
135: * If true, parse XML with entities as text
136: */
137: public void setEntitiesAsText(boolean entitiesAsText) {
138: _entitiesAsText = entitiesAsText;
139: }
140:
141: /**
142: * Creates a new XTP page. The stylesheet is given by
143: * <?xml-stylesheet href='...'?>, or default.xsl if none is specified.
144: *
145: * <p>The stylesheet is parsed immediately, but it is only applied
146: * when the request is processed. See XtpPage for the actual
147: * processing.
148: *
149: * @param path Path to the XTP file.
150: * @param uri The uri for the XTP file.
151: * @param uriPwd The parent of uri.
152: *
153: * @return the page or null for not found
154: */
155: Page createPage(Path path, String uri, String className,
156: ServletConfig config,
157: ArrayList<PersistentDependency> dependList)
158: throws Exception {
159: if (path == null || !path.canRead() || path.isDirectory())
160: return null;
161:
162: XtpPage xtpPage = new XtpPage(path, uri, className, _webApp,
163: _xslManager, _strictXml);
164: xtpPage.setManager(_jspManager);
165: xtpPage.setHtmlToLower(_toLower);
166:
167: return xtpPage;
168: }
169: }
|