KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22
23 import org.apache.batik.css.engine.CSSEngine;
24 import org.apache.batik.css.engine.CSSStyleSheetNode;
25 import org.apache.batik.css.engine.StyleSheet;
26 import org.apache.batik.dom.AbstractDocument;
27 import org.apache.batik.dom.util.XMLSupport;
28 import org.w3c.dom.DOMException JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30 import org.w3c.dom.events.Event JavaDoc;
31 import org.w3c.dom.events.EventListener JavaDoc;
32 import org.w3c.dom.stylesheets.LinkStyle;
33 import org.w3c.dom.svg.SVGStyleElement;
34
35 /**
36  * This class implements {@link SVGStyleElement}.
37  *
38  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
39  * @version $Id: SVGOMStyleElement.java,v 1.20 2005/02/22 09:13:01 cam Exp $
40  */

41 public class SVGOMStyleElement
42     extends SVGOMElement
43     implements CSSStyleSheetNode,
44                SVGStyleElement,
45                LinkStyle {
46
47     /**
48      * The attribute initializer.
49      */

50     protected final static AttributeInitializer attributeInitializer;
51     static {
52         attributeInitializer = new AttributeInitializer(1);
53         attributeInitializer.addAttribute(XMLSupport.XML_NAMESPACE_URI,
54                                           "xml", "space", "preserve");
55     }
56
57     /**
58      * The style sheet.
59      */

60     protected transient org.w3c.dom.stylesheets.StyleSheet sheet;
61
62     /**
63      * The DOM CSS style-sheet.
64      */

65     protected transient StyleSheet styleSheet;
66
67     /**
68      * The listener used to track the content changes.
69      */

70     protected transient EventListener JavaDoc domCharacterDataModifiedListener =
71         new DOMCharacterDataModifiedListener();
72
73     /**
74      * Creates a new SVGOMStyleElement object.
75      */

76     protected SVGOMStyleElement() {
77     }
78
79     /**
80      * Creates a new SVGOMStyleElement object.
81      * @param prefix The namespace prefix.
82      * @param owner The owner document.
83      */

84     public SVGOMStyleElement(String JavaDoc prefix, AbstractDocument owner) {
85         super(prefix, owner);
86     }
87
88     /**
89      * <b>DOM</b>: Implements {@link org.w3c.dom.Node#getLocalName()}.
90      */

91     public String JavaDoc getLocalName() {
92         return SVG_STYLE_TAG;
93     }
94
95     /**
96      * Returns the associated style-sheet.
97      */

98     public StyleSheet getCSSStyleSheet() {
99         if (styleSheet == null) {
100             if (getType().equals("text/css")) {
101                 SVGOMDocument doc = (SVGOMDocument)getOwnerDocument();
102                 CSSEngine e = doc.getCSSEngine();
103                 String JavaDoc text = "";
104                 Node n = getFirstChild();
105                 if (n != null) {
106                     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
107                     while (n != null) {
108                         if (n.getNodeType() == Node.CDATA_SECTION_NODE
109                             || n.getNodeType() == Node.TEXT_NODE)
110                             sb.append(n.getNodeValue());
111                         n = n.getNextSibling();
112                     }
113                     text = sb.toString();
114                 }
115                 URL JavaDoc burl = null;
116                 try {
117                     String JavaDoc bu = XMLBaseSupport.getCascadedXMLBase(this);
118                     if (bu != null) {
119                         burl = new URL JavaDoc(bu);
120                     }
121                 } catch (MalformedURLException JavaDoc ex) {
122                     // !!! TODO
123
ex.printStackTrace();
124                     throw new InternalError JavaDoc();
125                 }
126                 String JavaDoc media = getAttributeNS(null, SVG_MEDIA_ATTRIBUTE);
127                 styleSheet = e.parseStyleSheet(text, burl, media);
128                 addEventListener("DOMCharacterDataModified",
129                                  domCharacterDataModifiedListener,
130                                  false);
131             }
132         }
133         return styleSheet;
134     }
135
136     /**
137      * <b>DOM</b>: Implements {@link
138      * org.w3c.dom.stylesheets.LinkStyle#getSheet()}.
139      */

140     public org.w3c.dom.stylesheets.StyleSheet getSheet() {
141         throw new RuntimeException JavaDoc(" !!! Not implemented.");
142     }
143
144     /**
145      * <b>DOM</b>: Implements {@link SVGStyleElement#getXMLspace()}.
146      */

147     public String JavaDoc getXMLspace() {
148         return XMLSupport.getXMLSpace(this);
149     }
150
151     /**
152      * <b>DOM</b>: Implements {@link SVGStyleElement#setXMLspace(String)}.
153      */

154     public void setXMLspace(String JavaDoc space) throws DOMException JavaDoc {
155         setAttributeNS(XMLSupport.XML_NAMESPACE_URI,
156                        XMLSupport.XML_SPACE_ATTRIBUTE,
157                        space);
158     }
159
160     /**
161      * <b>DOM</b>: Implements {@link SVGStyleElement#getType()}.
162      */

163     public String JavaDoc getType() {
164         return getAttributeNS(null, SVG_TYPE_ATTRIBUTE);
165     }
166
167     /**
168      * <b>DOM</b>: Implements {@link SVGStyleElement#setType(String)}.
169      */

170     public void setType(String JavaDoc type) throws DOMException JavaDoc {
171         setAttributeNS(null, SVG_TYPE_ATTRIBUTE, type);
172     }
173
174     /**
175      * <b>DOM</b>: Implements {@link SVGStyleElement#getMedia()}.
176      */

177     public String JavaDoc getMedia() {
178         return getAttribute(SVG_MEDIA_ATTRIBUTE);
179     }
180
181     /**
182      * <b>DOM</b>: Implements {@link SVGStyleElement#setMedia(String)}.
183      */

184     public void setMedia(String JavaDoc media) throws DOMException JavaDoc {
185         setAttribute(SVG_MEDIA_ATTRIBUTE, media);
186     }
187
188     /**
189      * <b>DOM</b>: Implements {@link SVGStyleElement#getTitle()}.
190      */

191     public String JavaDoc getTitle() {
192         return getAttribute(SVG_TITLE_ATTRIBUTE);
193     }
194
195     /**
196      * <b>DOM</b>: Implements {@link SVGStyleElement#setTitle(String)}.
197      */

198     public void setTitle(String JavaDoc title) throws DOMException JavaDoc {
199         setAttribute(SVG_TITLE_ATTRIBUTE, title);
200     }
201
202     /**
203      * Returns the AttributeInitializer for this element type.
204      * @return null if this element has no attribute with a default value.
205      */

206     protected AttributeInitializer getAttributeInitializer() {
207         return attributeInitializer;
208     }
209
210     /**
211      * Returns a new uninitialized instance of this object's class.
212      */

213     protected Node newNode() {
214         return new SVGOMStyleElement();
215     }
216
217     /**
218      * The DOMCharacterDataModified listener.
219      */

220     protected class DOMCharacterDataModifiedListener
221         implements EventListener JavaDoc {
222         public void handleEvent(Event JavaDoc evt) {
223             styleSheet = null;
224         }
225     }
226 }
227
Popular Tags