/** * '$RCSfile$' * Purpose: A Class that implements a URL for use in identifying metacat * files. It also handles http urls * Copyright: 2000 Regents of the University of California and the * National Center for Ecological Analysis and Synthesis * Authors: Chad Berkley * * '$Author: jones $' * '$Date: 2006-11-10 18:25:38 +0000 (Fri, 10 Nov 2006) $' * '$Revision: 3077 $' * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package edu.ucsb.nceas.metacat; import java.net.MalformedURLException; import java.util.Hashtable; import java.util.Enumeration; public class MetacatURL { private String[][] params = new String[200][2]; private Hashtable paramsHash = new Hashtable(); private String protocol = null; private String host = null; private String url; /** * This constructor takes a string url and parses it according to the * following rules. * 1) The protocol of the url is the text before the "://" symbol. * 2) Parameter names are written first and are terminated with the = symbol * 3) Parameter values come 2nd and are terminated with an & except for the * last value * The form of the url looks like: * protocol://server.domain.com/servlet/?name1=val1&name2=val2&nameN=valN * notice there is no & after the last param. If one is there it is ignored. * * @param url the string to parse */ public MetacatURL(String url) throws MalformedURLException { this.url = url; parseURL(url); } /** * This method takes a string url and parses it according to the following * rules. * 1) The protocol of the url is the text before the "://" symbol. * 2) Parameter names are written first and are terminated with the = symbol * 3) Parameter values come 2nd and are terminated with an & except for the * last value * The form of the url looks like: * protocol://server.domain.com/servlet/?name1=val1&name2=val2&nameN=valN * notice there is no & after the last param. If one is there it is ignored. */ private void parseURL(String url) throws MalformedURLException { String pname = null; String param = null; String temp = ""; boolean ampflag = true; boolean poundflag = false; int arrcount = 0; //anything before this is the protocol int protocolIndex = url.indexOf("://"); if (protocolIndex == -1) { // URL badly formed, no protocol throw new MalformedURLException("Invalid URL format: " + "no protocol provided."); } this.protocol = url.substring(0, protocolIndex); paramsHash.put("protocol", this.protocol); if(this.protocol.equals("http")) {//if this is an http url params[0][0] = "httpurl"; params[0][1] = url.substring(0, url.length()); paramsHash.put(params[0][0], params[0][1]); for(int i=url.length()-1; i>0; i--) { if(url.charAt(i) == '/') { i=0; } else { String exchange = temp; temp = ""; temp += url.charAt(i); temp += exchange; } } params[1][0] = "filename"; params[1][1] = temp; paramsHash.put(params[1][0], params[1][1]); } else {//other urls that meet the metacat type url structure. int hostIndex = url.indexOf("?"); this.host = url.substring(protocolIndex + 3, hostIndex); paramsHash.put("host", this.host); for(int i=hostIndex + 1; i