KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > language > markup > xsp > XSPObjectHelper


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.language.markup.xsp;
17
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 import org.apache.cocoon.xml.dom.DOMStreamer;
22 import org.apache.cocoon.xml.XMLUtils;
23
24 import org.apache.excalibur.xml.sax.XMLizable;
25 import org.w3c.dom.Node JavaDoc;
26 import org.xml.sax.ContentHandler JavaDoc;
27 import org.xml.sax.SAXException JavaDoc;
28 import org.xml.sax.helpers.AttributesImpl JavaDoc;
29
30 /**
31  * Base class for XSP's object model manipulation logicsheets
32  *
33  * @author <a HREF="mailto:ricardo@apache.org">Ricardo Rocha</a>
34  * @author <a HREF="mailto:sylvain.wallez@anyware-tech.com">Sylvain Wallez</a>
35  * (Cocoon1 <code>xspExpr()</code> methods port)
36  * @version $Id: XSPObjectHelper.java 164808 2005-04-26 16:07:03Z vgritsenko $
37  */

38 public class XSPObjectHelper {
39
40     /**
41      * Output an element containing text only and no attributes
42      *
43      * @param contentHandler The SAX content handler
44      * @param name The element name
45      * @param data The data contained by the element
46      */

47     protected static void elementData(String JavaDoc uri,
48                                       String JavaDoc prefix,
49                                       ContentHandler JavaDoc contentHandler,
50                                       String JavaDoc name,
51                                       String JavaDoc data)
52     throws SAXException JavaDoc {
53         start(uri, prefix, contentHandler, name);
54         data(contentHandler, data);
55         end(uri, prefix, contentHandler, name);
56     }
57
58     /**
59      * Output an element containing text only and attributes
60      *
61      * @param contentHandler The SAX content handler
62      * @param name The element name
63      * @param data The data contained by the element
64      * @param attr The element attributes
65      */

66     protected static void elementData(String JavaDoc uri,
67                                       String JavaDoc prefix,
68                                       ContentHandler JavaDoc contentHandler,
69                                       String JavaDoc name,
70                                       String JavaDoc data,
71                                       AttributesImpl JavaDoc attr)
72     throws SAXException JavaDoc {
73         start(uri, prefix, contentHandler, name, attr);
74         data(contentHandler, data);
75         end(uri, prefix, contentHandler, name);
76     }
77
78     /**
79      * Start an element with the proper object's uri and prefix and no
80      * attributes
81      *
82      * @param contentHandler The SAX content handler
83      * @param name The element name
84      */

85     protected static void start(String JavaDoc uri,
86                                 String JavaDoc prefix,
87                                 ContentHandler JavaDoc contentHandler,
88                                 String JavaDoc name)
89     throws SAXException JavaDoc {
90         contentHandler.startElement(uri, name, prefix + ":" + name, XMLUtils.EMPTY_ATTRIBUTES);
91     }
92
93     /**
94      * Start an element with the proper object's uri and prefix and with
95      * attributes
96      *
97      * @param contentHandler The SAX content handler
98      * @param name The element name
99      * @param attr The element attributes
100      */

101     protected static void start(String JavaDoc uri,
102                                 String JavaDoc prefix,
103                                 ContentHandler JavaDoc contentHandler,
104                                 String JavaDoc name,
105                                 AttributesImpl JavaDoc attr)
106     throws SAXException JavaDoc {
107         contentHandler.startElement(uri, name, prefix + ":" + name, attr);
108     }
109
110     /**
111      * End an element with the proper object's uri and prefix
112      *
113      * @param contentHandler The SAX content handler
114      * @param name The element name
115      */

116     protected static void end(String JavaDoc uri,
117                               String JavaDoc prefix,
118                               ContentHandler JavaDoc contentHandler,
119                               String JavaDoc name)
120     throws SAXException JavaDoc {
121         contentHandler.endElement(uri, name, prefix + ":" + name);
122     }
123
124     /**
125      * Add an attribute
126      *
127      * @param attr The attribute list
128      * @param name The attribute name
129      * @param value The attribute value
130      */

131     protected static void addAttribute(AttributesImpl JavaDoc attr,
132                                        String JavaDoc name,
133                                        String JavaDoc value)
134     throws SAXException JavaDoc {
135         attr.addAttribute("", name, name, "CDATA", value);
136     }
137
138     /**
139      * Add string data
140      *
141      * @param contentHandler The SAX content handler
142      * @param data The string data
143      */

144     protected static void data(ContentHandler JavaDoc contentHandler, String JavaDoc data)
145     throws SAXException JavaDoc {
146         contentHandler.characters(data.toCharArray(), 0, data.length());
147     }
148
149     // <xsp:expr> methods
150

151     /**
152      * Implementation of &lt;xsp:expr&gt; for <code>char</code> :
153      * outputs characters representing the value.
154      *
155      * @param contentHandler the SAX content handler
156      * @param v the value
157      */

158     public static void xspExpr(ContentHandler JavaDoc contentHandler, char v)
159     throws SAXException JavaDoc {
160         data(contentHandler, String.valueOf(v));
161     }
162
163     /**
164      * Implementation of &lt;xsp:expr&gt; for <code>byte</code> :
165      * outputs characters representing the value.
166      *
167      * @param contentHandler the SAX content handler
168      * @param v the value
169      */

170     public static void xspExpr(ContentHandler JavaDoc contentHandler, byte v)
171     throws SAXException JavaDoc {
172         data(contentHandler, String.valueOf(v));
173     }
174
175     /**
176      * Implementation of &lt;xsp:expr&gt; for <code>boolean</code> :
177      * outputs characters representing the value (true / false).
178      *
179      * @param contentHandler the SAX content handler
180      * @param v the value
181      */

182     public static void xspExpr(ContentHandler JavaDoc contentHandler, boolean v)
183     throws SAXException JavaDoc {
184         data(contentHandler, String.valueOf(v));
185     }
186
187     /**
188      * Implementation of &lt;xsp:expr&gt; for <code>int</code> :
189      * outputs characters representing the value.
190      *
191      * @param contentHandler the SAX content handler
192      * @param v the value
193      */

194     public static void xspExpr(ContentHandler JavaDoc contentHandler, int v)
195     throws SAXException JavaDoc {
196         data(contentHandler, String.valueOf(v));
197     }
198
199     /**
200      * Implementation of &lt;xsp:expr&gt; for <code>long</code> :
201      * outputs characters representing the value.
202      *
203      * @param contentHandler the SAX content handler
204      * @param v the value
205      */

206     public static void xspExpr(ContentHandler JavaDoc contentHandler, long v)
207     throws SAXException JavaDoc {
208         data(contentHandler, String.valueOf(v));
209     }
210
211     /**
212      * Implementation of &lt;xsp:expr&gt; for <code>long</code> :
213      * outputs characters representing the value.
214      *
215      * @param contentHandler the SAX content handler
216      * @param v the value
217      */

218     public static void xspExpr(ContentHandler JavaDoc contentHandler, float v)
219     throws SAXException JavaDoc {
220         data(contentHandler, String.valueOf(v));
221     }
222
223     /**
224      * Implementation of &lt;xsp:expr&gt; for <code>double</code> :
225      * outputs characters representing the value.
226      *
227      * @param contentHandler the SAX content handler
228      * @param v the value
229      */

230     public static void xspExpr(ContentHandler JavaDoc contentHandler, double v)
231     throws SAXException JavaDoc {
232         data(contentHandler, String.valueOf(v));
233     }
234
235     /**
236      * Implementation of &lt;xsp:expr&gt; for <code>String</code> :
237      * outputs characters representing the value.
238      *
239      * @param contentHandler the SAX content handler
240      * @param text the value
241      */

242     public static void xspExpr(ContentHandler JavaDoc contentHandler, String JavaDoc text)
243     throws SAXException JavaDoc {
244         if (text != null) {
245             data(contentHandler, text);
246         }
247     }
248
249     // Now handled by XMLizable
250
// /**
251
// * Implementation of &lt;xsp:expr&gt; for <code>XMLFragment</code> :
252
// * outputs the value by calling <code>v.toSax(contentHandler)</code>.
253
// *
254
// * @param contentHandler the SAX content handler
255
// * @param v the XML fragment
256
// */
257
// public static void xspExpr(ContentHandler contentHandler, XMLFragment v) throws SAXException
258
// {
259
// if (v != null)
260
// {
261
// v.toSAX(contentHandler);
262
// }
263
// }
264

265     /**
266      * Implementation of &lt;xsp:expr&gt; for <code>XMLizable</code> :
267      * outputs the value by calling <code>v.toSax(contentHandler)</code>.
268      *
269      * @param contentHandler the SAX content handler
270      * @param v the XML fragment
271      */

272     public static void xspExpr(ContentHandler JavaDoc contentHandler, XMLizable v)
273     throws SAXException JavaDoc {
274         if (v != null) {
275             v.toSAX(contentHandler);
276         }
277     }
278
279     /**
280      * Implementation of &lt;xsp:expr&gt; for <code>org.w3c.dom.Node</code> :
281      * converts the Node to a SAX event stream.
282      *
283      * @param contentHandler the SAX content handler
284      * @param v the value
285      */

286     public static void xspExpr(ContentHandler JavaDoc contentHandler, Node JavaDoc v)
287     throws SAXException JavaDoc {
288         if (v != null) {
289             DOMStreamer streamer = new DOMStreamer(contentHandler);
290             streamer.stream(v);
291         }
292     }
293
294     /**
295      * Implementation of &lt;xsp:expr&gt; for <code>java.util.Collection</code> :
296      * outputs the value by calling <code>xspExpr()</code> on each element of the
297      * collection.
298      *
299      * @param contentHandler the SAX content handler
300      * @param v the XML fragment
301      */

302     public static void xspExpr(ContentHandler JavaDoc contentHandler, Collection JavaDoc v)
303     throws SAXException JavaDoc {
304         if (v != null) {
305             Iterator JavaDoc iterator = v.iterator();
306             while (iterator.hasNext()) {
307                 xspExpr(contentHandler, iterator.next());
308             }
309         }
310     }
311
312     /**
313      * Implementation of &lt;xsp:expr&gt; for <code>Object</code> depending on its class :
314      * <ul>
315      * <li>if it's an array, call <code>xspExpr()</code> on all its elements,</li>
316      * <li>if it's class has a specific <code>xspExpr()</code>implementation, use it,</li>
317      * <li>else, output it's string representation.</li>
318      * </ul>
319      *
320      * @param contentHandler the SAX content handler
321      * @param v the value
322      */

323     public static void xspExpr(ContentHandler JavaDoc contentHandler, Object JavaDoc v)
324     throws SAXException JavaDoc {
325         if (v == null) {
326             return;
327         }
328
329         // Array: recurse over each element
330
if (v.getClass().isArray()) {
331             Object JavaDoc[] elements = (Object JavaDoc[]) v;
332
333             for (int i = 0; i < elements.length; i++) {
334                 xspExpr(contentHandler, elements[i]);
335             }
336             return;
337         }
338
339         // Check handled object types in case they were not typed in the XSP
340

341         // XMLizable
342
if (v instanceof XMLizable) {
343             xspExpr(contentHandler, (XMLizable) v);
344             return;
345         }
346
347         // Now handled by XMLizable
348
// // XMLFragment
349
// if (v instanceof XMLFragment)
350
// {
351
// xspExpr(contentHandler, (XMLFragment)v);
352
// return;
353
// }
354

355         // Node
356
if (v instanceof Node JavaDoc) {
357             xspExpr(contentHandler, (Node JavaDoc) v);
358             return;
359         }
360
361         // Collection
362
if (v instanceof Collection JavaDoc) {
363             xspExpr(contentHandler, (Collection JavaDoc) v);
364             return;
365         }
366
367         // Give up: hope it's a string or has a meaningful string representation
368
data(contentHandler, String.valueOf(v));
369     }
370 }
371
Popular Tags