This repository was archived by the owner on Jan 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathTryMap.java
More file actions
271 lines (246 loc) · 7.01 KB
/
TryMap.java
File metadata and controls
271 lines (246 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* @(#)TryMap.java 1.19 06/10/30
*
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
package javax.help;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.*;
import java.io.*;
import java.beans.*;
import javax.help.event.*;
import javax.help.Map.ID;
/**
* A Map that can combine a number of other Maps in an
* efficient manner.
*
* Currently this is a brute-force implementation.
*
* @author Eduardo Pelegri-Llopart
* @version 1.11 03/10/99
*/
public class TryMap implements Map, Serializable {
private Vector maps; // All the maps
/**
* Creates an empty Map.
* This is useful for filtering and to add/remove to/from it.
*/
public TryMap() {
maps = new Vector();
}
/**
* Adds a map to a "filter" Map.
* Adding a composed map to another is equivalent to
* adding the entire Map individually.
*
* @param map The new Map to add. If Map is null it is not added.
*/
public void add(Map map) {
maps.addElement(map);
}
/**
* Removes a Map from this "filter" Map.
*
* @param map The Map to add.
* @return Whether the Map is already present. If the Map is
* null or was not previously added, returns "false".
*/
public boolean remove(Map map) {
return maps.removeElement(map);
}
/**
* Enumerates all the Maps in this TryMap.
*
* @return An enumeration of the Maps added.
*/
public Enumeration getMaps() {
return maps.elements();
}
/**
* Determines if the ID is valid (known to in the project file).
*
* @param id The ID to check. A null ID is a valid parameter
* @param hs The HelpSet against which to resolve the string.
* @return True if id is valid, false if not valid.
*/
public boolean isValidID(String id, HelpSet hs) {
debug("isValidID "+id);
for (Enumeration e = maps.elements();
e.hasMoreElements();) {
Map m = (Map) e.nextElement();
if (m.isValidID(id, hs)) {
return true;
}
}
return false;
}
/**
* Gets an enumeration of all the IDs in a Map.
*
* @param An enumeration of all the IDs in a Map.
*/
public Enumeration getAllIDs() {
return new TryEnumeration(maps.elements(), null);
}
/**
* Gets the URL that corresponds to a given ID in the Map.
*
* @param id The ID for which to get the URL. If <tt>id</tt> is null it is
* treated as an unresolved ID and returns null.
* @return URL The matching URL. Null if this Map cannot resolve the ID.
* @exception MalformedURLException if the URL specification found is malformed
*/
public URL getURLFromID(ID id) throws MalformedURLException {
debug("getURLFromID("+id+")");
URL back = null;
for (Enumeration e = maps.elements();
e.hasMoreElements(); ) {
Map m = (Map) e.nextElement();
back = m.getURLFromID(id);
if (back != null) {
return back;
}
}
return back;
}
/**
* Determines if the URL corresponds to an ID in the Map.
*
* @param url The URL to check on.
* @return True if this is an ID, false otherwise.
*/
public boolean isID(URL url) {
for (Enumeration e = maps.elements();
e.hasMoreElements(); ) {
Map m = (Map) e.nextElement();
if (m.isID(url)) {
return true;
}
}
return false;
}
/**
* Determines the ID for this URL.
*
* @param url The URL to get the ID for.
* @return The ID (Map.ID), or null if URL is not an ID
*/
public ID getIDFromURL(URL url) {
debug("getIDFromURL("+url+")");
ID back = null;
for (Enumeration e = maps.elements();
e.hasMoreElements(); ) {
Map m = (Map) e.nextElement();
back = m.getIDFromURL(url);
if (back != null) {
return back;
}
}
return null;
}
/**
* Determines the ID that is "closest" to this URL (with a given anchor).
*
* @param url A URL
* @return The closest ID in this map to the given URL
*/
public ID getClosestID(URL url) {
ID back = null;
// See if there is an exact match
back = getIDFromURL(url);
if (back != null) {
return back;
}
// for backwards compatability return a null if the URL is null
if (back == null && url == null) {
return null;
}
// no exact match try removing the ref if there is one
String ref = url.getRef();
if (ref != null) {
String urlString = url.toExternalForm();
urlString = urlString.substring(0,urlString.lastIndexOf(ref)-1);
try {
URL newURL = new URL(urlString);
for (Enumeration e = maps.elements();
e.hasMoreElements(); ) {
Map m = (Map) e.nextElement();
back = m.getIDFromURL(newURL);
if (back != null) {
return back;
}
}
} catch (MalformedURLException mue) {
}
}
return null;
}
/**
* Gets the the IDs related to this URL.
*
* @param URL The URL to compare the Map IDs to.
* @return Enumeration of IDs (Strings)
*/
public Enumeration getIDs(URL url) {
return new TryEnumeration(maps.elements(), url);
}
private static class TryEnumeration implements Enumeration {
private Enumeration e; // the maps
private Enumeration k; // the IDs within a map
private URL url;
public TryEnumeration(Enumeration e, URL url) {
this.e = e;
this.k = null;
this.url = url;
}
public boolean hasMoreElements() {
while (k == null ||
!k.hasMoreElements()) {
if (! e.hasMoreElements()) {
return false;
}
Map m = (Map) e.nextElement();
if (url == null) {
k = m.getAllIDs();
} else {
k = m.getIDs(url);
}
}
return k.hasMoreElements();
}
public Object nextElement() {
return k.nextElement(); // this is an ID
}
}
/**
* For printf debugging.
*/
private static final boolean debug = false;
private static void debug(String str) {
if (debug) {
System.out.println("TryMap: " + str);
}
}
}