View Javadoc
1   /*
2    * Copyright (C) 2021 INRAE AgroClim
3    *
4    * This file is part of Indicators.
5    *
6    * Indicators is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * Indicators is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with Indicators. If not, see <http://www.gnu.org/licenses/>.
18   */
19  package fr.inrae.agroclim.indicators.xml;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.util.Map;
24  import java.util.Objects;
25  
26  import org.xml.sax.EntityResolver;
27  import org.xml.sax.InputSource;
28  import org.xml.sax.SAXException;
29  
30  import lombok.NonNull;
31  import lombok.Setter;
32  
33  /**
34   * Resolver for provided DTD.
35   *
36   * Last changed : $Date$
37   *
38   * @author $Author$
39   * @version $Revision$
40   */
41  public final class DtdResolver implements EntityResolver {
42  
43      /**
44       * Public ID of doctype ⮕  Resource for the doctype file.
45       */
46      @Setter
47      private Map<String, InputStream> dtds;
48  
49      @Override
50      public InputSource resolveEntity(@NonNull final String publicId,
51              final String systemId) throws SAXException, IOException {
52          if (dtds != null && dtds.containsKey(publicId)) {
53              final InputStream dtdResource = dtds.get(publicId);
54              Objects.requireNonNull(dtdResource, "If DTD Public ID is provided, dtdResource must not be null!");
55              return new InputSource(dtdResource);
56          }
57          return null;
58      }
59  }