1 18 package org.apache.batik.dom.svg; 19 20 import org.apache.batik.css.engine.CSSEngine; 21 import org.apache.batik.dom.AbstractAttr; 22 import org.apache.batik.dom.AbstractDocument; 23 import org.apache.batik.dom.events.NodeEventTarget; 24 import org.apache.batik.util.SoftDoublyIndexedTable; 25 import org.w3c.dom.Attr ; 26 import org.w3c.dom.DOMException ; 27 import org.w3c.dom.NamedNodeMap ; 28 import org.w3c.dom.Node ; 29 import org.w3c.dom.events.MutationEvent ; 30 31 38 public abstract class AbstractElement 39 extends org.apache.batik.dom.AbstractElement 40 implements NodeEventTarget { 41 42 45 protected transient SoftDoublyIndexedTable liveAttributeValues; 46 47 50 protected AbstractElement() { 51 } 52 53 58 protected AbstractElement(String prefix, AbstractDocument owner) { 59 ownerDocument = owner; 60 setPrefix(prefix); 61 initializeAttributes(); 62 } 63 64 66 69 public NodeEventTarget getParentNodeEventTarget() { 70 return (NodeEventTarget) 71 CSSEngine.getLogicalParentNode(getParentNode()); 72 } 73 74 76 82 public LiveAttributeValue getLiveAttributeValue(String ns, String ln) { 83 if (liveAttributeValues == null) { 84 return null; 85 } 86 return (LiveAttributeValue)liveAttributeValues.get(ns, ln); 87 } 88 89 95 public void putLiveAttributeValue(String ns, String ln, 96 LiveAttributeValue val) { 97 if (liveAttributeValues == null) { 98 liveAttributeValues = new SoftDoublyIndexedTable(); 99 } 100 liveAttributeValues.put(ns, ln, val); 101 } 102 103 107 protected AttributeInitializer getAttributeInitializer() { 108 return null; 109 } 110 111 114 protected void initializeAttributes() { 115 AttributeInitializer ai = getAttributeInitializer(); 116 if (ai != null) { 117 ai.initializeAttributes(this); 118 } 119 } 120 121 125 protected boolean resetAttribute(String ns, String prefix, String ln) { 126 AttributeInitializer ai = getAttributeInitializer(); 127 if (ai == null) { 128 return false; 129 } 130 return ai.resetAttribute(this, ns, prefix, ln); 131 } 132 133 136 protected NamedNodeMap createAttributes() { 137 return new ExtendedNamedNodeHashMap(); 138 } 139 140 146 public void setUnspecifiedAttribute(String nsURI, String name, 147 String value) { 148 if (attributes == null) { 149 attributes = createAttributes(); 150 } 151 ((ExtendedNamedNodeHashMap)attributes). 152 setUnspecifiedAttribute(nsURI, name, value); 153 } 154 155 158 protected void attrAdded(Attr node, String newv) { 159 LiveAttributeValue lav = getLiveAttributeValue(node); 160 if (lav != null) { 161 lav.attrAdded(node, newv); 162 } 163 } 164 165 168 protected void attrModified(Attr node, String oldv, String newv) { 169 LiveAttributeValue lav = getLiveAttributeValue(node); 170 if (lav != null) { 171 lav.attrModified(node, oldv, newv); 172 } 173 } 174 175 178 protected void attrRemoved(Attr node, String oldv) { 179 LiveAttributeValue lav = getLiveAttributeValue(node); 180 if (lav != null) { 181 lav.attrRemoved(node, oldv); 182 } 183 } 184 185 189 private LiveAttributeValue getLiveAttributeValue(Attr node) { 190 String ns = node.getNamespaceURI(); 191 return getLiveAttributeValue(ns, (ns == null) 192 ? node.getNodeName() 193 : node.getLocalName()); 194 } 195 196 198 201 protected Node export(Node n, AbstractDocument d) { 202 super.export(n, d); 203 ((AbstractElement)n).initializeAttributes(); 204 205 super.export(n, d); 206 return n; 207 } 208 209 212 protected Node deepExport(Node n, AbstractDocument d) { 213 super.export(n, d); 214 ((AbstractElement)n).initializeAttributes(); 215 216 super.deepExport(n, d); 217 return n; 218 } 219 220 223 protected class ExtendedNamedNodeHashMap extends NamedNodeHashMap { 224 225 228 public ExtendedNamedNodeHashMap() { 229 } 230 231 237 public void setUnspecifiedAttribute(String nsURI, String name, 238 String value) { 239 Attr attr = getOwnerDocument().createAttributeNS(nsURI, name); 240 attr.setValue(value); 241 ((AbstractAttr)attr).setSpecified(false); 242 setNamedItemNS(attr); 243 } 244 245 249 public Node removeNamedItemNS(String namespaceURI, String localName) 250 throws DOMException { 251 if (isReadonly()) { 252 throw createDOMException 253 (DOMException.NO_MODIFICATION_ALLOWED_ERR, 254 "readonly.node.map", 255 new Object [] {}); 256 } 257 if (localName == null) { 258 throw createDOMException(DOMException.NOT_FOUND_ERR, 259 "attribute.missing", 260 new Object [] { "" }); 261 } 262 AbstractAttr n = (AbstractAttr)remove(namespaceURI, localName); 263 if (n == null) { 264 throw createDOMException(DOMException.NOT_FOUND_ERR, 265 "attribute.missing", 266 new Object [] { localName }); 267 } 268 n.setOwnerElement(null); 269 String prefix = n.getPrefix(); 270 271 if (!resetAttribute(namespaceURI, prefix, localName)) { 273 fireDOMAttrModifiedEvent(n.getNodeName(), n, 275 n.getNodeValue(), "", 276 MutationEvent.REMOVAL); 277 } 278 return n; 279 } 280 } 281 } 282 | Popular Tags |