KickJava   Java API By Example, From Geeks To Geeks.

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


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.xml.sax.*;
27 import org.xml.sax.helpers.*;
28 import javax.xml.transform.*;
29 import javax.xml.transform.stream.*;
30 import javax.xml.transform.sax.*;
31
32 public class XHTMLFastBuilder extends XHTMLTagStack {
33   TransformerHandler hd;
34   AttributesImpl atts;
35   String JavaDoc nextTag;
36   
37   public XHTMLFastBuilder(Writer out, String JavaDoc charset) throws TransformerConfigurationException {
38     StreamResult streamResult = new StreamResult(out);
39     SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
40     hd = tf.newTransformerHandler();
41     Transformer serializer = hd.getTransformer();
42     configureTransformer(serializer, charset);
43     hd.setResult(streamResult);
44     
45     try {
46       hd.startDocument();
47     } catch (SAXException ex) {
48       manageSAXExceptions(ex);
49     }
50     
51     atts = new AttributesImpl();
52   }
53   
54   private void openPendingTag() {
55     if (nextTag != null) {
56       try {
57         hd.startElement("", "", nextTag, atts);
58       } catch (SAXException ex) {
59         manageSAXExceptions(ex);
60       }
61       
62       atts.clear();
63       nextTag = null;
64     }
65   }
66   
67   public String JavaDoc getCurrentTagName() {
68     return tagStack.empty() ? null : (String JavaDoc) tagStack.peek();
69   }
70   
71   public XHTMLTagStack openTag(String JavaDoc tagName) {
72     openPendingTag();
73     nextTag = tagName;
74     tagStack.push(tagName);
75     return this;
76   }
77   
78   public XHTMLTagStack setAttribute(String JavaDoc name, String JavaDoc value) {
79     atts.addAttribute("", "", name, "CDATA", value);
80     return this;
81   }
82   
83   public XHTMLTagStack addText(String JavaDoc textData) {
84     openPendingTag();
85     
86     try {
87       hd.characters(textData.toCharArray(), 0, textData.length());
88     } catch (SAXException ex) {
89       manageSAXExceptions(ex);
90     }
91     
92     return this;
93   }
94   
95   public XHTMLTagStack addCDATA(String JavaDoc textData) {
96     openPendingTag();
97     
98     try {
99       hd.startCDATA();
100       addText(textData);
101       hd.endCDATA();
102     } catch (SAXException ex) {
103       manageSAXExceptions(ex);
104     }
105     
106     return this;
107   }
108   
109   protected void performCloseTag() {
110     openPendingTag();
111     
112     try {
113       hd.endElement("", "", (String JavaDoc) tagStack.pop());
114     } catch (SAXException ex) {
115       manageSAXExceptions(ex);
116     }
117   }
118   
119   public void flush() {
120     try {
121       hd.endDocument();
122     } catch (SAXException ex) {
123       manageSAXExceptions(ex);
124     }
125   }
126
127   private void manageSAXExceptions(SAXException ex) {
128     ex.printStackTrace();
129   }
130   
131   public static void main(String JavaDoc[] args) throws Exception JavaDoc {
132     XHTMLFastBuilder doc = new XHTMLFastBuilder(new PrintWriter(System.out), "UTF-8");
133     doc.test();
134     doc.flush();
135   }
136 }
137
Popular Tags