Hamlet.java :  » Web-Framework » Hamlets » com » ibm » hamlet » Java Open Source

Java Open Source » Web Framework » Hamlets 
Hamlets » com » ibm » hamlet » Hamlet.java
/**
 *
 *  Copyright International Business Machines Corporation 2004, 2006.
 * All rights reserved.
 *
 * The 'Hamlet' class provides an infrastructure to separate content from
 * presentation. Extend from this class to serve documents. Overwrite the
 * 'getElementRepeatCount', 'getElementReplacement', 'getElementAttributes',
 * and 'getElementIncludeSource' methods in order to provide dynamic content.
 * For more information see:
 *
 * http://www.ibm.com/developerworks/web/library/wa-hamlets/
 *
 *
 * File     : Hamlet.java
 * Created  : 2004/05/19
 *
 * @author    Rene Pawlitzek (rpa@zurich.ibm.com)
 * @version   2.00, 2004/05/19
 * @since     JDK 1.3
 *
 * History  : 2004/05/19, rpa, new file
 *            2004/08/31, rpa, code review
 *            2005/02/03, rpa, reduced number of callback parameters
 *            2005/02/07, rpa, code review
 *            2005/02/16, rpa, added support for includes
 *            2005/08/16, rpa, separated template engine code
 *            2005/08/16, rpa, introduced getElementIncludeSource
 *            2005/08/17, rpa, code review
 *            2006/03/09, rpa, added findTemplateClass
 *            2006/03/14, rpa, code review
 *            2006/07/11, rpa, added session id for includes
 *
 */


package com.ibm.hamlet;


import java.io.*;
import java.net.*;
import javax.servlet.http.*;
import org.apache.log4j.*;
import org.xml.sax.*;


public abstract class Hamlet extends HttpServlet implements ContentHandler {


  // log4j
  private static Category category = Category.getInstance (Hamlet.class);


  private  int       port;
  private  String    sid, path, oldTemplate;
  private  Template  templateClass;


  public int getElementRepeatCount (String id, String name, Attributes atts)
    throws Exception {
    return 0;
  } // getElementRepeatCount


  public String getElementReplacement (String id, String name, Attributes atts)
    throws Exception {
    return "";
  } // getElementReplacement


  public Attributes getElementAttributes (String id, String name, Attributes atts)
    throws Exception {
    return atts;
  } // getElementAttributes


  public InputStream getElementIncludeSource (String id, String name, Attributes atts)
    throws Exception {
    URL url = getIncludeURL (atts.getValue ("SRC"));
    return url.openStream ();
  } // getElementIncludeSource


  public String getDocumentType () {
    return "text/html";
  } // getDocumentType


  public URL getIncludeURL (String fileName) throws Exception {
    StringBuffer buf = new StringBuffer (path);
    buf.append ("/");
    int pos = fileName.indexOf ("?");
    if (pos != -1) {
      buf.append (fileName.substring (0, pos));
      buf.append (sid);
      buf.append (fileName.substring (pos));
    } else {
      buf.append (fileName);
      buf.append (sid);
    } // if
    category.debug ("Include URL: " + buf.toString ());
    return new URL ("http", "localhost", port, buf.toString (), null);
  } // getIncludeURL


  public TemplateEngine getTemplateEngine () {
    return new DefaultEngine ();
  } // getTemplateEngine


  private void initialize (HttpServletRequest req) {
    sid = "";
    HttpSession session = req.getSession (false);
    if (session != null)
      sid = ";jsessionid=" + session.getId ();
    port = req.getServerPort ();
    path = req.getContextPath ();
  } // initialize


  private void findTemplateClass (String template) throws Exception {
    if (!template.equals (oldTemplate)) {
      String className = RuntimeUtilities.getClassName (template);
      try {
        category.debug ("Loading class '" + className + "' ...");
        Class c = Class.forName (className);
        templateClass = (Template) c.newInstance ();
        category.debug ("Class '" + className + "' loaded");
        System.out.println ("Class '" + className + "' loaded");
      } catch (ClassNotFoundException e) {
        category.debug ("Cannot load class '" + className + "'");
      } // try
      oldTemplate = template;
    } // if
  } // findTemplateClass


  private void serveDoc (PrintWriter out, String template, ContentHandler handler)
    throws Exception {

    findTemplateClass (template);
    if (templateClass != null) {
      templateClass.serveDoc (out, handler);
    } else {
      TemplateEngine engine = getTemplateEngine ();
      InputStream in = getServletContext ().getResourceAsStream (template);
      category.debug ("Parsing '" + template + "' ...");
      long t1 = System.currentTimeMillis ();
      engine.perform (in, handler, out);
      long t2 = System.currentTimeMillis ();
      category.debug ("Parsed '" + template + "' in " + (t2 - t1) + " ms.");
    } // if

  } // serveDoc


  public void serveDoc (HttpServletRequest req, HttpServletResponse res,
    String template, ContentHandler handler) throws Exception {
    initialize (req);
    PrintWriter out = res.getWriter ();
    res.setContentType (getDocumentType ());
    serveDoc (out, template, handler);
  } // serveDoc


  public void serveDoc (HttpServletRequest req, HttpServletResponse res,
    String template) throws Exception {
    serveDoc (req, res, template, this);
  } // serveDoc


  public void serveDoc (HttpServletRequest req, HttpServletResponse res,
    Class c, ContentHandler handler) throws Exception {
    initialize (req);
    PrintWriter out = res.getWriter ();
    res.setContentType (getDocumentType ());
    Template templateClass = (Template) c.newInstance ();
    templateClass.serveDoc (out, handler);
  } // serveDoc


} // Hamlet


/* ----- End of File ---- */
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.