1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package net.sf.stax;
25
26 import org.xml.sax.Attributes;
27 import org.xml.sax.SAXException;
28 import org.xml.sax.SAXParseException;
29
30 /***
31 * StAX handler for any element which just contains a string.
32 * The boolean value, returned by the <code>endTree(StAXContext)</code>
33 * method, is available to a parent handler via its <code>endElement(String,
34 * String, String, Object, StAXContext)</code> method.
35 *
36 * <p>Ex.
37 * <pre>
38 * class MyHandler
39 * extends StAXContentHandlerBase
40 * {
41 * private String stringValue;
42 * private StringElementHandler stringHandler = new StringElementHandler();
43 *
44 * public void startElement(..., StAXDelegationContext dctx)
45 * {
46 * // ...
47 * dctx.delegate(stringHandler);
48 * }
49 *
50 * public void endElement(..., Object result, ...)
51 * {
52 * this.stringValue = (String) result;
53 * }
54 * }
55 * </pre>
56 * </p>
57 *
58 * <p>This class also supports mixed content models by subclassing
59 * and overriding the <code>startChildElement(String, String, String,
60 * Attributes, StAXDelegationContext)</code> method. By default, this
61 * class throws an exception if it sees a child element.</p>
62 *
63 * @see #startChildElement(String, String, String, Attributes, StAXDelegationContext)
64 * @see StAXContentHandlers#getStringElementHandler
65 *
66 * @author Matthew Pocock
67 * @author Thomas Down
68 * @author Michael Heuer
69 * @version $Revision: 1.6 $ $Date: 2006/01/02 20:37:34 $
70 */
71 public class StringElementHandler
72 extends StAXContentHandlerBase
73 {
74 /*** Nesting level. */
75 private int level = 0;
76
77 /*** Buffer to hold character data. */
78 private StringBuffer data = new StringBuffer();
79
80
81 /*** @see StAXContentHandler */
82 public final void startElement(final String nsURI,
83 final String localName,
84 final String qName,
85 final Attributes attrs,
86 final StAXDelegationContext dctx)
87 throws SAXException
88 {
89 level++;
90 if (level > 1)
91 {
92 startChildElement(nsURI, localName, qName, attrs, dctx);
93 }
94 data.delete(0, data.length());
95 }
96
97 /*** @see StAXContentHandler */
98 public void endElement(final String nsURI,
99 final String localName,
100 final String qName,
101 final Object result,
102 final StAXContext ctx)
103 throws SAXException
104 {
105 level--;
106 }
107
108 /***
109 * Handle any child elements found. By default, this throws
110 * an exception, but it may be overridden to handle mixed content
111 * models.
112 *
113 * @param nsURI namespace URI
114 * @param localName local name (without prefix)
115 * @param qName qualified XML name (with prefix)
116 * @param attrs attributes attached to the element
117 * @param dctx StAX delegation context
118 * @throws SAXException any SAX exception, possibly wrapping another exception
119 */
120 protected void startChildElement(final String nsURI,
121 final String localName,
122 final String qName,
123 final Attributes attrs,
124 final StAXDelegationContext dctx)
125 throws SAXException
126 {
127 throw new SAXParseException("found child element when expecting character data",
128 dctx.getLocator());
129 }
130
131 /*** @see StAXContentHandler */
132 public final void characters(final char[] ch,
133 final int start,
134 final int length,
135 final StAXContext ctx)
136 throws SAXException
137 {
138 data.append(ch, start, length);
139 }
140
141 /*** @see StAXContentHandler */
142 public final Object endTree(final StAXContext ctx)
143 throws SAXException
144 {
145 return data.substring(0);
146 }
147 }