1 29 30 package nextapp.echo2.webrender.output; 31 32 import nextapp.echo2.webrender.util.DomUtil; 33 34 import org.w3c.dom.Document ; 35 import org.w3c.dom.Element ; 36 import org.w3c.dom.NodeList ; 37 import org.w3c.dom.Text ; 38 39 43 public class HtmlDocument extends XmlDocument { 44 45 public static final String XHTML_1_0_TRANSITIONAL_PUBLIC_ID = "-//W3C//DTD XHTML 1.0 Transitional//EN"; 46 public static final String XHTML_1_0_TRANSITIONAL_SYSTSEM_ID = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"; 47 public static final String XHTML_1_0_NAMESPACE_URI = "http://www.w3.org/1999/xhtml"; 48 49 52 public HtmlDocument(String publicId, String systemId, String namespaceUri) { 53 super("html", publicId, systemId, namespaceUri); 54 Document document = getDocument(); 55 Element htmlElement = document.getDocumentElement(); 56 Element headElement = document.createElement("head"); 57 Element titleElement = document.createElement("title"); 58 titleElement.appendChild(document.createTextNode(" ")); 59 Element bodyElement = document.createElement("body"); 60 htmlElement.appendChild(headElement); 61 headElement.appendChild(titleElement); 62 htmlElement.appendChild(bodyElement); 63 } 64 65 70 public void addJavaScriptText(String code) { 71 Document document = getDocument(); 72 Element headElement = (Element ) document.getElementsByTagName("head").item(0); 73 Element scriptElement = document.createElement("script"); 74 Text textNode = document.createTextNode(code); 75 scriptElement.appendChild(textNode); 76 scriptElement.setAttribute("type", "text/javascript"); 77 headElement.appendChild(scriptElement); 78 } 79 80 85 public void addJavaScriptInclude(String uri) { 86 Document document = getDocument(); 87 Element headElement = (Element ) document.getElementsByTagName("head").item(0); 88 Element scriptElement = document.createElement("script"); 89 Text 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 101 public Element getBodyElement() { 102 return (Element ) getDocument().getElementsByTagName("body").item(0); 103 } 104 105 110 public Element getHeadElement() { 111 return (Element ) getDocument().getElementsByTagName("head").item(0); 112 } 113 114 119 public void setGenarator(String value) { 120 Element metaGeneratorElement = getDocument().createElement("meta"); 121 metaGeneratorElement.setAttribute("name", "generator"); 122 metaGeneratorElement.setAttribute("content", value); 123 getHeadElement().appendChild(metaGeneratorElement); 124 } 125 126 131 public void setTitle(String value) { 132 NodeList titles = getDocument().getElementsByTagName("title"); 133 if (titles.getLength() == 1) { 134 DomUtil.setElementText((Element ) titles.item(0), value == null ? "" : value); 135 } 136 } 137 } 138 | Popular Tags |