KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > webrender > output > HtmlDocument


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.webrender.output;
31
32 import nextapp.echo2.webrender.util.DomUtil;
33
34 import org.w3c.dom.Document JavaDoc;
35 import org.w3c.dom.Element JavaDoc;
36 import org.w3c.dom.NodeList JavaDoc;
37 import org.w3c.dom.Text JavaDoc;
38
39 /**
40  * A simple wrapper around JAXP/W3C DOM APIs to generate and render an
41  * XHTML 1.0 Transitional document.
42  */

43 public class HtmlDocument extends XmlDocument {
44     
45     public static final String JavaDoc XHTML_1_0_TRANSITIONAL_PUBLIC_ID = "-//W3C//DTD XHTML 1.0 Transitional//EN";
46     public static final String JavaDoc XHTML_1_0_TRANSITIONAL_SYSTSEM_ID = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";
47     public static final String JavaDoc XHTML_1_0_NAMESPACE_URI = "http://www.w3.org/1999/xhtml";
48     
49     /**
50      * Creates a new <code>HtmlDocument</code>.
51      */

52     public HtmlDocument(String JavaDoc publicId, String JavaDoc systemId, String JavaDoc namespaceUri) {
53         super("html", publicId, systemId, namespaceUri);
54         Document JavaDoc document = getDocument();
55         Element JavaDoc htmlElement = document.getDocumentElement();
56         Element JavaDoc headElement = document.createElement("head");
57         Element JavaDoc titleElement = document.createElement("title");
58         titleElement.appendChild(document.createTextNode(" "));
59         Element JavaDoc bodyElement = document.createElement("body");
60         htmlElement.appendChild(headElement);
61         headElement.appendChild(titleElement);
62         htmlElement.appendChild(bodyElement);
63     }
64     
65     /**
66      * Adds inline JavaScript code to the document.
67      *
68      * @param code the inline code
69      */

70     public void addJavaScriptText(String JavaDoc code) {
71         Document JavaDoc document = getDocument();
72         Element JavaDoc headElement = (Element JavaDoc) document.getElementsByTagName("head").item(0);
73         Element JavaDoc scriptElement = document.createElement("script");
74         Text JavaDoc textNode = document.createTextNode(code);
75         scriptElement.appendChild(textNode);
76         scriptElement.setAttribute("type", "text/javascript");
77         headElement.appendChild(scriptElement);
78     }
79     
80     /**
81      * Adds a JavaScript include reference to the document.
82      *
83      * @param uri the URI of the JavaScript code
84      */

85     public void addJavaScriptInclude(String JavaDoc uri) {
86         Document JavaDoc document = getDocument();
87         Element JavaDoc headElement = (Element JavaDoc) document.getElementsByTagName("head").item(0);
88         Element JavaDoc scriptElement = document.createElement("script");
89         Text JavaDoc textNode = document.createTextNode(" ");
90         scriptElement.appendChild(textNode);
91         scriptElement.setAttribute("type", "text/javascript");
92         scriptElement.setAttribute("src", uri);
93         headElement.appendChild(scriptElement);
94     }
95     
96     /**
97      * Retrieves the BODY element of the document.
98      *
99      * @return the BODY element
100      */

101     public Element JavaDoc getBodyElement() {
102         return (Element JavaDoc) getDocument().getElementsByTagName("body").item(0);
103     }
104     
105     /**
106      * Retrieves the HEAD element of the document.
107      *
108      * @return the HEAD element
109      */

110     public Element JavaDoc getHeadElement() {
111         return (Element JavaDoc) getDocument().getElementsByTagName("head").item(0);
112     }
113     
114     /**
115      * Sets the value of the "generator" META element.
116      *
117      * @param value the value
118      */

119     public void setGenarator(String JavaDoc value) {
120         Element JavaDoc metaGeneratorElement = getDocument().createElement("meta");
121         metaGeneratorElement.setAttribute("name", "generator");
122         metaGeneratorElement.setAttribute("content", value);
123         getHeadElement().appendChild(metaGeneratorElement);
124     }
125     
126     /**
127      * Convenience method to set the title of the document.
128      *
129      * @param value The new title value.
130      */

131     public void setTitle(String JavaDoc value) {
132         NodeList JavaDoc titles = getDocument().getElementsByTagName("title");
133         if (titles.getLength() == 1) {
134             DomUtil.setElementText((Element JavaDoc) titles.item(0), value == null ? "" : value);
135         }
136     }
137 }
138
Popular Tags