[5304] | 1 | /**
|
---|
| 2 | *#########################################################################
|
---|
| 3 | *
|
---|
| 4 | * A component of the Gatherer application, part of the Greenstone digital
|
---|
| 5 | * library suite from the New Zealand Digital Library Project at the
|
---|
| 6 | * University of Waikato, New Zealand.
|
---|
| 7 | *
|
---|
| 8 | * <BR><BR>
|
---|
| 9 | *
|
---|
| 10 | * Author: John Thompson, Greenstone Digital Library, University of Waikato
|
---|
| 11 | *
|
---|
| 12 | * <BR><BR>
|
---|
| 13 | *
|
---|
| 14 | * Copyright (C) 1999 New Zealand Digital Library Project
|
---|
| 15 | *
|
---|
| 16 | * <BR><BR>
|
---|
| 17 | *
|
---|
| 18 | * This program is free software; you can redistribute it and/or modify
|
---|
| 19 | * it under the terms of the GNU General Public License as published by
|
---|
| 20 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 21 | * (at your option) any later version.
|
---|
| 22 | *
|
---|
| 23 | * <BR><BR>
|
---|
| 24 | *
|
---|
| 25 | * This program is distributed in the hope that it will be useful,
|
---|
| 26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 28 | * GNU General Public License for more details.
|
---|
| 29 | *
|
---|
| 30 | * <BR><BR>
|
---|
| 31 | *
|
---|
| 32 | * You should have received a copy of the GNU General Public License
|
---|
| 33 | * along with this program; if not, write to the Free Software
|
---|
| 34 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 35 | *########################################################################
|
---|
| 36 | */
|
---|
| 37 | package org.greenstone.gatherer.util;
|
---|
| 38 |
|
---|
[5581] | 39 | import java.io.*;
|
---|
| 40 | import java.lang.*;
|
---|
| 41 | import java.net.*;
|
---|
| 42 | import java.util.*;
|
---|
[5304] | 43 |
|
---|
| 44 | /**
|
---|
| 45 | * Extends the standard URL class to include several new data elements and
|
---|
| 46 | * the ability to take a string and parse a valid url.
|
---|
| 47 | */
|
---|
| 48 | public class GURL
|
---|
| 49 | implements Cloneable, Serializable {
|
---|
| 50 |
|
---|
| 51 | private Integer type;
|
---|
| 52 |
|
---|
| 53 | private URL url;
|
---|
| 54 |
|
---|
| 55 | private String file;
|
---|
| 56 | private String host;
|
---|
| 57 | private String path;
|
---|
| 58 | private String protocol;
|
---|
| 59 | private String title = "empty";
|
---|
| 60 |
|
---|
| 61 | /** This Vector maintains a record of the url links from within
|
---|
| 62 | * this resource, and their type (EXTERNAL vs INTERNAL). Internal
|
---|
| 63 | * links can be 'hidden' from the user if desired, or a user can
|
---|
| 64 | * opt to have all internal links automatically moved into a
|
---|
| 65 | * collection if the page that depends upon them is moved. <BR>
|
---|
| 66 | * Each entry in the vector is of the form.
|
---|
| 67 | * links[i] = String url
|
---|
| 68 | * links[i+1] = int type
|
---|
| 69 | */
|
---|
| 70 | private Vector links;
|
---|
| 71 |
|
---|
| 72 | /* Static Integer identifiers for ease of use. */
|
---|
| 73 | static public Integer INTERNAL = new Integer(0);
|
---|
| 74 | static public Integer EXTERNAL = new Integer(1);
|
---|
| 75 |
|
---|
| 76 | public GURL(String url_str) {
|
---|
| 77 | this(url_str, GURL.EXTERNAL);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | public GURL(URL url) {
|
---|
| 81 | this(url, GURL.EXTERNAL);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | public GURL(URL url, Integer type) {
|
---|
| 85 | this.type = type;
|
---|
| 86 | this.url = url;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | public GURL(String url_str, Integer type) {
|
---|
| 90 | this.type = type;
|
---|
| 91 |
|
---|
| 92 | try {
|
---|
| 93 | if(!url_str.startsWith("file:") && url_str.indexOf("://") == -1) {
|
---|
| 94 | url_str = "http://" + url_str;
|
---|
| 95 | }
|
---|
| 96 | url = new URL(url_str);
|
---|
| 97 | }
|
---|
| 98 | catch (MalformedURLException e) {
|
---|
[5581] | 99 | // Fix it
|
---|
[5304] | 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | public void addLink(String url) {
|
---|
| 104 | addLink(url, GURL.EXTERNAL);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | public void addLink(String url, Integer type) {
|
---|
| 108 | if(links == null) {
|
---|
| 109 | links = new Vector();
|
---|
| 110 | }
|
---|
| 111 | links.add(url);
|
---|
| 112 | links.add(type);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | /** Used by tree renderer to display name. So we'll only return the
|
---|
| 116 | * filename.
|
---|
| 117 | */
|
---|
| 118 | public String getFile() {
|
---|
| 119 | return parseFile(url.getFile());
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | public String getHost() {
|
---|
| 123 | return url.getHost();
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[5785] | 126 | /* private String getLocalFile() {
|
---|
[5304] | 127 | String local_file = getPath() + File.separator + getFile();
|
---|
| 128 | // Strip msdos roots if they're still there.
|
---|
| 129 | while(local_file.indexOf(":") != -1) {
|
---|
| 130 | local_file = local_file.substring(0, local_file.indexOf(":")) + local_file.substring(local_file.indexOf(":") + 1);
|
---|
| 131 | }
|
---|
| 132 | return local_file;
|
---|
[5785] | 133 | } */
|
---|
[5304] | 134 |
|
---|
| 135 | public String getPath() {
|
---|
| 136 | return getHost() + parsePath(url.getFile());
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[5326] | 139 | public int getPort() {
|
---|
| 140 | return url.getPort();
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[5304] | 143 | public String getProtocol() {
|
---|
| 144 | String protocol = url.getProtocol();
|
---|
| 145 | if(protocol.equals("file")) {
|
---|
| 146 | return protocol + ":";
|
---|
| 147 | }
|
---|
| 148 | return protocol + "://";
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | public URL getURL() {
|
---|
| 152 | return url;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | public String toString() {
|
---|
| 156 | return url.toString();
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | public boolean valid() {
|
---|
| 160 | return (url != null);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | private String parseFile(String raw) {
|
---|
| 164 | if(raw != null && !raw.equals("")) {
|
---|
[5581] | 165 | // Remove everything upto and including the last '/'
|
---|
[5304] | 166 | if(raw.indexOf("/") != -1) {
|
---|
| 167 | return raw.substring(raw.lastIndexOf("/") + 1);
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 | return raw;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | private String parsePath(String raw) {
|
---|
| 174 | if(raw != null && !raw.equals("")) {
|
---|
[5581] | 175 | // Remove everything after the last "/"
|
---|
[5304] | 176 | if(raw.indexOf("/") != -1) {
|
---|
| 177 | return raw.substring(0, raw.lastIndexOf("/"));
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 | return raw;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | }
|
---|