source: branches/2.2/jabberit_messenger/java_source/src/nu/fw/jeti/util/QueryServers.java @ 3102

Revision 3102, 3.3 KB checked in by amuller, 14 years ago (diff)

Ticket #986 - Efetuado merge para o Branch 2.2( atualizacao do modulo)

  • Property svn:executable set to *
Line 
1/*
2 *      Jeti, a Java Jabber client, Copyright (C) 2004 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 me at eric@jeti.tk or Jabber at jeti@jabber.org
21 *
22 *      Created on 8-aug-2004
23 */
24 
25package nu.fw.jeti.util;
26
27import java.io.IOException;
28import java.io.InputStream;
29import java.net.URL;
30import java.util.LinkedList;
31import java.util.List;
32
33import javax.xml.parsers.FactoryConfigurationError;
34import javax.xml.parsers.ParserConfigurationException;
35import javax.xml.parsers.SAXParser;
36import javax.xml.parsers.SAXParserFactory;
37
38import nu.fw.jeti.backend.Start;
39import nu.fw.jeti.jabber.JID;
40import nu.fw.jeti.jabber.elements.DiscoItem;
41
42import org.xml.sax.Attributes;
43import org.xml.sax.SAXException;
44import org.xml.sax.helpers.DefaultHandler;
45
46/**
47 * @author E.S. de Boer
48 *
49 */
50public class QueryServers
51{
52        private static List servers;
53               
54        public static List getServers()
55        {
56                if(servers==null) readServerXML();
57                return servers;
58        }
59       
60        private static void readServerXML()
61        {
62                SAXParser parser=null;
63                try{parser = SAXParserFactory.newInstance().newSAXParser();}
64                catch (FactoryConfigurationError ex){ex.printStackTrace();}
65                catch (SAXException ex){ex.printStackTrace();}
66                catch (ParserConfigurationException ex){ex.printStackTrace();}
67                InputStream  data = null;
68                try
69                {
70                        data = (new URL(Start.dataURL + "servers.xml")).openStream();
71                }
72                catch (IOException ex)
73                {
74                        //ex.printStackTrace();
75                        data = QueryServers.class.getResourceAsStream("/servers.xml");
76                }
77                List items = new LinkedList();
78                if(data != null)
79                {
80                        try
81                        {
82                                parser.parse(data,new DiscoItemsHandler(items));
83                        }
84                        catch (SAXException ex)
85                        {
86                                ex.printStackTrace();
87                        }
88                        catch (IOException ex)
89                        {
90                                ex.printStackTrace();
91                        }
92                }
93                servers = items;
94        }
95       
96
97    static class DiscoItemsHandler extends DefaultHandler
98    {
99         private List items;
100       
101        public DiscoItemsHandler(List items)
102        {
103            this.items = items;
104        }
105           
106        public void startElement(String uri, String localName, String qName, Attributes attr)
107        {
108            if(qName.equals("item"))
109            {
110                if(items==null)items = new LinkedList();
111                try
112                {
113                    JID jid = JID.checkedJIDFromString(attr.getValue("jid"));
114                    items.add(new DiscoItem(jid,attr.getValue("name"),attr.getValue("node"),attr.getValue("action")));
115                } catch (InstantiationException e)
116                {
117                    Log.xmlParseException(e);
118                }
119            }
120        }
121        }
122}
123/*
124 * Overrides for emacs
125 * Local variables:
126 * tab-width: 4
127 * End:
128 */
Note: See TracBrowser for help on using the repository browser.