1 22 package org.jboss.xb.binding; 23 24 import org.xml.sax.Attributes ; 25 26 import java.util.List ; 27 import java.util.ArrayList ; 28 29 35 public class AttributesImpl 36 implements Attributes 37 { 38 private final List attrList; 39 40 public AttributesImpl(Attributes attrs) 41 { 42 this(attrs == null ? 0 : attrs.getLength()); 43 44 if(attrs != null) 45 { 46 for(int i = 0; i < attrs.getLength(); ++i) 47 { 48 add( 49 attrs.getURI(i), 50 attrs.getLocalName(i), 51 attrs.getQName(i), 52 attrs.getType(i), 53 attrs.getValue(i) 54 ); 55 } 56 } 57 } 58 59 public AttributesImpl(int size) 60 { 61 this.attrList = new ArrayList (size); 62 } 63 64 public void add(String namespaceUri, String localName, String qName, String type, String value) 65 { 66 attrList.add(new AttributeImpl(namespaceUri, localName, qName, type, value)); 67 } 68 69 public void addAll(Attributes attrs) 70 { 71 for(int i = 0; i < attrs.getLength(); ++i) 72 { 73 add(attrs.getURI(i), attrs.getLocalName(i), attrs.getQName(i), attrs.getType(i), attrs.getValue(i)); 74 } 75 } 76 77 79 public int getLength() 80 { 81 return attrList.size(); 82 } 83 84 public String getURI(int index) 85 { 86 return getAttribute(index).namespaceUri; 87 } 88 89 public String getLocalName(int index) 90 { 91 return getAttribute(index).localName; 92 } 93 94 public String getQName(int index) 95 { 96 return getAttribute(index).qName; 97 } 98 99 public String getType(int index) 100 { 101 return getAttribute(index).type; 102 } 103 104 public String getValue(int index) 105 { 106 return getAttribute(index).value; 107 } 108 109 public int getIndex(String uri, String localName) 110 { 111 int i = 0; 112 while(i < attrList.size()) 113 { 114 final AttributeImpl attr = getAttribute(i++); 115 if( 116 (attr.namespaceUri == null ? uri == null : attr.namespaceUri.equals(uri)) && 117 (attr.localName == null ? localName == null : attr.localName.equals(localName)) 118 ) 119 { 120 break; 121 } 122 } 123 124 if (i == attrList.size()) 125 return -1; 126 127 return i; 128 } 129 130 public int getIndex(String qName) 131 { 132 int i = 0; 133 while(i < attrList.size()) 134 { 135 final AttributeImpl attr = getAttribute(i++); 136 if(attr.qName.equals(qName)) 137 { 138 break; 139 } 140 } 141 142 if (i == attrList.size()) 143 return -1; 144 145 return i; 146 } 147 148 public String getType(String uri, String localName) 149 { 150 AttributeImpl attr = null; 151 int i = 0; 152 while(i < attrList.size()) 153 { 154 attr = getAttribute(i++); 155 if( 156 (attr.namespaceUri == null ? uri == null : attr.namespaceUri.equals(uri)) && 157 (attr.localName == null ? localName == null : attr.localName.equals(localName)) 158 ) 159 { 160 break; 161 } 162 } 163 164 if (attr == null) 165 return null; 166 167 return attr.type; 168 } 169 170 public String getType(String qName) 171 { 172 AttributeImpl attr = null; 173 int i = 0; 174 while(i < attrList.size()) 175 { 176 attr = getAttribute(i++); 177 if(attr.qName.equals(qName)) 178 { 179 break; 180 } 181 } 182 183 if (attr == null) 184 return null; 185 186 return attr.type; 187 } 188 189 public String getValue(String uri, String localName) 190 { 191 AttributeImpl attr = null; 192 int i = 0; 193 while(i < attrList.size()) 194 { 195 attr = getAttribute(i++); 196 if( 197 (attr.namespaceUri == null ? uri == null : attr.namespaceUri.equals(uri)) && 198 (attr.localName == null ? localName == null : attr.localName.equals(localName)) 199 ) 200 { 201 break; 202 } 203 } 204 205 if (attr == null) 206 return null; 207 208 return attr.value; 209 } 210 211 public String getValue(String qName) 212 { 213 AttributeImpl attr = null; 214 int i = 0; 215 while(i < attrList.size()) 216 { 217 attr = getAttribute(i++); 218 if(attr.qName.equals(qName)) 219 { 220 break; 221 } 222 } 223 224 if (attr == null) 225 return null; 226 227 return attr.value; 228 } 229 230 232 public String toString() 233 { 234 String result; 235 if(this.attrList.isEmpty()) 236 { 237 result = "[]"; 238 } 239 else 240 { 241 StringBuffer sb = new StringBuffer (); 242 sb.append('['); 243 sb.append(getQName(0)).append('=').append(getValue(0)); 244 for(int i = 1; i < attrList.size(); ++i) 245 { 246 sb.append(", ").append(getQName(i)).append('=').append(getValue(i)); 247 } 248 sb.append(']'); 249 result = sb.toString(); 250 } 251 return result; 252 } 253 254 256 private AttributeImpl getAttribute(int index) 257 { 258 return (AttributeImpl)attrList.get(index); 259 } 260 261 263 private static final class AttributeImpl 264 { 265 public final String namespaceUri; 266 public final String localName; 267 public final String qName; 268 public final String type; 269 public final String value; 270 271 public AttributeImpl(String namespaceUri, String localName, String qName, String type, String value) 272 { 273 this.namespaceUri = namespaceUri; 274 this.localName = localName; 275 this.qName = qName; 276 this.type = type; 277 this.value = value; 278 } 279 280 public boolean equals(Object o) 281 { 282 if(this == o) return true; 283 if(!(o instanceof AttributeImpl)) return false; 284 285 final AttributeImpl attribute = (AttributeImpl)o; 286 287 if(localName != null ? !localName.equals(attribute.localName) : attribute.localName != null) return false; 288 if(namespaceUri != null ? !namespaceUri.equals(attribute.namespaceUri) : attribute.namespaceUri != null) return false; 289 if(qName != null ? !qName.equals(attribute.qName) : attribute.qName != null) return false; 290 if(type != null ? !type.equals(attribute.type) : attribute.type != null) return false; 291 if(value != null ? !value.equals(attribute.value) : attribute.value != null) return false; 292 293 return true; 294 } 295 296 public int hashCode() 297 { 298 int result; 299 result = (namespaceUri != null ? namespaceUri.hashCode() : 0); 300 result = 29 * result + (localName != null ? localName.hashCode() : 0); 301 result = 29 * result + (qName != null ? qName.hashCode() : 0); 302 result = 29 * result + (type != null ? type.hashCode() : 0); 303 result = 29 * result + (value != null ? value.hashCode() : 0); 304 return result; 305 } 306 } 307 } | Popular Tags |