~vcs-imports/xena/trunk

« back to all changes in this revision

Viewing changes to ext/src/javahelp/jhMaster/JavaHelp/src/new/javax/help/.svn/text-base/TryMap.java.svn-base

  • Committer: matthewoliver
  • Date: 2009-12-10 03:18:07 UTC
  • Revision ID: vcs-imports@canonical.com-20091210031807-l086qguzdlljtkl9
Merged Xena Testing into Xena Stable for the Xena 5 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * @(#)TryMap.java      1.19 06/10/30
 
3
 * 
 
4
 * Copyright (c) 2006 Sun Microsystems, Inc.  All Rights Reserved.
 
5
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
6
 * 
 
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.
 
12
 * 
 
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).
 
18
 * 
 
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.
 
22
 * 
 
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
 
25
 * have any questions.
 
26
 */
 
27
 
 
28
package javax.help;
 
29
 
 
30
import java.net.URL;
 
31
import java.net.MalformedURLException;
 
32
import java.util.*;
 
33
import java.io.*;
 
34
import java.beans.*;
 
35
import javax.help.event.*;
 
36
import javax.help.Map.ID;
 
37
 
 
38
/**
 
39
 * A Map that can combine a number of other Maps in an 
 
40
 * efficient manner.
 
41
 *
 
42
 * Currently this is a brute-force implementation.
 
43
 *
 
44
 * @author Eduardo Pelegri-Llopart
 
45
 * @version     1.11    03/10/99
 
46
 */
 
47
public class TryMap implements Map, Serializable {
 
48
    private Vector maps;        // All the maps
 
49
 
 
50
    /**
 
51
     * Creates an empty Map.
 
52
     * This is useful for filtering and to add/remove to/from it.
 
53
     */
 
54
    public TryMap() {
 
55
        maps = new Vector();
 
56
    }
 
57
 
 
58
    /**
 
59
     * Adds a map to a "filter" Map.
 
60
     * Adding a composed map to another is equivalent to
 
61
     * adding the entire Map individually.
 
62
     *
 
63
     * @param map The new Map to add. If Map is null it is not added.
 
64
     */
 
65
    public void add(Map map) {
 
66
        maps.addElement(map);
 
67
    }
 
68
 
 
69
    /**
 
70
     * Removes a Map from this "filter" Map.
 
71
     *
 
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".
 
75
     */
 
76
    public boolean remove(Map map) {
 
77
        return maps.removeElement(map);
 
78
    }
 
79
 
 
80
    /**
 
81
     * Enumerates all the Maps in this TryMap.
 
82
     *
 
83
     * @return An enumeration of the Maps added.
 
84
     */
 
85
    public Enumeration getMaps() {
 
86
        return maps.elements();
 
87
    }
 
88
 
 
89
    /**
 
90
     * Determines if the ID is valid (known to in the project file).
 
91
     * 
 
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.
 
95
     */
 
96
 
 
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)) {
 
103
                return true;
 
104
            }
 
105
        }
 
106
        return false;
 
107
    }
 
108
 
 
109
    /**
 
110
     * Gets an enumeration of all the IDs in a Map.
 
111
     *
 
112
     * @param An enumeration of all the IDs in a Map.
 
113
     */
 
114
    public Enumeration getAllIDs() {
 
115
        return new TryEnumeration(maps.elements(), null);
 
116
    }
 
117
 
 
118
    /**
 
119
     * Gets the URL that corresponds to a given ID in the Map.
 
120
     *
 
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
 
125
     */
 
126
    public URL getURLFromID(ID id) throws MalformedURLException {
 
127
        debug("getURLFromID("+id+")");
 
128
        URL back = null;
 
129
        for (Enumeration e = maps.elements();
 
130
             e.hasMoreElements(); ) {
 
131
            Map m = (Map) e.nextElement();
 
132
            back = m.getURLFromID(id);
 
133
            if (back != null) {
 
134
                return back;
 
135
            }
 
136
        }
 
137
        return back;
 
138
    }
 
139
 
 
140
    /**
 
141
     * Determines if the URL corresponds to an ID in the Map.
 
142
     *
 
143
     * @param url The URL to check on.
 
144
     * @return True if this is an ID, false otherwise.
 
145
     */
 
