001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.webrender.output;
031:
032: import nextapp.echo2.webrender.util.DomUtil;
033:
034: import org.w3c.dom.Document;
035: import org.w3c.dom.Element;
036: import org.w3c.dom.NodeList;
037: import org.w3c.dom.Text;
038:
039: /**
040: * A simple wrapper around JAXP/W3C DOM APIs to generate and render an
041: * XHTML 1.0 Transitional document.
042: */
043: public class HtmlDocument extends XmlDocument {
044:
045: public static final String XHTML_1_0_TRANSITIONAL_PUBLIC_ID = "-//W3C//DTD XHTML 1.0 Transitional//EN";
046: public static final String XHTML_1_0_TRANSITIONAL_SYSTSEM_ID = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";
047: public static final String XHTML_1_0_NAMESPACE_URI = "http://www.w3.org/1999/xhtml";
048:
049: /**
050: * Creates a new <code>HtmlDocument</code>.
051: */
052: public HtmlDocument(String publicId, String systemId,
053: String namespaceUri) {
054: super ("html", publicId, systemId, namespaceUri);
055: Document document = getDocument();
056: Element htmlElement = document.getDocumentElement();
057: Element headElement = document.createElement("head");
058: Element titleElement = document.createElement("title");
059: titleElement.appendChild(document.createTextNode(" "));
060: Element bodyElement = document.createElement("body");
061: htmlElement.appendChild(headElement);
062: headElement.appendChild(titleElement);
063: htmlElement.appendChild(bodyElement);
064: }
065:
066: /**
067: * Adds inline JavaScript code to the document.
068: *
069: * @param code the inline code
070: */
071: public void addJavaScriptText(String code) {
072: Document document = getDocument();
073: Element headElement = (Element) document.getElementsByTagName(
074: "head").item(0);
075: Element scriptElement = document.createElement("script");
076: Text textNode = document.createTextNode(code);
077: scriptElement.appendChild(textNode);
078: scriptElement.setAttribute("type", "text/javascript");
079: headElement.appendChild(scriptElement);
080: }
081:
082: /**
083: * Adds a JavaScript include reference to the document.
084: *
085: * @param uri the URI of the JavaScript code
086: */
087: public void addJavaScriptInclude(String uri) {
088: Document document = getDocument();
089: Element headElement = (Element) document.getElementsByTagName(
090: "head").item(0);
091: Element scriptElement = document.createElement("script");
092: Text textNode = document.createTextNode(" ");
093: scriptElement.appendChild(textNode);
094: scriptElement.setAttribute("type", "text/javascript");
095: scriptElement.setAttribute("src", uri);
096: headElement.appendChild(scriptElement);
097: }
098:
099: /**
100: * Retrieves the BODY element of the document.
101: *
102: * @return the BODY element
103: */
104: public Element getBodyElement() {
105: return (Element) getDocument().getElementsByTagName("body")
106: .item(0);
107: }
108:
109: /**
110: * Retrieves the HEAD element of the document.
111: *
112: * @return the HEAD element
113: */
114: public Element getHeadElement() {
115: return (Element) getDocument().getElementsByTagName("head")
116: .item(0);
117: }
118:
119: /**
120: * Sets the value of the "generator" META element.
121: *
122: * @param value the value
123: */
124: public void setGenarator(String value) {
125: Element metaGeneratorElement = getDocument().createElement(
126: "meta");
127: metaGeneratorElement.setAttribute("name", "generator");
128: metaGeneratorElement.setAttribute("content", value);
129: getHeadElement().appendChild(metaGeneratorElement);
130: }
131:
132: /**
133: * Convenience method to set the title of the document.
134: *
135: * @param value The new title value.
136: */
137: public void setTitle(String value) {
138: NodeList titles = getDocument().getElementsByTagName("title");
139: if (titles.getLength() == 1) {
140: DomUtil.setElementText((Element) titles.item(0),
141: value == null ? "" : value);
142: }
143: }
144: }
|