001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2022 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;
021
022import java.io.IOException;
023import java.io.InputStream;
024import java.util.Map;
025
026import javax.xml.parsers.ParserConfigurationException;
027import javax.xml.parsers.SAXParserFactory;
028
029import org.xml.sax.InputSource;
030import org.xml.sax.SAXException;
031import org.xml.sax.SAXParseException;
032import org.xml.sax.XMLReader;
033import org.xml.sax.helpers.DefaultHandler;
034
035/**
036 * Contains the common implementation of a loader, for loading a configuration
037 * from an XML file.
038 * <p>
039 * The error handling policy can be described as being austere, dead set,
040 * disciplinary, dour, draconian, exacting, firm, forbidding, grim, hard, hard-
041 * boiled, harsh, harsh, in line, iron-fisted, no-nonsense, oppressive,
042 * persnickety, picky, prudish, punctilious, puritanical, rigid, rigorous,
043 * scrupulous, set, severe, square, stern, stickler, straight, strait-laced,
044 * stringent, stuffy, stuffy, tough, unpermissive, unsparing and uptight.
045 * </p>
046 *
047 * @noinspection ThisEscapedInObjectConstruction
048 * @noinspectionreason ThisEscapedInObjectConstruction - only reference is used and not
049 *      accessed until initialized
050 */
051public class XmlLoader
052    extends DefaultHandler {
053
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     *
062     * @param publicIdToResourceNameMap maps public IDs to DTD resource names
063     * @throws SAXException if an error occurs
064     * @throws ParserConfigurationException if an error occurs
065     */
066    protected XmlLoader(Map<String, String> publicIdToResourceNameMap)
067            throws SAXException, ParserConfigurationException {
068        this.publicIdToResourceNameMap = Map.copyOf(publicIdToResourceNameMap);
069        parser = createXmlReader(this);
070    }
071
072    /**
073     * Parses the specified input source.
074     *
075     * @param inputSource the input source to parse.
076     * @throws IOException if an error occurs
077     * @throws SAXException in an error occurs
078     */
079    public void parseInputSource(InputSource inputSource)
080            throws IOException, SAXException {
081        parser.parse(inputSource);
082    }
083
084    @Override
085    public InputSource resolveEntity(String publicId, String systemId)
086            throws SAXException, IOException {
087        final String dtdResourceName;
088        if (publicId == null) {
089            dtdResourceName = null;
090        }
091        else {
092            dtdResourceName = publicIdToResourceNameMap.get(publicId);
093        }
094        final InputSource inputSource;
095        if (dtdResourceName == null) {
096            inputSource = super.resolveEntity(publicId, systemId);
097        }
098        else {
099            final ClassLoader loader =
100                    getClass().getClassLoader();
101            final InputStream dtdIs =
102                    loader.getResourceAsStream(dtdResourceName);
103
104            inputSource = new InputSource(dtdIs);
105        }
106        return inputSource;
107    }
108
109    @Override
110    public void error(SAXParseException exception) throws SAXException {
111        throw exception;
112    }
113
114    /**
115     * Helper method to create {@code XMLReader}.
116     *
117     * @param handler the content handler
118     * @return new XMLReader instance
119     * @throws ParserConfigurationException if a parser cannot be created
120     * @throws SAXException for SAX errors
121     */
122    private static XMLReader createXmlReader(DefaultHandler handler)
123            throws SAXException, ParserConfigurationException {
124        final SAXParserFactory factory = SAXParserFactory.newInstance();
125        LoadExternalDtdFeatureProvider.setFeaturesBySystemProperty(factory);
126        factory.setValidating(true);
127        final XMLReader xmlReader = factory.newSAXParser().getXMLReader();
128        xmlReader.setContentHandler(handler);
129        xmlReader.setEntityResolver(handler);
130        xmlReader.setErrorHandler(handler);
131        return xmlReader;
132    }
133
134    /**
135     * Used for setting specific for secure java installations features to SAXParserFactory.
136     * Pulled out as a separate class in order to suppress Pitest mutations.
137     */
138    public static final class LoadExternalDtdFeatureProvider {
139
140        /** System property name to enable external DTD load. */
141        public static final String ENABLE_EXTERNAL_DTD_LOAD = "checkstyle.enableExternalDtdLoad";
142
143        /** Feature that enables loading external DTD when loading XML files. */
144        public static final String LOAD_EXTERNAL_DTD =
145                "http://apache.org/xml/features/nonvalidating/load-external-dtd";
146        /** Feature that enables including external general entities in XML files. */
147        public static final String EXTERNAL_GENERAL_ENTITIES =
148                "http://xml.org/sax/features/external-general-entities";
149        /** Feature that enables including external parameter entities in XML files. */
150        public static final String EXTERNAL_PARAMETER_ENTITIES =
151                "http://xml.org/sax/features/external-parameter-entities";
152
153        /** Stop instances being created. **/
154        private LoadExternalDtdFeatureProvider() {
155        }
156
157        /**
158         * Configures SAXParserFactory with features required
159         * to use external DTD file loading, this is not activated by default to no allow
160         * usage of schema files that checkstyle do not know
161         * it is even security problem to allow files from outside.
162         *
163         * @param factory factory to be configured with special features
164         * @throws SAXException if an error occurs
165         * @throws ParserConfigurationException if an error occurs
166         */
167        public static void setFeaturesBySystemProperty(SAXParserFactory factory)
168                throws SAXException, ParserConfigurationException {
169
170            final boolean enableExternalDtdLoad = Boolean.parseBoolean(
171                System.getProperty(ENABLE_EXTERNAL_DTD_LOAD, "false"));
172
173            factory.setFeature(LOAD_EXTERNAL_DTD, enableExternalDtdLoad);
174            factory.setFeature(EXTERNAL_GENERAL_ENTITIES, enableExternalDtdLoad);
175            factory.setFeature(EXTERNAL_PARAMETER_ENTITIES, enableExternalDtdLoad);
176        }
177
178    }
179
180}