[3952] | 1 | /* |
---|
| 2 | * Jeti, a Java Jabber client, Copyright (C) 2001 E.S. de Boer |
---|
| 3 | * |
---|
| 4 | * This program is free software; you can redistribute it and/or modify |
---|
| 5 | * it under the terms of the GNU General Public License as published by |
---|
| 6 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | * (at your option) any later version. |
---|
| 8 | * |
---|
| 9 | * This program is distributed in the hope that it will be useful, |
---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | * GNU General Public License for more details. |
---|
| 13 | * |
---|
| 14 | * You should have received a copy of the GNU General Public License |
---|
| 15 | * along with this program; if not, write to the Free Software |
---|
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | * |
---|
| 18 | * For questions, comments etc, |
---|
| 19 | * use the website at http://jeti.jabberstudio.org |
---|
| 20 | * or mail/IM me at jeti@jabber.org |
---|
| 21 | * |
---|
| 22 | * created 2001 |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | package nu.fw.jeti.backend; |
---|
| 26 | |
---|
| 27 | import nu.fw.jeti.jabber.elements.Packet; |
---|
| 28 | import nu.fw.jeti.jabber.handlers.ExtensionHandler; |
---|
| 29 | import nu.fw.jeti.jabber.handlers.PacketHandler; |
---|
| 30 | import nu.fw.jeti.jabber.handlers.UnknownPacketHandler; |
---|
| 31 | import nu.fw.jeti.util.Log; |
---|
| 32 | import org.xml.sax.Attributes; |
---|
| 33 | import org.xml.sax.SAXException; |
---|
| 34 | import org.xml.sax.helpers.DefaultHandler; |
---|
| 35 | |
---|
| 36 | /** |
---|
| 37 | * @author E.S. de Boer |
---|
| 38 | * @version 1.0 |
---|
| 39 | */ |
---|
| 40 | |
---|
| 41 | public class JabberHandler extends DefaultHandler |
---|
| 42 | { |
---|
| 43 | private int depth;//current depth in xml doc |
---|
| 44 | private PacketHandler packetHandeler; |
---|
| 45 | private ExtensionHandler extensionHandler; |
---|
| 46 | private Handlers handlers; |
---|
| 47 | private PacketReceiver packetReceiver; |
---|
| 48 | private StringBuffer completeXML = new StringBuffer(); |
---|
| 49 | |
---|
| 50 | public JabberHandler(PacketReceiver connect,Handlers handlers) |
---|
| 51 | { |
---|
| 52 | this.handlers =handlers; |
---|
| 53 | this.packetReceiver = connect; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | public void changePacketReceiver(PacketReceiver reciever) |
---|
| 57 | { |
---|
| 58 | packetReceiver = reciever; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | public void startDocument() |
---|
| 62 | { |
---|
| 63 | //System.out.println("<?xml version='1.0' encoding='UTF-8'?>"); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | public void endDocument(){ System.out.println("end document");} |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | public void startElement(String namespaceURI, |
---|
| 71 | String sName, // simple name |
---|
| 72 | String qName, // qualified name |
---|
| 73 | Attributes attrs) |
---|
| 74 | throws SAXException |
---|
| 75 | { |
---|
| 76 | try{ |
---|
| 77 | switch(depth++) |
---|
| 78 | { |
---|
| 79 | case 0: |
---|
| 80 | if("stream:stream".equals(qName)) |
---|
| 81 | { |
---|
| 82 | String sessionID = attrs.getValue("id"); |
---|
| 83 | String xmppVersion = attrs.getValue("version"); |
---|
| 84 | //connected only when connecting so cast to connect make interface for register? |
---|
| 85 | ((ConnectionPacketReceiver)packetReceiver).connected(sessionID,xmppVersion); |
---|
| 86 | //no connection id on sun server so don't throw the error |
---|
| 87 | //if(sessionID == null) throw new SAXException("session id = null"); |
---|
| 88 | return; |
---|
| 89 | } |
---|
| 90 | else if(qName.equals("stream:error")) break; |
---|
| 91 | throw new SAXException("No stream:stream element"); |
---|
| 92 | case 1://iq message and presence |
---|
| 93 | completeXML = new StringBuffer();//new packet so clear stringbuffer |
---|
| 94 | //if(qName.equals("stream:error")) break; |
---|
| 95 | packetHandeler = handlers.getPacketHandler(qName); |
---|
| 96 | extensionHandler = null; |
---|
| 97 | if(packetHandeler == null) |
---|
| 98 | {//unknown handler |
---|
| 99 | Log.notParsedXML(qName + " is not a message, presence or iq packet"); |
---|
| 100 | packetHandeler = new UnknownPacketHandler(); |
---|
| 101 | } |
---|
| 102 | packetHandeler.startHandling(attrs); |
---|
| 103 | break; |
---|
| 104 | default: |
---|
| 105 | String xmlns = attrs.getValue("xmlns"); |
---|
| 106 | if(xmlns !=null) |
---|
| 107 | {//if contains xmlns new extension //extension in extension? |
---|
| 108 | ExtensionHandler tempExtensionHandler = handlers.getExtensionHandler(xmlns); |
---|
| 109 | if(tempExtensionHandler == null) |
---|
| 110 | {// unknown extension handler |
---|
| 111 | tempExtensionHandler = handlers.getExtensionHandler("unknown"); |
---|
| 112 | } |
---|
| 113 | if (extensionHandler != null) tempExtensionHandler.setParent(extensionHandler);//extension in extension |
---|
| 114 | extensionHandler = tempExtensionHandler; |
---|
| 115 | extensionHandler.setName(qName); |
---|
| 116 | extensionHandler.startHandling(attrs); |
---|
| 117 | } |
---|
| 118 | else if(extensionHandler !=null) |
---|
| 119 | {//no new extension |
---|
| 120 | extensionHandler.up(); |
---|
| 121 | extensionHandler.startElement(qName,attrs); |
---|
| 122 | } |
---|
| 123 | else packetHandeler.startElement(qName, attrs); |
---|
| 124 | } |
---|
| 125 | completeXML.append("<"); |
---|
| 126 | completeXML.append(qName); |
---|
| 127 | if (attrs != null) { |
---|
| 128 | String aName = null; |
---|
| 129 | for (int i = 0; i < attrs.getLength(); i++) { |
---|
| 130 | aName = attrs.getQName(i); |
---|
| 131 | completeXML.append(" "); |
---|
| 132 | completeXML.append(aName+"=\""+attrs.getValue(i)+"\""); |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | completeXML.append(">"); |
---|
| 136 | } catch (Exception e){throw new SAXException(e);} |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | public void endElement(String namespaceURI, |
---|
| 140 | String sName, // simple name |
---|
| 141 | String qName // qualified name |
---|
| 142 | ) |
---|
| 143 | throws SAXException |
---|
| 144 | { |
---|
| 145 | try{ |
---|
| 146 | completeXML.append("</"); |
---|
| 147 | completeXML.append(qName); |
---|
| 148 | completeXML.append(">"); |
---|
| 149 | switch(--depth){ |
---|
| 150 | case 0: // We're back at the end of the root |
---|
| 151 | break; |
---|
| 152 | case 1:// The Packet is finished |
---|
| 153 | Log.completeXML(completeXML); |
---|
| 154 | /* |
---|
| 155 | if(qName.equals("stream:error")) |
---|
| 156 | { |
---|
| 157 | Packet packet = packetHandeler.build(); |
---|
| 158 | Log.xmlPacket(packet); |
---|
| 159 | packetReceiver.streamError(packet); |
---|
| 160 | break; |
---|
| 161 | } |
---|
| 162 | */ |
---|
| 163 | try{ |
---|
| 164 | Packet packet = packetHandeler.build(); |
---|
| 165 | Log.xmlPacket(packet); |
---|
| 166 | packetReceiver.receivePackets(packet); |
---|
| 167 | }catch(InstantiationException e){Log.xmlParseException(e);} |
---|
| 168 | catch (UnsupportedOperationException e) |
---|
| 169 | {//only way to stop xml parser |
---|
| 170 | if("end xmlparser".equals(e.getMessage())) {throw new SAXException("end xmlparser");} |
---|
| 171 | } |
---|
| 172 | catch(Exception e) |
---|
| 173 | { |
---|
| 174 | e.printStackTrace(); |
---|
| 175 | } |
---|
| 176 | break; |
---|
| 177 | default: // Move back up the tree |
---|
| 178 | if(extensionHandler != null) |
---|
| 179 | {//in extension |
---|
| 180 | //if(extensionHandler.getName().equals(qName))//name matched start tag so close extension |
---|
| 181 | if(extensionHandler.isTop()) |
---|
| 182 | { |
---|
| 183 | ExtensionHandler parent = extensionHandler.getParent(); |
---|
| 184 | if(parent != null) |
---|
| 185 | {//extension has parent add this extension to parent |
---|
| 186 | try{ |
---|
| 187 | parent.addExtension(extensionHandler.build()); |
---|
| 188 | }catch(InstantiationException e){Log.xmlParseException(e);} |
---|
| 189 | extensionHandler.setParent(null);//clear parent |
---|
| 190 | extensionHandler = parent; |
---|
| 191 | } |
---|
| 192 | else |
---|
| 193 | {//add extension to packet |
---|
| 194 | try{ |
---|
| 195 | packetHandeler.addExtension(extensionHandler.build()); |
---|
| 196 | }catch(InstantiationException e){Log.xmlParseException(e);} |
---|
| 197 | extensionHandler = null; |
---|
| 198 | } |
---|
| 199 | }// element end inside a extension |
---|
| 200 | else |
---|
| 201 | { |
---|
| 202 | extensionHandler.down(); |
---|
| 203 | extensionHandler.endElement(qName); |
---|
| 204 | } |
---|
| 205 | }// element end inside a packet |
---|
| 206 | else packetHandeler.endElement(qName); |
---|
| 207 | } |
---|
| 208 | } catch (Exception e){throw new SAXException(e);} |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | public void characters(char buf[], int offset, int len) throws SAXException |
---|
| 212 | { |
---|
| 213 | try{ |
---|
| 214 | String text = new String(buf, offset,len); //.trim(); |
---|
| 215 | if(extensionHandler != null) extensionHandler.characters(text); |
---|
| 216 | else if (packetHandeler!=null) packetHandeler.characters(text); |
---|
| 217 | completeXML.append(text.trim()); |
---|
| 218 | } catch (Exception e){throw new SAXException(e);} |
---|
| 219 | } |
---|
| 220 | } |
---|
| 221 | /* |
---|
| 222 | * Overrides for emacs |
---|
| 223 | * Local variables: |
---|
| 224 | * tab-width: 4 |
---|
| 225 | * End: |
---|
| 226 | */ |
---|