001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2015 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.api; 021 022import java.io.IOException; 023import java.io.InputStream; 024import java.util.HashMap; 025import java.util.Map; 026 027import javax.xml.parsers.ParserConfigurationException; 028import javax.xml.parsers.SAXParserFactory; 029 030import org.xml.sax.InputSource; 031import org.xml.sax.SAXException; 032import org.xml.sax.SAXParseException; 033import org.xml.sax.XMLReader; 034import org.xml.sax.helpers.DefaultHandler; 035 036import com.google.common.collect.Maps; 037 038/** 039 * Contains the common implementation of a loader, for loading a configuration 040 * from an XML file. 041 * <p> 042 * The error handling policy can be described as being austere, dead set, 043 * disciplinary, dour, draconian, exacting, firm, forbidding, grim, hard, hard- 044 * boiled, harsh, harsh, in line, iron-fisted, no-nonsense, oppressive, 045 * persnickety, picky, prudish, punctilious, puritanical, rigid, rigorous, 046 * scrupulous, set, severe, square, stern, stickler, straight, strait-laced, 047 * stringent, stuffy, stuffy, tough, unpermissive, unsparing and uptight. 048 * </p> 049 * 050 * @author Oliver Burn 051 */ 052public abstract class AbstractLoader 053 extends DefaultHandler { 054 /** Maps public id to resolve to resource name for the DTD. */ 055 private final Map<String, String> publicIdToResourceNameMap; 056 /** Parser to read XML files. **/ 057 private final XMLReader parser; 058 059 /** 060 * Creates a new instance. 061 * @param publicId the public ID for the DTD to resolve 062 * @param dtdResourceName the resource for the DTD 063 * @throws SAXException if an error occurs 064 * @throws ParserConfigurationException if an error occurs 065 */ 066 protected AbstractLoader(String publicId, String dtdResourceName) 067 throws SAXException, ParserConfigurationException { 068 this(new HashMap<String, String>(1)); 069 publicIdToResourceNameMap.put(publicId, dtdResourceName); 070 } 071 072 /** 073 * Creates a new instance. 074 * @param publicIdToResourceNameMap maps public IDs to DTD resource names 075 * @throws SAXException if an error occurs 076 * @throws ParserConfigurationException if an error occurs 077 */ 078 protected AbstractLoader(Map<String, String> publicIdToResourceNameMap) 079 throws SAXException, ParserConfigurationException { 080 this.publicIdToResourceNameMap = 081 Maps.newHashMap(publicIdToResourceNameMap); 082 final SAXParserFactory factory = SAXParserFactory.newInstance(); 083 factory.setValidating(true); 084 factory.setNamespaceAware(true); 085 parser = factory.newSAXParser().getXMLReader(); 086 parser.setContentHandler(this); 087 parser.setEntityResolver(this); 088 parser.setErrorHandler(this); 089 } 090 091 /** 092 * Parses the specified input source. 093 * @param inputSource the input source to parse. 094 * @throws IOException if an error occurs 095 * @throws SAXException in an error occurs 096 */ 097 public void parseInputSource(InputSource inputSource) 098 throws IOException, SAXException { 099 parser.parse(inputSource); 100 } 101 102 @Override 103 public InputSource resolveEntity(String publicId, String systemId) 104 throws SAXException, IOException { 105 if (publicIdToResourceNameMap.keySet().contains(publicId)) { 106 final String dtdResourceName = 107 publicIdToResourceNameMap.get(publicId); 108 final ClassLoader loader = 109 getClass().getClassLoader(); 110 final InputStream dtdIs = 111 loader.getResourceAsStream(dtdResourceName); 112 113 return new InputSource(dtdIs); 114 } 115 return super.resolveEntity(publicId, systemId); 116 } 117 118 @Override 119 public void error(SAXParseException exception) throws SAXException { 120 throw exception; 121 } 122 123 @Override 124 public void fatalError(SAXParseException exception) throws SAXException { 125 throw exception; 126 } 127}