1 18 package org.apache.batik.dom.svg; 19 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 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 ; 29 import org.w3c.dom.Node ; 30 import org.w3c.dom.events.Event ; 31 import org.w3c.dom.events.EventListener ; 32 import org.w3c.dom.stylesheets.LinkStyle; 33 import org.w3c.dom.svg.SVGStyleElement; 34 35 41 public class SVGOMStyleElement 42 extends SVGOMElement 43 implements CSSStyleSheetNode, 44 SVGStyleElement, 45 LinkStyle { 46 47 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 60 protected transient org.w3c.dom.stylesheets.StyleSheet sheet; 61 62 65 protected transient StyleSheet styleSheet; 66 67 70 protected transient EventListener domCharacterDataModifiedListener = 71 new DOMCharacterDataModifiedListener(); 72 73 76 protected SVGOMStyleElement() { 77 } 78 79 84 public SVGOMStyleElement(String prefix, AbstractDocument owner) { 85 super(prefix, owner); 86 } 87 88 91 public String getLocalName() { 92 return SVG_STYLE_TAG; 93 } 94 95 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 text = ""; 104 Node n = getFirstChild(); 105 if (n != null) { 106 StringBuffer sb = new StringBuffer (); 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 burl = null; 116 try { 117 String bu = XMLBaseSupport.getCascadedXMLBase(this); 118 if (bu != null) { 119 burl = new URL (bu); 120 } 121 } catch (MalformedURLException ex) { 122 ex.printStackTrace(); 124 throw new InternalError (); 125 } 126 String 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 140 public org.w3c.dom.stylesheets.StyleSheet getSheet() { 141 throw new RuntimeException (" !!! Not implemented."); 142 } 143 144 147 public String getXMLspace() { 148 return XMLSupport.getXMLSpace(this); 149 } 150 151 154 public void setXMLspace(String space) throws DOMException { 155 setAttributeNS(XMLSupport.XML_NAMESPACE_URI, 156 XMLSupport.XML_SPACE_ATTRIBUTE, 157 space); 158 } 159 160 163 public String getType() { 164 return getAttributeNS(null, SVG_TYPE_ATTRIBUTE); 165 } 166 167 170 public void setType(String type) throws DOMException { 171 setAttributeNS(null, SVG_TYPE_ATTRIBUTE, type); 172 } 173 174 177 public String getMedia() { 178 return getAttribute(SVG_MEDIA_ATTRIBUTE); 179 } 180 181 184 public void setMedia(String media) throws DOMException { 185 setAttribute(SVG_MEDIA_ATTRIBUTE, media); 186 } 187 188 191 public String getTitle() { 192 return getAttribute(SVG_TITLE_ATTRIBUTE); 193 } 194 195 198 public void setTitle(String title) throws DOMException { 199 setAttribute(SVG_TITLE_ATTRIBUTE, title); 200 } 201 202 206 protected AttributeInitializer getAttributeInitializer() { 207 return attributeInitializer; 208 } 209 210 213 protected Node newNode() { 214 return new SVGOMStyleElement(); 215 } 216 217 220 protected class DOMCharacterDataModifiedListener 221 implements EventListener { 222 public void handleEvent(Event evt) { 223 styleSheet = null; 224 } 225 } 226 } 227 | Popular Tags |