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 |
|
---|
38 |
|
---|
39 |
|
---|
40 |
|
---|
41 |
|
---|
42 |
|
---|
43 | package org.greenstone.gatherer.util;
|
---|
44 |
|
---|
45 | import java.io.File;
|
---|
46 | import java.io.Serializable;
|
---|
47 |
|
---|
48 | import java.lang.Cloneable;
|
---|
49 |
|
---|
50 | import java.net.MalformedURLException;
|
---|
51 | import java.net.URL;
|
---|
52 |
|
---|
53 | import java.util.Vector;
|
---|
54 |
|
---|
55 | import javax.swing.JProgressBar;
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Extends the standard URL class to include several new data elements and
|
---|
59 | * the ability to take a string and parse a valid url.
|
---|
60 | */
|
---|
61 | public class GURL
|
---|
62 | implements Cloneable, Serializable {
|
---|
63 |
|
---|
64 | private Integer type;
|
---|
65 |
|
---|
66 | private URL url;
|
---|
67 |
|
---|
68 | private String file;
|
---|
69 | private String host;
|
---|
70 | private String path;
|
---|
71 | private String protocol;
|
---|
72 | private String title = "empty";
|
---|
73 |
|
---|
74 | /** This Vector maintains a record of the url links from within
|
---|
75 | * this resource, and their type (EXTERNAL vs INTERNAL). Internal
|
---|
76 | * links can be 'hidden' from the user if desired, or a user can
|
---|
77 | * opt to have all internal links automatically moved into a
|
---|
78 | * collection if the page that depends upon them is moved. <BR>
|
---|
79 | * Each entry in the vector is of the form.
|
---|
80 | * links[i] = String url
|
---|
81 | * links[i+1] = int type
|
---|
82 | */
|
---|
83 | private Vector links;
|
---|
84 |
|
---|
85 | /* Static Integer identifiers for ease of use. */
|
---|
86 | static public Integer INTERNAL = new Integer(0);
|
---|
87 | static public Integer EXTERNAL = new Integer(1);
|
---|
88 |
|
---|
89 | public GURL(String url_str) {
|
---|
90 | this(url_str, GURL.EXTERNAL);
|
---|
91 | }
|
---|
92 |
|
---|
93 | public GURL(URL url) {
|
---|
94 | this(url, GURL.EXTERNAL);
|
---|
95 | }
|
---|
96 |
|
---|
97 | public GURL(URL url, Integer type) {
|
---|
98 | this.type = type;
|
---|
99 | this.url = url;
|
---|
100 | }
|
---|
101 |
|
---|
102 | public GURL(String url_str, Integer type) {
|
---|
103 | this.type = type;
|
---|
104 |
|
---|
105 | try {
|
---|
106 | if(!url_str.startsWith("file:") && url_str.indexOf("://") == -1) {
|
---|
107 | url_str = "http://" + url_str;
|
---|
108 | }
|
---|
109 | url = new URL(url_str);
|
---|
110 | }
|
---|
111 | catch (MalformedURLException e) {
|
---|
112 | // Fix it
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | public void addLink(String url) {
|
---|
117 | addLink(url, GURL.EXTERNAL);
|
---|
118 | }
|
---|
119 |
|
---|
120 | public void addLink(String url, Integer type) {
|
---|
121 | if(links == null) {
|
---|
122 | links = new Vector();
|
---|
123 | }
|
---|
124 | links.add(url);
|
---|
125 | links.add(type);
|
---|
126 | }
|
---|
127 |
|
---|
128 | /** Used by tree renderer to display name. So we'll only return the
|
---|
129 | * filename.
|
---|
130 | */
|
---|
131 | public String getFile() {
|
---|
132 | return parseFile(url.getFile());
|
---|
133 | }
|
---|
134 |
|
---|
135 | public String getHost() {
|
---|
136 | return url.getHost();
|
---|
137 | }
|
---|
138 |
|
---|
139 | public String getLocalFile() {
|
---|
140 | String local_file = getPath() + File.separator + getFile();
|
---|
141 | // Strip msdos roots if they're still there.
|
---|
142 | while(local_file.indexOf(":") != -1) {
|
---|
143 | local_file = local_file.substring(0, local_file.indexOf(":")) + local_file.substring(local_file.indexOf(":") + 1);
|
---|
144 | }
|
---|
145 | return local_file;
|
---|
146 | }
|
---|
147 |
|
---|
148 | public String getPath() {
|
---|
149 | return getHost() + parsePath(url.getFile());
|
---|
150 | }
|
---|
151 |
|
---|
152 | public String getProtocol() {
|
---|
153 | String protocol = url.getProtocol();
|
---|
154 | if(protocol.equals("file")) {
|
---|
155 | return protocol + ":";
|
---|
156 | }
|
---|
157 | return protocol + "://";
|
---|
158 | }
|
---|
159 |
|
---|
160 | public URL getURL() {
|
---|
161 | return url;
|
---|
162 | }
|
---|
163 |
|
---|
164 | public String toString() {
|
---|
165 | return url.toString();
|
---|
166 | }
|
---|
167 |
|
---|
168 | public boolean valid() {
|
---|
169 | return (url != null);
|
---|
170 | }
|
---|
171 |
|
---|
172 | private String parseFile(String raw) {
|
---|
173 | if(raw != null && !raw.equals("")) {
|
---|
174 | // Remove everything upto and including the last '/'
|
---|
175 | if(raw.indexOf("/") != -1) {
|
---|
176 | return raw.substring(raw.lastIndexOf("/") + 1);
|
---|
177 | }
|
---|
178 | }
|
---|
179 | return raw;
|
---|
180 | }
|
---|
181 |
|
---|
182 | private String parsePath(String raw) {
|
---|
183 | if(raw != null && !raw.equals("")) {
|
---|
184 | // Remove everything after the last "/"
|
---|
185 | if(raw.indexOf("/") != -1) {
|
---|
186 | return raw.substring(0, raw.lastIndexOf("/"));
|
---|
187 | }
|
---|
188 | }
|
---|
189 | return raw;
|
---|
190 | }
|
---|
191 |
|
---|
192 | }
|
---|