View Javadoc

1   /*
2   
3       stax  Stack API for XML.
4       Copyright (c) 2001-2006 held jointly by the individual authors.
5   
6       This library is free software; you can redistribute it and/or modify it
7       under the terms of the GNU Lesser General Public License as published
8       by the Free Software Foundation; either version 2.1 of the License, or (at
9       your option) any later version.
10  
11      This library is distributed in the hope that it will be useful, but WITHOUT
12      ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
13      FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14      License for more details.
15  
16      You should have received a copy of the GNU Lesser General Public License
17      along with this library;  if not, write to the Free Software Foundation,
18      Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
19  
20      > http://www.gnu.org/copyleft/lesser.html
21      > http://www.opensource.org/licenses/lgpl-license.php
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 }