KickJava   Java API By Example, From Geeks To Geeks.

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


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.svg;
19
20 import org.apache.batik.css.engine.CSSEngine;
21 import org.apache.batik.dom.AbstractDocument;
22 import org.apache.batik.dom.util.DOMUtilities;
23 import org.apache.batik.util.SVGConstants;
24 import org.apache.batik.util.XMLConstants;
25 import org.w3c.dom.DOMException JavaDoc;
26 import org.w3c.dom.Element JavaDoc;
27 import org.w3c.dom.Node JavaDoc;
28 import org.w3c.dom.svg.SVGAnimatedEnumeration;
29 import org.w3c.dom.svg.SVGAnimatedInteger;
30 import org.w3c.dom.svg.SVGAnimatedLength;
31 import org.w3c.dom.svg.SVGAnimatedNumber;
32 import org.w3c.dom.svg.SVGAnimatedString;
33 import org.w3c.dom.svg.SVGElement;
34 import org.w3c.dom.svg.SVGException;
35 import org.w3c.dom.svg.SVGFitToViewBox;
36 import org.w3c.dom.svg.SVGSVGElement;
37
38 /**
39  * This class implements the {@link SVGElement} interface.
40  *
41  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
42  * @version $Id: SVGOMElement.java,v 1.18 2005/02/22 09:13:01 cam Exp $
43  */

44 public abstract class SVGOMElement
45     extends AbstractElement
46     implements SVGElement,
47                SVGConstants {
48
49     /**
50      * Is this element immutable?
51      */

52     protected transient boolean readonly;
53
54     /**
55      * The element prefix.
56      */

57     protected String JavaDoc prefix;
58
59     /**
60      * The SVG context to get SVG specific informations.
61      */

62     protected transient SVGContext svgContext;
63
64     /**
65      * Creates a new Element object.
66      */

67     protected SVGOMElement() {
68     }
69
70     /**
71      * Creates a new Element object.
72      * @param prefix The namespace prefix.
73      * @param owner The owner document.
74      */

75     protected SVGOMElement(String JavaDoc prefix, AbstractDocument owner) {
76         super(prefix, owner);
77     }
78
79     /**
80      * <b>DOM</b>: Implements {@link SVGElement#getId()}.
81      */

82     public String JavaDoc getId() {
83         return getAttributeNS(null, "id");
84     }
85
86     /**
87      * <b>DOM</b>: Implements {@link SVGElement#setId(String)}.
88      */

89     public void setId(String JavaDoc id) {
90         setAttributeNS(null, "id", id);
91     }
92
93     /**
94      * <b>DOM</b>: Implements {@link SVGElement#getXMLbase()}.
95      */

96     public String JavaDoc getXMLbase() {
97         return XMLBaseSupport.getXMLBase(this);
98     }
99
100     /**
101      * <b>DOM</b>: Implements {@link SVGElement#setXMLbase(String)}.
102      */

103     public void setXMLbase(String JavaDoc xmlbase) throws DOMException JavaDoc {
104         setAttributeNS(XMLConstants.XML_NAMESPACE_URI, "xml:base", xmlbase);
105     }
106
107     /**
108      * <b>DOM</b>: Implements {@link SVGElement#getOwnerSVGElement()}.
109      */

110     public SVGSVGElement getOwnerSVGElement() {
111         for (Element JavaDoc e = CSSEngine.getParentCSSStylableElement(this);
112              e != null;
113              e = CSSEngine.getParentCSSStylableElement(e)) {
114             if (e instanceof SVGSVGElement) {
115                 return (SVGSVGElement)e;
116             }
117         }
118         return null;
119     }
120
121     /**
122      * <b>DOM</b>: Implements {@link SVGElement#getViewportElement()}.
123      */

124     public SVGElement getViewportElement() {
125         for (Element JavaDoc e = CSSEngine.getParentCSSStylableElement(this);
126              e != null;
127              e = CSSEngine.getParentCSSStylableElement(e)) {
128             if (e instanceof SVGFitToViewBox) {
129                 return (SVGElement)e;
130             }
131         }
132         return null;
133     }
134
135     /**
136      * <b>DOM</b>: Implements {@link Node#getNodeName()}.
137      */

138     public String JavaDoc getNodeName() {
139         if (prefix == null || prefix.equals("")) {
140             return getLocalName();
141         }
142         String JavaDoc ln = getLocalName();
143         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(prefix.length() + ln.length() + 1);
144         sb.append(prefix).append(':').append(ln);
145         return sb.toString();
146     }
147
148     /**
149      * <b>DOM</b>: Implements {@link Node#getNamespaceURI()}.
150      */

151     public String JavaDoc getNamespaceURI() {
152         return SVGDOMImplementation.SVG_NAMESPACE_URI;
153     }
154
155     /**
156      * <b>DOM</b>: Implements {@link Node#setPrefix(String)}.
157      */

158     public void setPrefix(String JavaDoc prefix) throws DOMException JavaDoc {
159         if (isReadonly()) {
160         throw createDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
161                      "readonly.node",
162                      new Object JavaDoc[] { new Integer JavaDoc(getNodeType()),
163                             getNodeName() });
164         }
165         if (prefix != null &&
166             !prefix.equals("") &&
167             !DOMUtilities.isValidName(prefix)) {
168         throw createDOMException(DOMException.INVALID_CHARACTER_ERR,
169                      "prefix",
170                      new Object JavaDoc[] { new Integer JavaDoc(getNodeType()),
171                             getNodeName(),
172                             prefix });
173         }
174         this.prefix = prefix;
175     }
176
177     // SVGContext ////////////////////////////////////////////////////
178

