[FOray-commit] SF.net SVN: foray: [7734] 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-24 15:07:35
|
Revision: 7734 Author: victormote Date: 2006-06-24 08:07:24 -0700 (Sat, 24 Jun 2006) ViewCVS: http://svn.sourceforge.net/foray/?rev=7734&view=rev Log Message: ----------- Move URL protocol-related classes to the common.url package. Suggested by Vincent Hennebert. Modified Paths: -------------- trunk/foray/foray-common/src/java/org/foray/common/url/classpath/Handler.java Added Paths: ----------- trunk/foray/foray-common/src/java/org/foray/common/url/AbstractURLStreamHandler.java trunk/foray/foray-common/src/java/org/foray/common/url/FOrayURLStreamHandlerFactory.java Removed Paths: ------------- trunk/foray/foray-common/src/java/org/foray/common/AbstractURLStreamHandler.java trunk/foray/foray-common/src/java/org/foray/common/FOrayURLStreamHandlerFactory.java Deleted: trunk/foray/foray-common/src/java/org/foray/common/AbstractURLStreamHandler.java =================================================================== --- trunk/foray/foray-common/src/java/org/foray/common/AbstractURLStreamHandler.java 2006-06-20 04:41:42 UTC (rev 7733) +++ trunk/foray/foray-common/src/java/org/foray/common/AbstractURLStreamHandler.java 2006-06-24 15:07:24 UTC (rev 7734) @@ -1,138 +0,0 @@ -/* - * Copyright 2006 The FOray Project. - * http://www.foray.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * This work is in part derived from the following work(s), used with the - * permission of the licensor: - * Apache FOP, licensed by the Apache Software Foundation - * - */ - -/* $Id$ */ - -/* Known contributors: - * Vincent Hennebert (original author) - */ - -package org.foray.common; - -import java.io.IOException; -import java.net.URLStreamHandler; -import java.util.StringTokenizer; - -/** - * Abstract superclass for all URLStreamHandlers using the FOray scheme for - * custom URLStreamHandler registration. - */ -public abstract class AbstractURLStreamHandler extends URLStreamHandler { - - /** - * Register an instance of this class with the custom - * URLStreamHandlerFactory so that this handler will be used for URLs of - * protocol "classpath". - */ - protected static void register(String protocol, URLStreamHandler handler) { - /* Validate the input. */ - if (protocol == null) { - return; - } - if (handler == null) { - return; - } - - /* Get the Factory instance. */ - FOrayURLStreamHandlerFactory factory = - FOrayURLStreamHandlerFactory.getURLStreamHandlerFactory(); - - /* See if this protocol is already registered with the factory. */ - URLStreamHandler registeredHandler = factory.createURLStreamHandler( - protocol); - if (registeredHandler != null) { - /* If already registed, that one is the one being used, and there - * is no way to change it. */ - return; - } - - /* Register the protocol with the Factory. */ - try { - factory.registerProtocol(protocol, handler); - } catch (IOException e) { - /* This should never happen. We have already checked whether the - * protocol is previously registered, and we know that neither the - * protocol or handler are null. The only thing we could do is - * pass the exception upstream, but there is nothing they can do - * about it either. */ - return; - } - - /* The FOray Factory will not be able to register itself with Java in a - * case where other software running has already registered another - * URLStreamHandlerFactory first. - * Therefore, we will give Java another opportunity to find our custom - * handler by placing its location in the Java environment. - */ - - /* Parse the path. */ - Class theClass = handler.getClass(); - String classPath = theClass.getName(); - int lastTokenDelimiter = classPath.lastIndexOf('.'); - String className = classPath.substring(lastTokenDelimiter + 1, - classPath.length()); - - /* Validate the class name. */ - if (! className.equals("Handler")) { - return; - } - - Package thePackage = theClass.getPackage(); - String packageName = thePackage.getName(); - lastTokenDelimiter = packageName.lastIndexOf('.'); - String lastSubPackage = packageName.substring(lastTokenDelimiter + 1, - packageName.length()); - /* The last sub-package must be equal to the name of the protocol, or - * Java won't find it. */ - if (! lastSubPackage.equals(protocol)) { - return; - } - - /* Add the packagePrefix to the Java search path. */ - String packagePrefix = packageName.substring(0, lastTokenDelimiter); - String javaProperty = "java.protocol.handler.pkgs"; - String searchPath = System.getProperty(javaProperty); - - /* The search path doesn't exist. Create it. */ - if (searchPath == null) { - System.setProperty(javaProperty, packagePrefix); - return; - } - - /* The search path does exist. See if the package prefix is already - * registered. */ - StringTokenizer packagePrefixIter = new StringTokenizer( - searchPath, "|"); - while (packagePrefixIter.hasMoreTokens()) { - String token = packagePrefixIter.nextToken(); - if (token.equals(packagePrefix)) { - /* It is already registered. No need to do it again. */ - return; - } - } - - /* It is not registered, so we need to add it. */ - searchPath += "|" + packagePrefix; - System.setProperty(javaProperty, searchPath); - } - -} Deleted: trunk/foray/foray-common/src/java/org/foray/common/FOrayURLStreamHandlerFactory.java =================================================================== --- trunk/foray/foray-common/src/java/org/foray/common/FOrayURLStreamHandlerFactory.java 2006-06-20 04:41:42 UTC (rev 7733) +++ trunk/foray/foray-common/src/java/org/foray/common/FOrayURLStreamHandlerFactory.java 2006-06-24 15:07:24 UTC (rev 7734) @@ -1,120 +0,0 @@ -/* - * Copyright 2006 The FOray Project. - * http://www.foray.org - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * This work is in part derived from the following work(s), used with the - * permission of the licensor: - * Apache FOP, licensed by the Apache Software Foundation - * - */ - -/* -$Id$ - */ - -/* Known contributors: - * Vincent Hennebert (original author) - */ - -package org.foray.common; - -import java.io.IOException; -import java.net.URL; -import java.net.URLStreamHandler; -import java.net.URLStreamHandlerFactory; -import java.util.HashMap; - -/** - * URLStreamHandlerFactory implementation that allows custom protocols to be - * registered and accessed. - */ -public class FOrayURLStreamHandlerFactory implements URLStreamHandlerFactory { - - /** The singleton instance of this class. */ - private static FOrayURLStreamHandlerFactory urlSHFactory; - - /** - * Map of the protocols. - * The key is a String containing the protocol name. - * The value is the {@link URLStreamHandler} which should be used 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 - * {@link FOrayURLStreamHandlerFactory#getURLStreamHandlerFactory()}. - */ - private FOrayURLStreamHandlerFactory() { - } - - 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; - } - return (URLStreamHandler) object; - } - - /** - * Registers a new {@link URLStreamHandler} for use with a protocol. - * @param protocol The protocol to be registered. - * @param handler The {@link URLStreamHandler} instance which should be - * used for URLs whose protocol is <code>protocol</code>. - * @throws IOException 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 void registerProtocol(String protocol, URLStreamHandler handler) - throws IOException { - if (protocol == null) { - throw new IOException("Cannot register a null " - + "protocol."); - } - if (handler == null) { - throw new IOException("Cannot register a null " - + "URLStreamHandler."); - } - if (this.protocols.containsKey(protocol)) { - throw new IOException("Protocol \n" + protocol - + "\" has already been registered."); - } - this.protocols.put(protocol, handler); - } - - /** - * Provides access to a custom URLStreamHandlerFactory instance, creating - * it and registering it with Java if needed. - * @return The custom URLStreamHandlerFactory instance for this session. - */ - public static synchronized FOrayURLStreamHandlerFactory - getURLStreamHandlerFactory() { - if (FOrayURLStreamHandlerFactory.urlSHFactory == null) { - FOrayURLStreamHandlerFactory.urlSHFactory - = new FOrayURLStreamHandlerFactory(); - URL.setURLStreamHandlerFactory( - FOrayURLStreamHandlerFactory.urlSHFactory); - } - return FOrayURLStreamHandlerFactory.urlSHFactory; - } - -} Copied: trunk/foray/foray-common/src/java/org/foray/common/url/AbstractURLStreamHandler.java (from rev 7706, trunk/foray/foray-common/src/java/org/foray/common/AbstractURLStreamHandler.java) =================================================================== --- trunk/foray/foray-common/src/java/org/foray/common/url/AbstractURLStreamHandler.java (rev 0) +++ trunk/foray/foray-common/src/java/org/foray/common/url/AbstractURLStreamHandler.java 2006-06-24 15:07:24 UTC (rev 7734) @@ -0,0 +1,139 @@ +/* + * Copyright 2006 The FOray Project. + * http://www.foray.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * This work is in part derived from the following work(s), used with the + * permission of the licensor: + * Apache FOP, licensed by the Apache Software Foundation + * + */ + +/* $Id$ */ + +/* Known contributors: + * Vincent Hennebert (original author) + */ + +package org.foray.common.url; + + +import java.io.IOException; +import java.net.URLStreamHandler; +import java.util.StringTokenizer; + +/** + * Abstract superclass for all URLStreamHandlers using the FOray scheme for + * custom URLStreamHandler registration. + */ +public abstract class AbstractURLStreamHandler extends URLStreamHandler { + + /** + * Register an instance of this class with the custom + * URLStreamHandlerFactory so that this handler will be used for URLs of + * protocol "classpath". + */ + protected static void register(String protocol, URLStreamHandler handler) { + /* Validate the input. */ + if (protocol == null) { + return; + } + if (handler == null) { + return; + } + + /* Get the Factory instance. */ + FOrayURLStreamHandlerFactory factory = + FOrayURLStreamHandlerFactory.getURLStreamHandlerFactory(); + + /* See if this protocol is already registered with the factory. */ + URLStreamHandler registeredHandler = factory.createURLStreamHandler( + protocol); + if (registeredHandler != null) { + /* If already registed, that one is the one being used, and there + * is no way to change it. */ + return; + } + + /* Register the protocol with the Factory. */ + try { + factory.registerProtocol(protocol, handler); + } catch (IOException e) { + /* This should never happen. We have already checked whether the + * protocol is previously registered, and we know that neither the + * protocol or handler are null. The only thing we could do is + * pass the exception upstream, but there is nothing they can do + * about it either. */ + return; + } + + /* The FOray Factory will not be able to register itself with Java in a + * case where other software running has already registered another + * URLStreamHandlerFactory first. + * Therefore, we will give Java another opportunity to find our custom + * handler by placing its location in the Java environment. + */ + + /* Parse the path. */ + Class theClass = handler.getClass(); + String classPath = theClass.getName(); + int lastTokenDelimiter = classPath.lastIndexOf('.'); + String className = classPath.substring(lastTokenDelimiter + 1, + classPath.length()); + + /* Validate the class name. */ + if (! className.equals("Handler")) { + return; + } + + Package thePackage = theClass.getPackage(); + String packageName = thePackage.getName(); + lastTokenDelimiter = packageName.lastIndexOf('.'); + String lastSubPackage = packageName.substring(lastTokenDelimiter + 1, + packageName.length()); + /* The last sub-package must be equal to the name of the protocol, or + * Java won't find it. */ + if (! lastSubPackage.equals(protocol)) { + return; + } + + /* Add the packagePrefix to the Java search path. */ + String packagePrefix = packageName.substring(0, lastTokenDelimiter); + String javaProperty = "java.protocol.handler.pkgs"; + String searchPath = System.getProperty(javaProperty); + + /* The search path doesn't exist. Create it. */ + if (searchPath == null) { + System.setProperty(javaProperty, packagePrefix); + return; + } + + /* The search path does exist. See if the package prefix is already + * registered. */ + StringTokenizer packagePrefixIter = new StringTokenizer( + searchPath, "|"); + while (packagePrefixIter.hasMoreTokens()) { + String token = packagePrefixIter.nextToken(); + if (token.equals(packagePrefix)) { + /* It is already registered. No need to do it again. */ + return; + } + } + + /* It is not registered, so we need to add it. */ + searchPath += "|" + packagePrefix; + System.setProperty(javaProperty, searchPath); + } + +} Copied: trunk/foray/foray-common/src/java/org/foray/common/url/FOrayURLStreamHandlerFactory.java (from rev 7703, trunk/foray/foray-common/src/java/org/foray/common/FOrayURLStreamHandlerFactory.java) =================================================================== --- trunk/foray/foray-common/src/java/org/foray/common/url/FOrayURLStreamHandlerFactory.java (rev 0) +++ trunk/foray/foray-common/src/java/org/foray/common/url/FOrayURLStreamHandlerFactory.java 2006-06-24 15:07:24 UTC (rev 7734) @@ -0,0 +1,120 @@ +/* + * Copyright 2006 The FOray Project. + * http://www.foray.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * This work is in part derived from the following work(s), used with the + * permission of the licensor: + * Apache FOP, licensed by the Apache Software Foundation + * + */ + +/* +$Id$ + */ + +/* Known contributors: + * Vincent Hennebert (original author) + */ + +package org.foray.common.url; + +import java.io.IOException; +import java.net.URL; +import java.net.URLStreamHandler; +import java.net.URLStreamHandlerFactory; +import java.util.HashMap; + +/** + * URLStreamHandlerFactory implementation that allows custom protocols to be + * registered and accessed. + */ +public class FOrayURLStreamHandlerFactory implements URLStreamHandlerFactory { + + /** The singleton instance of this class. */ + private static FOrayURLStreamHandlerFactory urlSHFactory; + + /** + * Map of the protocols. + * The key is a String containing the protocol name. + * The value is the {@link URLStreamHandler} which should be used 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 + * {@link FOrayURLStreamHandlerFactory#getURLStreamHandlerFactory()}. + */ + private FOrayURLStreamHandlerFactory() { + } + + 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; + } + return (URLStreamHandler) object; + } + + /** + * Registers a new {@link URLStreamHandler} for use with a protocol. + * @param protocol The protocol to be registered. + * @param handler The {@link URLStreamHandler} instance which should be + * used for URLs whose protocol is <code>protocol</code>. + * @throws IOException 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 void registerProtocol(String protocol, URLStreamHandler handler) + throws IOException { + if (protocol == null) { + throw new IOException("Cannot register a null " + + "protocol."); + } + if (handler == null) { + throw new IOException("Cannot register a null " + + "URLStreamHandler."); + } + if (this.protocols.containsKey(protocol)) { + throw new IOException("Protocol \n" + protocol + + "\" has already been registered."); + } + this.protocols.put(protocol, handler); + } + + /** + * Provides access to a custom URLStreamHandlerFactory instance, creating + * it and registering it with Java if needed. + * @return The custom URLStreamHandlerFactory instance for this session. + */ + public static synchronized FOrayURLStreamHandlerFactory + getURLStreamHandlerFactory() { + if (FOrayURLStreamHandlerFactory.urlSHFactory == null) { + FOrayURLStreamHandlerFactory.urlSHFactory + = new FOrayURLStreamHandlerFactory(); + URL.setURLStreamHandlerFactory( + FOrayURLStreamHandlerFactory.urlSHFactory); + } + return FOrayURLStreamHandlerFactory.urlSHFactory; + } + +} Modified: trunk/foray/foray-common/src/java/org/foray/common/url/classpath/Handler.java =================================================================== --- trunk/foray/foray-common/src/java/org/foray/common/url/classpath/Handler.java 2006-06-20 04:41:42 UTC (rev 7733) +++ trunk/foray/foray-common/src/java/org/foray/common/url/classpath/Handler.java 2006-06-24 15:07:24 UTC (rev 7734) @@ -28,7 +28,7 @@ package org.foray.common.url.classpath; -import org.foray.common.AbstractURLStreamHandler; +import org.foray.common.url.AbstractURLStreamHandler; import java.io.IOException; import java.io.InputStream; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |