1 package com.puppycrawl.tools.checkstyle.api; 20 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.util.Map ; 24 import java.util.HashMap ; 25 import javax.xml.parsers.ParserConfigurationException ; 26 import javax.xml.parsers.SAXParserFactory ; 27 import org.xml.sax.InputSource ; 28 import org.xml.sax.SAXException ; 29 import org.xml.sax.SAXParseException ; 30 import org.xml.sax.XMLReader ; 31 import org.xml.sax.helpers.DefaultHandler ; 32 33 46 public abstract class AbstractLoader 47 extends DefaultHandler 48 { 49 50 private final Map mPublicIdToResourceNameMap; 51 52 private final XMLReader mParser; 53 54 61 protected AbstractLoader(String aPublicId, String aDtdResourceName) 62 throws SAXException , ParserConfigurationException 63 { 64 this(new HashMap (1)); 65 mPublicIdToResourceNameMap.put(aPublicId, aDtdResourceName); 66 } 67 68 74 protected AbstractLoader(Map aPublicIdToResourceNameMap) 75 throws SAXException , ParserConfigurationException 76 { 77 mPublicIdToResourceNameMap = new HashMap (aPublicIdToResourceNameMap); 78 final SAXParserFactory factory = SAXParserFactory.newInstance(); 79 factory.setValidating(true); 80 factory.setNamespaceAware(true); 81 mParser = factory.newSAXParser().getXMLReader(); 82 mParser.setContentHandler(this); 83 mParser.setEntityResolver(this); 84 mParser.setErrorHandler(this); 85 } 86 87 93 public void parseInputSource(InputSource aInputSource) 94 throws IOException , SAXException 95 { 96 mParser.parse(aInputSource); 97 } 98 99 100 public InputSource resolveEntity(String aPublicId, String aSystemId) 101 throws SAXException 102 { 103 if (mPublicIdToResourceNameMap.keySet().contains(aPublicId)) { 104 final String dtdResourceName = 105 (String ) mPublicIdToResourceNameMap.get(aPublicId); 106 final ClassLoader loader = 107 Thread.currentThread().getContextClassLoader(); 108 final InputStream dtdIS = 109 loader.getResourceAsStream(dtdResourceName); 110 if (dtdIS == null) { 111 throw new SAXException ( 112 "Unable to load internal dtd " + dtdResourceName); 113 } 114 return new InputSource (dtdIS); 115 } 116 try { 121 if (false) { 122 throw new IOException (""); 123 } 124 return super.resolveEntity(aPublicId, aSystemId); 125 } 126 catch (final IOException e) { 127 throw new SAXException ("" + e, e); 128 } 129 } 130 131 132 public void warning(SAXParseException aEx) throws SAXException 133 { 134 throw aEx; 135 } 136 137 138 public void error(SAXParseException aEx) throws SAXException 139 { 140 throw aEx; 141 } 142 143 144 public void fatalError(SAXParseException aEx) throws SAXException 145 { 146 throw aEx; 147 } 148 } 149 | Popular Tags |