krysalis-update/src/java/org/krysalis/depot/update/util/net VirtualResourceLocator.java,NONE,1.1

Nick Chalko <[email protected]> Wed, 08 Dec 2004 09:07:17 +0000
Newsgroups gmane.comp.krysalis.cvs
Message-ID <[email protected]>
Update of /cvsroot/krysalis/krysalis-update/src/java/org/krysalis/depot/update/util/net
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24726/src/java/org/krysalis/depot/update/util/net

Added Files:
	VirtualResourceLocator.java 
Log Message:
Copied from the apache incubator.

--- NEW FILE: VirtualResourceLocator.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  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.
 */

package org.krysalis.depot.update.util.net;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.krysalis.depot.update.protocols.Protocol;
import org.krysalis.depot.update.util.UpdateConstants;
import org.krysalis.depot.update.util.io.FileUtils;
import org.krysalis.depot.update.util.io.ResolvedFile;

/**
 * This class exists for two reasons:
 * 
 * 1) So we can separate ourselves from VFS FileObjects (yet still
 * leverage VFS and benefit from it's manage protocols.)
 * 
 * 2) because java.net.URL won't allow protocols that java.net 
 * can't process. A shame. Not sure if the Java URI class is better 
 * about this, but we want to be compatible over older JDKs and also
 * this is an L not an I. 
 * 
 * @author <a href="http://incubator.apache.org/depot">The Apache Incubator Depot Project</a>
 */
public class VirtualResourceLocator {

	public static final String VRL_CLASSNAME =
		VirtualResourceLocator.class.getName();

	private String m_url = null;

	// Parsed out..
	private String m_protocol = null;
	private boolean m_isDirectory = false;

	public VirtualResourceLocator(String url) {
		parseString(url);
	}

	public VirtualResourceLocator(VirtualResourceLocator context, String url) {

		// If we have a protocol, we have an absolute URL
		if (-1 != url.indexOf(":")) {
			// Absolute URL
			parseString(url);
		}
		else {
			// :TODO: Basic right now, just concatenates,
			// no # processing
			parseString(context + (context.isDirectory() ? "" : "/") + url);
		}
	}

	public VirtualResourceLocator(URL url) {
		parseString(url.toExternalForm());
	}

	public VirtualResourceLocator(ResolvedFile file) {
		parse(FileUtils.getFileURL(file));
	}

	private void parse(URL url) {
		parseString(url.toExternalForm());
	}

	private void parseString(String url) {
		m_url = url;

		// Extract Protocol
		int colonPosn = url.indexOf(':');
		if ( (-1 != colonPosn) && ( 1 < colonPosn) )
			m_protocol = url.substring(0, colonPosn);
		else
			m_protocol = Protocol.FILE_PROTOCOL;

		// Crude, but.
		m_isDirectory = m_url.endsWith("/");
	}

	/**
	 * May not be possible for all VirtualResourceLocators
	 * 
	 * @return
	 * @throws MalformedURLException
	 */
	public URL getURL() throws MalformedURLException {
		return new URL(m_url);
	}

	/**
	 * May not be possible for all VirtualResourceLocators
	 * 
	 * @return
	 * @throws Exception
	 */
	public File getFile()  {
		String fileString = m_url;
		if (fileString.startsWith(UpdateConstants.FILE_PREFIX))
			fileString =
				fileString.substring(UpdateConstants.FILE_PREFIX.length());

		return new File(fileString);
	}

	/**
	 * Get parsed protocol type, file or http or ...
	 * 
	 * @return Protocol.*
	 * @see Protocol
	 */
	public String getProtocol() {
		return m_protocol;
	}

	public String toString() {
		return toExternalForm();
	}

	public String toExternalForm() {
		return m_url;
	}

	private String getURLNoEndingSlash() {
		String me = toExternalForm();
		int length = me.length();
		if (isDirectory())
			me = me.substring(0, length - 1);
		return me;
	}

	public String getBasename() {
		String me = getURLNoEndingSlash();

		int sepPosn = me.lastIndexOf('/');
		return (-1 == sepPosn) ? me : me.substring(sepPosn + 1);
	}

	/** 
	 * Get the parent directory for this VRL
	 * 
	 * @return a VRL (or null if no parent exists).
	 */
	public VirtualResourceLocator getParent() {
		String parentString = null;

		String me = getURLNoEndingSlash();
		int sepPosn = me.lastIndexOf('/');

		if (-1 != sepPosn)
			parentString = me.substring(0, sepPosn + 1);

		return (null == parentString)
			? null
			: new VirtualResourceLocator(parentString);
	}

	//
	// :TODO: A guess, at best
	//
	public boolean isDirectory() {
		return m_isDirectory;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return m_url.hashCode();
	}

	public boolean equals(Object obj) {
		boolean isEqual = false;
		if (obj instanceof VirtualResourceLocator) {
			VirtualResourceLocator rhs = (VirtualResourceLocator) obj;
			if (m_protocol.equals(rhs.m_protocol) && m_url.equals(rhs.m_url)) {
				isEqual = true;
			}
		}
		return isEqual;
	}

	public static VirtualResourceLocator getTestVRL() {
		return new VirtualResourceLocator(UpdateConstants.DEPOT_HOME);
	}
}



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now. 
http://productguide.itmanagersjournal.com/