179     /**
180      * Sets the SVG context to use to get SVG specific informations.
181      *
182      * @param ctx the SVG context
183      */

184     public void setSVGContext(SVGContext ctx) {
185         svgContext = ctx;
186     }
187
188     /**
189      * Returns the SVG context used to get SVG specific informations.
190      */

191     public SVGContext getSVGContext() {
192         return svgContext;
193     }
194
195     // ExtendedNode //////////////////////////////////////////////////
196

197     /**
198      * Creates an SVGException with the appropriate error message.
199      */

200     public SVGException createSVGException(short type,
201                                            String JavaDoc key,
202                                            Object JavaDoc [] args) {
203         try {
204             return new SVGOMException
205                 (type, getCurrentDocument().formatMessage(key, args));
206         } catch (Exception JavaDoc e) {
207             return new SVGOMException(type, key);
208         }
209     }
210
211     /**
212      * Tests whether this node is readonly.
213      */

214     public boolean isReadonly() {
215         return readonly;
216     }
217
218     /**
219      * Sets this node readonly attribute.
220      */

221     public void setReadonly(boolean v) {
222         readonly = v;
223     }
224
225     /**
226      * Manages the query of an SVGAnimatedString.
227      * @param ns The namespace of the attribute.
228      * @param ln The local name of the attribute.
229      */

230     protected SVGAnimatedString getAnimatedStringAttribute(String JavaDoc ns,
231                                                            String JavaDoc ln) {
232         SVGAnimatedString result =
233             (SVGAnimatedString)getLiveAttributeValue(ns, ln);
234         if (result == null) {
235             result = new SVGOMAnimatedString(this, ns, ln);
236             putLiveAttributeValue(ns, ln, (LiveAttributeValue)result);
237         }
238         return result;
239     }
240
241     /**
242      * Manages the query of an SVGAnimatedNumber.
243      * @param ns The namespace of the attribute.
244      * @param ln The local name of the attribute.
245      * @param val The value if the attribute is not specified.
246      */

