Source code: edu/ou/kmi/buddyspace/xml/FileHandler.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 * dwaite 20001007
31 * Changed reliance on ProtocolHandler to be a reliance on ConnectionBean.
32 * This will probably change in the near future so that it instead receives
33 * a reference to an accessor class.
34 * @author David Waite <a href="mailto:dwaite@jabber.com">
35 * <i><dwaite@jabber.com></i></a>
36 * @author $Author: brouk $
37 * @version $Revision: 1.4 $
38 *
39 * j.komzak
40 * Changed to provide access to files.
41 */
42
43 package edu.ou.kmi.buddyspace.xml;
44
45 /*
46 * FileHandler.java
47 *
48 * Project: BuddySpace
49 * (C) Copyright Knowledge Media Institute 2002
50 *
51 *
52 * Created on 28 August 2002, 10:50
53 */
54
55 import java.io.*;
56
57 import org.jabber.jabberbeans.*;
58 import org.jabber.jabberbeans.sax.*;
59
60 import org.xml.sax.*;
61
62 /**
63 * <code>FileHandler</code> provides XML storing/reading in/from files.
64 * It relies on <code>XMLStreamDocumentHandler</code> and
65 * a <code>Parser</code>.
66 *
67 * @author Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
68 */
69 public class FileHandler implements XMLFileDocInterface {
70
71 private InputStream in = null;
72 private String parsername;
73 private boolean parsingDone;
74 private static final String CHANGE_PARSER_TOO_LATE =
75 "You must set the parser before connecting.";
76 private static final String DEFAULT_PARSER = "com.microstar.xml.SAXDriver";
77 private XMLFileDocInterface listener = null;
78
79 /** Constructor */
80 public FileHandler() {
81 parsername = DEFAULT_PARSER;
82 }
83
84 /** Sets input stream */
85 public void setInputStream(InputStream in)
86 {
87 this.in = in;
88 parsingDone = false;
89 }
90
91 /** Sets listener */
92 public void setListener(XMLFileDocInterface listener) {
93 this.listener = listener;
94 }
95
96 /** Runs the handler */
97 public final void run()
98 {
99 Parser parser;
100 XMLFileDocHandler docHandler;
101
102 if (in == null)
103 throw new RuntimeException
104 ("attempting to start InputStreamHandler without input to " +
105 "monitor");
106 try {
107 parser = (Parser)Class.forName(parsername).newInstance();
108 }
109 catch (Exception e) {
110 throw new RuntimeException("InputStreamHandler:"+e.toString());
111 }
112
113 docHandler=new XMLFileDocHandler();
114 docHandler.setDataHandler(this);
115
116 ((SubHandler)docHandler).setParser(parser);
117
118 parser.setDocumentHandler(docHandler);
119
120 try
121 {
122 final InputSource is = new InputSource(new InputStreamReader(in,"UTF8"));
123
124 parser.parse(is);
125 }
126 catch (IOException e)
127 {
128 if (parsingDone == true)
129 return;
130
131 unexpectedThreadDeath(e);
132 }
133 catch (SAXException e)
134 {
135 if (parsingDone == true)
136 return;
137 unexpectedThreadDeath(e);
138 }
139
140 parsingDone = true;
141 }
142
143 /** Shuts down */
144 public void shutdown() {
145 parsingDone = true;
146 }
147
148 /** Sets parser */
149 public void setParser(String parsername) {
150 if (in != null)
151 throw new RuntimeException(CHANGE_PARSER_TOO_LATE);
152 this.parsername = parsername;
153 }
154
155 /** Returns parser */
156 public String getParser() {
157 return parsername;
158 }
159
160 // *** implementation of interface ***
161
162 /** Called when xml data received - sets data in listener */
163 public void received(XMLData d) {
164 if (listener != null)
165 listener.received(d);
166 }
167
168 /** Called on unexpected thread death */
169 public void unexpectedThreadDeath(Exception e) {
170 if (listener != null)
171 listener.unexpectedThreadDeath(e);
172 }
173
174 }