1 56 57 package org.jdom.contrib.ids; 58 59 import org.jdom.Attribute; 60 import org.jdom.Document; 61 import org.jdom.Element; 62 import org.jdom.Namespace; 63 import org.jdom.Parent; 64 65 66 73 public class IdAttribute extends Attribute { 74 75 public IdAttribute(String name, String value, Namespace namespace) { 76 super(name, value, namespace); 77 } 78 79 public IdAttribute(String name, String value, 80 int type, Namespace namespace) { 81 super(name, value, type, namespace); 82 } 83 84 public IdAttribute(String name, String value) { 85 this(name, value, UNDECLARED_TYPE, Namespace.NO_NAMESPACE); 86 } 87 88 public IdAttribute(String name, String value, int type) { 89 this(name, value, type, Namespace.NO_NAMESPACE); 90 } 91 92 protected Attribute setParent(Element parent) { 93 Parent oldParent = this.getParent(); 94 95 super.setParent(parent); 96 97 if (this.getAttributeType() == Attribute.ID_TYPE) { 98 Document doc; 99 100 if (oldParent != null) { 102 doc = oldParent.getDocument(); 103 if (doc instanceof IdDocument) { 104 ((IdDocument)doc).removeId(this.getValue()); 105 } 106 } 107 doc = this.getDocument(); 108 if (doc instanceof IdDocument) { 109 ((IdDocument)doc).addId(this.getValue(), this.getParent()); 110 } 111 } 112 return this; 113 } 114 115 public Attribute setValue(String value) { 116 String oldValue = this.getValue(); 117 118 super.setValue(value); 119 120 if (this.getAttributeType() == Attribute.ID_TYPE) { 121 Document doc = this.getDocument(); 123 if (doc instanceof IdDocument) { 124 ((IdDocument)doc).removeId(oldValue); 125 ((IdDocument)doc).addId(this.getValue(), this.getParent()); 126 } 127 } 128 return this; 129 } 130 131 public Attribute setAttributeType(int type) { 132 int oldType = this.getAttributeType(); 133 134 if (type != oldType) { 135 super.setAttributeType(type); 136 137 Document doc = this.getDocument(); 139 if (doc instanceof IdDocument) { 140 if (oldType == Attribute.ID_TYPE) { 141 ((IdDocument)doc).removeId(this.getValue()); 142 } 143 if (type == Attribute.ID_TYPE) { 144 ((IdDocument)doc).addId(this.getValue(), this.getParent()); 145 } 146 } 147 } 148 return this; 149 } 150 } 151 152 | Popular Tags |