001: /**
002: *
003: * © Copyright International Business Machines Corporation 2004, 2006.
004: * All rights reserved.
005: *
006: * The 'Hamlet' class provides an infrastructure to separate content from
007: * presentation. Extend from this class to serve documents. Overwrite the
008: * 'getElementRepeatCount', 'getElementReplacement', 'getElementAttributes',
009: * and 'getElementIncludeSource' methods in order to provide dynamic content.
010: * For more information see:
011: *
012: * http://www.ibm.com/developerworks/web/library/wa-hamlets/
013: *
014: *
015: * File : Hamlet.java
016: * Created : 2004/05/19
017: *
018: * @author Rene Pawlitzek (rpa@zurich.ibm.com)
019: * @version 2.00, 2004/05/19
020: * @since JDK 1.3
021: *
022: * History : 2004/05/19, rpa, new file
023: * 2004/08/31, rpa, code review
024: * 2005/02/03, rpa, reduced number of callback parameters
025: * 2005/02/07, rpa, code review
026: * 2005/02/16, rpa, added support for includes
027: * 2005/08/16, rpa, separated template engine code
028: * 2005/08/16, rpa, introduced getElementIncludeSource
029: * 2005/08/17, rpa, code review
030: * 2006/03/09, rpa, added findTemplateClass
031: * 2006/03/14, rpa, code review
032: * 2006/07/11, rpa, added session id for includes
033: *
034: */package com.ibm.hamlet;
035:
036: import java.io.*;
037: import java.net.*;
038: import javax.servlet.http.*;
039: import org.apache.log4j.*;
040: import org.xml.sax.*;
041:
042: public abstract class Hamlet extends HttpServlet implements
043: ContentHandler {
044:
045: // log4j
046: private static Category category = Category
047: .getInstance(Hamlet.class);
048:
049: private int port;
050: private String sid, path, oldTemplate;
051: private Template templateClass;
052:
053: public int getElementRepeatCount(String id, String name,
054: Attributes atts) throws Exception {
055: return 0;
056: } // getElementRepeatCount
057:
058: public String getElementReplacement(String id, String name,
059: Attributes atts) throws Exception {
060: return "";
061: } // getElementReplacement
062:
063: public Attributes getElementAttributes(String id, String name,
064: Attributes atts) throws Exception {
065: return atts;
066: } // getElementAttributes
067:
068: public InputStream getElementIncludeSource(String id, String name,
069: Attributes atts) throws Exception {
070: URL url = getIncludeURL(atts.getValue("SRC"));
071: return url.openStream();
072: } // getElementIncludeSource
073:
074: public String getDocumentType() {
075: return "text/html";
076: } // getDocumentType
077:
078: public URL getIncludeURL(String fileName) throws Exception {
079: StringBuffer buf = new StringBuffer(path);
080: buf.append("/");
081: int pos = fileName.indexOf("?");
082: if (pos != -1) {
083: buf.append(fileName.substring(0, pos));
084: buf.append(sid);
085: buf.append(fileName.substring(pos));
086: } else {
087: buf.append(fileName);
088: buf.append(sid);
089: } // if
090: category.debug("Include URL: " + buf.toString());
091: return new URL("http", "localhost", port, buf.toString(), null);
092: } // getIncludeURL
093:
094: public TemplateEngine getTemplateEngine() {
095: return new DefaultEngine();
096: } // getTemplateEngine
097:
098: private void initialize(HttpServletRequest req) {
099: sid = "";
100: HttpSession session = req.getSession(false);
101: if (session != null)
102: sid = ";jsessionid=" + session.getId();
103: port = req.getServerPort();
104: path = req.getContextPath();
105: } // initialize
106:
107: private void findTemplateClass(String template) throws Exception {
108: if (!template.equals(oldTemplate)) {
109: String className = RuntimeUtilities.getClassName(template);
110: try {
111: category.debug("Loading class '" + className + "' ...");
112: Class c = Class.forName(className);
113: templateClass = (Template) c.newInstance();
114: category.debug("Class '" + className + "' loaded");
115: System.out.println("Class '" + className + "' loaded");
116: } catch (ClassNotFoundException e) {
117: category.debug("Cannot load class '" + className + "'");
118: } // try
119: oldTemplate = template;
120: } // if
121: } // findTemplateClass
122:
123: private void serveDoc(PrintWriter out, String template,
124: ContentHandler handler) throws Exception {
125:
126: findTemplateClass(template);
127: if (templateClass != null) {
128: templateClass.serveDoc(out, handler);
129: } else {
130: TemplateEngine engine = getTemplateEngine();
131: InputStream in = getServletContext().getResourceAsStream(
132: template);
133: category.debug("Parsing '" + template + "' ...");
134: long t1 = System.currentTimeMillis();
135: engine.perform(in, handler, out);
136: long t2 = System.currentTimeMillis();
137: category.debug("Parsed '" + template + "' in " + (t2 - t1)
138: + " ms.");
139: } // if
140:
141: } // serveDoc
142:
143: public void serveDoc(HttpServletRequest req,
144: HttpServletResponse res, String template,
145: ContentHandler handler) throws Exception {
146: initialize(req);
147: PrintWriter out = res.getWriter();
148: res.setContentType(getDocumentType());
149: serveDoc(out, template, handler);
150: } // serveDoc
151:
152: public void serveDoc(HttpServletRequest req,
153: HttpServletResponse res, String template) throws Exception {
154: serveDoc(req, res, template, this );
155: } // serveDoc
156:
157: public void serveDoc(HttpServletRequest req,
158: HttpServletResponse res, Class c, ContentHandler handler)
159: throws Exception {
160: initialize(req);
161: PrintWriter out = res.getWriter();
162: res.setContentType(getDocumentType());
163: Template templateClass = (Template) c.newInstance();
164: templateClass.serveDoc(out, handler);
165: } // serveDoc
166:
167: } // Hamlet
168:
169: /* ----- End of File ---- */
|