1 11 package org.eclipse.pde.internal.ui.compare; 12 13 import org.xml.sax.Attributes ; 14 15 20 public class AttributesImpl implements Attributes { 21 22 23 private ListNode fHead; 24 25 26 private ListNode fTail; 27 28 29 private int fLength; 30 31 32 33 public int getLength() { 34 return fLength; 35 } 36 37 38 public int getIndex(String raw) { 39 ListNode place= fHead; 40 int index= 0; 41 while (place != null) { 42 if (place.raw.equals(raw)) { 43 return index; 44 } 45 index++; 46 place= place.next; 47 } 48 return -1; 49 } 50 51 52 public int getIndex(String uri, String local) { 53 ListNode place= fHead; 54 int index= 0; 55 while (place != null) { 56 if (place.uri.equals(uri) && place.local.equals(local)) { 57 return index; 58 } 59 index++; 60 place= place.next; 61 } 62 return -1; 63 } 64 65 66 public String getURI(int index) { 67 68 ListNode node= getListNodeAt(index); 69 return node != null ? node.uri : null; 70 } 71 72 73 public String getLocalName(int index) { 74 75 ListNode node= getListNodeAt(index); 76 return node != null ? node.local : null; 77 } 78 79 80 public String getQName(int index) { 81 82 ListNode node= getListNodeAt(index); 83 return node != null ? node.raw : null; 84 85 } 86 87 88 public String getType(int index) { 89 90 ListNode node= getListNodeAt(index); 91 return (node != null) ? node.type : null; 92 } 93 94 95 public String getType(String uri, String local) { 96 97 ListNode node= getListNode(uri, local); 98 return (node != null) ? node.type : null; 99 100 } 101 102 103 public String getType(String raw) { 104 105 ListNode node= getListNode(raw); 106 return (node != null) ? node.type : null; 107 } 108 109 110 public String getValue(int index) { 111 112 ListNode node= getListNodeAt(index); 113 return (node != null) ? node.value : null; 114 } 115 116 117 public String getValue(String uri, String local) { 118 119 ListNode node= getListNode(uri, local); 120 return (node != null) ? node.value : null; 121 } 122 123 124 public String getValue(String raw) { 125 126 ListNode node= getListNode(raw); 127 return (node != null) ? node.value : null; 128 } 129 130 131 public void addAttribute(String raw, String type, String value) { 132 addAttribute(null, null, raw, type, value); 133 } 134 135 136 public void addAttribute( 137 String uri, 138 String local, 139 String raw, 140 String type, 141 String value) { 142 143 ListNode node= new ListNode(uri, local, raw, type, value); 144 if (fLength == 0) { 145 fHead= node; 146 } else { 147 fTail.next= node; 148 } 149 fTail= node; 150 fLength++; 151 } 152 153 154 public void insertAttributeAt( 155 int index, 156 String raw, 157 String type, 158 String value) { 159 insertAttributeAt(index, null, null, raw, type, value); 160 } 161 162 163 public void insertAttributeAt( 164 int index, 165 String uri, 166 String local, 167 String raw, 168 String type, 169 String value) { 170 171 if (fLength == 0 || index >= fLength) { 173 addAttribute(uri, local, raw, type, value); 174 return; 175 } 176 177 ListNode node= new ListNode(uri, local, raw, type, value); 179 if (index < 1) { 180 node.next= fHead; 181 fHead= node; 182 } else { 183 ListNode prev= getListNodeAt(index - 1); 184 node.next= prev.next; 185 prev.next= node; 186 } 187 fLength++; 188 } 189 190 191 public void removeAttributeAt(int index) { 192 193 if (fLength == 0) 194 return; 195 196 if (index == 0) { 197 fHead= fHead.next; 198 if (fHead == null) { 199 fTail= null; 200 } 201 fLength--; 202 } else { 203 ListNode prev= getListNodeAt(index - 1); 204 ListNode node= getListNodeAt(index); 205 if (node != null) { 206 prev.next= node.next; 207 if (node == fTail) { 208 fTail= prev; 209 } 210 fLength--; 211 } 212 } 213 } 214 215 216 public void removeAttribute(String raw) { 217 removeAttributeAt(getIndex(raw)); 218 } 219 220 221 public void removeAttribute(String uri, String local) { 222 removeAttributeAt(getIndex(uri, local)); 223 } 224 225 226 private ListNode getListNodeAt(int i) { 227 228 for (ListNode place= fHead; place != null; place= place.next) { 229 if (--i == -1) { 230 return place; 231 } 232 } 233 return null; 234 } 235 236 237 public ListNode getListNode(String uri, String local) { 238 239 if (uri != null && local != null) { 240 ListNode place= fHead; 241 while (place != null) { 242 if (place.uri != null 243 && place.local != null 244 && place.uri.equals(uri) 245 && place.local.equals(local)) { 246 return place; 247 } 248 place= place.next; 249 } 250 } 251 return null; 252 } 253 254 255 private ListNode getListNode(String raw) { 256 257 if (raw != null) { 258 for (ListNode place= fHead; place != null; place= place.next) { 259 if (place.raw != null && place.raw.equals(raw)) { 260 return place; 261 } 262 } 263 } 264 265 return null; 266 } 267 268 269 public String toString() { 270 StringBuffer str= new StringBuffer (); 271 272 str.append('['); 273 str.append("len="); str.append(fLength); 275 str.append(", {"); for (ListNode place= fHead; place != null; place= place.next) { 277 str.append(place.toString()); 278 if (place.next != null) { 279 str.append(", "); } 281 } 282 str.append("}]"); 284 return str.toString(); 285 } 286 287 290 static class ListNode { 291 292 293 public String uri; 294 295 296 public String local; 297 298 299 public String raw; 300 301 302 public String type; 303 304 305 public String value; 306 307 308 public ListNode next; 309 310 311 public ListNode( 312 String uri0, 313 String local0, 314 String raw0, 315 String type0, 316 String value0) { 317 318 this.uri= uri0; 319 this.local= local0; 320 this.raw= raw0; 321 this.type= type0; 322 this.value= value0; 323 324 } 325 326 327 public String toString() { 328 return raw != null ? raw : local; 329 } 330 } 331 } 332 | Popular Tags |