Source Code Cross Referenced for Hamlet.java in  » Web-Framework » Hamlets » com » ibm » hamlet » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » Hamlets » com.ibm.hamlet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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 ---- */
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.