001: /*
002: * PageViewer.java December 2003
003: *
004: * Copyright (C) 2003, Niall Gallagher <niallg@users.sf.net>
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.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General
016: * Public License along with this library; if not, write to the
017: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
018: * Boston, MA 02111-1307 USA
019: */
020:
021: package simple.template.page;
022:
023: import simple.template.layout.Viewer;
024: import simple.page.Model;
025: import simple.page.Page;
026: import java.io.PrintWriter;
027:
028: /**
029: * The <code>PageViewer</code> provides an implementation
030: * of the <code>Viewer</code> interface. This provides a
031: * simple that delegates directly to the <code>Page</code>
032: * provides by the <cite>Page</code> templating system.
033: * This provides all the methods nessecary to populate the
034: * data source used and to render the template content.
035: *
036: * @author Niall Gallagher
037: */
038: final class PageViewer extends PageDatabase implements Viewer {
039:
040: /**
041: * The template instance used to generate the content.
042: */
043: private Page page;
044:
045: /**
046: * Constructor for the <code>PageViewer</code> object.
047: * This creates a viewer using the issued template object.
048: * The viewer is initialized using the properties of the
049: * issued <code>Model</code>. These properties can be
050: * overridden without affecting the containers database.
051: *
052: * @param page template object used to generate output
053: * @param data properties used to configure the output
054: */
055: public PageViewer(Page page, Model data) {
056: this .data = data;
057: this .page = page;
058: }
059:
060: /**
061: * Displays the contents of the generated template output to
062: * the issued <code>Writer</code>. This encapsulates the means
063: * of rendering the template to a single method. Internally
064: * the properties that are set within the document will be
065: * used to configure the template, enabling dynamic output.
066: * <p>
067: * If there are any problems parsing the template or emitting
068: * its contents an exception is thrown. However, if it is
069: * successfully processed it will be written to the issued
070: * output, which will remain unflushed for performance.
071: *
072: * @param out the output to write the template rendering to
073: *
074: * @throws Exception thrown if there is a problem parsing or
075: * emitting the template
076: */
077: public void write(PrintWriter out) throws Exception {
078: page.write(out, data);
079: }
080:
081: /**
082: * This is used to acquire a HTTP Content-Type header, which
083: * can be used to describe the contents of the viewer. This
084: * will typically be the MIME type and character set of the
085: * template, for example "text/html; charset=UTF-8". This is
086: * provided so that the document can describe the type.
087: *
088: * @return this returns the content type of the contents
089: */
090: public String getContentType() {
091: return page.getContentType();
092: }
093:
094: /**
095: * This provides the character encoding of the page. This is
096: * typically UTF-8, however it can be any supported character
097: * encoding for the Java platform. This is used to ensure the
098: * conversion from characters to bytes is known.
099: *
100: * @return this returns the character encoding of the page
101: */
102: public String getCharset() {
103: return page.getCharset();
104: }
105: }
|