001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.language.markup.xsp;
018:
019: import java.util.Collection;
020: import java.util.Iterator;
021:
022: import org.apache.cocoon.xml.dom.DOMStreamer;
023: import org.apache.cocoon.xml.XMLUtils;
024:
025: import org.apache.excalibur.xml.sax.XMLizable;
026: import org.w3c.dom.Node;
027: import org.xml.sax.ContentHandler;
028: import org.xml.sax.SAXException;
029: import org.xml.sax.helpers.AttributesImpl;
030:
031: /**
032: * Base class for XSP's object model manipulation logicsheets
033: *
034: * @author <a href="mailto:ricardo@apache.org">Ricardo Rocha</a>
035: * @author <a href="mailto:sylvain.wallez@anyware-tech.com">Sylvain Wallez</a>
036: * (Cocoon1 <code>xspExpr()</code> methods port)
037: * @version $Id: XSPObjectHelper.java 433543 2006-08-22 06:22:54Z crossley $
038: */
039: public class XSPObjectHelper {
040:
041: /**
042: * Output an element containing text only and no attributes
043: *
044: * @param contentHandler The SAX content handler
045: * @param name The element name
046: * @param data The data contained by the element
047: */
048: protected static void elementData(String uri, String prefix,
049: ContentHandler contentHandler, String name, String data)
050: throws SAXException {
051: start(uri, prefix, contentHandler, name);
052: data(contentHandler, data);
053: end(uri, prefix, contentHandler, name);
054: }
055:
056: /**
057: * Output an element containing text only and attributes
058: *
059: * @param contentHandler The SAX content handler
060: * @param name The element name
061: * @param data The data contained by the element
062: * @param attr The element attributes
063: */
064: protected static void elementData(String uri, String prefix,
065: ContentHandler contentHandler, String name, String data,
066: AttributesImpl attr) throws SAXException {
067: start(uri, prefix, contentHandler, name, attr);
068: data(contentHandler, data);
069: end(uri, prefix, contentHandler, name);
070: }
071:
072: /**
073: * Start an element with the proper object's uri and prefix and no
074: * attributes
075: *
076: * @param contentHandler The SAX content handler
077: * @param name The element name
078: */
079: protected static void start(String uri, String prefix,
080: ContentHandler contentHandler, String name)
081: throws SAXException {
082: contentHandler.startElement(uri, name, prefix + ":" + name,
083: XMLUtils.EMPTY_ATTRIBUTES);
084: }
085:
086: /**
087: * Start an element with the proper object's uri and prefix and with
088: * attributes
089: *
090: * @param contentHandler The SAX content handler
091: * @param name The element name
092: * @param attr The element attributes
093: */
094: protected static void start(String uri, String prefix,
095: ContentHandler contentHandler, String name,
096: AttributesImpl attr) throws SAXException {
097: contentHandler.startElement(uri, name, prefix + ":" + name,
098: attr);
099: }
100:
101: /**
102: * End an element with the proper object's uri and prefix
103: *
104: * @param contentHandler The SAX content handler
105: * @param name The element name
106: */
107: protected static void end(String uri, String prefix,
108: ContentHandler contentHandler, String name)
109: throws SAXException {
110: contentHandler.endElement(uri, name, prefix + ":" + name);
111: }
112:
113: /**
114: * Add an attribute
115: *
116: * @param attr The attribute list
117: * @param name The attribute name
118: * @param value The attribute value
119: */
120: protected static void addAttribute(AttributesImpl attr,
121: String name, String value) throws SAXException {
122: attr.addAttribute("", name, name, "CDATA", value);
123: }
124:
125: /**
126: * Add string data
127: *
128: * @param contentHandler The SAX content handler
129: * @param data The string data
130: */
131: protected static void data(ContentHandler contentHandler,
132: String data) throws SAXException {
133: contentHandler.characters(data.toCharArray(), 0, data.length());
134: }
135:
136: // <xsp:expr> methods
137:
138: /**
139: * Implementation of <xsp:expr> for <code>char</code> :
140: * outputs characters representing the value.
141: *
142: * @param contentHandler the SAX content handler
143: * @param v the value
144: */
145: public static void xspExpr(ContentHandler contentHandler, char v)
146: throws SAXException {
147: data(contentHandler, String.valueOf(v));
148: }
149:
150: /**
151: * Implementation of <xsp:expr> for <code>byte</code> :
152: * outputs characters representing the value.
153: *
154: * @param contentHandler the SAX content handler
155: * @param v the value
156: */
157: public static void xspExpr(ContentHandler contentHandler, byte v)
158: throws SAXException {
159: data(contentHandler, String.valueOf(v));
160: }
161:
162: /**
163: * Implementation of <xsp:expr> for <code>boolean</code> :
164: * outputs characters representing the value (true / false).
165: *
166: * @param contentHandler the SAX content handler
167: * @param v the value
168: */
169: public static void xspExpr(ContentHandler contentHandler, boolean v)
170: throws SAXException {
171: data(contentHandler, String.valueOf(v));
172: }
173:
174: /**
175: * Implementation of <xsp:expr> for <code>int</code> :
176: * outputs characters representing the value.
177: *
178: * @param contentHandler the SAX content handler
179: * @param v the value
180: */
181: public static void xspExpr(ContentHandler contentHandler, int v)
182: throws SAXException {
183: data(contentHandler, String.valueOf(v));
184: }
185:
186: /**
187: * Implementation of <xsp:expr> for <code>long</code> :
188: * outputs characters representing the value.
189: *
190: * @param contentHandler the SAX content handler
191: * @param v the value
192: */
193: public static void xspExpr(ContentHandler contentHandler, long v)
194: throws SAXException {
195: data(contentHandler, String.valueOf(v));
196: }
197:
198: /**
199: * Implementation of <xsp:expr> for <code>long</code> :
200: * outputs characters representing the value.
201: *
202: * @param contentHandler the SAX content handler
203: * @param v the value
204: */
205: public static void xspExpr(ContentHandler contentHandler, float v)
206: throws SAXException {
207: data(contentHandler, String.valueOf(v));
208: }
209:
210: /**
211: * Implementation of <xsp:expr> for <code>double</code> :
212: * outputs characters representing the value.
213: *
214: * @param contentHandler the SAX content handler
215: * @param v the value
216: */
217: public static void xspExpr(ContentHandler contentHandler, double v)
218: throws SAXException {
219: data(contentHandler, String.valueOf(v));
220: }
221:
222: /**
223: * Implementation of <xsp:expr> for <code>String</code> :
224: * outputs characters representing the value.
225: *
226: * @param contentHandler the SAX content handler
227: * @param text the value
228: */
229: public static void xspExpr(ContentHandler contentHandler,
230: String text) throws SAXException {
231: if (text != null) {
232: data(contentHandler, text);
233: }
234: }
235:
236: // Now handled by XMLizable
237: // /**
238: // * Implementation of <xsp:expr> for <code>XMLFragment</code> :
239: // * outputs the value by calling <code>v.toSax(contentHandler)</code>.
240: // *
241: // * @param contentHandler the SAX content handler
242: // * @param v the XML fragment
243: // */
244: // public static void xspExpr(ContentHandler contentHandler, XMLFragment v) throws SAXException
245: // {
246: // if (v != null)
247: // {
248: // v.toSAX(contentHandler);
249: // }
250: // }
251:
252: /**
253: * Implementation of <xsp:expr> for <code>XMLizable</code> :
254: * outputs the value by calling <code>v.toSax(contentHandler)</code>.
255: *
256: * @param contentHandler the SAX content handler
257: * @param v the XML fragment
258: */
259: public static void xspExpr(ContentHandler contentHandler,
260: XMLizable v) throws SAXException {
261: if (v != null) {
262: v.toSAX(contentHandler);
263: }
264: }
265:
266: /**
267: * Implementation of <xsp:expr> for <code>org.w3c.dom.Node</code> :
268: * converts the Node to a SAX event stream.
269: *
270: * @param contentHandler the SAX content handler
271: * @param v the value
272: */
273: public static void xspExpr(ContentHandler contentHandler, Node v)
274: throws SAXException {
275: if (v != null) {
276: DOMStreamer streamer = new DOMStreamer(contentHandler);
277: streamer.stream(v);
278: }
279: }
280:
281: /**
282: * Implementation of <xsp:expr> for <code>java.util.Collection</code> :
283: * outputs the value by calling <code>xspExpr()</code> on each element of the
284: * collection.
285: *
286: * @param contentHandler the SAX content handler
287: * @param v the XML fragment
288: */
289: public static void xspExpr(ContentHandler contentHandler,
290: Collection v) throws SAXException {
291: if (v != null) {
292: Iterator iterator = v.iterator();
293: while (iterator.hasNext()) {
294: xspExpr(contentHandler, iterator.next());
295: }
296: }
297: }
298:
299: /**
300: * Implementation of <xsp:expr> for <code>Object</code> depending on its class :
301: * <ul>
302: * <li>if it's an array, call <code>xspExpr()</code> on all its elements,</li>
303: * <li>if it's class has a specific <code>xspExpr()</code>implementation, use it,</li>
304: * <li>else, output it's string representation.</li>
305: * </ul>
306: *
307: * @param contentHandler the SAX content handler
308: * @param v the value
309: */
310: public static void xspExpr(ContentHandler contentHandler, Object v)
311: throws SAXException {
312: if (v == null) {
313: return;
314: }
315:
316: // Array: recurse over each element
317: if (v.getClass().isArray()) {
318: Object[] elements = (Object[]) v;
319:
320: for (int i = 0; i < elements.length; i++) {
321: xspExpr(contentHandler, elements[i]);
322: }
323: return;
324: }
325:
326: // Check handled object types in case they were not typed in the XSP
327:
328: // XMLizable
329: if (v instanceof XMLizable) {
330: xspExpr(contentHandler, (XMLizable) v);
331: return;
332: }
333:
334: // Now handled by XMLizable
335: // // XMLFragment
336: // if (v instanceof XMLFragment)
337: // {
338: // xspExpr(contentHandler, (XMLFragment)v);
339: // return;
340: // }
341:
342: // Node
343: if (v instanceof Node) {
344: xspExpr(contentHandler, (Node) v);
345: return;
346: }
347:
348: // Collection
349: if (v instanceof Collection) {
350: xspExpr(contentHandler, (Collection) v);
351: return;
352: }
353:
354: // Give up: hope it's a string or has a meaningful string representation
355: data(contentHandler, String.valueOf(v));
356: }
357: }
|