1 18 package org.apache.batik.bridge; 19 20 import java.io.InputStream ; 21 import java.io.IOException ; 22 import java.util.HashMap ; 23 24 import org.apache.batik.dom.svg.SAXSVGDocumentFactory; 25 import org.apache.batik.dom.svg.SVGDocumentFactory; 26 import org.apache.batik.dom.util.DocumentDescriptor; 27 import org.apache.batik.util.CleanerThread; 28 29 import org.w3c.dom.Document ; 30 import org.w3c.dom.Element ; 31 import org.w3c.dom.svg.SVGDocument; 32 33 40 public class DocumentLoader { 41 42 46 protected SVGDocumentFactory documentFactory; 47 48 54 protected HashMap cacheMap = new HashMap (); 55 56 59 protected UserAgent userAgent; 60 61 64 protected DocumentLoader() { } 65 66 70 public DocumentLoader(UserAgent userAgent) { 71 this.userAgent = userAgent; 72 documentFactory = new SAXSVGDocumentFactory 73 (userAgent.getXMLParserClassName(), true); 74 documentFactory.setValidating(userAgent.isXMLParserValidating()); 75 } 76 77 public Document checkCache(String uri) { 78 int n = uri.lastIndexOf('/'); 79 if (n == -1) 80 n = 0; 81 n = uri.indexOf('#', n); 82 if (n != -1) { 83 uri = uri.substring(0, n); 84 } 85 DocumentState state; 86 synchronized (cacheMap) { 87 state = (DocumentState)cacheMap.get(uri); 88 } 89 if (state != null) 90 return state.getDocument(); 91 return null; 92 } 93 94 100 public Document loadDocument(String uri) throws IOException { 101 Document ret = checkCache(uri); 102 if (ret != null) 103 return ret; 104 105 SVGDocument document = documentFactory.createSVGDocument(uri); 106 107 DocumentDescriptor desc = documentFactory.getDocumentDescriptor(); 108 DocumentState state = new DocumentState(uri, document, desc); 109 synchronized (cacheMap) { 110 cacheMap.put(uri, state); 111 } 112 113 return state.getDocument(); 114 } 115 116 122 public Document loadDocument(String uri, InputStream is) 123 throws IOException { 124 Document ret = checkCache(uri); 125 if (ret != null) 126 return ret; 127 128 SVGDocument document = documentFactory.createSVGDocument(uri, is); 129 130 DocumentDescriptor desc = documentFactory.getDocumentDescriptor(); 131 DocumentState state = new DocumentState(uri, document, desc); 132 synchronized (cacheMap) { 133 cacheMap.put(uri, state); 134 } 135 136 return state.getDocument(); 137 } 138 139 142 public UserAgent getUserAgent(){ 143 return userAgent; 144 } 145 146 149 public void dispose() { 150 synchronized (cacheMap) { 152 cacheMap.clear(); 153 } 154 } 155 156 164 public int getLineNumber(Element e) { 165 String uri = ((SVGDocument)e.getOwnerDocument()).getURL(); 166 DocumentState state; 167 synchronized (cacheMap) { 168 state = (DocumentState)cacheMap.get(uri); 169 } 170 if (state == null) { 171 return -1; 172 } else { 173 return state.desc.getLocationLine(e); 174 } 175 } 176 177 180 private class DocumentState extends CleanerThread.SoftReferenceCleared { 181 182 private String uri; 183 private DocumentDescriptor desc; 184 185 public DocumentState(String uri, 186 Document document, 187 DocumentDescriptor desc) { 188 super(document); 189 this.uri = uri; 190 this.desc = desc; 191 } 192 193 public void cleared() { 194 synchronized (cacheMap) { 195 cacheMap.remove(uri); 196 } 197 } 198 199 public DocumentDescriptor getDocumentDescriptor() { 200 return desc; 201 } 202 203 public String getURI() { 204 return uri; 205 } 206 207 public Document getDocument() { 208 return (Document )get(); 209 } 210 } 211 212 } 213 | Popular Tags |