00001 package gov.nasa.arc.ase.util.graph; 00002 00003 import java.util.Hashtable; 00004 import java.util.Enumeration; 00005 import java.util.StringTokenizer; 00006 00007 public class Attributes { 00008 private Hashtable ht; 00009 00010 public Attributes() { 00011 ht = new Hashtable(); 00012 } 00013 public Attributes(Attributes a) { 00014 ht = new Hashtable(); 00015 00016 for(Enumeration e = a.ht.keys(); e.hasMoreElements(); ) { 00017 Object key = e.nextElement(); 00018 ht.put(key, a.ht.get(key)); 00019 } 00020 } 00021 public Attributes(String s) { 00022 ht = new Hashtable(); 00023 00024 if(s.equals("-")) return; 00025 00026 StringTokenizer st = new StringTokenizer(s, ","); 00027 while(st.hasMoreTokens()) { 00028 String e = st.nextToken(); 00029 00030 int idx = e.indexOf("="); 00031 00032 String key; 00033 String value; 00034 00035 if(idx == -1) { 00036 key = e; 00037 value = ""; 00038 } else { 00039 key = e.substring(0, idx); 00040 value = e.substring(idx+1); 00041 } 00042 00043 ht.put(key, value); 00044 } 00045 } 00046 public boolean getBoolean(String name) { 00047 return ht.get(name) != null; 00048 } 00049 public int getInt(String name) { 00050 Object o = ht.get(name); 00051 00052 if(o == null) return 0; 00053 try { 00054 return Integer.parseInt((String)o); 00055 } catch(NumberFormatException e) { 00056 return 0; 00057 } 00058 } 00059 public String getString(String name) { 00060 return ((String)ht.get(name)); 00061 } 00062 public void setBoolean(String name, boolean value) { 00063 if(value) 00064 ht.put(name, ""); 00065 else 00066 ht.remove(name); 00067 } 00068 public void setInt(String name, int value) { 00069 ht.put(name, Integer.toString(value)); 00070 } 00071 public void setString(String name, String value) { 00072 ht.put(name, value); 00073 } 00074 public String toString() { 00075 if(ht.size() == 0) return "-"; 00076 00077 StringBuffer sb = new StringBuffer(); 00078 for(Enumeration e = ht.keys(); e.hasMoreElements(); ) { 00079 Object key = e.nextElement(); 00080 Object value = (String)ht.get(key); 00081 00082 sb.append(key); 00083 if(!value.equals("")) { 00084 sb.append('='); 00085 sb.append(value); 00086 } 00087 if(e.hasMoreElements()) sb.append(','); 00088 } 00089 return sb.toString(); 00090 } 00091 public void unset(String name) { 00092 ht.remove(name); 00093 } 00094 }