1 18 package org.apache.batik.dom; 19 20 import org.apache.batik.dom.util.DOMUtilities; 21 import org.w3c.dom.Attr ; 22 import org.w3c.dom.DOMException ; 23 import org.w3c.dom.Element ; 24 import org.w3c.dom.Node ; 25 import org.w3c.dom.events.MutationEvent ; 26 27 33 public abstract class AbstractAttr extends AbstractParentNode implements Attr { 34 37 protected String nodeName; 38 39 42 protected boolean unspecified; 43 44 47 protected boolean isIdAttr; 48 49 52 protected AbstractElement ownerElement; 53 54 57 protected AbstractAttr() { 58 } 59 60 67 protected AbstractAttr(String name, AbstractDocument owner) 68 throws DOMException { 69 ownerDocument = owner; 70 if (!DOMUtilities.isValidName(name)) { 71 throw createDOMException(DOMException.INVALID_CHARACTER_ERR, 72 "xml.name", 73 new Object [] { name }); 74 } 75 } 76 77 public boolean isId() { return isIdAttr; } 78 79 public void setIsId(boolean isId) { 80 isIdAttr = isId; 81 } 82 83 86 public void setNodeName(String v) { 87 nodeName = v; 88 isIdAttr = ownerDocument.isId(this); 89 } 90 91 95 public String getNodeName() { 96 return nodeName; 97 } 98 99 103 public short getNodeType() { 104 return ATTRIBUTE_NODE; 105 } 106 107 111 public String getNodeValue() throws DOMException { 112 Node first = getFirstChild(); 113 if (first == null) { 114 return ""; 115 } 116 Node n = first.getNextSibling(); 117 if (n == null) { 118 return first.getNodeValue(); 119 } 120 StringBuffer result = new StringBuffer (first.getNodeValue()); 121 do { 122 result.append(n.getNodeValue()); 123 n = n.getNextSibling(); 124 } while (n != null); 125 return result.toString(); 126 } 127 128 131 public void setNodeValue(String nodeValue) throws DOMException { 132 if (isReadonly()) { 133 throw createDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, 134 "readonly.node", 135 new Object [] { new Integer (getNodeType()), 136 getNodeName() }); 137 } 138 139 String s = getNodeValue(); 140 141 Node n; 143 while ((n = getFirstChild()) != null) { 144 removeChild(n); 145 } 146 147 String val = (nodeValue == null) ? "" : nodeValue; 148 149 n = getOwnerDocument().createTextNode(val); 151 appendChild(n); 152 153 if (ownerElement != null) { 154 ownerElement.fireDOMAttrModifiedEvent(nodeName, 155 this, 156 s, 157 val, 158 MutationEvent.MODIFICATION); 159 } 160 } 161 162 166 public String getName() { 167 return getNodeName(); 168 } 169 170 174 public boolean getSpecified() { 175 return !unspecified; 176 } 177 178 181 public void setSpecified(boolean v) { 182 unspecified = !v; 183 } 184 185 189 public String getValue() { 190 return getNodeValue(); 191 } 192 193 196 public void setValue(String value) throws DOMException { 197 setNodeValue(value); 198 } 199 200 203 public void setOwnerElement(AbstractElement v) { 204 ownerElement = v; 205 } 206 207 210 public Element getOwnerElement() { 211 return ownerElement; 212 } 213 214 217 protected void nodeAdded(Node n) { 218 setSpecified(true); 219 } 220 221 224 protected void nodeToBeRemoved(Node n) { 225 setSpecified(true); 226 } 227 228 231 protected Node export(Node n, AbstractDocument d) { 232 super.export(n, d); 233 AbstractAttr aa = (AbstractAttr)n; 234 aa.nodeName = nodeName; 235 aa.unspecified = false; 236 aa.isIdAttr = d.isId(aa); 237 return n; 238 } 239 240 243 protected Node deepExport(Node n, AbstractDocument d) { 244 super.deepExport(n, d); 245 AbstractAttr aa = (AbstractAttr)n; 246 aa.nodeName = nodeName; 247 aa.unspecified = false; 248 aa.isIdAttr = d.isId(aa); 249 return n; 250 } 251 252 256 protected Node copyInto(Node n) { 257 super.copyInto(n); 258 AbstractAttr aa = (AbstractAttr)n; 259 aa.nodeName = nodeName; 260 aa.unspecified = unspecified; 261 aa.isIdAttr = isIdAttr; 262 return n; 263 } 264 265 269 protected Node deepCopyInto(Node n) { 270 super.deepCopyInto(n); 271 AbstractAttr aa = (AbstractAttr)n; 272 aa.nodeName = nodeName; 273 aa.unspecified = unspecified; 274 aa.isIdAttr = isIdAttr; 275 return n; 276 } 277 278 281 protected void checkChildType(Node n, boolean replace) { 282 switch (n.getNodeType()) { 283 case TEXT_NODE: 284 case ENTITY_REFERENCE_NODE: 285 case DOCUMENT_FRAGMENT_NODE: 286 break; 287 default: 288 throw createDOMException 289 (DOMException.HIERARCHY_REQUEST_ERR, 290 "child.type", 291 new Object [] { new Integer (getNodeType()), 292 getNodeName(), 293 new Integer (n.getNodeType()), 294 n.getNodeName() }); 295 } 296 } 297 298 301 protected void fireDOMSubtreeModifiedEvent() { 302 AbstractDocument doc = getCurrentDocument(); 303 if (doc.getEventsEnabled()) { 304 super.fireDOMSubtreeModifiedEvent(); 305 if (getOwnerElement() != null) { 306 ((AbstractElement)getOwnerElement()). 307 fireDOMSubtreeModifiedEvent(); 308 } 309 } 310 } 311 } 312 | Popular Tags |