01: package org.geotiff.image;
02:
03: import java.util.Properties;
04: import java.util.HashMap;
05: import java.util.Map;
06: import java.util.Set;
07: import java.util.Iterator;
08: import java.io.InputStream;
09: import java.io.IOException;
10:
11: /**
12: * The <code>KeyMap</code> represents the two-way mapping
13: * between a single set of GeoTIFF codes and their
14: * symbolic string names. The <code>KeyRegistry</code>
15: * keeps track of all of these.
16: * @see KeyRegistry
17: */
18: public class KeyMap extends Properties {
19:
20: private String name;
21:
22: private HashMap inverse = new HashMap();
23:
24: /** Empty Constructor for serialization */
25: public KeyMap() {
26: super ();
27: }
28:
29: /** Constructor for named resource file */
30: public KeyMap(String resourceName) throws IOException {
31: super ();
32: InputStream propfile = getClass().getResourceAsStream(
33: resourceName);
34: if (propfile == null) {
35: throw new IOException("Resource not found");
36: }
37: load(propfile);
38:
39: // Construct the inverse of the table
40: Iterator iter = entrySet().iterator();
41: while (iter.hasNext()) {
42: Map.Entry entry = (Map.Entry) iter.next();
43: try {
44: inverse.put(entry.getValue(), entry.getKey());
45: } catch (Exception e) {
46: // okay to fail for repeated.
47: }
48: }
49: }
50:
51: /**
52: * Gets property from key; if value starts with a $
53: * it is an alias to another property. The method is
54: * non-recursive, and only follows one level of
55: * indirection.
56: * @param key the symbolic key to resolve
57: */
58: public String getProperty2(String key) {
59: String val = getProperty(key);
60: if (val == null)
61: return val;
62: if (val.startsWith("$")) {
63: val = getProperty(val.substring(1));
64: }
65: return val;
66: }
67:
68: /**
69: * Gets integer code from named key
70: * @param key the symbolic key to resolve
71: */
72: int getCode(String key) {
73: if (key == null)
74: return -1;
75: String sval = getProperty2(key);
76: if (sval == null)
77: return -1;
78: else
79: return Integer.parseInt(sval.trim());
80: }
81:
82: /**
83: * Gets primary string key from code value.
84: * @param code the numeric code to lookup.
85: */
86: String getKey(int code) {
87: return (String) inverse.get((new Integer(code)).toString());
88: }
89:
90: }
|