source: trunk/gli/src/org/greenstone/gatherer/util/GURL.java@ 12049

Last change on this file since 12049 was 12049, checked in by mdewsnip, 18 years ago

(FindBugs) No longer Cloneable, made a couple of variables final, and removed a dead function.

  • Property svn:keywords set to Author Date Id Revision
File size: 4.2 KB
Line 
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 */
37package org.greenstone.gatherer.util;
38
39import java.io.*;
40import java.lang.*;
41import java.net.*;
42import java.util.*;
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 */
48public class GURL
49 implements 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 final public Integer INTERNAL = new Integer(0);
74 static final 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) {
99 // Fix it
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
126 public String getPath() {
127 return getHost() + parsePath(url.getFile());
128 }
129
130 public int getPort() {
131 return url.getPort();
132 }
133
134 public String getProtocol() {
135 String protocol = url.getProtocol();
136 if(protocol.equals("file")) {
137 return protocol + ":";
138 }
139 return protocol + "://";
140 }
141
142 public URL getURL() {
143 return url;
144 }
145
146 public String toString() {
147 return url.toString();
148 }
149
150 public boolean valid() {
151 return (url != null);
152 }
153
154 private String parseFile(String raw) {
155 if(raw != null && !raw.equals("")) {
156 // Remove everything upto and including the last '/'
157 if(raw.indexOf("/") != -1) {
158 return raw.substring(raw.lastIndexOf("/") + 1);
159 }
160 }
161 return raw;
162 }
163
164 private String parsePath(String raw) {
165 if(raw != null && !raw.equals("")) {
166 // Remove everything after the last "/"
167 if(raw.indexOf("/") != -1) {
168 return raw.substring(0, raw.lastIndexOf("/"));
169 }
170 }
171 return raw;
172 }
173}
Note: See TracBrowser for help on using the repository browser.