1 18 package org.apache.batik.dom.svg; 19 20 import java.io.IOException ; 21 import java.io.ObjectInputStream ; 22 import java.net.URL ; 23 import java.util.Locale ; 24 import java.util.MissingResourceException ; 25 26 import org.apache.batik.css.engine.CSSEngine; 27 import org.apache.batik.dom.AbstractStylableDocument; 28 import org.apache.batik.dom.GenericAttr; 29 import org.apache.batik.dom.GenericAttrNS; 30 import org.apache.batik.dom.GenericCDATASection; 31 import org.apache.batik.dom.GenericComment; 32 import org.apache.batik.dom.GenericDocumentFragment; 33 import org.apache.batik.dom.GenericDocumentType; 34 import org.apache.batik.dom.GenericElement; 35 import org.apache.batik.dom.GenericEntityReference; 36 import org.apache.batik.dom.GenericProcessingInstruction; 37 import org.apache.batik.dom.GenericText; 38 import org.apache.batik.dom.StyleSheetFactory; 39 import org.apache.batik.dom.util.XMLSupport; 40 import org.apache.batik.i18n.Localizable; 41 import org.apache.batik.i18n.LocalizableSupport; 42 import org.apache.batik.util.SVGConstants; 43 44 import org.w3c.dom.Attr ; 45 import org.w3c.dom.CDATASection ; 46 import org.w3c.dom.Comment ; 47 import org.w3c.dom.DOMException ; 48 import org.w3c.dom.DOMImplementation ; 49 import org.w3c.dom.Document ; 50 import org.w3c.dom.DocumentFragment ; 51 import org.w3c.dom.DocumentType ; 52 import org.w3c.dom.Element ; 53 import org.w3c.dom.EntityReference ; 54 import org.w3c.dom.Node ; 55 import org.w3c.dom.ProcessingInstruction ; 56 import org.w3c.dom.Text ; 57 import org.w3c.dom.svg.SVGDocument; 58 import org.w3c.dom.svg.SVGLangSpace; 59 import org.w3c.dom.svg.SVGSVGElement; 60 61 67 public class SVGOMDocument 68 extends AbstractStylableDocument 69 implements SVGDocument, 70 SVGConstants { 71 72 75 protected final static String RESOURCES = 76 "org.apache.batik.dom.svg.resources.Messages"; 77 78 81 protected transient LocalizableSupport localizableSupport = 82 new LocalizableSupport(RESOURCES, getClass().getClassLoader()); 83 84 87 protected String referrer = ""; 88 89 92 protected URL url; 93 94 97 protected transient boolean readonly; 98 99 102 protected SVGOMDocument() { 103 } 104 105 108 public SVGOMDocument(DocumentType dt, DOMImplementation impl) { 109 super(dt, impl); 110 } 111 112 115 public void setLocale(Locale l) { 116 super.setLocale(l); 117 localizableSupport.setLocale(l); 118 } 119 120 123 public String formatMessage(String key, Object [] args) 124 throws MissingResourceException { 125 try { 126 return super.formatMessage(key, args); 127 } catch (MissingResourceException e) { 128 return localizableSupport.formatMessage(key, args); 129 } 130 } 131 132 135 public String getTitle() { 136 StringBuffer sb = new StringBuffer (); 137 boolean preserve = false; 138 139 for (Node n = getDocumentElement().getFirstChild(); 140 n != null; 141 n = n.getNextSibling()) { 142 String ns = n.getNamespaceURI(); 143 if (ns != null && ns.equals(SVG_NAMESPACE_URI)) { 144 if (n.getLocalName().equals(SVG_TITLE_TAG)) { 145 preserve = ((SVGLangSpace)n).getXMLspace().equals("preserve"); 146 for (n = n.getFirstChild(); 147 n != null; 148 n = n.getNextSibling()) { 149 if (n.getNodeType() == Node.TEXT_NODE) { 150 sb.append(n.getNodeValue()); 151 } 152 } 153 break; 154 } 155 } 156 } 157 158 String s = sb.toString(); 159 return (preserve) 160 ? XMLSupport.preserveXMLSpace(s) 161 : XMLSupport.defaultXMLSpace(s); 162 } 163 164 167 public String getReferrer() { 168 return referrer; 169 } 170 171 174 public void setReferrer(String s) { 175 referrer = s; 176 } 177 178 181 public String getDomain() { 182 return (url == null) ? null : url.getHost(); 183 } 184 185 188 public SVGSVGElement getRootElement() { 189 return (SVGSVGElement)getDocumentElement(); 190 } 191 192 195 public String getURL() { 196 return (url == null) ? null : url.toString(); 197 } 198 199 202 public URL getURLObject() { 203 return url; 204 } 205 206 209 public void setURLObject(URL url) { 210 this.url = url; 211 } 212 213 216 public Element createElement(String tagName) throws DOMException { 217 return new GenericElement(tagName.intern(), this); 218 } 219 220 223 public DocumentFragment createDocumentFragment() { 224 return new GenericDocumentFragment(this); 225 } 226 227 230 public Text createTextNode(String data) { 231 return new GenericText(data, this); 232 } 233 234 237 public Comment createComment(String data) { 238 return new GenericComment(data, this); 239 } 240 241 244 public CDATASection createCDATASection(String data) throws DOMException { 245 return new GenericCDATASection(data, this); 246 } 247 248 254 public ProcessingInstruction createProcessingInstruction(String target, 255 String data) 256 throws DOMException { 257 if ("xml-stylesheet".equals(target)) { 258 return new SVGStyleSheetProcessingInstruction 259 (data, this, (StyleSheetFactory)getImplementation()); 260 } 261 return new GenericProcessingInstruction(target, data, this); 262 } 263 264 267 public Attr createAttribute(String name) throws DOMException { 268 return new GenericAttr(name.intern(), this); 269 } 270 271 274 public EntityReference createEntityReference(String name) 275 throws DOMException { 276 return new GenericEntityReference(name, this); 277 } 278 279 282 public Attr createAttributeNS(String namespaceURI, String qualifiedName) 283 throws DOMException { 284 if (namespaceURI == null) { 285 return new GenericAttr(qualifiedName.intern(), this); 286 } else { 287 return new GenericAttrNS(namespaceURI.intern(), 288 qualifiedName.intern(), 289 this); 290 } 291 } 292 293 296 public Element createElementNS(String namespaceURI, String qualifiedName) 297 throws DOMException { 298 SVGDOMImplementation impl = (SVGDOMImplementation)implementation; 299 return impl.createElementNS(this, namespaceURI, qualifiedName); 300 } 301 302 306 public boolean isId(Attr node) { 307 if (node.getNamespaceURI() != null) return false; 308 return SVG_ID_ATTRIBUTE.equals(node.getNodeName()); 309 } 310 311 313 316 public boolean isReadonly() { 317 return readonly; 318 } 319 320 323 public void setReadonly(boolean v) { 324 readonly = v; 325 } 326 327 330 protected Node newNode() { 331 return new SVGOMDocument(); 332 } 333 334 338 protected Node copyInto(Node n) { 339 super.copyInto(n); 340 SVGOMDocument sd = (SVGOMDocument)n; 341 sd.localizableSupport = new LocalizableSupport 342 (RESOURCES, getClass().getClassLoader()); 343 sd.referrer = referrer; 344 sd.url = url; 345 return n; 346 } 347 348 352 protected Node deepCopyInto(Node n) { 353 super.deepCopyInto(n); 354 SVGOMDocument sd = (SVGOMDocument)n; 355 sd.localizableSupport = new LocalizableSupport 356 (RESOURCES, getClass().getClassLoader()); 357 sd.referrer = referrer; 358 sd.url = url; 359 return n; 360 } 361 362 364 367 private void readObject(ObjectInputStream s) 368 throws IOException , ClassNotFoundException { 369 s.defaultReadObject(); 370 371 localizableSupport = new LocalizableSupport 372 (RESOURCES, getClass().getClassLoader()); 373 } 374 } 375 | Popular Tags |