Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: edu/ou/kmi/buddyspace/xml/XDataFieldHandler.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>&lt;shawn@black9.net&gt;</i></a>
23   *
24   * @author  $Author: brouk $
25   * @version $Revision: 1.2 $
26   *
27   * j.komzak
28   * Changed into XDataFieldHandler
29   */
30  
31  package edu.ou.kmi.buddyspace.xml;
32  
33  /*
34   * XDataFieldHandler.java
35   *
36   * Project: BuddySpace
37   * (C) Copyright Knowledge Media Institute 2002
38   *
39   *
40   * Created on 5 February 2003, 11:56
41   */
42  import org.jabber.jabberbeans.Extension.*;
43  import org.jabber.jabberbeans.util.*;
44  import org.jabber.jabberbeans.*;
45  import org.jabber.jabberbeans.sax.SubHandler;
46  import org.xml.sax.SAXException;
47  import org.xml.sax.AttributeList;
48  
49  /**
50   * Handler class to build &lt;field&gt objects inside jabber:x:data
51   *
52   * @author  Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
53   */
54  public class XDataFieldHandler extends SubHandler
55  {
56    /** used to capture data between element tags */
57    private StringBuffer elementChars;
58  
59    /** builder for XDataField objects */
60    private XDataFieldBuilder builder;
61  
62  
63    /*
64     * Creates a new <code>XDataFieldHandler</code> instance.
65     */
66    public XDataFieldHandler()
67    {
68      super();
69      builder = new XDataFieldBuilder();
70    }
71  
72  
73    /*
74     * This is an exact copy of the characters function in the main handler
75     *
76     * @param ch character string detected
77     * @param start start position
78     * @param length length of string
79     * @exception SAXException thrown on error
80     */
81    public void characters(char[] ch, int start, int length) throws SAXException
82    {elementChars.append(ch,start,length);}
83  
84    
85    /**
86           * Gets called when the underlying engine decides to pass an entity and
87           * all sub-entities off to your subhandler.<p>
88           *
89           * Upon seeing the element that this subhandler handles, we call this
90           * constructor, passing in the attributes.
91           *
92           * @param name name of the element which we are handling.
93           * @param attributes list of attributes on this element
94           */
95          protected final void startHandler(String name,AttributeList attributes)
96              throws SAXException
97          {
98              elementChars=new StringBuffer();
99              builder.reset();
100             
101             builder.setType(attributes.getValue("type"));
102             builder.setLabel(attributes.getValue("label"));
103             builder.setVar(attributes.getValue("var"));
104         }
105     
106         /*
107    * <code>handleStartElement</code> is overloaded by the new class to
108    * provide logic to handle the element code.
109    *
110    * @param name a <code>String</code> value
111    * @param attributes an <code>AttributeList</code> value
112    * @exception SAXException if an error occurs
113    */
114   protected void handleStartElement(String name, AttributeList attributes) throws SAXException
115   {      
116     elementChars=new StringBuffer();
117 
118     //Start new handler for items that *may* be containers, but never for namespaces "ns".
119     if ("value".equals(name) || "desc".equals(name))
120                     //setChildSubHandler(new SingleValueTagHandler(), name, attributes);
121                     return;
122                 
123                 else if ("option".equals(name))
124                     setChildSubHandler(new XDataFieldOptionHandler(), name, attributes);
125       
126                 else if ("required".equals(name))
127                     builder.setRequired(true);
128   }
129 
130         
131   /*
132    * <code>handleEndElement</code> is overloaded by the new class to
133    * provide logic to handle element code.
134    *
135    * @param name a <code>String</code> value
136    * @exception SAXException if an error occurs
137    */
138   protected void handleEndElement(String name) throws SAXException
139   {
140                 if("value".equals(name))
141                     builder.addValue(new String(elementChars));
142                 
143                 else if("desc".equals(name))
144                     builder.setDesc(new String(elementChars));
145   }
146 
147         
148   /*
149    * Stophandler is the same as end element, except that it is called saying
150    * that the subhandler is no longer in scope.
151    *
152    * @param Object a value being returned to the parent - the parent is 
153    * meant to interpret this result.
154    */
155   protected Object stopHandler(String name) throws SAXException
156   {
157     try{return builder.build();}
158     catch (InstantiationException e)
159     {
160       e.fillInStackTrace();
161       throw new SAXException(e);
162     }
163   }
164   
165   
166   /*
167    * <code>receiveChildData</code> is called when a child handler exits,
168    * returning control to this code. The now-defunct handler along with the
169    * data object are both returned.
170    *
171    * @param subHandler a <code>SubHandler</code> value
172    * @param o an <code>Object</code> value
173    */
174   protected void receiveChildData(SubHandler subHandler, Object o)
175   {
176     //Add the returned object to the child vector
177     builder.addChild((XMLData)o);
178   }
179 }