KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > XtpManager


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jsp;
30
31 import com.caucho.log.Log;
32 import com.caucho.server.webapp.WebApp;
33 import com.caucho.vfs.Path;
34 import com.caucho.vfs.PersistentDependency;
35
36 import javax.servlet.jsp.JspFactory JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39
40 /**
41  * Manages XTP templates. The XtpManager allows for a template style of
42  * XTP. A servlet can use XTP for its output.
43  *
44  * <p>The template API lets servlets assign implicit script variables for
45  * JavaScript. Filling up a HashMap with the variable name will do the
46  * trick.
47  *
48  * <p>An example servlet may look something like the following. If
49  * the stylesheet or the generated JSP file use JavaScript,
50  * testObject will be assigned to the global variable "test".
51  *
52  * <p><pre><code>
53  * void service(ServletRequest req, ServletResponse res)
54  * {
55  * // do some processing here
56  *
57  * // get the XTP template
58  * XtpManager manager = XtpManager.getManager(getServletContext());
59  * Page page = manager.createPage("WEB-INF/xtp/test.xtp");
60  *
61  * // fill in implicit variables (optional)
62  * HashMap vars = new HashMap();
63  * vars.put("test", testObject);
64  *
65  * // execute the template
66  * page.service(req, res, vars);
67  * }
68  * </code></pre>
69  *
70  * @see Page
71  */

72 public class XtpManager extends PageManager {
73   private static final Logger JavaDoc log = Log.open(XtpManager.class);
74
75   private boolean _strictXml;
76   private boolean _toLower = true;
77   private boolean _entitiesAsText = true;
78   private XslManager _xslManager;
79   private JspManager _jspManager;
80   private String JavaDoc _defaultStylesheet = "default.xsl";
81
82   XtpManager()
83   {
84   }
85
86   void initWebApp(WebApp context)
87   {
88     super.initWebApp(context);
89     
90     if (JspFactory.getDefaultFactory() == null)
91       JspFactory.setDefaultFactory(new QJspFactory());
92     
93     _xslManager = new XslManager(context);
94     _jspManager = new JspManager();
95     _jspManager.initWebApp(context);
96   }
97
98   /**
99    * Returns the default stylesheet.
100    */

101   public String JavaDoc getDefaultStylesheet()
102   {
103     return _defaultStylesheet;
104   }
105
106   /**
107    * Sets the default stylesheet.
108    */

109   public void setDefaultStylesheet(String JavaDoc stylesheet)
110   {
111     _defaultStylesheet = stylesheet;
112   }
113
114   /**
115    * Requires XTP documents conform to strict XML. If false (the
116    * default), XTP files are loose HTML.
117    */

118   public void setStrictXml(boolean strictXml)
119   {
120     _strictXml = strictXml;
121   }
122
123   /**
124    * Requires XTL stylesheets to conform to strict XSL. If false (the
125    * default), XTP files follow the XSLT-lite syntax.
126    */

127   public void setStrictXsl(boolean strictXsl)
128   {
129     _xslManager.setStrictXsl(strictXsl);
130   }
131
132   /**
133    * If true, HTML tags are normalized to lower-case.
134    */

135   public void setToLower(boolean toLower)
136   {
137     _toLower = toLower;
138   }
139
140   /**
141    * If true, parse XML with entities as text
142    */

143   public void setEntitiesAsText(boolean entitiesAsText)
144   {
145     _entitiesAsText = entitiesAsText;
146   }
147
148   /**
149    * Creates a new XTP page. The stylesheet is given by
150    * &lt;?xml-stylesheet HREF='...'?>, or default.xsl if none is specified.
151    *
152    * <p>The stylesheet is parsed immediately, but it is only applied
153    * when the request is processed. See XtpPage for the actual
154    * processing.
155    *
156    * @param path Path to the XTP file.
157    * @param uri The uri for the XTP file.
158    * @param uriPwd The parent of uri.
159    *
160    * @return the page or null for not found
161    */

162   Page createPage(Path path, String JavaDoc uri, String JavaDoc className,
163           ArrayList JavaDoc<PersistentDependency> dependList)
164     throws Exception JavaDoc
165   {
166     if (path == null || ! path.canRead() || path.isDirectory())
167       return null;
168
169     XtpPage xtpPage = new XtpPage(path, uri,
170                                   className,
171                                   _webApp, _xslManager, _strictXml);
172     xtpPage.setManager(_jspManager);
173     xtpPage.setHtmlToLower(_toLower);
174
175     return xtpPage;
176   }
177 }
178
Popular Tags