Source code: edu/ou/kmi/buddyspace/xml/XMLFileDocHandler.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 * Portions created by or assigned to Jabber.com, Inc. are
18 * Copyright (c) 2000 Jabber.com, Inc. All Rights Reserved. Contact
19 * information for Jabber.com, Inc. is available at http://www.jabber.com/.
20 *
21 * Portions Copyright (c) 1999-2000 David Waite
22 *
23 * Acknowledgements
24 *
25 * Special thanks to the Jabber Open Source Contributors for their
26 * suggestions and support of Jabber.
27 *
28 * Changes
29 *
30 * @author David Waite <a href="mailto:dwaite@jabber.com">
31 * <i><dwaite@jabber.com></i></a>
32 * @author $Author: brouk $
33 * @version $Revision: 1.4 $
34 *
35 * j.komzak
36 * Changed to work with files.
37 */
38
39 package edu.ou.kmi.buddyspace.xml;
40
41 /*
42 * XMLFileDocHandler.java
43 *
44 * Project: BuddySpace
45 * (C) Copyright Knowledge Media Institute 2002
46 *
47 *
48 * Created on 28 August 2002, 10:50
49 */
50
51 import org.xml.sax.*;
52
53 import org.jabber.jabberbeans.*;
54 import org.jabber.jabberbeans.sax.*;
55 import org.jabber.jabberbeans.Packet;
56 import org.jabber.jabberbeans.ConnectionBean;
57
58 import java.io.IOException;
59 import java.io.FileNotFoundException;
60
61 /**
62 * Main file handler given to a SAX-enabled parser.
63 *
64 * @author Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
65 */
66 public class XMLFileDocHandler extends SubHandler {
67 /** instantiating handler, used for sending messages back up
68 * the pipe. */
69 protected XMLFileDocInterface isi;
70
71 /**
72 * Creates a new <code>XMLFileDocHandler</code> instance.
73 */
74 public XMLFileDocHandler()
75 {
76 super();
77 try
78 {
79 setHandlerFactory(new HandlerFactory());
80 }
81 catch (IOException e)
82 {
83 throw new RuntimeException
84 ("error with extension factory");
85 }
86 }
87
88 /**
89 * set the XMLFileDocInterface, used to inform client code of new
90 * received data.
91 *
92 * @param isi XMLFileDocInterface
93 */
94 public final void setDataHandler(XMLFileDocInterface isi)
95 { this.isi = isi; }
96
97 /**
98 * handle the start of an element, including finding an appropriate
99 * handler for the element or namespace type
100 *
101 * @param name element name
102 * @param attributes element attributes
103 * @throws SAXException unknown root element, or XML parsing error
104 */
105 public void handleStartElement(String name,
106 AttributeList attributes)
107 throws SAXException
108 {
109 SubHandler subhandler=getHandlerFactory().
110 getHandlerInstance(name,attributes);
111
112 if (subhandler==null)
113 throw new SAXException("Unknown/invalid root element: " + name);
114 setChildSubHandler(subhandler,name,attributes);
115 }
116
117 /**
118 * <code>handleEndElement</code> method
119 *
120 * @param name a <code>String</code> value
121 * @exception SAXException if an error occurs
122 */
123 public final void handleEndElement(String name)
124 throws SAXException
125 { }
126
127 /**
128 * <code>receiveChildData</code> receives packets constructed by the
129 * subordinate handlers, and shuttles it to the protocol handler
130 *
131 * @param subHandler a <code>SubHandler</code> value of the handler the
132 * data came from, unused
133 * @param p an <code>Object</code> value, a Packet
134 * @throws SAXException if the data is invalid (i.e. null)
135 */
136 public void receiveChildData(SubHandler subHandler,Object p)
137 throws SAXException
138 {
139 if (p==null)
140 throw new SAXException("null data returned from child");
141
142 isi.received((XMLData)p);
143 }
144
145 /**
146 * <code>stopHandler</code> returns null since there is no 'data'
147 * returned which was not already sent via the 'receive' method.
148 *
149 * @return an <code>Object</code> value, unused.
150 * @exception SAXException if an error occurs
151 */
152 public final Object stopHandler(String name)
153 throws SAXException
154 {return null;}
155 }