[FOray-commit] SF.net SVN: foray: [7676] trunk/foray/foray-common/src/java/org/foray/common
Modular XSL-FO Implementation for Java.
Status: Alpha
Brought to you by:
victormote
|
From: <vic...@us...> - 2006-06-17 18:12:06
|
Revision: 7676 Author: victormote Date: 2006-06-17 10:58:32 -0700 (Sat, 17 Jun 2006) ViewCVS: http://svn.sourceforge.net/foray/?rev=7676&view=rev Log Message: ----------- Make loading the URLStreamHandlers dynamic. Modified Paths: -------------- trunk/foray/foray-common/src/java/org/foray/common/ClasspathHandler.java trunk/foray/foray-common/src/java/org/foray/common/FOrayURLStreamHandlerFactory.java Modified: trunk/foray/foray-common/src/java/org/foray/common/ClasspathHandler.java =================================================================== --- trunk/foray/foray-common/src/java/org/foray/common/ClasspathHandler.java 2006-06-17 17:07:54 UTC (rev 7675) +++ trunk/foray/foray-common/src/java/org/foray/common/ClasspathHandler.java 2006-06-17 17:58:32 UTC (rev 7676) @@ -61,7 +61,9 @@ } public static void register() { - FOrayURLStreamHandlerFactory.getURLStreamHandlerFactory(); + FOrayURLStreamHandlerFactory factory = + FOrayURLStreamHandlerFactory.getURLStreamHandlerFactory(); + factory.registerHandler("classpath", ClasspathHandler.class); } } Modified: trunk/foray/foray-common/src/java/org/foray/common/FOrayURLStreamHandlerFactory.java =================================================================== --- trunk/foray/foray-common/src/java/org/foray/common/FOrayURLStreamHandlerFactory.java 2006-06-17 17:07:54 UTC (rev 7675) +++ trunk/foray/foray-common/src/java/org/foray/common/FOrayURLStreamHandlerFactory.java 2006-06-17 17:58:32 UTC (rev 7676) @@ -29,6 +29,7 @@ import java.net.URL; import java.net.URLStreamHandler; import java.net.URLStreamHandlerFactory; +import java.util.HashMap; /** * URLStreamHandlerFactory implementation that allows custom protocols to be @@ -40,6 +41,14 @@ private static FOrayURLStreamHandlerFactory urlSHFactory; /** + * Map of the protocols. + * The key is a String containing the protocol name. + * The value is the Class which should be instantiated as the + * {@link URLStreamHandler} for that protocol. + */ + private HashMap protocols = new HashMap(); + + /** * Private constructor. * This is a singleton, and there should only be one instance per session. * To get that instance, use @@ -49,13 +58,62 @@ } public URLStreamHandler createURLStreamHandler(String protocol) { - if (protocol.equals("classpath")) { - return new ClasspathHandler(); + Object object = this.protocols.get(protocol); + if (object == null) { + return null; } + Class urlStreamHandler = (Class) object; + Object newInstance = null; + try { + newInstance = urlStreamHandler.newInstance(); + } catch (InstantiationException e) { + return null; + } catch (IllegalAccessException e) { + return null; + } + if (newInstance instanceof URLStreamHandler) { + return (URLStreamHandler) newInstance; + } + + /* If we don't know how to instantiate it, return null. Java will then + * look through the more normal places instead. */ return null; } /** + * Registers a new {@link URLStreamHandler} for use with a protocol. + * @param protocol The protocol to be registered. + * @param handlerClass The {@link URLStreamHandler} subclass which should be + * instantiated as the {@link URLStreamHandler} for <code>protocol</code>. + * @return True if the registration was successful, false if not. + */ + public boolean registerHandler(String protocol, Class handlerClass) { + if (protocol == null + || handlerClass == null) { + return false; + } + if (this.protocols.containsKey(protocol)) { + return false; + } + /* Make sure handlerClass is a subclass of URLStreamHandler. */ + boolean validClass = false; + Class superclass = handlerClass.getSuperclass(); + while (superclass != null + && ! validClass) { + if (superclass == URLStreamHandler.class) { + validClass = true; + } else { + superclass = superclass.getSuperclass(); + } + } + if (! validClass) { + return false; + } + this.protocols.put(protocol, handlerClass); + return true; + } + + /** * Provides access to a custom URLStreamHandlerFactory instance, creating * it and registering it with Java if needed. * @return The custom URLStreamHandlerFactory instance for this session. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |