1 18 package org.apache.batik.bridge; 19 20 import java.io.IOException ; 21 import java.net.MalformedURLException ; 22 23 import org.apache.batik.dom.svg.SVGOMDocument; 24 import org.apache.batik.dom.svg.XMLBaseSupport; 25 import org.apache.batik.util.ParsedURL; 26 import org.w3c.dom.Document ; 27 import org.w3c.dom.Element ; 28 import org.w3c.dom.Node ; 29 import org.w3c.dom.svg.SVGDocument; 30 31 37 public class URIResolver { 38 41 protected SVGOMDocument document; 42 43 46 protected String documentURI; 47 48 51 protected DocumentLoader documentLoader; 52 53 58 public URIResolver(SVGDocument doc, DocumentLoader dl) { 59 document = (SVGOMDocument)doc; 60 documentLoader = dl; 61 } 62 63 71 public Element getElement(String uri, Element ref) 72 throws MalformedURLException , IOException { 73 74 Node n = getNode(uri, ref); 75 if (n == null) { 76 return null; 77 } else if (n.getNodeType() == Node.DOCUMENT_NODE) { 78 throw new IllegalArgumentException (); 79 } else { 80 return (Element )n; 81 } 82 } 83 84 92 public Node getNode(String uri, Element ref) 93 throws MalformedURLException , IOException , SecurityException { 94 95 String baseURI = XMLBaseSupport.getCascadedXMLBase(ref); 96 if ((baseURI == null) && 99 (uri.startsWith("#"))) 100 return document.getElementById(uri.substring(1)); 101 102 ParsedURL purl = new ParsedURL(baseURI, uri); 103 105 if (documentURI == null) 106 documentURI = document.getURL(); 107 108 String frag = purl.getRef(); 109 if ((frag != null) && (documentURI != null)) { 110 ParsedURL pDocURL = new ParsedURL(documentURI); 111 if (pDocURL.sameFile(purl)) { 114 return document.getElementById(frag); 116 } 117 } 118 119 ParsedURL pDocURL = null; 123 if (documentURI != null) { 124 pDocURL = new ParsedURL(documentURI); 125 } 126 127 UserAgent userAgent = documentLoader.getUserAgent(); 128 userAgent.checkLoadExternalResource(purl, pDocURL); 129 130 String purlStr = purl.toString(); 131 if (frag != null) { 132 purlStr = purlStr.substring(0, purlStr.length()-(frag.length()+1)); 133 } 134 135 Document doc = documentLoader.loadDocument(purlStr); 136 if (frag != null) 137 return doc.getElementById(frag); 138 return doc; 139 } 140 } 141 | Popular Tags |