1 package org.sapia.archie.impl; 2 3 import java.util.Iterator ; 4 import java.util.Map ; 5 import java.util.Properties ; 6 7 import org.sapia.archie.NamePart; 8 9 17 public class AttributeNamePart implements NamePart{ 18 19 static final String EMPTY_NAME = ""; 20 21 private String _name = EMPTY_NAME; 22 23 private Properties _attributes = new Properties (); 24 25 void setName(String name){ 26 _name = name; 27 } 28 29 void addProperty(String name, String val){ 30 _attributes.setProperty(name, val); 31 } 32 33 36 public String getName(){ 37 return _name; 38 } 39 40 43 public int hashCode() { 44 return _name.hashCode(); 45 } 46 49 public boolean equals(Object o){ 50 try{ 51 return _name.equals(((AttributeNamePart)o).getName()); 52 }catch(ClassCastException e){ 53 return false; 54 } 55 } 56 57 65 public boolean matches(AttributeNamePart other){ 66 Map.Entry entry; 67 Iterator entries = other._attributes.entrySet().iterator(); 68 String value; 69 if(!other.getName().equals(_name)){ 70 return false; 71 } 72 while(entries.hasNext()){ 73 entry = (Map.Entry )entries.next(); 74 value = _attributes.getProperty(entry.getKey().toString()); 75 if(value == null && entry.getValue() == null){ 76 continue; 77 } 78 else if(value == null && entry.getValue() != null){ 79 return false; 80 } 81 else if(value != null && entry.getValue() == null){ 82 return false; 83 } 84 else if(value != null && entry.getValue() != null && 85 entry.getValue().toString().equals(value)){ 86 continue; 87 } 88 else{ 89 return false; 90 } 91 } 92 return true; 93 } 94 95 98 public Properties getAttributes(){ 99 return _attributes; 100 } 101 102 105 public String asString() { 106 StringBuffer buf = new StringBuffer (_name); 107 if (_attributes.size() > 0) { 108 Map.Entry entry; 109 buf.append(AttributeNameParser.QMARK); 110 111 Iterator itr = _attributes.entrySet().iterator(); 112 int count = 0; 113 114 while (itr.hasNext()) { 115 entry = (Map.Entry ) itr.next(); 116 117 if (count > 0) { 118 buf.append(AttributeNameParser.AMP); 119 } 120 121 buf.append(entry.getKey().toString()).append(AttributeNameParser.EQ) 122 .append(entry.getValue()); 123 count++; 124 } 125 } 126 return buf.toString(); 127 } 128 129 public String toString(){ 130 return asString(); 131 } 132 133 } 134 | Popular Tags |