package nu.fw.jeti.backend; import java.io.*; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import nu.fw.jeti.plugins.PluginsInfo; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; /** *
Title: im
*Description:
*Copyright: Copyright (c) 2001
*Company:
* @author E.S. de Boer * @version 1.0 */ public class Input extends Thread { private InputSource in; private ConnectionPacketReceiver backend; private JabberHandler jH; private boolean closing = false; public Input(InputStream stream,ConnectionPacketReceiver backend,JabberHandler jH) { try{ if(PluginsInfo.isPluginLoaded("xmpp")) { in = new InputSource(new XMPPInputStream(new InputStreamReader(stream,"UTF-8"))); } else { in = new InputSource(new InputStreamReader(stream,"UTF8")); //in = new InputSource(new LogInputStream(new InputStreamReader(stream,"UTF8"))); } }catch(UnsupportedEncodingException e){e.printStackTrace();} this.backend = backend; this.jH = jH; start(); } public void disconnect(){ try { System.out.println("closing " + in.getCharacterStream()); closing = true; in.getCharacterStream().close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void run() { try{ /** @todo look at parsers */ //direct, higer mem? //SAXParser parser =new org.apache.crimson.jaxp.SAXParserFactoryImpl().newSAXParser(); SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); parser.parse(in,jH); } catch(ParserConfigurationException pce){pce.printStackTrace();} catch (SAXParseException spe) { Exception e = spe.getException(); if(e != null) e.printStackTrace(); else System.err.println(spe.getMessage()); } catch(SAXException se) { if("end xmlparser".equals(se.getMessage())) { closing = true; return; } Exception e = se.getException(); if(e != null) e.printStackTrace(); else System.err.println(se.getMessage()); } catch(IOException e) { // No need to do anything here. System.err.println(e.getMessage()); } catch (UnsupportedOperationException e) { if("end xmlparser".equals(e.getMessage())) { closing = true; return; } System.err.println(e.getMessage()); } finally{ System.out.println(in.getCharacterStream() + "closed"); if(!closing) backend.inputDeath(); } } // //prints inputstream // class LogInputStream extends FilterReader // { // LogInputStream(Reader reader) // { // super(reader); // } // // public int read(char[] cbuf, int off, int len) throws IOException // { // int readed = super.read(cbuf, off, len); // if(readed == -1) System.out.println("end of stream detected readed"); // else System.out.println(new String(cbuf, off,readed).trim()); // return readed; // } // // // public int read(char[] cbuf) throws IOException // { // int readed = super.read(cbuf); // System.out.println(new String(cbuf).trim()); // return readed; // } // // public void close() // {//stop close of stream from parser // System.out.println("close stream"); // } // } } class XMPPInputStream extends FilterReader { XMPPInputStream(Reader reader) { super(reader); } //stop close of stream from parser, //needed to prevent stopping xml parser to close the socket public void close() { System.out.println("close stream" + this); } } /* * Overrides for emacs * Local variables: * tab-width: 4 * End: */