247     protected SVGAnimatedNumber getAnimatedNumberAttribute(String JavaDoc ns,
248                                                            String JavaDoc ln,
249                                                            float val) {
250         SVGAnimatedNumber result =
251             (SVGAnimatedNumber)getLiveAttributeValue(ns, ln);
252         if (result == null) {
253             result = new SVGOMAnimatedNumber(this, ns, ln, val);
254             putLiveAttributeValue(ns, ln, (LiveAttributeValue)result);
255         }
256         return result;
257     }
258
259     /**
260      * Manages the query of an SVGAnimatedInteger.
261      * @param ns The namespace of the attribute.
262      * @param ln The local name of the attribute.
263      * @param val The value if the attribute is not specified.
264      */

265     protected SVGAnimatedInteger getAnimatedIntegerAttribute(String JavaDoc ns,
266                                                              String JavaDoc ln,
267                                                              int val) {
268         SVGAnimatedInteger result =
269             (SVGAnimatedInteger)getLiveAttributeValue(ns, ln);
270         if (result == null) {
271             result = new SVGOMAnimatedInteger(this, ns, ln, val);
272             putLiveAttributeValue(ns, ln, (LiveAttributeValue)result);
273         }
274         return result;
275     }
276
277     /**
278      * Manages the query of an SVGAnimatedEnumeration.
279      * @param ns The namespace of the attribute.
280      * @param ln The local name of the attribute.
281      * @param val The values in the enumeration.
282      * @param def The value if the attribute is not specified.
283      */

284     protected SVGAnimatedEnumeration
285         getAnimatedEnumerationAttribute(String JavaDoc ns, String JavaDoc ln,
286                                         String JavaDoc[] val, short def) {
287         SVGAnimatedEnumeration result =
288             (SVGAnimatedEnumeration)getLiveAttributeValue(ns, ln);
289         if (result == null) {
290             result = new SVGOMAnimatedEnumeration(this, ns, ln, val, def);
291             putLiveAttributeValue(ns, ln, (LiveAttributeValue)result);
292         }
293         return result;
294     }
295
296     /**
297      * Manages the query of an SVGAnimatedNumber.
298      * @param ns The namespace of the attribute.
299      * @param ln The local name of the attribute.
300      * @param val The value if the attribute is not specified.
301      * @param dir The length direction.
302      */

303     protected SVGAnimatedLength getAnimatedLengthAttribute(String JavaDoc ns,
304                                                            String JavaDoc ln,
305                                                            String JavaDoc val,
306                                                            short dir) {
307         SVGAnimatedLength result =
308             (SVGAnimatedLength)getLiveAttributeValue(ns, ln);
309         if (result == null) {
310             result = new SVGOMAnimatedLength(this, ns, ln, val, dir);
311             putLiveAttributeValue(ns, ln, (LiveAttributeValue)result);
312         }
313         return result;
314     }
315
316     // Importation/Cloning ///////////////////////////////////////////
317

318     /**
319      * Exports this node to the given document.
320      */

321     protected Node JavaDoc export(Node JavaDoc n, AbstractDocument d) {
322     super.export(n, d);
323     SVGOMElement e = (SVGOMElement)n;
324     e.prefix = prefix;
325     return n;
326     }
327
328     /**
329      * Deeply exports this node to the given document.
330      */

331     protected Node JavaDoc deepExport(Node JavaDoc n, AbstractDocument d) {
332     super.deepExport(n, d);
333     SVGOMElement e = (SVGOMElement)n;
334     e.prefix = prefix;
335     return n;
336     }
337
338     /**
339      * Copy the fields of the current node into the given node.
340      * @param n a node of the type of this.
341      */

342     protected Node JavaDoc copyInto(Node JavaDoc n) {
343     super.copyInto(n);
344     SVGOMElement e = (SVGOMElement)n;
345     e.prefix = prefix;
346     return n;
347     }
348
349     /**
350      * Deeply copy the fields of the current node into the given node.
351      * @param n a node of the type of this.
352      */

353     protected Node JavaDoc deepCopyInto(Node JavaDoc n) {
354     super.deepCopyInto(n);
355     SVGOMElement e = (SVGOMElement)n;
356     e.prefix = prefix;
357     return n;
358     }
359 }
360
Popular Tags