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