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/XDataItemHandler.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.1 $
26   *
27   * j.komzak
28   * Changed into XDataItemHandler for forms
29   */
30  
31  package edu.ou.kmi.buddyspace.xml;
32  
33  /*
34   * XDataItemHandler.java
35   *
36   * Project: BuddySpace
37   * (C) Copyright Knowledge Media Institute 2002
38   *
39   *
40   * Created on 26 March 2003, 11:42
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 item tag objects
50   * from &lt;x xmlns='jabber:x:data' type='form'&gt;.
51   *
52   * @author  Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
53   */
54  public class XDataItemHandler extends SubHandler
55  {
56    /** used to capture data between element tags */
57    private StringBuffer elementChars;
58  
59    /** builder for XData objects */
60    private XDataItemBuilder builder;
61  
62    /*
63     * Creates a new <code>XDataHandler</code> instance.
64     */
65    public XDataItemHandler()
66    {
67      super();
68      builder = new XDataItemBuilder();
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.setReported("reported".equals(name));
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 ("field".equals(name))
117                     setChildSubHandler(new XDataFieldHandler(), 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("title".equals(name))
131                     builder.setTitle(new String(elementChars));
132                 else if("instructions".equals(name))
133                     builder.setInstructions(new String(elementChars));*/
134   }
135 
136         
137   /*
138    * Stophandler is the same as end element, except that it is called saying
139    * that the subhandler is no longer in scope.
140    *
141    * @param Object a value being returned to the parent - the parent is 
142    * meant to interpret this result.
143    */
144   protected Object stopHandler(String name) throws SAXException
145   {
146     try{return builder.build();}
147     catch (InstantiationException e)
148     {
149       e.fillInStackTrace();
150       throw new SAXException(e);
151     }
152   }
153   
154   
155   /*
156    * <code>receiveChildData</code> is called when a child handler exits,
157    * returning control to this code. The now-defunct handler along with the
158    * data object are both returned.
159    *
160    * @param subHandler a <code>SubHandler</code> value
161    * @param o an <code>Object</code> value
162    */
163   protected void receiveChildData(SubHandler subHandler, Object o)
164   {
165                 //Add the returned object to the child vector
166                 if (o instanceof XDataField)
167                     builder.addField((XDataField)o);
168                 /*else if (o instanceof XDataItem)
169                     builder.addItem((XDataItem)o);*/
170   }
171 }