146
    public boolean isID(URL url) {
 
147
        for (Enumeration e = maps.elements();
 
148
             e.hasMoreElements(); ) {
 
149
            Map m = (Map) e.nextElement();
 
150
            if (m.isID(url)) {
 
151
                return true;
 
152
            }
 
153
        }
 
154
        return false;
 
155
    }
 
156
 
 
157
 
 
158
    /**
 
159
     * Determines the ID for this URL.
 
160
     * 
 
161
     * @param url The URL to get the ID for.
 
162
     * @return The ID (Map.ID), or null if URL is not an ID
 
163
     */
 
164
    public ID getIDFromURL(URL url) {
 
165
        debug("getIDFromURL("+url+")");
 
166
        ID back = null;
 
167
        for (Enumeration e = maps.elements();
 
168
             e.hasMoreElements(); ) {
 
169
            Map m = (Map) e.nextElement();
 
170
            back = m.getIDFromURL(url);
 
171
            if (back != null) {
 
172
                return back;
 
173
            }
 
174
        }
 
175
        return null;
 
176
    }
 
177
 
 
178
    /**
 
179
     * Determines the ID that is "closest" to this URL (with a given anchor).
 
180
     *
 
181
     * @param url A URL
 
182
     * @return The closest ID in this map to the given URL
 
183
     */
 
184
    public ID getClosestID(URL url) {
 
185
        ID back = null;
 
186
        // See if there is an exact match
 
187
        back = getIDFromURL(url);
 
188
        if (back != null) {
 
189
            return back;
 
190
        }
 
191
 
 
192
        // for backwards compatability return a null if the URL is null
 
193
        if (back == null && url == null) {
 
194
            return null;
 
195
        }
 
196
         
 
197
        // no exact match try removing the ref if there is one
 
198
        String ref = url.getRef();
 
199
        if (ref != null) {
 
200
            String urlString = url.toExternalForm();
 
201
            urlString = urlString.substring(0,urlString.lastIndexOf(ref)-1);
 
202
            try {
 
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);
 
208
                    if (back != null) {
 
209
                        return back;
 
210
                    }
 
211
                }
 
212
            } catch (MalformedURLException mue) {
 
213
            }
 
214
        }
 
215
        return null;
 
216
    }
 
217
 
 
218
    /**
 
219
     * Gets the the IDs related to this URL.
 
220
     *
 
221
     * @param URL The URL to compare the Map IDs to.
 
222
     * @return Enumeration of IDs (Strings)
 
223
     */
 
224
    public Enumeration getIDs(URL url) {
 
225
        return new TryEnumeration(maps.elements(), url);
 
226
    }
 
227
 
 
228
    private static class TryEnumeration implements Enumeration {
 
229
        private Enumeration e;  // the maps
 
230
        private Enumeration k;  // the IDs within a map
 
231
        private URL url;
 
232
 
 
233
        public TryEnumeration(Enumeration e, URL url) {
 
234
            this.e = e;
 
235
            this.k = null;
 
236
            this.url = url;
 
237
        }
 
238
 
 
239
        public boolean hasMoreElements() {
 
240
            while (k == null ||
 
241
                   !k.hasMoreElements()) {
 
242
                if (! e.hasMoreElements()) {
 
243
                    return false;
 
244
                }
 
245
                Map m = (Map) e.nextElement();
 
246
                if (url == null) {
 
247
                    k = m.getAllIDs();
 
248
                } else {
 
249
                    k = m.getIDs(url);
 
250
                }
 
251
            }
 
252
            return k.hasMoreElements();
 
253
        }
 
254
 
 
255
        public Object nextElement() {
 
256
            return k.nextElement(); // this is an ID
 
257
        }
 
258
 
 
259
    }
 
260
 
 
261
    /**
 
262
     * For printf debugging.
 
263
     */
 
264
    private static final boolean debug = false;
 
265
    private static void debug(String str) {
 
266
        if (debug) {
 
267
            System.out.println("TryMap: " + str);
 
268
        }
 
269
    }
 
270
 
 
271
}