1 10 11 package com.triactive.jdo.util; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.security.AccessController ; 16 import java.security.PrivilegedAction ; 17 import javax.jdo.JDOHelper; 18 import javax.xml.parsers.DocumentBuilder ; 19 import javax.xml.parsers.DocumentBuilderFactory ; 20 import javax.xml.parsers.ParserConfigurationException ; 21 import org.xml.sax.EntityResolver ; 22 import org.xml.sax.InputSource ; 23 import org.xml.sax.SAXException ; 24 25 26 32 33 public class XMLHelper 34 { 35 41 private static final String USE_VALIDATING_XML_PARSER_PROPERTY = 42 "com.triactive.jdo.useValidatingXmlParser"; 43 44 45 48 private XMLHelper() {} 49 50 51 61 public static DocumentBuilder getDocumentBuilder() 62 throws ParserConfigurationException 63 { 64 boolean validating = 65 new Boolean (System.getProperty(USE_VALIDATING_XML_PARSER_PROPERTY, "true")).booleanValue(); 66 67 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 68 factory.setValidating(validating); 69 70 DocumentBuilder builder = factory.newDocumentBuilder(); 71 builder.setEntityResolver(new JDOEntityResolver()); 72 73 return builder; 74 } 75 76 77 private static class JDOEntityResolver implements EntityResolver 78 { 79 private static final String RECOGNIZED_PUBLIC_ID = 80 "-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 1.0//EN"; 81 82 public InputSource resolveEntity(String publicId, String systemId) 83 throws SAXException , IOException 84 { 85 boolean isJdoDtd; 86 87 if (publicId == null) 88 isJdoDtd = systemId.startsWith("file:") && systemId.endsWith("/jdo.dtd"); 89 else 90 isJdoDtd = RECOGNIZED_PUBLIC_ID.equals(publicId); 91 92 if (isJdoDtd) 93 { 94 InputStream stream = (InputStream )AccessController.doPrivileged( 95 new PrivilegedAction () 96 { 97 public Object run() 98 { 99 return JDOHelper.class.getClassLoader().getResourceAsStream("javax/jdo/jdo.dtd"); 100 } 101 } 102 ); 103 104 return stream == null ? null : new InputSource (stream); 105 } 106 else 107 return null; 108 } 109 } 110 } 111 112 113 | Popular Tags |