KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > dom > AbstractAttr


1 /*
2
3    Copyright 2000-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;
19
20 import org.apache.batik.dom.util.DOMUtilities;
21 import org.w3c.dom.Attr JavaDoc;
22 import org.w3c.dom.DOMException JavaDoc;
23 import org.w3c.dom.Element JavaDoc;
24 import org.w3c.dom.Node JavaDoc;
25 import org.w3c.dom.events.MutationEvent JavaDoc;
26
27 /**
28  * This class implements the {@link org.w3c.dom.Attr} interface.
29  *
30  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
31  * @version $Id: AbstractAttr.java,v 1.16 2005/03/19 00:59:41 deweese Exp $
32  */

33 public abstract class AbstractAttr extends AbstractParentNode implements Attr JavaDoc {
34     /**
35      * The name of this node.
36      */

37     protected String JavaDoc nodeName;
38
39     /**
40      * Whether this attribute was not specified in the original document.
41      */

42     protected boolean unspecified;
43
44     /**
45      * Whether this attribute is an ID attribute
46      */

47     protected boolean isIdAttr;
48
49     /**
50      * The owner element.
51      */

52     protected AbstractElement ownerElement;
53
54     /**
55      * Creates a new Attr object.
56      */

57     protected AbstractAttr() {
58     }
59
60     /**
61      * Creates a new Attr object.
62      * @param name The attribute name for validation purposes.
63      * @param owner The owner document.
64      * @exception DOMException
65      * INVALID_CHARACTER_ERR: if name contains invalid characters,
66      */

67     protected AbstractAttr(String JavaDoc name, AbstractDocument owner)
68         throws DOMException JavaDoc {
69     ownerDocument = owner;
70     if (!DOMUtilities.isValidName(name)) {
71         throw createDOMException(DOMException.INVALID_CHARACTER_ERR,
72                      "xml.name",
73                      new Object JavaDoc[] { name });
74     }
75     }
76
77     public boolean isId() { return isIdAttr; }
78
79     public void setIsId(boolean isId) {
80         isIdAttr = isId;
81     }
82
83     /**
84      * Sets the node name.
85      */

86     public void setNodeName(String JavaDoc v) {
87     nodeName = v;
88         isIdAttr = ownerDocument.isId(this);
89     }
90
91     /**
92      * <b>DOM</b>: Implements {@link org.w3c.dom.Node#getNodeName()}.
93      * @return {@link #nodeName}.
94      */

95     public String JavaDoc getNodeName() {
96     return nodeName;
97     }
98
99     /**
100      * <b>DOM</b>: Implements {@link org.w3c.dom.Node#getNodeType()}.
101      * @return {@link org.w3c.dom.Node#ATTRIBUTE_NODE}
102      */

103     public short getNodeType() {
104     return ATTRIBUTE_NODE;
105     }
106
107     /**
108      * <b>DOM</b>: Implements {@link org.w3c.dom.Node#getNodeValue()}.
109      * @return The content of the attribute.
110      */

111     public String JavaDoc getNodeValue() throws DOMException JavaDoc {
112         Node JavaDoc first = getFirstChild();
113         if (first == null) {
114             return "";
115         }
116         Node JavaDoc n = first.getNextSibling();
117         if (n == null) {
118             return first.getNodeValue();
119         }
120     StringBuffer JavaDoc result = new StringBuffer JavaDoc(first.getNodeValue());
121     do {
122         result.append(n.getNodeValue());
123             n = n.getNextSibling();
124     } while (n != null);
125     return result.toString();
126     }
127
128     /**
129      * <b>DOM</b>: Implements {@link org.w3c.dom.Node#setNodeValue(String)}.
130      */

131     public void setNodeValue(String JavaDoc nodeValue) throws DOMException JavaDoc {
132     if (isReadonly()) {
133         throw createDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
134                      "readonly.node",
135                      new Object JavaDoc[] { new Integer JavaDoc(getNodeType()),
136                             getNodeName() });
137     }
138
139         String JavaDoc s = getNodeValue();
140
141     // Remove all the children
142
Node JavaDoc n;
143     while ((n = getFirstChild()) != null) {
144         removeChild(n);
145     }
146
147         String JavaDoc val = (nodeValue == null) ? "" : nodeValue;
148
149     // Create and append a new child.
150
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     /**
163      * <b>DOM</b>: Implements {@link org.w3c.dom.Attr#getName()}.
164      * @return {@link #getNodeName()}.
165      */

166     public String JavaDoc getName() {
167     return getNodeName();
168     }
169
170     /**
171      * <b>DOM</b>: Implements {@link org.w3c.dom.Attr#getSpecified()}.
172      * @return !{@link #unspecified}.
173      */

174     public boolean getSpecified() {
175     return !unspecified;
176     }
177
178     /**
179      * Sets the specified attribute.
180      */

181     public void setSpecified(boolean v) {
182     unspecified = !v;
183     }
184
185     /**
186      * <b>DOM</b>: Implements {@link org.w3c.dom.Attr#getValue()}.
187      * @return {@link #getNodeValue()}.
188      */

189     public String JavaDoc getValue() {
190     return getNodeValue();
191     }
192
193     /**
194      * <b>DOM</b>: Implements {@link org.w3c.dom.Attr#setValue(String)}.
195      */

196     public void setValue(String JavaDoc value) throws DOMException JavaDoc {
197     setNodeValue(value);
198     }
199
200     /**
201      * Sets the owner element.
202      */

203     public void setOwnerElement(AbstractElement v) {
204     ownerElement = v;
205     }
206     
207     /**
208      * <b>DOM</b>: Implements {@link org.w3c.dom.Attr#getOwnerElement()}.
209      */

210     public Element JavaDoc getOwnerElement() {
211     return ownerElement;
212     }
213
214     /**
215      * Called when a child node has been added.
216      */

217     protected void nodeAdded(Node JavaDoc n) {
218         setSpecified(true);
219     }
220
221     /**
222      * Called when a child node is going to be removed.
223      */

224     protected void nodeToBeRemoved(Node JavaDoc n) {
225         setSpecified(true);
226     }
227
228     /**
229      * Exports this node to the given document.
230      */

231     protected Node JavaDoc export(Node JavaDoc 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     /**
241      * Deeply exports this node to the given document.
242      */

243     protected Node JavaDoc deepExport(Node JavaDoc 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     /**
253      * Copy the fields of the current node into the given node.
254      * @param n a node of the type of this.
255      */

256     protected Node JavaDoc copyInto(Node JavaDoc 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     /**
266      * Deeply copy the fields of the current node into the given node.
267      * @param n a node of the type of this.
268      */

269     protected Node JavaDoc deepCopyInto(Node JavaDoc 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     /**
279      * Checks the validity of a node to be inserted.
280      */

281     protected void checkChildType(Node JavaDoc 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 JavaDoc[] { new Integer JavaDoc(getNodeType()),
292                                             getNodeName(),
293                                 new Integer JavaDoc(n.getNodeType()),
294                                             n.getNodeName() });
295     }
296     }
297
298     /**
299      * Fires a DOMSubtreeModified event.
300      */

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