001: /*
002: HttpdBase4J: An embeddable Java web server framework that supports HTTP, HTTPS,
003: templated content and serving content from inside a jar or archive.
004: Copyright (C) 2007 Donald Munro
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not,see http://www.gnu.org/licenses/lgpl.txt
018: */
019:
020: package net.homeip.donaldm.httpdbase4j;
021:
022: import java.io.BufferedInputStream;
023: import java.io.BufferedWriter;
024: import java.io.ByteArrayInputStream;
025: import java.io.File;
026: import java.io.FileWriter;
027: import java.io.InputStream;
028: import org.antlr.stringtemplate.StringTemplate;
029:
030: /**
031: * Convenience class for implementing template processing classes
032: * @see Templatable
033: * @author Donald Munro
034: */
035: public class TemplatableAdapter implements Templatable
036: //====================================================
037: {
038: /**
039: * An overidable super class implementation of templateFile. Overiding classes
040: * should populate the template and then return super.templateFile(...).
041: * @param template The StringTemplate instance
042: * @param request The Request instance
043: * @return A temporary file of the contents of the template output or null.
044: */
045: public File templateFile(StringTemplate template, Request request,
046: StringBuffer mimeType, File dir)
047: //---------------------------------------------------------------------
048: {
049: File tempFile = null;
050: BufferedWriter bw = null;
051: try {
052: if (dir == null)
053: tempFile = File.createTempFile("TMP", ".tmp.html");
054: else
055: tempFile = File.createTempFile("TMP", ".tmp.html", dir);
056: tempFile.deleteOnExit();
057: bw = new BufferedWriter(new FileWriter(tempFile));
058: bw.write(template.toString());
059: if (mimeType != null) {
060: mimeType.setLength(0);
061: mimeType.append(Http.MIME_HTML);
062: }
063: } catch (Exception e) {
064: Httpd.Log(Httpd.LogLevel.ERROR,
065: "Error creating temporary output file "
066: + tempFile.getAbsolutePath(), e);
067: return null;
068: } finally {
069: if (bw != null)
070: try {
071: bw.close();
072: } catch (Exception e) {
073: }
074: }
075: return tempFile;
076: }
077:
078: /**
079: * An overidable super class implementation of templateString. Overiding
080: * classes should populate the template and then return
081: * super.templateString(...).
082: * @param template The StringTemplate instance
083: * @param request The Request instance
084: * @return A string representing the contents of the template output or null.
085: */
086: public String templateString(StringTemplate template,
087: Request request, StringBuffer mimeType)
088: //-----------------------------------------------------------------------
089: {
090: String s = null;
091: try {
092: s = template.toString();
093: } catch (Throwable t) {
094: Httpd.Log(Httpd.LogLevel.ERROR,
095: "Error generating template string", t);
096: return null;
097: }
098: return s;
099: }
100:
101: /**
102: * An overidable super class implementation of templateStream. Overiding
103: * classes should populate the template and then return
104: * super.templateStream(...).
105: * @param template The StringTemplate instance
106: * @param request The Request instance
107: * @return A stream representing the contents of the template output or null.
108: */
109: public InputStream templateStream(StringTemplate template,
110: Request request, StringBuffer mimeType)
111: //--------------------------------------------------------------------------
112: {
113: String s = null;
114: try {
115: s = template.toString();
116: } catch (Throwable t) {
117: Httpd.Log(Httpd.LogLevel.ERROR,
118: "Error generating template string", t);
119: return null;
120: }
121: if (s == null)
122: return null;
123:
124: return new BufferedInputStream(new ByteArrayInputStream(s
125: .getBytes()));
126: }
127:
128: }
|