KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > util > XHTMLBuilder


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2007 Luciano Vernaschi
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, writeBodyContent to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * You can contact the author at http://www.cromoteca.com
20  * and at info@cromoteca.com
21  */

22
23 package org.meshcms.util;
24
25 import java.io.*;
26 import org.w3c.dom.*;
27 import javax.xml.parsers.*;
28 import javax.xml.transform.*;
29 import javax.xml.transform.stream.*;
30 import javax.xml.transform.dom.*;
31
32 public class XHTMLBuilder {
33   Document xmlDocument;
34   Element headElement;
35   Element titleElement;
36   Element bodyElement;
37   Fragment headFragment;
38   Fragment bodyFragment;
39   
40   public XHTMLBuilder() {
41     try {
42       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
43       DocumentBuilder builder = factory.newDocumentBuilder();
44       DOMImplementation impl = builder.getDOMImplementation();
45       xmlDocument = impl.createDocument(null, "html", null);
46       Node htmlNode = xmlDocument.getFirstChild();
47       headElement = xmlDocument.createElement("head");
48       titleElement = xmlDocument.createElement("title");
49       bodyElement = xmlDocument.createElement("body");
50       titleElement.appendChild(xmlDocument.createTextNode("Generated XHTML Code"));
51       headElement.appendChild(titleElement);
52       htmlNode.appendChild(headElement);
53       htmlNode.appendChild(bodyElement);
54     } catch (Exception JavaDoc ex) {
55       ex.printStackTrace();
56     }
57     
58     headFragment = new Fragment(headElement);
59     bodyFragment = new Fragment(bodyElement);
60   }
61   
62   public Fragment getHead() {
63     return headFragment;
64   }
65   
66   public Fragment getBody() {
67     return bodyFragment;
68   }
69   
70   public Element getHeadElement() {
71     return headElement;
72   }
73   
74   public Element getTitleElement() {
75     return titleElement;
76   }
77   
78   public Element getBodyElement() {
79     return bodyElement;
80   }
81   
82   public void setTitle(String JavaDoc title) {
83     if (title == null) {
84       throw new IllegalArgumentException JavaDoc("Provided title is null");
85     }
86     
87     Node node;
88     
89     while((node = titleElement.getLastChild()) != null) {
90       titleElement.removeChild(node);
91     }
92     
93     titleElement.appendChild(xmlDocument.createTextNode(title));
94   }
95   
96   public void normalize() {
97     xmlDocument.normalize();
98   }
99   
100   public void writeFullDocument(Writer out, String JavaDoc charset) {
101     write(xmlDocument, out, charset);
102   }
103   
104   public void writeBodyContent(Writer out, String JavaDoc charset) {
105     Node n = bodyElement.getFirstChild();
106     
107     while (n != null) {
108       write(n, out, charset);
109       n = n.getNextSibling();
110     }
111   }
112   
113   private void write(Node node, Writer out, String JavaDoc charset) {
114     try {
115       DOMSource domSource = new DOMSource(node);
116       StreamResult streamResult = new StreamResult(out);
117       TransformerFactory tf = TransformerFactory.newInstance();
118       Transformer serializer = tf.newTransformer();
119       XHTMLTagStack.configureTransformer(serializer, charset);
120       serializer.transform(domSource, streamResult);
121     } catch (Exception JavaDoc ex) {
122       ex.printStackTrace();
123     }
124   }
125   
126   public class Fragment extends XHTMLTagStack {
127     Element mainElement;
128     
129     public Fragment(Element mainElement) {
130       this.mainElement = mainElement;
131     }
132     
133     public Element getCurrentTag() {
134       return tagStack.empty() ? mainElement : (Element) tagStack.peek();
135     }
136     
137     public String JavaDoc getCurrentTagName() {
138       return tagStack.empty() ? null : ((Element) tagStack.peek()).getTagName();
139     }
140     
141     public XHTMLTagStack openTag(String JavaDoc tagName) {
142       Element tag = xmlDocument.createElement(tagName);
143       getCurrentTag().appendChild(tag);
144       tagStack.push(tag);
145       return this;
146     }
147     
148     public XHTMLTagStack setAttribute(String JavaDoc name, String JavaDoc value) {
149       getCurrentTag().setAttribute(name, value);
150       return this;
151     }
152     
153     public XHTMLTagStack addText(String JavaDoc textData) {
154       getCurrentTag().appendChild(xmlDocument.createTextNode(textData));
155       return this;
156     }
157     
158     public XHTMLTagStack addCDATA(String JavaDoc textData) {
159       getCurrentTag().appendChild(xmlDocument.createCDATASection(textData));
160       return this;
161     }
162     
163     protected void performCloseTag() {
164       tagStack.pop();
165     }
166   }
167   
168   public static void main(String JavaDoc[] args) {
169     XHTMLBuilder doc = new XHTMLBuilder();
170     doc.getBody().test();
171     doc.writeBodyContent(new PrintWriter(System.out), "UTF-8");
172   }
173 }
174
Popular Tags