01: package org.gomba.utils.xml;
02:
03: import org.xml.sax.Attributes;
04: import org.xml.sax.ContentHandler;
05: import org.xml.sax.SAXException;
06: import org.xml.sax.helpers.AttributesImpl;
07:
08: /**
09: * Utility class to generate SAX events using a <code>ContentHandler</code>.
10: *
11: * @author Flavio Tordini
12: * @version $Id: ContentHandlerUtils.java,v 1.1.1.1 2004/06/16 13:15:12 flaviotordini Exp $
13: */
14: public final class ContentHandlerUtils {
15:
16: /**
17: * Empty namespace URI.
18: */
19: public static final String DUMMY_NSU = "";
20:
21: /**
22: * Empty attributes.
23: */
24: public static final Attributes DUMMY_ATTS = new AttributesImpl();
25:
26: /**
27: * Private constructor.
28: */
29: private ContentHandlerUtils() {
30: // dummy
31: }
32:
33: public static void tag(ContentHandler handler, String tagName,
34: String text) throws SAXException {
35: tag(handler, tagName, text, DUMMY_ATTS, DUMMY_NSU);
36: }
37:
38: public static void tag(ContentHandler handler, String tagName,
39: String text, Attributes atts) throws SAXException {
40: tag(handler, tagName, text, atts, DUMMY_NSU);
41: }
42:
43: /**
44: * Create SAX events for an element that has not any child.
45: *
46: * @param handler
47: * The SAX ContentHandler
48: * @param tagName
49: * the name of the element
50: * @param text
51: * the element body
52: * @param atts
53: * the element attributes
54: * @param nsu
55: * the namespace URI
56: */
57: public static void tag(ContentHandler handler, String tagName,
58: String text, Attributes atts, String nsu)
59: throws SAXException {
60: // if we don't have no text and no attributes let's not create the
61: // element at all
62: if (text == null && atts.equals(DUMMY_ATTS)) {
63: return;
64: }
65:
66: handler.startElement(nsu, tagName, tagName, DUMMY_ATTS);
67: if (text != null) {
68: handler.characters(text.toCharArray(), 0, text.length());
69: }
70: handler.endElement(nsu, tagName, tagName);
71: }
72:
73: }
|