001: //
002: // Copyright (C) 2005 United States Government as represented by the
003: // Administrator of the National Aeronautics and Space Administration
004: // (NASA). All Rights Reserved.
005: //
006: // This software is distributed under the NASA Open Source Agreement
007: // (NOSA), version 1.3. The NOSA has been approved by the Open Source
008: // Initiative. See the file NOSA-1.3-JPF at the top of the distribution
009: // directory tree for the complete NOSA document.
010: //
011: // THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
012: // KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
013: // LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
014: // SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
015: // A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
016: // THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
017: // DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
018: //
019: package gov.nasa.ltl.graph;
020:
021: import java.io.PrintStream;
022:
023: import java.util.Enumeration;
024: import java.util.Hashtable;
025: import java.util.StringTokenizer;
026:
027: /**
028: * DOCUMENT ME!
029: */
030: public class Attributes {
031: private Hashtable ht;
032:
033: public Attributes() {
034: ht = new Hashtable();
035: }
036:
037: public Attributes(Attributes a) {
038: ht = new Hashtable();
039:
040: for (Enumeration e = a.ht.keys(); e.hasMoreElements();) {
041: Object key = e.nextElement();
042: ht.put(key, a.ht.get(key));
043: }
044: }
045:
046: public Attributes(String s) {
047: ht = new Hashtable();
048:
049: if (s.equals("-")) {
050: return;
051: }
052:
053: StringTokenizer st = new StringTokenizer(s, ",");
054:
055: while (st.hasMoreTokens()) {
056: String e = st.nextToken();
057:
058: int idx = e.indexOf("=");
059:
060: String key;
061: String value;
062:
063: if (idx == -1) {
064: key = e;
065: value = "";
066: } else {
067: key = e.substring(0, idx);
068: value = e.substring(idx + 1);
069: }
070:
071: ht.put(key, value);
072: }
073: }
074:
075: public void setBoolean(String name, boolean value) {
076: if (value) {
077: ht.put(name, "");
078: } else {
079: ht.remove(name);
080: }
081: }
082:
083: public boolean getBoolean(String name) {
084: return ht.get(name) != null;
085: }
086:
087: public void setInt(String name, int value) {
088: ht.put(name, Integer.toString(value));
089: }
090:
091: public int getInt(String name) {
092: Object o = ht.get(name);
093:
094: if (o == null) {
095: return 0;
096: }
097:
098: try {
099: return Integer.parseInt((String) o);
100: } catch (NumberFormatException e) {
101: return 0;
102: }
103: }
104:
105: public void setString(String name, String value) {
106: ht.put(name, value);
107: }
108:
109: public String getString(String name) {
110: return (String) ht.get(name);
111: }
112:
113: public synchronized void save(PrintStream out, int format) {
114: switch (format) {
115: // case Graph.SM_FORMAT: save_sm(out); break;
116: // case Graph.FSP_FORMAT: save_fsp(out); break;
117: case Graph.XML_FORMAT:
118: save_xml(out);
119:
120: break;
121: }
122: }
123:
124: public String toString() {
125: if (ht.size() == 0) {
126: return "-";
127: }
128:
129: StringBuffer sb = new StringBuffer();
130:
131: for (Enumeration e = ht.keys(); e.hasMoreElements();) {
132: Object key = e.nextElement();
133: Object value = (String) ht.get(key);
134:
135: sb.append(key);
136:
137: if (!value.equals("")) {
138: sb.append('=');
139: sb.append(value);
140: }
141:
142: if (e.hasMoreElements()) {
143: sb.append(',');
144: }
145: }
146:
147: return sb.toString();
148: }
149:
150: public void unset(String name) {
151: ht.remove(name);
152: }
153:
154: private synchronized void save_xml(PrintStream out) {
155: if (ht.size() == 0) {
156: return;
157: }
158:
159: for (Enumeration e = ht.keys(); e.hasMoreElements();) {
160: String key = (String) e.nextElement();
161: String value = (String) ht.get(key);
162:
163: if (value == "") {
164: out.println("<" + key + "/>");
165: } else {
166: out.println("<" + key + ">" + value + "</" + key + ">");
167: }
168: }
169: }
170: }
|