Revision: 7779
Author: victormote
Date: 2006-07-08 15:05:19 -0700 (Sat, 08 Jul 2006)
ViewCVS: http://svn.sourceforge.net/foray/?rev=7779&view=rev
Log Message:
-----------
Add factory class for creating URL instances.
Added Paths:
-----------
trunk/foray/foray-common/src/java/org/foray/common/url/URLFactory.java
Added: trunk/foray/foray-common/src/java/org/foray/common/url/URLFactory.java
===================================================================
--- trunk/foray/foray-common/src/java/org/foray/common/url/URLFactory.java (rev 0)
+++ trunk/foray/foray-common/src/java/org/foray/common/url/URLFactory.java 2006-07-08 22:05:19 UTC (rev 7779)
@@ -0,0 +1,196 @@
+/*
+ * 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$ */
+
+package org.foray.common.url;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLStreamHandler;
+
+/**
+ * Creates URL instances using the FOray custom {@link URLStreamHandler}
+ * capabilities.
+ * The public methods in this class have a one-to-one correspondence with
+ * the {@link URL} constructors, and are intended to be a replacement for them.
+ * This class essentially interposes itself between the calling code and the
+ * {@link URL} constructors to use its own {@link URLStreamHandler} resolution.
+ * This is needed because there is no clean way to be assured that the
+ * {@link URL} constructors use the desired custom {@link URLStreamHandler}
+ * implementation.
+ *
+ * @see ProtocolRegistrationBroker where the various strategies for dealing with
+ * custom {@link URLStreamHandler} implementations are discussed, and where
+ * one can be selected.
+ */
+public class URLFactory {
+
+ /**
+ * Private constructor. This should never be instantiated.
+ */
+ private URLFactory() { }
+
+ /**
+ * @see URL#URL(java.lang.String) which this method mimics.
+ */
+ public static URL createURL(String spec) throws MalformedURLException {
+ /* Mimics logic in URL. */
+ return createURL(null, spec);
+ }
+
+ /**
+ * @see URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
+ * which this method mimics.
+ */
+ public static URL createURL(String protocol, String host, int port,
+ String file) throws MalformedURLException {
+ /* Mimics logic in URL. */
+ return createURL(protocol, host, port, file, null);
+ }
+
+ /**
+ * @see URL#URL(java.lang.String, java.lang.String, int, java.lang.String,
+ * java.net.URLStreamHandler) which this method mimics.
+ */
+ public static URL createURL(String protocol, String host, int port,
+ String file, URLStreamHandler handler)
+ throws MalformedURLException {
+ if (handler == null) {
+ /* Interposes to use the custom URLStreamHandler registration. */
+ handler = getCustomURLStreamHandler(protocol);
+ }
+ /* Now uses the regular URL constructor. */
+ return new URL(protocol, host, port, file, handler);
+ }
+
+ /**
+ * @see URL#URL(java.lang.String, java.lang.String, java.lang.String)
+ * which this method mimics.
+ */
+ public static URL createURL(String protocol, String host, String file)
+ throws MalformedURLException {
+ /* Mimics logic in URL. */
+ return createURL(protocol, host, -1, file);
+ }
+
+ /**
+ * @see URL#URL(java.net.URL, java.lang.String) which this method mimics.
+ */
+ public static URL createURL(URL context, String spec)
+ throws MalformedURLException {
+ /* Mimics logic in URL. */
+ return createURL(context, spec, null);
+ }
+
+ /**
+ * @see URL#URL(java.net.URL, java.lang.String, java.net.URLStreamHandler)
+ * which this method mimics.
+ */
+ public static URL createURL(URL context, String spec,
+ URLStreamHandler handler) throws MalformedURLException {
+ if (handler == null) {
+ /* Interposes to use the custom URLStreamHandler registration. */
+ String protocol = parseProtocol(spec);
+ handler = getCustomURLStreamHandler(protocol);
+ }
+ /* Now uses the regular URL constructor. */
+ return new URL(context, spec, handler);
+ }
+
+ private static URLStreamHandler getCustomURLStreamHandler(String protocol) {
+ return ProtocolRegistrationBroker.getURLStreamHandler(protocol);
+ }
+
+ /**
+ * Extracts the protocol portion of a URL specification.
+ * @param spec The URL specifications from which a protocol should be
+ * extracted.
+ * @return The extracted protocol.
+ * @see URL#URL(java.net.URL, java.lang.String, java.net.URLStreamHandler)
+ * from which this code is derived.
+ */
+ public static String parseProtocol(String spec) {
+ int i, limit, c;
+ int start = 0;
+ String newProtocol = null;
+ boolean aRef=false;
+
+ limit = spec.length();
+ while ((limit > 0) && (spec.charAt(limit - 1) <= ' ')) {
+ limit--; //eliminate trailing whitespace
+ }
+ while ((start < limit) && (spec.charAt(start) <= ' ')) {
+ start++; // eliminate leading whitespace
+ }
+
+ if (spec.regionMatches(true, start, "url:", 0, 4)) {
+ start += 4;
+ }
+ if (start < spec.length() && spec.charAt(start) == '#') {
+ /* we're assuming this is a ref relative to the context URL.
+ * This means protocols cannot start w/ '#', but we must parse
+ * ref URL's like: "hello:there" w/ a ':' in them.
+ */
+ aRef=true;
+ }
+ for (i = start ; !aRef && (i < limit) &&
+ ((c = spec.charAt(i)) != '/') ; i++) {
+ if (c == ':') {
+ String s = spec.substring(start, i).toLowerCase();
+ if (isValidProtocol(s)) {
+ newProtocol = s;
+ start = i + 1;
+ }
+ break;
+ }
+ }
+ return newProtocol;
+ }
+
+ /**
+ * Returns true if specified string is a valid protocol name.
+ * This method is derived from the {@link URL} method with the same
+ * signature.
+ */
+ public static boolean isValidProtocol(String protocol) {
+ int len = protocol.length();
+ if (len < 1) {
+ return false;
+ }
+ char c = protocol.charAt(0);
+ if (! Character.isLetter(c)) {
+ return false;
+ }
+ for (int i = 1; i < len; i++) {
+ c = protocol.charAt(i);
+ if (! Character.isLetterOrDigit(c)
+ && c != '.'
+ && c != '+'
+ && c != '-') {
+ return false;
+ }
+ }
+ return true;
+ }
+
+}
Property changes on: trunk/foray/foray-common/src/java/org/foray/common/url/URLFactory.java
___________________________________________________________________
Name: svn:keywords
+ "Author Id Rev Date URL"
Name: svn:eol-style
+ native
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|