1 16 package org.apache.cocoon.components.language.markup.xsp; 17 18 import java.util.Collection ; 19 import java.util.Iterator ; 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 ; 26 import org.xml.sax.ContentHandler ; 27 import org.xml.sax.SAXException ; 28 import org.xml.sax.helpers.AttributesImpl ; 29 30 38 public class XSPObjectHelper { 39 40 47 protected static void elementData(String uri, 48 String prefix, 49 ContentHandler contentHandler, 50 String name, 51 String data) 52 throws SAXException { 53 start(uri, prefix, contentHandler, name); 54 data(contentHandler, data); 55 end(uri, prefix, contentHandler, name); 56 } 57 58 66 protected static void elementData(String uri, 67 String prefix, 68 ContentHandler contentHandler, 69 String name, 70 String data, 71 AttributesImpl attr) 72 throws SAXException { 73 start(uri, prefix, contentHandler, name, attr); 74 data(contentHandler, data); 75 end(uri, prefix, contentHandler, name); 76 } 77 78 85 protected static void start(String uri, 86 String prefix, 87 ContentHandler contentHandler, 88 String name) 89 throws SAXException { 90 contentHandler.startElement(uri, name, prefix + ":" + name, XMLUtils.EMPTY_ATTRIBUTES); 91 } 92 93 101 protected static void start(String uri, 102 String prefix, 103 ContentHandler contentHandler, 104 String name, 105 AttributesImpl attr) 106 throws SAXException { 107 contentHandler.startElement(uri, name, prefix + ":" + name, attr); 108 } 109 110 116 protected static void end(String uri, 117 String prefix, 118 ContentHandler contentHandler, 119 String name) 120 throws SAXException { 121 contentHandler.endElement(uri, name, prefix + ":" + name); 122 } 123 124 131 protected static void addAttribute(AttributesImpl attr, 132 String name, 133 String value) 134 throws SAXException { 135 attr.addAttribute("", name, name, "CDATA", value); 136 } 137 138 144 protected static void data(ContentHandler contentHandler, String data) 145 throws SAXException { 146 contentHandler.characters(data.toCharArray(), 0, data.length()); 147 } 148 149 151 158 public static void xspExpr(ContentHandler contentHandler, char v) 159 throws SAXException { 160 data(contentHandler, String.valueOf(v)); 161 } 162 163 170 public static void xspExpr(ContentHandler contentHandler, byte v) 171 throws SAXException { 172 data(contentHandler, String.valueOf(v)); 173 } 174 175 182 public static void xspExpr(ContentHandler contentHandler, boolean v) 183 throws SAXException { 184 data(contentHandler, String.valueOf(v)); 185 } 186 187 194 public static void xspExpr(ContentHandler contentHandler, int v) 195 throws SAXException { 196 data(contentHandler, String.valueOf(v)); 197 } 198 199 206 public static void xspExpr(ContentHandler contentHandler, long v) 207 throws SAXException { 208 data(contentHandler, String.valueOf(v)); 209 } 210 211 218 public static void xspExpr(ContentHandler contentHandler, float v) 219 throws SAXException { 220 data(contentHandler, String.valueOf(v)); 221 } 222 223 230 public static void xspExpr(ContentHandler contentHandler, double v) 231 throws SAXException { 232 data(contentHandler, String.valueOf(v)); 233 } 234 235 242 public static void xspExpr(ContentHandler contentHandler, String text) 243 throws SAXException { 244 if (text != null) { 245 data(contentHandler, text); 246 } 247 } 248 249 265 272 public static void xspExpr(ContentHandler contentHandler, XMLizable v) 273 throws SAXException { 274 if (v != null) { 275 v.toSAX(contentHandler); 276 } 277 } 278 279 286 public static void xspExpr(ContentHandler contentHandler, Node v) 287 throws SAXException { 288 if (v != null) { 289 DOMStreamer streamer = new DOMStreamer(contentHandler); 290 streamer.stream(v); 291 } 292 } 293 294 302 public static void xspExpr(ContentHandler contentHandler, Collection v) 303 throws SAXException { 304 if (v != null) { 305 Iterator iterator = v.iterator(); 306 while (iterator.hasNext()) { 307 xspExpr(contentHandler, iterator.next()); 308 } 309 } 310 } 311 312 323 public static void xspExpr(ContentHandler contentHandler, Object v) 324 throws SAXException { 325 if (v == null) { 326 return; 327 } 328 329 if (v.getClass().isArray()) { 331 Object [] elements = (Object []) v; 332 333 for (int i = 0; i < elements.length; i++) { 334 xspExpr(contentHandler, elements[i]); 335 } 336 return; 337 } 338 339 341 if (v instanceof XMLizable) { 343 xspExpr(contentHandler, (XMLizable) v); 344 return; 345 } 346 347 355 if (v instanceof Node ) { 357 xspExpr(contentHandler, (Node ) v); 358 return; 359 } 360 361 if (v instanceof Collection ) { 363 xspExpr(contentHandler, (Collection ) v); 364 return; 365 } 366 367 data(contentHandler, String.valueOf(v)); 369 } 370 } 371 | Popular Tags |