Source code: edu/ou/kmi/buddyspace/xml/XDataFieldOptionHandler.java
1 /*
2 * License
3 *
4 * The contents of this file are subject to the Jabber Open Source License
5 * Version 1.0 (the "License"). You may not copy or use this file, in either
6 * source code or executable form, except in compliance with the License. You
7 * may obtain a copy of the License at http://www.jabber.com/license/ or at
8 * http://www.opensource.org/.
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * Copyrights
16 *
17 * Shawn Wilton
18 *
19 * Changes
20 *
21 * @author Shawn Wilton <a href="mailto:shawn@black9.net">
22 * <i><shawn@black9.net></i></a>
23 *
24 * @author $Author: brouk $
25 * @version $Revision: 1.2 $
26 *
27 * j.komzak
28 * Changed into XDataFieldOptionHandler
29 */
30
31 package edu.ou.kmi.buddyspace.xml;
32
33 /*
34 * XDataFieldOptionHandler.java
35 *
36 * Project: BuddySpace
37 * (C) Copyright Knowledge Media Institute 2002
38 *
39 *
40 * Created on 5 February 2003, 13:02
41 */
42 import org.jabber.jabberbeans.Extension.*;
43 import org.jabber.jabberbeans.util.*;
44 import org.jabber.jabberbeans.sax.SubHandler;
45 import org.xml.sax.SAXException;
46 import org.xml.sax.AttributeList;
47
48 /**
49 * Handler class to build <option> objects inside jabber:x:data fields
50 *
51 * @author Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
52 */
53 public class XDataFieldOptionHandler extends SubHandler
54 {
55 /** used to capture data between element tags */
56 private StringBuffer elementChars;
57
58 /** builder for XDataFieldOption objects */
59 private XDataFieldOptionBuilder builder;
60
61
62 /*
63 * Creates a new <code>XDataFieldOptionHandler</code> instance.
64 */
65 public XDataFieldOptionHandler()
66 {
67 super();
68 builder = new XDataFieldOptionBuilder();
69 }
70
71
72 /*
73 * This is an exact copy of the characters function in the main handler
74 *
75 * @param ch character string detected
76 * @param start start position
77 * @param length length of string
78 * @exception SAXException thrown on error
79 */
80 public void characters(char[] ch, int start, int length) throws SAXException
81 {elementChars.append(ch,start,length);}
82
83
84 /**
85 * Gets called when the underlying engine decides to pass an entity and
86 * all sub-entities off to your subhandler.<p>
87 *
88 * Upon seeing the element that this subhandler handles, we call this
89 * constructor, passing in the attributes.
90 *
91 * @param name name of the element which we are handling.
92 * @param attributes list of attributes on this element
93 */
94 protected final void startHandler(String name,AttributeList attributes)
95 throws SAXException
96 {
97 elementChars=new StringBuffer();
98 builder.reset();
99
100 builder.setLabel(attributes.getValue("label"));
101 }
102
103 /*
104 * <code>handleStartElement</code> is overloaded by the new class to
105 * provide logic to handle the element code.
106 *
107 * @param name a <code>String</code> value
108 * @param attributes an <code>AttributeList</code> value
109 * @exception SAXException if an error occurs
110 */
111 protected void handleStartElement(String name, AttributeList attributes) throws SAXException
112 {
113 elementChars=new StringBuffer();
114
115 //Start new handler for items that *may* be containers, but never for namespaces "ns".
116 if ("value".equals(name))
117 ;//setChildSubHandler(new SingleValueTagHandler(), name, attributes);
118 }
119
120
121 /*
122 * <code>handleEndElement</code> is overloaded by the new class to
123 * provide logic to handle element code.
124 *
125 * @param name a <code>String</code> value
126 * @exception SAXException if an error occurs
127 */
128 protected void handleEndElement(String name) throws SAXException
129 {
130 if("value".equals(name))
131 builder.setValue(new String(elementChars));
132 }
133
134
135 /*
136 * Stophandler is the same as end element, except that it is called saying
137 * that the subhandler is no longer in scope.
138 *
139 * @param Object a value being returned to the parent - the parent is
140 * meant to interpret this result.
141 */
142 protected Object stopHandler(String name) throws SAXException
143 {
144 try{return builder.build();}
145 catch (InstantiationException e)
146 {
147 e.fillInStackTrace();
148 throw new SAXException(e);
149 }
150 }
151
152
153 /*
154 * <code>receiveChildData</code> is called when a child handler exits,
155 * returning control to this code. The now-defunct handler along with the
156 * data object are both returned.
157 *
158 * @param subHandler a <code>SubHandler</code> value
159 * @param o an <code>Object</code> value
160 */
161 protected void receiveChildData(SubHandler subHandler, Object o)
162 {
163 //Add the returned object to the child vector
164 //builder.addValue((SingleValueTag)o);
165 }
166 }