/* * Jeti, a Java Jabber client, Copyright (C) 2007 E.S. de Boer * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * For questions, comments etc, * use the website at http://jeti.jabberstudio.org */ //created on Mar 11, 2007 package nu.fw.jeti.backend; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.net.URL; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.helpers.DefaultHandler; import nu.fw.jeti.events.DiscoveryListener; import nu.fw.jeti.jabber.Backend; import nu.fw.jeti.jabber.JID; import nu.fw.jeti.jabber.elements.DiscoItem; import nu.fw.jeti.jabber.elements.DiscoveryInfo; import nu.fw.jeti.jabber.elements.DiscoveryItem; import nu.fw.jeti.jabber.elements.IQDiscoInfo; import nu.fw.jeti.jabber.elements.IQXCaps; import nu.fw.jeti.jabber.handlers.IQDiscoInfoHandler; import nu.fw.jeti.util.Log; import nu.fw.jeti.util.PreferencesHandler; public class CapabilitiesCache implements DiscoveryListener { private Map capabilities; private Map cache; private Backend backend; public CapabilitiesCache(Backend backend) { this.backend = backend; capabilities = new HashMap(); cache = new HashMap(); } public void setCapability(JID jid, IQXCaps caps){ String cap = caps.getNode() + "#" + caps.getVer(); addCap(jid, cap); if(caps.getExt() != null){ String[] extensions = caps.getExt().split(" "); for (int i = 0; i < extensions.length; i++) { cap = caps.getNode() +"#"+ extensions[i]; addCap(jid, cap); } } } private void addCap(JID jid, String cap) { if(cache.containsKey(cap)){ getJIDCapabilities(jid).addCapabilities((IQDiscoInfo)cache.get(cap)); } else { backend.getInfo(jid, cap, this); } } private Capabilities getJIDCapabilities(JID jid) { Capabilities jidCapabilities = null; if(capabilities.containsKey(jid)){ jidCapabilities = (Capabilities) capabilities.get(jid); } else { jidCapabilities = new Capabilities(); capabilities.put(jid,jidCapabilities); } return jidCapabilities; } public void discoveryInfoResult(JID jid, DiscoveryInfo info){ if(!info.hasFeatures())return; IQDiscoInfo disco = (IQDiscoInfo)info; cache.put(disco.getNode(), disco); getJIDCapabilities(jid).addCapabilities(disco); } public void discoveryItemResult(JID jid, DiscoveryItem item){} /** * Has JID jid capability capability * @param jid * @param capability */ public boolean hasCapability(JID jid, String capability){ return getJIDCapabilities(jid).hasCapability(capability); } public void save() { StringBuffer xml = new StringBuffer(); xml.append(""); xml.append(""); for (Iterator iter = cache.values().iterator(); iter.hasNext();) { ((IQDiscoInfo)iter.next()).appendToXML(xml); xml.append("\n"); } xml.append(""); System.out.println(xml.toString()); try { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(Start.path + "capabilitiesCache.xml"), "UTF8")); writer.write(xml.toString()); writer.close(); } catch (IOException ex2) { ex2.printStackTrace(); } } static class DiscoInfoHandler extends DefaultHandler { private Map queries; private IQDiscoInfoHandler discoHandler; public DiscoInfoHandler(Map queries) { this.queries = queries; discoHandler = new IQDiscoInfoHandler(); } public void startElement(String uri, String localName, String qName, Attributes attr) { if(qName.equals("queries")); //do nothing top level else if(qName.equals("query")) { discoHandler.startHandling(attr); } else discoHandler.startElement(qName, attr); } public void endElement(String uri, String localName, String qName){ if(qName.equals("query")){ IQDiscoInfo info = (IQDiscoInfo) discoHandler.build(); queries.put(info.getNode(),info); } } } }