~vcs-imports/xena/trunk

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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
 * @(#)FlatMap.java	1.36 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.URLConnection;
import java.net.MalformedURLException;
import java.util.*;
import java.io.*;
import java.beans.*;
import javax.help.event.*;
import javax.help.Map.ID;
import com.sun.java.help.impl.*;

/**
 * A FlatMap is a simple implementation of a Map.  It is used to represent a
 * Map for a single file.
 *
 * @author Eduardo Pelegri-Llopart
 * @version	1.18	01/22/99
 */
public class FlatMap implements Map, Serializable {
    private URL base;		// URL to this map
    private ResourceBundle resource; // the resource
    private HelpSet helpset;	// the top HelpSet

    /**
     * PublicID (known to this XML processor) to the DTD for version 1.0 of the Map
     */
    public static final String publicIDString =
        "-//Sun Microsystems Inc.//DTD JavaHelp Map Version 1.0//EN";

    /**
     * PublicID (known to this XML processor) to the DTD for version 1.0 of the Map
     */
    public static final String publicIDString_V2 =
        "-//Sun Microsystems Inc.//DTD JavaHelp Map Version 2.0//EN";

  /**
     * Create a FlatMap from a given URL.
     *
    * @param source The URL that is the source for all references in this Map.
    * @param hs The HelpSet providing "context" for this Map.
    * @throws IllegalArgumentException if hs doesn't have nested HelpSets.
     */
    public FlatMap(URL base, HelpSet hs) throws IOException {
	debug("Creating FlatMap for: "+base);

	// Verify that this helpset indeed does not have nested HelpSets.
	for (Enumeration e = hs.getHelpSets();
	     e.hasMoreElements(); ) {
	    throw new IllegalArgumentException
		("Cannot create - HelpSet is not flat");
	}

	//	InputStream is = base.openStream();
	//	resource = new MapResourceBundle(is);
	resource = new FlatMapResourceBundle(base);
        this.base = base;
	this.helpset = hs;
    }

    /**
     * The HelpSet for this Map.
     */
    public HelpSet getHelpSet() {
	return helpset;
    }

    /**
     * Determines whether the given ID is valid. If hs is null
     * it is ignored.
     * 
     * @param id The String ID.
     * @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);

	try {
	    String tmp = resource.getString(id);
	} catch (MissingResourceException e) {
	    return false;
	}
	return true;
    }

    /**
     * Gets an enumeration of all the IDs in a Map.
     *
     * @return An enumeration of all the IDs in a Map.
     */
    public Enumeration getAllIDs() {
	return new FlatEnumeration(resource.getKeys(), helpset);
    }

    /**
     * Gets the URL that corresponds to a given ID in the map.
     *
     * @param iden The iden to get the URL for. If iden is null it is
     * treated as an unresolved ID and will return null.
     * @return URL The matching URL.  Null if this map cannot solve the ID
     * @exception MalformedURLException if the URLspecification found  is malformed
     */
    public URL getURLFromID(ID iden) throws MalformedURLException {
	debug("getURLFromID("+iden+")");

	String id = iden.id;
	HelpSet hs = iden.hs;
	if (id == null) {
	    return null;
	}
	String tmp = null;
	try {
	    tmp = resource.getString(id);
	    URL back = new URL(base, tmp);
	    return back;
	} catch (MissingResourceException e) {
	    return null;
	}
    }

    /**
     * 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, otherwise false.
     */
    public boolean isID(URL url) {
	URL tmp;
	for (Enumeration e = resource.getKeys() ; e.hasMoreElements() ;) {
	    try {
		String key = (String) e.nextElement();
		tmp = new URL(base, (String) resource.getObject(key));
		// sameFile() ignores the anchor! - epll
		if (url.sameFile(tmp) == true) {
		    return true;
		}
	    } catch (Exception ex) {
	    }
	}
	return false;
    }


    /**
     * Gets 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) {
	String tmp;
	URL tmpURL;
	if (url == null) return null;
	String urlString = url.toExternalForm();
	for (Enumeration e = resource.getKeys() ; e.hasMoreElements() ;) {
	    String key = (String) e.nextElement();
	    try {
		tmp = resource.getString(key);
		tmpURL = new URL(base, tmp);

		// Sometimes tmp will be null because not all keys are ids
		if (tmpURL == null) continue;
		String tmpString = tmpURL.toExternalForm();
		if (urlString.compareTo(tmpString) == 0) {
		    return ID.create(key, helpset);
		}
	    } catch (Exception ex) {
	    }
	}
	return null;
    }

    /**
     * Determines the ID that is "closest" to this URL (with a given anchor).
     *
     * The definition of this is up to the implementation of Map.  In particular,
     * it may be the same as getIDFromURL().
     *
     * @param url A URL
     * @return The closest ID in this map to the given URL
     */
    public ID getClosestID(URL url) {
	return getIDFromURL(url);
    }


    /**
     * Determines the IDs related to this URL.
     *
     * @param URL The URL to compare the Map IDs to.
     * @return Enumeration of Map.IDs
     */
    public Enumeration getIDs(URL url) {
	String tmp=null;
	URL tmpURL=null;
	Vector ids = new Vector();
	for (Enumeration e = resource.getKeys() ; e.hasMoreElements() ;) {
	    String key = (String) e.nextElement();
	    try {
		tmp = resource.getString(key);
		tmpURL = new URL(base, tmp);
		if (url.sameFile(tmpURL) == true) {
		    ids.addElement(key);
		}
	    } catch (Exception ex) {
	    }
	}
	return new FlatEnumeration(ids.elements(), helpset);
    }

    private static class FlatEnumeration implements Enumeration {
	private Enumeration e;
	private HelpSet hs;

	public FlatEnumeration(Enumeration e, HelpSet hs) {
	    this.e = e;
	    this.hs = hs;
	}

	public boolean hasMoreElements() {
	    return e.hasMoreElements();
	}

	public Object nextElement() {
	    Object back = null;
	    try {
		back = ID.create((String) e.nextElement(), hs);
	    } catch (Exception ex) {
	    }
	    return back;
	}
    }


    /**
     * FlatMapResourceBundle is a ResourceBundle but unlike most 
     * ResourceBundles it is not locale-based and is loaded via the
     * constructor, not getBundle.
     */
    protected class FlatMapResourceBundle extends ResourceBundle 
        implements ParserListener, Serializable  
    {

	private Hashtable lookup = null;
	private boolean startedmap;
	private URL source;

	/**
	 * Creates the FlatMap from the data.
	 */
	public FlatMapResourceBundle(URL url) {
	    source = url;
	    Reader src;
	    try {
		URLConnection uc = url.openConnection();
		src = XmlReader.createReader(uc);
		parse(src);
		src.close();
	    } catch (Exception e) {
		reportMessage("Exception caught while parsing "+url+" "+
				   e.toString(), false);
	    }
	    parsingEnded();
	    for (Enumeration e = lookup.keys() ; e.hasMoreElements() ;) {
		String key1 = (String) e.nextElement();
		String url1 = (String) lookup.get(key1);
	    }
	}

	/**
	 * Overrides ResourceBundle, same semantics.
	 */
	public final Object handleGetObject(String key) {
	    return lookup.get(key); // this class ignores locales
	}

	/**
	 * Implements ResourceBundle.getKeys.
	 */
	public Enumeration getKeys() {
	    return lookup.keys();
	}

	/**
	 * Parses a reader into a MutableTreeNode
	 * Only one of these at a time.
	 */
	synchronized void parse(Reader src)
	    throws IOException 
	{
	    lookup = new Hashtable(10);

	    Parser parser = new Parser(src); // the XML parser instance
	    parser.addParserListener(this);
	    parser.parse();
	}

	/**
	 *  A tag was parsed.
	 */
	public void tagFound(ParserEvent e) {
	    Locale locale = null;
	    Tag tag = e.getTag();
	    FlatMap.debug("TagFound: "+tag.name);
	    TagProperties attr = tag.atts;

	    // Nothing tricky about mapID it doesn't have any hierarchy to it.
	    if (tag.name.equals("mapID")) {
		if (!startedmap) {
		    parsingError("map.invalidMapFormat");
		}

		String target = null;
		String url = null;
		if (attr != null) {
		    target = attr.getProperty("target");
		    url = attr.getProperty("url");
		}
		if (target == null || url == null) {
		    reportMessage("Failure in mapID Creation;", true);
		    reportMessage("  target: "+ target, true);
		    reportMessage("  url: "+ url, true);
		    return;
		}
		lookup.put(target, url);
		return;
	    } else if (tag.name.equals("map")) {
		if (!tag.isEnd) {
		    if (attr != null) {
		        String version = attr.getProperty("version");
			if (version != null && 
			    (version.compareTo("1.0") != 0 &&
			     version.compareTo("2.0") != 0)) {
			    parsingError("map.unknownVersion", version);
			}
		    }
		    if (startedmap) {
			parsingError("map.invalidMapFormat");
		    }
		    startedmap = true;
		} else {
		    if (startedmap) {
			startedmap = false;
		    }
		}
		return;
	    }
	}

	/**
	 *  A PI was parsed.  This method is not intended to be of general use.
	 */
	public void piFound(ParserEvent e) {
	    // ignore
	}

	/**
	 *  A DOCTYPE was parsed.  This method is not intended to be of general use.
	 */
	public void doctypeFound(ParserEvent e) {
	    String publicID = e.getPublicId();
	    if (publicID == null ||
		(publicID.compareTo(publicIDString) != 0 &&
		 publicID.compareTo(publicIDString_V2) != 0)) {
		parsingError("map.wrongPublicID", publicID);
	    }
	}

	/**
	 * A continous block of text was parsed.
	 */
	public void textFound(ParserEvent e) {
	    // At the current time I don't care about text. All the text is
	    // within the attributes in the tag
	}

	// The remaing events from Parser are ignored
	public void commentFound(ParserEvent e) {}

	public void errorFound(ParserEvent e){
	    reportMessage(e.getText(), false);
	}


	private Vector messages = new Vector();
	private boolean validParse = true;

	/**
	 * Reports an error message.
	 */
	public void reportMessage(String msg, boolean validParse) {
	    messages.addElement(msg);
	    this.validParse = this.validParse && validParse;
	}

	/**
	 * Enumerates all the error messages.
	 */
	public Enumeration listMessages() {
	    return messages.elements();
	}

	/**
	 * Parsing has ended.  We are given a last chance to do something
	 * to the HelpSet
	 */
	private void parsingEnded() {
	    if (! validParse) {
		if (lookup != null) {
		    lookup.clear();
		}

		// A parse with problems...
		FlatMap.debug("Parsing failed for "+source);

		for (Enumeration e = messages.elements();
		     e.hasMoreElements();) {
		    String msg = (String) e.nextElement();

		    FlatMap.debug(msg);
		}
	    } else {
		// little memory clean up
		source = null;
	    }
	}

	// Convenience methods
	private void parsingError(String key) {
	    String s = HelpUtilities.getText(key);
	    reportMessage(s, false); // tree will be wrong
	}

	private void parsingError(String key, String s) {
	    String msg = HelpUtilities.getText(key, s);
	    reportMessage(msg, false); // tree will be wrong
	}

    }

    /**
     * For printf debugging...
     */
    private static final boolean debug = false;
    private static void debug(String str) {
        if (debug) {
            System.out.println("FlatMap: " + str);
        }
    }

}