1 /**
2  * The utillib library.
3  * More information is available at http://www.jinchess.com/.
4  * Copyright (C) 2003 Alexander Maryanovsky.
5  * All rights reserved.
6  *
7  * The utillib library is free software; you can redistribute
8  * it and/or modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * The utillib library is distributed in the hope that it will
13  * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with utillib library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 
22 package free.util;
23 
24 import java.util.Hashtable;
25 import java.net.URLStreamHandlerFactory;
26 import java.net.URLStreamHandler;
27 
28 
29 /**
30  * An implementation of <code>URLStreamHandlerFactory</code> which maps protocol
31  * names to instances of <code>URLStreamHandlerFactory</code> with a simple
32  * hashtable.
33  */
34 
35 public class MapURLStreamHandlerFactory implements URLStreamHandlerFactory{
36 
37 
38 
39   /**
40    * A hashtable mapping protocol names to instances of
41    * <code>URLStreamHandler</code> which handle that protocol.
42    */
43 
44   private final Hashtable handlers = new Hashtable();
45 
46 
47 
48   /**
49    * Creates a new, empty, <code>MapURLStreamHandlerFactory</code>.
50    */
51 
MapURLStreamHandlerFactory()52   public MapURLStreamHandlerFactory(){
53 
54   }
55 
56 
57 
58   /**
59    * Sets the specified protocol to be handled by the specified
60    * <code>URLStreamHandler</code>. The protocol name is case insensitive.
61    */
62 
setHandler(String protocol, URLStreamHandler handler)63   public void setHandler(String protocol, URLStreamHandler handler){
64     handlers.put(protocol.toLowerCase(), handler);
65   }
66 
67 
68 
69   /**
70    * Returns the <code>URLStreamHandler</code> for the specified protocol.
71    */
72 
getHandler(String protocol)73   public URLStreamHandler getHandler(String protocol){
74     return (URLStreamHandler)handlers.get(protocol.toLowerCase());
75   }
76 
77 
78 
79   /**
80    * Returns the <code>URLStreamHandler</code> for the specified protocol by
81    * delegating to <code>getHandler(String protocol)</code>.
82    */
83 
createURLStreamHandler(String protocol)84   public URLStreamHandler createURLStreamHandler(String protocol){
85     return getHandler(protocol);
86   }
87 
88 
89 
90 }
91