2
* @(#)TryMap.java 1.19 06/10/30
4
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7
* This code is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU General Public License version 2 only, as
9
* published by the Free Software Foundation. Sun designates this
10
* particular file as subject to the "Classpath" exception as provided
11
* by Sun in the LICENSE file that accompanied this code.
13
* This code is distributed in the hope that it will be useful, but WITHOUT
14
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16
* version 2 for more details (a copy is included in the LICENSE file that
17
* accompanied this code).
19
* You should have received a copy of the GNU General Public License version
20
* 2 along with this work; if not, write to the Free Software Foundation,
21
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
24
* CA 95054 USA or visit www.sun.com if you need additional information or
31
import java.net.MalformedURLException;
35
import javax.help.event.*;
36
import javax.help.Map.ID;
39
* A Map that can combine a number of other Maps in an
42
* Currently this is a brute-force implementation.
44
* @author Eduardo Pelegri-Llopart
45
* @version 1.11 03/10/99
47
public class TryMap implements Map, Serializable {
48
private Vector maps; // All the maps
51
* Creates an empty Map.
52
* This is useful for filtering and to add/remove to/from it.
59
* Adds a map to a "filter" Map.
60
* Adding a composed map to another is equivalent to
61
* adding the entire Map individually.
63
* @param map The new Map to add. If Map is null it is not added.
65
public void add(Map map) {
70
* Removes a Map from this "filter" Map.
72
* @param map The Map to add.
73
* @return Whether the Map is already present. If the Map is
74
* null or was not previously added, returns "false".
76
public boolean remove(Map map) {
77
return maps.removeElement(map);
81
* Enumerates all the Maps in this TryMap.
83
* @return An enumeration of the Maps added.
85
public Enumeration getMaps() {
86
return maps.elements();
90
* Determines if the ID is valid (known to in the project file).
92
* @param id The ID to check. A null ID is a valid parameter
93
* @param hs The HelpSet against which to resolve the string.
94
* @return True if id is valid, false if not valid.
97
public boolean isValidID(String id, HelpSet hs) {
98
debug("isValidID "+id);
99
for (Enumeration e = maps.elements();
100
e.hasMoreElements();) {
101
Map m = (Map) e.nextElement();
102
if (m.isValidID(id, hs)) {
110
* Gets an enumeration of all the IDs in a Map.
112
* @param An enumeration of all the IDs in a Map.
114
public Enumeration getAllIDs() {
115
return new TryEnumeration(maps.elements(), null);
119
* Gets the URL that corresponds to a given ID in the Map.
121
* @param id The ID for which to get the URL. If <tt>id</tt> is null it is
122
* treated as an unresolved ID and returns null.
123
* @return URL The matching URL. Null if this Map cannot resolve the ID.
124
* @exception MalformedURLException if the URL specification found is malformed
126
public URL getURLFromID(ID id) throws MalformedURLException {
127
debug("getURLFromID("+id+")");
129
for (Enumeration e = maps.elements();
130
e.hasMoreElements(); ) {
131
Map m = (Map) e.nextElement();
132
back = m.getURLFromID(id);
141
* Determines if the URL corresponds to an ID in the Map.
143
* @param url The URL to check on.
144
* @return True if this is an ID, false otherwise.
146
public boolean isID(URL url) {
147
for (Enumeration e = maps.elements();
148
e.hasMoreElements(); ) {
149
Map m = (Map) e.nextElement();
159
* Determines the ID for this URL.
161
* @param url The URL to get the ID for.
162
* @return The ID (Map.ID), or null if URL is not an ID
164
public ID getIDFromURL(URL url) {
165
debug("getIDFromURL("+url+")");
167
for (Enumeration e = maps.elements();
168
e.hasMoreElements(); ) {
169
Map m = (Map) e.nextElement();
170
back = m.getIDFromURL(url);
179
* Determines the ID that is "closest" to this URL (with a given anchor).
182
* @return The closest ID in this map to the given URL
184
public ID getClosestID(URL url) {
186
// See if there is an exact match
187
back = getIDFromURL(url);
192
// for backwards compatability return a null if the URL is null
193
if (back == null && url == null) {
197
// no exact match try removing the ref if there is one
198
String ref = url.getRef();
200
String urlString = url.toExternalForm();
201
urlString = urlString.substring(0,urlString.lastIndexOf(ref)-1);
203
URL newURL = new URL(urlString);
204
for (Enumeration e = maps.elements();
205
e.hasMoreElements(); ) {
206
Map m = (Map) e.nextElement();
207
back = m.getIDFromURL(newURL);
212
} catch (MalformedURLException mue) {
219
* Gets the the IDs related to this URL.
221
* @param URL The URL to compare the Map IDs to.
222
* @return Enumeration of IDs (Strings)
224
public Enumeration getIDs(URL url) {
225
return new TryEnumeration(maps.elements(), url);
228
private static class TryEnumeration implements Enumeration {
229
private Enumeration e; // the maps
230
private Enumeration k; // the IDs within a map
233
public TryEnumeration(Enumeration e, URL url) {
239
public boolean hasMoreElements() {
241
!k.hasMoreElements()) {
242
if (! e.hasMoreElements()) {
245
Map m = (Map) e.nextElement();
252
return k.hasMoreElements();
255
public Object nextElement() {
256
return k.nextElement(); // this is an ID
262
* For printf debugging.
264
private static final boolean debug = false;
265
private static void debug(String str) {
267
System.out.println("TryMap: " + str);