// =========================================================================== // Copyright (c) 1996 Mort Bay Consulting Pty. Ltd. All rights reserved. // $Id: Page.java,v 1.15.2.3 2003/06/04 04:47:37 starksm Exp $ // --------------------------------------------------------------------------- package org.mortbay.html; import java.io.IOException; import java.io.Writer; import java.util.Dictionary; import java.util.Hashtable; /* --------------------------------------------------------------------- */ /** HTML Page. * A HTML Page extends composite with the addition of the HTML Header * tags, fields and elements. * Furthermore, individual parts of the page may be written or the * progressive page be output with flush. *
* Pages contain parameters and named sections. These are used by * derived Page classes that implement a Look and Feel. Page users * may add to name sections such as "Margin" or "Footer" and set * parameters such as "HelpUrl" without knowledge of how the look and feel * will arrange these. To assist with standard look and feel creation * Page defines a set of standard names for many common parameters * and sections. *
* If named sections are used, the page constructor or completeSections * must add the named section to the page in the appropriate places. * If named sections are not added to the page, then they can only be * written with an explicit call to write(out,"section",end); * Changes in behaviour to section creation and adding, should be controlled * via page properties. *
* @see Composite
* @version $Id: Page.java,v 1.15.2.3 2003/06/04 04:47:37 starksm Exp $
* @author Greg Wilkins
*/
public class Page extends Composite
{
/* ----------------------------------------------------------------- */
public static final String
Request="Request",
Response="Response",
Header="Header",
Title="Title",
Section="Section",
HeaderSize="HdrSize", // HeaderSize string suitable for FRAMESET
Footer="Footer",
FooterSize="FtrSize", // FooterSize string suitable for FRAMESET
Content="Content",
ContentSize="CntSize",
Margin="Margin",
MarginSize="MrgSize",
LeftMargin="Left",
LeftMarginSize="LMSize",
RightMargin="Right",
RightMarginSize="RMSize",
Help="Help",
Home="Home",
Heading="Heading",
Up="Up",
Prev="Prev",
Next="Next",
Back="Back",
Target="Target",
BaseUrl="BaseUrl",
FgColour="FgColour",
BgColour="BgColour",
HighlightColour="HlColour",
PageType="PageType",
NoTitle="No Title"
;
/* ----------------------------------------------------------------- */
protected Hashtable properties = new Hashtable(10);
/* ----------------------------------------------------------------- */
Hashtable sections = new Hashtable(10);
private Composite head= new Composite();
private String base="";
private boolean writtenHtmlHead = false;
private boolean writtenBodyTag = false;
/* ----------------------------------------------------------------- */
public Page()
{
this(NoTitle);
}
/* ----------------------------------------------------------------- */
public Page(String title)
{
title(title);
}
/* ----------------------------------------------------------------- */
public Page(String title, String attributes)
{
title(title);
attribute(attributes);
}
/* ----------------------------------------------------------------- */
/** Set page title.
* @return This Page (for chained commands)
*/
public Page title(String title)
{
properties.put(Title,title);
String heading = (String)properties.get(Heading);
if (heading==null||heading.equals(NoTitle))
properties.put(Heading,title);
return this;
}
/* ----------------------------------------------------------------- */
/** Add element or object to the page header.
* @param o The Object to add. If it is a String or Element, it is
* added directly, otherwise toString() is called.
* @return This Page (for chained commands)
*/
public Page addHeader(Object o)
{
head.add("\n");
head.add(o);
return this;
}
/* ----------------------------------------------------------------- */
/** Set page background image.
* @return This Page (for chained commands)
*/
public final Page setBackGroundImage(String bg)
{
attribute("background",bg);
return this;
}
/* ----------------------------------------------------------------- */
/** Set page background color.
* @return This Page (for chained commands)
*/
public final Page setBackGroundColor(String color)
{
properties.put(BgColour,color);
attribute("bgcolor",color);
return this;
}
/* ----------------------------------------------------------------- */
/** Set the URL Base for the Page.
* @param target Default link target, null if none.
* @param href Default absolute href, null if none.
* @return This Page (for chained commands)
*/
public final Page setBase(String target, String href)
{
base="
* writeHtmlHead(out)
* writeBodyTag(out)
* writeElements(out)
* writeHtmlEnd(out)
*/
public void write(Writer out)
throws IOException
{
writeHtmlHead(out);
writeBodyTag(out);
writeElements(out);
writeHtmlEnd(out);
out.flush();
}
/* ------------------------------------------------------------ */
/** Write HTML page head tags.
* Write tags <HTML><head> .... </head>
*/
public void writeHtmlHead(Writer out)
throws IOException
{
if (!writtenHtmlHead)
{
writtenHtmlHead=true;
completeSections();
out.write("