KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > dom > svg > AbstractElement


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

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 JavaDoc;
26 import org.w3c.dom.DOMException JavaDoc;
27 import org.w3c.dom.NamedNodeMap JavaDoc;
28 import org.w3c.dom.Node JavaDoc;
29 import org.w3c.dom.events.MutationEvent JavaDoc;
30
31 /**
32  * This class provides a superclass to implement an SVG element, or
33  * an element interoperable with the SVG elements.
34  *
35  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
36  * @version $Id: AbstractElement.java,v 1.11 2004/08/18 07:13:13 vhardy Exp $
37  */

38 public abstract class AbstractElement
39     extends org.apache.batik.dom.AbstractElement
40     implements NodeEventTarget {
41     
42     /**
43      * The live attribute values.
44      */

45     protected transient SoftDoublyIndexedTable liveAttributeValues;
46
47     /**
48      * Creates a new Element object.
49      */

50     protected AbstractElement() {
51     }
52
53     /**
54      * Creates a new Element object.
55      * @param prefix The namespace prefix.
56      * @param owner The owner document.
57      */

58     protected AbstractElement(String JavaDoc prefix, AbstractDocument owner) {
59         ownerDocument = owner;
60         setPrefix(prefix);
61     initializeAttributes();
62     }
63
64     // NodeEventTarget ////////////////////////////////////////////////////
65

66     /**
67      * Implements {@link NodeEventTarget#getParentNodeEventTarget()}.
68      */

69     public NodeEventTarget getParentNodeEventTarget() {
70         return (NodeEventTarget)
71             CSSEngine.getLogicalParentNode(getParentNode());
72     }
73
74     // Attributes /////////////////////////////////////////////////////////
75

76     /**
77      * Returns the live attribute value associated with given
78      * attribute, if any.
79      * @param ns The attribute's namespace.
80      * @param ln The attribute's local name.
81      */

82     public LiveAttributeValue getLiveAttributeValue(String JavaDoc ns, String JavaDoc ln) {
83         if (liveAttributeValues == null) {
84             return null;
85         }
86         return (LiveAttributeValue)liveAttributeValues.get(ns, ln);
87     }
88
89     /**
90      * Associates a live attribute value to this element.
91      * @param ns The attribute's namespace.
92      * @param ln The attribute's local name.
93      * @param val The live value.
94      */

95     public void putLiveAttributeValue(String JavaDoc ns, String JavaDoc ln,
96                                       LiveAttributeValue val) {
97         if (liveAttributeValues == null) {
98             liveAttributeValues = new SoftDoublyIndexedTable();
99         }
100         liveAttributeValues.put(ns, ln, val);
101     }
102
103     /**
104      * Returns the AttributeInitializer for this element type.
105      * @return null if this element has no attribute with a default value.
106      */

107     protected AttributeInitializer getAttributeInitializer() {
108         return null;
109     }
110
111     /**
112      * Initializes the attributes of this element to their default value.
113      */

114     protected void initializeAttributes() {
115         AttributeInitializer ai = getAttributeInitializer();
116         if (ai != null) {
117             ai.initializeAttributes(this);
118         }
119     }
120
121     /**
122      * Resets an attribute to the default value.
123      * @return true if a default value is known for the given attribute.
124      */

125     protected boolean resetAttribute(String JavaDoc ns, String JavaDoc prefix, String JavaDoc ln) {
126         AttributeInitializer ai = getAttributeInitializer();
127         if (ai == null) {
128             return false;
129         }
130         return ai.resetAttribute(this, ns, prefix, ln);
131     }
132
133     /**
134      * Creates the attribute list.
135      */

136     protected NamedNodeMap JavaDoc createAttributes() {
137     return new ExtendedNamedNodeHashMap();
138     }
139
140     /**
141      * Sets an unspecified attribute.
142      * @param nsURI The attribute namespace URI.
143      * @param name The attribute's qualified name.
144      * @param value The attribute's default value.
145     */

146     public void setUnspecifiedAttribute(String JavaDoc nsURI, String JavaDoc name,
147                                         String JavaDoc value) {
148     if (attributes == null) {
149         attributes = createAttributes();
150     }
151         ((ExtendedNamedNodeHashMap)attributes).
152             setUnspecifiedAttribute(nsURI, name, value);
153     }
154
155     /**
156      * Called when an attribute has been added.
157      */

158     protected void attrAdded(Attr node, String JavaDoc newv) {
159         LiveAttributeValue lav = getLiveAttributeValue(node);
160         if (lav != null) {
161             lav.attrAdded(node, newv);
162         }
163     }
164
165     /**
166      * Called when an attribute has been modified.
167      */

168     protected void attrModified(Attr node, String JavaDoc oldv, String JavaDoc newv) {
169         LiveAttributeValue lav = getLiveAttributeValue(node);
170         if (lav != null) {
171             lav.attrModified(node, oldv, newv);
172         }
173     }
174
175     /**
176      * Called when an attribute has been removed.
177      */

178     protected void attrRemoved(Attr node, String JavaDoc oldv) {
179         LiveAttributeValue lav = getLiveAttributeValue(node);
180         if (lav != null) {
181             lav.attrRemoved(node, oldv);
182         }
183     }
184
185     /**
186      * Gets Returns the live attribute value associated with given
187      * attribute, if any.
188      */

189     private LiveAttributeValue getLiveAttributeValue(Attr node) {
190         String JavaDoc ns = node.getNamespaceURI();
191         return getLiveAttributeValue(ns, (ns == null)
192                                      ? node.getNodeName()
193                                      : node.getLocalName());
194     }
195
196     // Importation ////////////////////////////////////////////////////
197

198     /**
199      * Exports this node to the given document.
200      */

201     protected Node JavaDoc export(Node JavaDoc n, AbstractDocument d) {
202     super.export(n, d);
203         ((AbstractElement)n).initializeAttributes();
204
205     super.export(n, d);
206     return n;
207     }
208
209     /**
210      * Deeply exports this node to the given document.
211      */

212     protected Node JavaDoc deepExport(Node JavaDoc n, AbstractDocument d) {
213     super.export(n, d);
214         ((AbstractElement)n).initializeAttributes();
215
216     super.deepExport(n, d);
217     return n;
218     }
219
220     /**
221      * An implementation of the {@link NamedNodeMap}.
222      */

223     protected class ExtendedNamedNodeHashMap extends NamedNodeHashMap {
224
225         /**
226          * Creates a new ExtendedNamedNodeHashMap object.
227          */

228         public ExtendedNamedNodeHashMap() {
229         }
230
231     /**
232      * Adds an unspecified attribute to the map.
233          * @param nsURI The attribute namespace URI.
234          * @param name The attribute's qualified name.
235          * @param value The attribute's default value.
236      */

237     public void setUnspecifiedAttribute(String JavaDoc nsURI, String JavaDoc name,
238                                             String JavaDoc value) {
239         Attr attr = getOwnerDocument().createAttributeNS(nsURI, name);
240         attr.setValue(value);
241         ((AbstractAttr)attr).setSpecified(false);
242             setNamedItemNS(attr);
243     }
244
245     /**
246      * <b>DOM</b>: Implements {@link
247          * NamedNodeMap#removeNamedItemNS(String,String)}.
248      */

249     public Node JavaDoc removeNamedItemNS(String JavaDoc namespaceURI, String JavaDoc localName)
250         throws DOMException JavaDoc {
251         if (isReadonly()) {
252         throw createDOMException
253                     (DOMException.NO_MODIFICATION_ALLOWED_ERR,
254                      "readonly.node.map",
255                      new Object JavaDoc[] {});
256         }
257         if (localName == null) {
258         throw createDOMException(DOMException.NOT_FOUND_ERR,
259                      "attribute.missing",
260                      new Object JavaDoc[] { "" });
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 JavaDoc[] { localName });
267         }
268         n.setOwnerElement(null);
269             String JavaDoc prefix = n.getPrefix();
270         
271             // Reset the attribute to its default value
272
if (!resetAttribute(namespaceURI, prefix, localName)) {
273                 // Mutation event
274
fireDOMAttrModifiedEvent(n.getNodeName(), n,
275                                          n.getNodeValue(), "",
276                                          MutationEvent.REMOVAL);
277             }
278             return n;
279     }
280     }
281 }
282
Popular Tags