[1014] | 1 | package nu.fw.jeti.backend; |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | import java.io.*; |
---|
| 5 | |
---|
| 6 | import javax.xml.parsers.ParserConfigurationException; |
---|
| 7 | import javax.xml.parsers.SAXParser; |
---|
| 8 | import javax.xml.parsers.SAXParserFactory; |
---|
| 9 | |
---|
| 10 | import nu.fw.jeti.plugins.PluginsInfo; |
---|
| 11 | |
---|
| 12 | import org.xml.sax.InputSource; |
---|
| 13 | import org.xml.sax.SAXException; |
---|
| 14 | import org.xml.sax.SAXParseException; |
---|
| 15 | |
---|
| 16 | /** |
---|
| 17 | * <p>Title: im</p> |
---|
| 18 | * <p>Description: </p> |
---|
| 19 | * <p>Copyright: Copyright (c) 2001</p> |
---|
| 20 | * <p>Company: </p> |
---|
| 21 | * @author E.S. de Boer |
---|
| 22 | * @version 1.0 |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | public class Input extends Thread |
---|
| 26 | { |
---|
| 27 | private InputSource in; |
---|
| 28 | private ConnectionPacketReceiver backend; |
---|
| 29 | private JabberHandler jH; |
---|
| 30 | private boolean closing = false; |
---|
| 31 | |
---|
| 32 | public Input(InputStream stream,ConnectionPacketReceiver backend,JabberHandler jH) |
---|
| 33 | { |
---|
| 34 | try{ |
---|
| 35 | if(PluginsInfo.isPluginLoaded("xmpp")) |
---|
| 36 | { |
---|
| 37 | in = new InputSource(new XMPPInputStream(new InputStreamReader(stream,"UTF-8"))); |
---|
| 38 | } |
---|
| 39 | else |
---|
| 40 | { |
---|
| 41 | in = new InputSource(new InputStreamReader(stream,"UTF8")); |
---|
| 42 | //in = new InputSource(new LogInputStream(new InputStreamReader(stream,"UTF8"))); |
---|
| 43 | } |
---|
| 44 | }catch(UnsupportedEncodingException e){e.printStackTrace();} |
---|
| 45 | this.backend = backend; |
---|
| 46 | this.jH = jH; |
---|
| 47 | start(); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | public void disconnect(){ |
---|
| 51 | try { |
---|
| 52 | System.out.println("closing " + in.getCharacterStream()); |
---|
| 53 | closing = true; |
---|
| 54 | in.getCharacterStream().close(); |
---|
| 55 | } catch (IOException e) { |
---|
| 56 | // TODO Auto-generated catch block |
---|
| 57 | e.printStackTrace(); |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | public void run() |
---|
| 62 | { |
---|
| 63 | try{ |
---|
| 64 | |
---|
| 65 | /** @todo look at parsers */ |
---|
| 66 | |
---|
| 67 | //direct, higer mem? |
---|
| 68 | //SAXParser parser =new org.apache.crimson.jaxp.SAXParserFactoryImpl().newSAXParser(); |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); |
---|
| 72 | parser.parse(in,jH); |
---|
| 73 | } |
---|
| 74 | catch(ParserConfigurationException pce){pce.printStackTrace();} |
---|
| 75 | catch (SAXParseException spe) |
---|
| 76 | { |
---|
| 77 | Exception e = spe.getException(); |
---|
| 78 | if(e != null) e.printStackTrace(); |
---|
| 79 | else System.err.println(spe.getMessage()); |
---|
| 80 | } |
---|
| 81 | catch(SAXException se) |
---|
| 82 | { |
---|
| 83 | if("end xmlparser".equals(se.getMessage())) { |
---|
| 84 | closing = true; |
---|
| 85 | return; |
---|
| 86 | } |
---|
| 87 | Exception e = se.getException(); |
---|
| 88 | if(e != null) e.printStackTrace(); |
---|
| 89 | else System.err.println(se.getMessage()); |
---|
| 90 | } |
---|
| 91 | catch(IOException e) |
---|
| 92 | { |
---|
| 93 | // No need to do anything here. |
---|
| 94 | System.err.println(e.getMessage()); |
---|
| 95 | } |
---|
| 96 | catch (UnsupportedOperationException e) { |
---|
| 97 | if("end xmlparser".equals(e.getMessage())) { |
---|
| 98 | closing = true; |
---|
| 99 | return; |
---|
| 100 | } |
---|
| 101 | System.err.println(e.getMessage()); |
---|
| 102 | } |
---|
| 103 | finally{ |
---|
| 104 | System.out.println(in.getCharacterStream() + "closed"); |
---|
| 105 | if(!closing) backend.inputDeath(); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | // //prints inputstream |
---|
| 111 | // class LogInputStream extends FilterReader |
---|
| 112 | // { |
---|
| 113 | // LogInputStream(Reader reader) |
---|
| 114 | // { |
---|
| 115 | // super(reader); |
---|
| 116 | // } |
---|
| 117 | // |
---|
| 118 | // public int read(char[] cbuf, int off, int len) throws IOException |
---|
| 119 | // { |
---|
| 120 | // int readed = super.read(cbuf, off, len); |
---|
| 121 | // if(readed == -1) System.out.println("end of stream detected readed"); |
---|
| 122 | // else System.out.println(new String(cbuf, off,readed).trim()); |
---|
| 123 | // return readed; |
---|
| 124 | // } |
---|
| 125 | // |
---|
| 126 | // |
---|
| 127 | // public int read(char[] cbuf) throws IOException |
---|
| 128 | // { |
---|
| 129 | // int readed = super.read(cbuf); |
---|
| 130 | // System.out.println(new String(cbuf).trim()); |
---|
| 131 | // return readed; |
---|
| 132 | // } |
---|
| 133 | // |
---|
| 134 | // public void close() |
---|
| 135 | // {//stop close of stream from parser |
---|
| 136 | // System.out.println("close stream"); |
---|
| 137 | // } |
---|
| 138 | // } |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | class XMPPInputStream extends FilterReader |
---|
| 142 | { |
---|
| 143 | XMPPInputStream(Reader reader) |
---|
| 144 | { |
---|
| 145 | super(reader); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | //stop close of stream from parser, |
---|
| 149 | //needed to prevent stopping xml parser to close the socket |
---|
| 150 | public void close() |
---|
| 151 | { |
---|
| 152 | System.out.println("close stream" + this); |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | /* |
---|
| 157 | * Overrides for emacs |
---|
| 158 | * Local variables: |
---|
| 159 | * tab-width: 4 |
---|
| 160 | * End: |
---|
| 161 | */ |
---|