1 57 58 package sax.helpers; 59 60 import org.xml.sax.AttributeList ; 61 62 67 public class AttributeListImpl 68 implements AttributeList { 69 70 74 75 private ListNode head; 76 77 78 private ListNode tail; 79 80 81 private int length; 82 83 87 88 public int getLength() { 89 return length; 90 } 91 92 93 public String getName(int index) { 94 95 ListNode node = getNodeAt(index); 96 return (node != null) ? node.name : null; 97 98 } 100 101 public String getType(int index) { 102 103 ListNode node = getNodeAt(index); 104 return (node != null) ? node.type : null; 105 106 } 108 109 public String getValue(int index) { 110 111 ListNode node = getNodeAt(index); 112 return (node != null) ? node.value : null; 113 114 } 116 117 public String getType(String name) { 118 119 ListNode node = getNodeAt(name); 120 return (node != null) ? node.type : null; 121 122 } 124 125 public String getValue(String name) { 126 127 ListNode node = getNodeAt(name); 128 return (node != null) ? node.value : null; 129 130 } 132 136 137 public void addAttribute(String name, String type, String value) { 138 139 ListNode node = new ListNode(name, type, value); 140 if (length == 0) { 141 head = node; 142 } 143 else { 144 tail.next = node; 145 } 146 tail = node; 147 length++; 148 149 } 151 152 public void insertAttributeAt(int index, 153 String name, String type, String value) { 154 155 if (length == 0 || index >= length) { 157 addAttribute(name, type, value); 158 return; 159 } 160 161 ListNode node = new ListNode(name, type, value); 163 if (index < 1) { 164 node.next = head; 165 head = node; 166 } 167 else { 168 ListNode prev = getNodeAt(index - 1); 169 node.next = prev.next; 170 prev.next = node; 171 } 172 length++; 173 174 } 176 177 public void removeAttributeAt(int index) { 178 179 if (length == 0) { 180 return; 181 } 182 183 if (index == 0) { 184 head = head.next; 185 if (head == null) { 186 tail = null; 187 } 188 length--; 189 } 190 else { 191 ListNode prev = getNodeAt(index - 1); 192 ListNode node = getNodeAt(index); 193 if (node != null) { 194 prev.next = node.next; 195 if (node == tail) { 196 tail = prev; 197 } 198 length--; 199 } 200 } 201 202 } 204 208 209 private ListNode getNodeAt(int i) { 210 211 for (ListNode place = head; place != null; place = place.next) { 212 if (--i == -1) { 213 return place; 214 } 215 } 216 217 return null; 218 219 } 221 222 private ListNode getNodeAt(String name) { 223 224 if (name != null) { 225 for (ListNode place = head; place != null; place = place.next) { 226 if (place.name.equals(name)) { 227 return place; 228 } 229 } 230 } 231 232 return null; 233 234 } 236 240 241 public String toString() { 242 StringBuffer str = new StringBuffer (); 243 244 str.append('['); 245 str.append("len="); 246 str.append(length); 247 str.append(", {"); 248 for (ListNode place = head; place != null; place = place.next) { 249 str.append(place.name); 250 if (place.next != null) { 251 str.append(", "); 252 } 253 } 254 str.append("}]"); 255 256 return str.toString(); 257 258 } 260 264 267 static class ListNode { 268 269 273 274 public String name; 275 276 277 public String type; 278 279 280 public String value; 281 282 283 public ListNode next; 284 285 289 290 public ListNode(String name, String type, String value) { 291 this.name = name; 292 this.type = type; 293 this.value = value; 294 } 295 296 } 298 } | Popular Tags |