KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > groovy > xml > MarkupBuilder


1 /*
2  $Id: MarkupBuilder.java,v 1.9 2005/02/03 03:25:33 sstirling Exp $
3
4  Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
5
6  Redistribution and use of this software and associated documentation
7  ("Software"), with or without modification, are permitted provided
8  that the following conditions are met:
9
10  1. Redistributions of source code must retain copyright
11     statements and notices. Redistributions must also contain a
12     copy of this document.
13
14  2. Redistributions in binary form must reproduce the
15     above copyright notice, this list of conditions and the
16     following disclaimer in the documentation and/or other
17     materials provided with the distribution.
18
19  3. The name "groovy" must not be used to endorse or promote
20     products derived from this Software without prior written
21     permission of The Codehaus. For written permission,
22     please contact info@codehaus.org.
23
24  4. Products derived from this Software may not be called "groovy"
25     nor may "groovy" appear in their names without prior written
26     permission of The Codehaus. "groovy" is a registered
27     trademark of The Codehaus.
28
29  5. Due credit should be given to The Codehaus -
30     http://groovy.codehaus.org/
31
32  THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
33  ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
34  NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
35  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
36  THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
43  OF THE POSSIBILITY OF SUCH DAMAGE.
44
45  */

46 package groovy.xml;
47
48 import groovy.util.BuilderSupport;
49 import groovy.util.IndentPrinter;
50
51 import java.io.PrintWriter JavaDoc;
52 import java.io.Writer JavaDoc;
53 import java.util.Iterator JavaDoc;
54 import java.util.Map JavaDoc;
55
56 /**
57  * A helper class for creating XML or HTML markup
58  *
59  * @author <a HREF="mailto:james@coredevelopers.net">James Strachan</a>
60  * @author Stefan Matthias Aust
61  * @author <a HREF="mailto:scottstirling@rcn.com">Scott Stirling</a>
62  * @version $Revision: 1.9 $
63  */

64 public class MarkupBuilder extends BuilderSupport {
65
66     private IndentPrinter out;
67     private boolean nospace;
68     private int state;
69     private boolean nodeIsEmpty = true;
70
71     public MarkupBuilder() {
72         this(new IndentPrinter());
73     }
74
75     public MarkupBuilder(PrintWriter JavaDoc writer) {
76         this(new IndentPrinter(writer));
77     }
78
79     public MarkupBuilder(Writer JavaDoc writer) {
80         this(new IndentPrinter(new PrintWriter JavaDoc(writer)));
81     }
82
83     public MarkupBuilder(IndentPrinter out) {
84         this.out = out;
85     }
86
87     protected void setParent(Object JavaDoc parent, Object JavaDoc child) {
88     }
89
90     /*
91     public Object getProperty(String property) {
92         if (property.equals("_")) {
93             nospace = true;
94             return null;
95         } else {
96             Object node = createNode(property);
97             nodeCompleted(getCurrent(), node);
98             return node;
99         }
100     }
101     */

102
103     protected Object JavaDoc createNode(Object JavaDoc name) {
104         toState(1, name);
105         return name;
106     }
107
108     protected Object JavaDoc createNode(Object JavaDoc name, Object JavaDoc value) {
109         toState(2, name);
110         out.print(">");
111         out.print(transformValue(value.toString()));
112         return name;
113     }
114
115     protected Object JavaDoc createNode(Object JavaDoc name, Map JavaDoc attributes, Object JavaDoc value) {
116         toState(1, name);
117         for (Iterator JavaDoc iter = attributes.entrySet().iterator(); iter.hasNext();) {
118             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
119             out.print(" ");
120             print(transformName(entry.getKey().toString()));
121             out.print("='");
122             print(transformValue(entry.getValue().toString()));
123             out.print("'");
124         }
125         if (value != null)
126         {
127             nodeIsEmpty = false;
128             out.print(">" + transformValue(value.toString()) + "</" + name + ">");
129         }
130         return name;
131     }
132
133     protected Object JavaDoc createNode(Object JavaDoc name, Map JavaDoc attributes) {
134         return createNode(name, attributes, null);
135     }
136     
137     protected void nodeCompleted(Object JavaDoc parent, Object JavaDoc node) {
138         toState(3, node);
139         out.flush();
140     }
141
142     protected void print(Object JavaDoc node) {
143         out.print(node == null ? "null" : node.toString());
144     }
145
146     protected Object JavaDoc getName(String JavaDoc methodName) {
147         return super.getName(transformName(methodName));
148     }
149
150     protected String JavaDoc transformName(String JavaDoc name) {
151         if (name.startsWith("_")) name = name.substring(1);
152         return name.replace('_', '-');
153     }
154
155     /**
156      * Returns a String with special XML characters escaped as entities so that
157      * output XML is valid. Escapes the following characters as corresponding
158      * entities:
159      * <ul>
160      * <li>\' as &amp;quot;</li>
161      * <li>&amp; as &amp;amp;</li>
162      * <li>&lt; as &amp;lt;</li>
163      * <li>&gt; as &amp;gt;</li>
164      * </ul>
165      *
166      * @param value to be searched and replaced for XML special characters.
167      * @return value with XML characters escaped
168      */

169     protected String JavaDoc transformValue(String JavaDoc value) {
170         // & has to be checked and replaced before others
171
if (value.matches(".*&.*")) {
172             value = value.replaceAll("&", "&amp;");
173         }
174         if (value.matches(".*\\'.*")) {
175             value = value.replaceAll("\\'", "&quot;");
176         }
177         if (value.matches(".*<.*")) {
178             value = value.replaceAll("<", "&lt;");
179         }
180         if (value.matches(".*>.*")) {
181             value = value.replaceAll(">", "&gt;");
182         }
183         return value;
184     }
185
186     private void toState(int next, Object JavaDoc name) {
187         switch (state) {
188             case 0:
189                 switch (next) {
190                     case 1:
191                     case 2:
192                         out.print("<");
193                         print(name);
194                         break;
195                     case 3:
196                         throw new Error JavaDoc();
197                 }
198                 break;
199             case 1:
200                 switch (next) {
201                     case 1:
202                     case 2:
203                         out.print(">");
204                         if (nospace) {
205                             nospace = false;
206                         } else {
207                             out.println();
208                             out.incrementIndent();
209                             out.printIndent();
210                         }
211                         out.print("<");
212                         print(name);
213                         break;
214                     case 3:
215                         if (nodeIsEmpty) {
216                             out.print(" />");
217                         }
218                         break;
219                 }
220                 break;
221             case 2:
222                 switch (next) {
223                     case 1:
224                     case 2:
225                         throw new Error JavaDoc();
226                     case 3:
227                         out.print("</");
228                         print(name);
229                         out.print(">");
230                         break;
231                 }
232                 break;
233             case 3:
234                 switch (next) {
235                     case 1:
236                     case 2:
237                         if (nospace) {
238                             nospace = false;
239                         } else {
240                             out.println();
241                             out.printIndent();
242                         }
243                         out.print("<");
244                         print(name);
245                         break;
246                     case 3:
247                         if (nospace) {
248                             nospace = false;
249                         } else {
250                             out.println();
251                             out.decrementIndent();
252                             out.printIndent();
253                         }
254                         out.print("</");
255                         print(name);
256                         out.print(">");
257                         break;
258                 }
259                 break;
260         }
261         state = next;
262     }
263
264 }
265
Popular Tags