[FOray-commit] SF.net SVN: foray: [7677] 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:55
|
Revision: 7677 Author: victormote Date: 2006-06-17 11:12:50 -0700 (Sat, 17 Jun 2006) ViewCVS: http://svn.sourceforge.net/foray/?rev=7677&view=rev Log Message: ----------- First pass at making it dynamic was much more complicated than it needs to be. Instead of passing the Class during registration, pass an instance of it. 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:58:32 UTC (rev 7676) +++ trunk/foray/foray-common/src/java/org/foray/common/ClasspathHandler.java 2006-06-17 18:12:50 UTC (rev 7677) @@ -63,7 +63,8 @@ public static void register() { FOrayURLStreamHandlerFactory factory = FOrayURLStreamHandlerFactory.getURLStreamHandlerFactory(); - factory.registerHandler("classpath", ClasspathHandler.class); + ClasspathHandler handler = new ClasspathHandler(); + factory.registerHandler("classpath", handler); } } 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:58:32 UTC (rev 7676) +++ trunk/foray/foray-common/src/java/org/foray/common/FOrayURLStreamHandlerFactory.java 2006-06-17 18:12:50 UTC (rev 7677) @@ -29,6 +29,7 @@ import java.net.URL; import java.net.URLStreamHandler; import java.net.URLStreamHandlerFactory; +import java.security.InvalidParameterException; import java.util.HashMap; /** @@ -43,8 +44,8 @@ /** * 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. + * The value is the {@link URLStreamHandler} which should be used for that + * protocol. */ private HashMap protocols = new HashMap(); @@ -60,57 +61,40 @@ public URLStreamHandler createURLStreamHandler(String protocol) { Object object = this.protocols.get(protocol); if (object == null) { + /* If we don't know how to instantiate it, return null. Java will + * then look through the more normal places instead. */ 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; + return (URLStreamHandler) object; } /** * 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. + * @param handler The {@link URLStreamHandler} instance which should be + * used for URLs whose protocol is <code>protocol</code>. + * @throws InvalidParameterException For any of the following reasons: + * <ul> + * <li>If <code>protocol</code> or <code>handlerClass</code> are + * null.</li> + * <li>If <code>protocol</code> has already been registered</li> + * </ul> */ - public boolean registerHandler(String protocol, Class handlerClass) { - if (protocol == null - || handlerClass == null) { - return false; + public void registerHandler(String protocol, URLStreamHandler handler) + throws InvalidParameterException { + if (protocol == null) { + throw new InvalidParameterException("Cannot register a null " + + "protocol."); } + if (handler == null) { + throw new InvalidParameterException("Cannot register a null " + + "URLStreamHandler."); + } if (this.protocols.containsKey(protocol)) { - return false; + throw new InvalidParameterException("Protocol \n" + protocol + + "\" has already been registered."); } - /* 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; + this.protocols.put(protocol, handler); } /** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |