KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > ltl > graph > Attributes


1
2 //
3
// Copyright (C) 2005 United States Government as represented by the
4
// Administrator of the National Aeronautics and Space Administration
5
// (NASA). All Rights Reserved.
6
//
7
// This software is distributed under the NASA Open Source Agreement
8
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
9
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
10
// directory tree for the complete NOSA document.
11
//
12
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
13
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
14
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
15
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
16
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
17
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
18
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
19
//
20
package gov.nasa.ltl.graph;
21
22 import java.io.PrintStream JavaDoc;
23
24 import java.util.Enumeration JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27
28
29 /**
30  * DOCUMENT ME!
31  */

32 public class Attributes {
33   private Hashtable JavaDoc ht;
34
35   public Attributes () {
36     ht = new Hashtable JavaDoc();
37   }
38
39   public Attributes (Attributes a) {
40     ht = new Hashtable JavaDoc();
41
42     for (Enumeration JavaDoc e = a.ht.keys(); e.hasMoreElements();) {
43       Object JavaDoc key = e.nextElement();
44       ht.put(key, a.ht.get(key));
45     }
46   }
47
48   public Attributes (String JavaDoc s) {
49     ht = new Hashtable JavaDoc();
50
51     if (s.equals("-")) {
52       return;
53     }
54
55     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s, ",");
56
57     while (st.hasMoreTokens()) {
58       String JavaDoc e = st.nextToken();
59
60       int idx = e.indexOf("=");
61
62       String JavaDoc key;
63       String JavaDoc value;
64
65       if (idx == -1) {
66         key = e;
67         value = "";
68       } else {
69         key = e.substring(0, idx);
70         value = e.substring(idx + 1);
71       }
72
73       ht.put(key, value);
74     }
75   }
76
77   public void setBoolean (String JavaDoc name, boolean value) {
78     if (value) {
79       ht.put(name, "");
80     } else {
81       ht.remove(name);
82     }
83   }
84
85   public boolean getBoolean (String JavaDoc name) {
86     return ht.get(name) != null;
87   }
88
89   public void setInt (String JavaDoc name, int value) {
90     ht.put(name, Integer.toString(value));
91   }
92
93   public int getInt (String JavaDoc name) {
94     Object JavaDoc o = ht.get(name);
95
96     if (o == null) {
97       return 0;
98     }
99
100     try {
101       return Integer.parseInt((String JavaDoc) o);
102     } catch (NumberFormatException JavaDoc e) {
103       return 0;
104     }
105   }
106
107   public void setString (String JavaDoc name, String JavaDoc value) {
108     ht.put(name, value);
109   }
110
111   public String JavaDoc getString (String JavaDoc name) {
112     return (String JavaDoc) ht.get(name);
113   }
114
115   public synchronized void save (PrintStream JavaDoc out, int format) {
116     switch (format) {
117     // case Graph.SM_FORMAT: save_sm(out); break;
118
// case Graph.FSP_FORMAT: save_fsp(out); break;
119
case Graph.XML_FORMAT:
120       save_xml(out);
121
122       break;
123     }
124   }
125
126   public String JavaDoc toString () {
127     if (ht.size() == 0) {
128       return "-";
129     }
130
131     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
132
133     for (Enumeration JavaDoc e = ht.keys(); e.hasMoreElements();) {
134       Object JavaDoc key = e.nextElement();
135       Object JavaDoc value = (String JavaDoc) ht.get(key);
136
137       sb.append(key);
138
139       if (!value.equals("")) {
140         sb.append('=');
141         sb.append(value);
142       }
143
144       if (e.hasMoreElements()) {
145         sb.append(',');
146       }
147     }
148
149     return sb.toString();
150   }
151
152   public void unset (String JavaDoc name) {
153     ht.remove(name);
154   }
155
156   private synchronized void save_xml (PrintStream JavaDoc out) {
157     if (ht.size() == 0) {
158       return;
159     }
160
161     for (Enumeration JavaDoc e = ht.keys(); e.hasMoreElements();) {
162       String JavaDoc key = (String JavaDoc) e.nextElement();
163       String JavaDoc value = (String JavaDoc) ht.get(key);
164
165       if (value == "") {
166         out.println("<" + key + "/>");
167       } else {
168         out.println("<" + key + ">" + value + "</" + key + ">");
169       }
170     }
171   }
172 }
Popular Tags