1 19 20 package org.netbeans.modules.xml.text.syntax.dom; 21 22 import java.util.*; 23 import javax.swing.text.BadLocationException ; 24 25 import org.w3c.dom.*; 26 import org.netbeans.modules.xml.text.syntax.*; 27 import org.netbeans.modules.xml.spi.dom.*; 28 import org.netbeans.editor.*; 29 30 38 public abstract class Tag extends SyntaxNode implements Element, XMLTokenIDs { 39 40 protected NamedNodeMap domAttributes; 41 42 protected String name; 43 44 public Tag(XMLSyntaxSupport support, TokenItem from, int to, String name, Collection attribs) { 45 super( support, from,to ); 46 this.name = name; 47 } 48 49 public final short getNodeType() { 50 return Node.ELEMENT_NODE; 51 } 52 53 public final String getNodeName() { 54 return getTagName(); 55 } 56 57 public final String getTagName() { 58 return name; 59 } 60 61 65 public synchronized NamedNodeMap getAttributes() { 66 67 if (domAttributes != null) return domAttributes; 69 70 Map map = new HashMap(3); 71 72 SCAN_LOOP: 73 for (TokenItem next = first.getNext(); next != null; next = next.getNext()) { 74 TokenID id = next.getTokenID(); 75 String name; 76 String value; 77 if (id == ARGUMENT) { 78 TokenItem attributeStart = next; 79 name = next.getImage(); 80 while (next.getTokenID() != VALUE) { 81 next = next.getNext(); 82 if (next == null) break SCAN_LOOP; 83 } 84 85 String image = next.getImage(); 87 char test = image.charAt(0); 88 if (image.length() == 1) { 89 if (test == '"' || test == '\'') { 90 next = next.getNext(); 91 } 92 } 93 94 if (next == null) break SCAN_LOOP; 95 value = next.getImage(); 96 97 Object key = NamedNodeMapImpl.createKey(name); 98 map.put(key, new AttrImpl(support, attributeStart, this)); 99 100 next = Util.skipAttributeValue(next, test); 101 if (next == null) break SCAN_LOOP; 102 } else if (id == WS) { 103 } else { 105 break; } 107 } 108 109 return new NamedNodeMapImpl(map); 111 } 112 113 public String getAttribute(String name) { 114 Attr attribute = getAttributeNode(name); 115 if (attribute == null) return null; 116 return attribute.getValue(); 117 } 118 119 public final void setAttribute(String name, String value) { 120 NamedNodeMap attributes = getAttributes(); 121 Node attr = attributes.getNamedItem(name); 122 if (attr != null) { 123 attr.setNodeValue(value); 124 } else { 125 String stringToInsert = " " + name + "=" + '"' + value + '"'; 126 127 BaseDocument doc = (BaseDocument)support.getDocument(); 129 doc.atomicLock(); 130 131 int insertStart = offset + length - 1; 134 135 SCAN_LOOP: 136 for (TokenItem next = first.getNext(); next != null; next = next.getNext()) { 137 TokenID id = next.getTokenID(); 138 if (id == ARGUMENT) { 139 while (next.getTokenID() != VALUE) { 140 next = next.getNext(); 141 if (next == null) break SCAN_LOOP; 142 } 143 144 if (next == null) break SCAN_LOOP; 145 146 String image = next.getImage(); 147 char test = image.charAt(0); 148 149 while (next.getTokenID() == VALUE || next.getTokenID() == CHARACTER) { 150 String actualValue = Util.actualAttributeValue(image); 151 if (!actualValue.equals(image)) { 152 insertStart = next.getOffset() + actualValue.length(); 153 break SCAN_LOOP; 154 } 155 next = next.getNext(); 156 if (next == null) break SCAN_LOOP; 157 158 image = next.getImage(); 161 insertStart = next.getOffset(); 162 if (image.length() > 0 && image.charAt(image.length() - 1) == '>') { 163 insertStart += image.length() - 1; 165 if (image.length() > 1 && image.charAt(image.length() - 2) == '/') { 166 insertStart--; 168 } 169 } 170 } 171 172 if (next == null) break SCAN_LOOP; 173 } else if (id == WS) { 174 } else { 176 break; } 178 } 179 180 try { 182 doc.insertString(insertStart, stringToInsert, null); 183 doc.invalidateSyntaxMarks(); 184 } catch( BadLocationException e ) { 185 throw new DOMException(DOMException.INVALID_STATE_ERR , e.getMessage()); 186 } finally { 187 doc.atomicUnlock(); 188 } 189 } 190 191 retokenizeObject(); 193 } 194 195 public final void removeAttribute(String name) { 196 throw new ROException(); 197 } 198 199 public Attr getAttributeNode(String name) { 200 NamedNodeMap map = getAttributes(); 201 Node node = map.getNamedItem(name); 202 return (Attr) node; 203 } 204 205 public final Attr setAttributeNode(Attr attribute) { 206 throw new ROException(); 207 } 208 209 public final Attr removeAttributeNode(Attr attribute) { 210 throw new ROException(); 211 } 212 213 public NodeList getElementsByTagName(String name) { 214 throw new ROException(); 215 } 216 217 221 public Node getPreviousSibling() { 222 SyntaxNode prev = getStartTag(); 223 if (prev == null) return null; 224 prev = findPrevious(prev); 225 if (prev instanceof StartTag) { 226 return null; 227 } else { 228 return prev; 229 } 230 } 231 232 236 public Node getNextSibling() { 237 SyntaxNode next = getEndTag(); 238 if (next == null) return null; 239 next = findNext(next); 240 if (next instanceof EndTag) { 241 return null; 242 } else { 243 return next; 244 } 245 } 246 247 public Node getFirstChild() { 248 NodeList list = getChildNodes(); 249 if (list.getLength() == 0) return null; 250 return getChildNodes().item(0); 251 } 252 253 public Node getLastChild() { 254 NodeList list = getChildNodes(); 255 if (list.getLength() == 0) return null; 256 return list.item(list.getLength()); 257 } 258 259 protected abstract Tag getStartTag(); 260 261 protected abstract Tag getEndTag(); 262 263 268 269 271 public String getAttributeNS(String namespaceURI, String localName) { 272 throw new UOException(); 273 } 274 275 public void setAttributeNS(String namespaceURI, String qualifiedName, String value) { 276 throw new UOException(); 277 } 278 279 public void removeAttributeNS(String namespaceURI, String localName) { 280 throw new UOException(); 281 } 282 283 public Attr getAttributeNodeNS(String namespaceURI, String localName) { 284 throw new UOException(); 285 } 286 287 public Attr setAttributeNodeNS(Attr newAttr) { 288 throw new UOException(); 289 } 290 291 public NodeList getElementsByTagNameNS(String namespaceURI, String localName) { 292 throw new UOException(); 293 } 294 295 public boolean hasAttribute(String name) { 296 throw new UOException(); 297 } 298 299 public boolean hasAttributeNS(String namespaceURI, String localName) { 300 throw new UOException(); 301 } 302 303 public void retokenizeObject() { 304 try { 306 first = support.getTokenChain(offset, support.getDocument().getLength()); 307 } catch (BadLocationException e) { 308 throw new DOMException(DOMException.INVALID_STATE_ERR , e.getMessage()); 309 } 310 } 311 312 321 public final boolean equals(Object obj) { 322 if (obj == this) return true; 323 if (obj instanceof Tag) { 324 Tag tag = (Tag) obj; 325 Tag t1 = tag.getStartTag(); 326 Tag t2 = getStartTag(); 327 if (t1 == null || t2 == null) return false; 328 return t1.superEquals(t2); 329 } 330 return false; 331 } 332 333 private boolean superEquals(Tag tag) { 334 return super.equals(tag); 335 } 336 337 340 public final int hashCode() { 341 Tag tag = getStartTag(); 342 if (tag == null || tag == this) { 343 return super.hashCode(); 344 } else { 345 return tag.hashCode(); 346 } 347 } 348 } 349 350 | Popular Tags |