1 18 package org.apache.batik.dom.svg; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.Reader ; 23 import java.io.StringReader ; 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 import java.util.MissingResourceException ; 27 import java.util.Properties ; 28 29 import org.apache.batik.dom.util.SAXDocumentFactory; 30 import org.apache.batik.dom.util.XLinkSupport; 31 import org.apache.batik.dom.svg12.SVG12DOMImplementation; 32 import org.apache.batik.util.MimeTypeConstants; 33 import org.apache.batik.util.ParsedURL; 34 35 import org.w3c.dom.Document ; 36 import org.w3c.dom.DOMImplementation ; 37 import org.w3c.dom.svg.SVGDocument; 38 import org.xml.sax.InputSource ; 39 import org.xml.sax.SAXException ; 40 41 48 public class SAXSVGDocumentFactory 49 extends SAXDocumentFactory 50 implements SVGDocumentFactory { 51 52 public static final Object LOCK = new Object (); 53 54 57 public static final String KEY_PUBLIC_IDS = "publicIds"; 58 59 62 public static final String KEY_SKIPPABLE_PUBLIC_IDS = "skippablePublicIds"; 63 64 67 public static final String KEY_SKIP_DTD = "skipDTD"; 68 69 72 public static final String KEY_SYSTEM_ID = "systemId."; 73 74 77 protected final static String DTDIDS = 78 "org.apache.batik.dom.svg.resources.dtdids"; 79 80 83 protected final static String HTTP_CHARSET = "charset"; 84 85 88 protected static String dtdids; 89 90 93 protected static String skippable_dtdids; 94 95 98 protected static String skip_dtd; 99 100 103 protected static Properties dtdProps; 104 105 109 public SAXSVGDocumentFactory(String parser) { 110 super(SVGDOMImplementation.getDOMImplementation(), parser); 111 } 112 113 118 public SAXSVGDocumentFactory(String parser, boolean dd) { 119 super(SVGDOMImplementation.getDOMImplementation(), parser, dd); 120 } 121 122 public SVGDocument createSVGDocument(String uri) throws IOException { 123 return (SVGDocument)createDocument(uri); 124 } 125 126 132 public SVGDocument createSVGDocument(String uri, InputStream inp) 133 throws IOException { 134 return (SVGDocument)createDocument(uri, inp); 135 } 136 137 143 public SVGDocument createSVGDocument(String uri, Reader r) 144 throws IOException { 145 return (SVGDocument)createDocument(uri, r); 146 } 147 148 154 public Document createDocument(String uri) throws IOException { 155 ParsedURL purl = new ParsedURL(uri); 156 157 InputStream is = purl.openStream(MimeTypeConstants.MIME_TYPES_SVG); 158 159 InputSource isrc = new InputSource (is); 160 161 String contentType = purl.getContentType(); 166 int cindex = -1; 167 if (contentType != null) { 168 contentType = contentType.toLowerCase(); 169 cindex = contentType.indexOf(HTTP_CHARSET); 170 } 171 172 if (cindex != -1) { 173 int i = cindex + HTTP_CHARSET.length(); 174 int eqIdx = contentType.indexOf('=', i); 175 if (eqIdx != -1) { 176 eqIdx++; 178 String charset; 179 int idx = contentType.indexOf(',', eqIdx); 185 int semiIdx = contentType.indexOf(';', eqIdx); 186 if ((semiIdx != -1) && ((semiIdx < idx) || (idx == -1))) 187 idx = semiIdx; 188 if (idx != -1) 189 charset = contentType.substring(eqIdx, idx); 190 else 191 charset = contentType.substring(eqIdx); 192 isrc.setEncoding(charset.trim()); 193 } 194 } 195 196 isrc.setSystemId(uri); 197 198 Document doc = super.createDocument 199 (SVGDOMImplementation.SVG_NAMESPACE_URI, "svg", uri, isrc); 200 try { 201 ((SVGOMDocument)doc).setURLObject(new URL (purl.toString())); 202 } catch (MalformedURLException mue) { 203 throw new IOException ("Malformed URL: " + uri); 205 } 206 207 return doc; 208 } 209 210 216 public Document createDocument(String uri, InputStream inp) 217 throws IOException { 218 Document doc; 219 InputSource is = new InputSource (inp); 220 is.setSystemId(uri); 221 222 try { 223 doc = super.createDocument 224 (SVGDOMImplementation.SVG_NAMESPACE_URI, "svg", uri, is); 225 if (uri != null) { 226 ((SVGOMDocument)doc).setURLObject(new URL (uri)); 227 } 228 } catch (MalformedURLException e) { 229 throw new IOException (e.getMessage()); 230 } 231 return doc; 232 } 233 234 240 public Document createDocument(String uri, Reader r) 241 throws IOException { 242 Document doc; 243 InputSource is = new InputSource (r); 244 is.setSystemId(uri); 245 246 try { 247 doc = super.createDocument 248 (SVGDOMImplementation.SVG_NAMESPACE_URI, "svg", uri, is); 249 if (uri != null) { 250 ((SVGOMDocument)doc).setURLObject(new URL (uri)); 251 } 252 } catch (MalformedURLException e) { 253 throw new IOException (e.getMessage()); 254 } 255 return doc; 256 } 257 258 265 public Document createDocument(String ns, String root, String uri) 266 throws IOException { 267 if (!SVGDOMImplementation.SVG_NAMESPACE_URI.equals(ns) || 268 !"svg".equals(root)) { 269 throw new RuntimeException ("Bad root element"); 270 } 271 return createDocument(uri); 272 } 273 274 282 public Document createDocument(String ns, String root, String uri, 283 InputStream is) throws IOException { 284 if (!SVGDOMImplementation.SVG_NAMESPACE_URI.equals(ns) || 285 !"svg".equals(root)) { 286 throw new RuntimeException ("Bad root element"); 287 } 288 return createDocument(uri, is); 289 } 290 291 299 public Document createDocument(String ns, String root, String uri, 300 Reader r) throws IOException { 301 if (!SVGDOMImplementation.SVG_NAMESPACE_URI.equals(ns) || 302 !"svg".equals(root)) { 303 throw new RuntimeException ("Bad root element"); 304 } 305 return createDocument(uri, r); 306 } 307 308 public DOMImplementation getDOMImplementation(String ver) { 309 if ((ver == null) || (ver.length()==0) || 310 ver.equals("1.0") || ver.equals("1.1")) 311 return SVGDOMImplementation.getDOMImplementation(); 312 313 return SVG12DOMImplementation.getDOMImplementation(); 314 } 315 316 320 public void startDocument() throws SAXException { 321 super.startDocument(); 322 namespaces.put("", SVGDOMImplementation.SVG_NAMESPACE_URI); 323 namespaces.put("xlink", XLinkSupport.XLINK_NAMESPACE_URI); 324 } 325 326 330 public InputSource resolveEntity(String publicId, String systemId) 331 throws SAXException { 332 try { 333 synchronized (LOCK) { 334 if (dtdProps == null) { 336 dtdProps = new Properties (); 337 try { 338 Class cls = SAXSVGDocumentFactory.class; 339 InputStream is = cls.getResourceAsStream 340 ("resources/dtdids.properties"); 341 dtdProps.load(is); 342 } catch (IOException ioe) { 343 throw new SAXException (ioe); 344 } 345 } 346 347 if (dtdids == null) 348 dtdids = dtdProps.getProperty(KEY_PUBLIC_IDS); 349 350 if (skippable_dtdids == null) 351 skippable_dtdids = 352 dtdProps.getProperty(KEY_SKIPPABLE_PUBLIC_IDS); 353 354 if (skip_dtd == null) 355 skip_dtd = dtdProps.getProperty(KEY_SKIP_DTD); 356 } 357 358 if (publicId == null) 359 return null; 361 if (!isValidating && 362 (skippable_dtdids.indexOf(publicId) != -1)) { 363 return new InputSource (new StringReader (skip_dtd)); 368 } 369 370 if (dtdids.indexOf(publicId) != -1) { 371 String localSystemId = 372 dtdProps.getProperty(KEY_SYSTEM_ID + 373 publicId.replace(' ', '_')); 374 375 if (localSystemId != null && !"".equals(localSystemId)) { 376 return new InputSource 377 (getClass().getResource(localSystemId).toString()); 378 } 379 } 380 } catch (MissingResourceException e) { 381 throw new SAXException (e); 382 } 383 return null; 385 } 386 } 387 | Popular Tags |