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.InputStream;
22  import java.util.Map;
23  
24  import javax.xml.XMLConstants;
25  import javax.xml.parsers.ParserConfigurationException;
26  import javax.xml.parsers.SAXParser;
27  import javax.xml.parsers.SAXParserFactory;
28  import javax.xml.transform.Source;
29  import javax.xml.transform.sax.SAXSource;
30  
31  import org.xml.sax.InputSource;
32  import org.xml.sax.SAXException;
33  import org.xml.sax.XMLReader;
34  
35  import jakarta.xml.bind.JAXBContext;
36  import jakarta.xml.bind.JAXBException;
37  import jakarta.xml.bind.Unmarshaller;
38  import lombok.Setter;
39  
40  /**
41   * Builder pattern to configure JAXB Unmarshaller.
42   *
43   * Last changed : $Date$
44   *
45   * @author $Author$
46   * @version $Revision$
47   */
48  public final class UnmarshallerBuilder {
49  
50      /**
51       * Public ID of doctype ⮕  Resource for the doctype file.
52       */
53      @Setter
54      private Map<String, InputStream> dtds;
55  
56      /**
57       * The class related to the object to serialize.
58       */
59      @Setter
60      private Class<?>[] classesToBeBound;
61  
62      /**
63       * @return JAXB unmarshaller
64       * @throws JAXBException error while getting context or creating unmarshaller
65       */
66      public Unmarshaller build() throws JAXBException {
67          final JAXBContext context = JAXBContext.newInstance(classesToBeBound);
68          return context.createUnmarshaller();
69      }
70  
71      /**
72       * @param inputStream file stream
73       * @return source to unmarshal
74       * @throws ParserConfigurationException while creating SAX Parser
75       * @throws org.xml.sax.SAXException while creating SAX Parser
76       */
77      public Source buildSource(final InputStream inputStream) throws ParserConfigurationException, SAXException {
78          final SAXParserFactory parserFactory = SAXParserFactory.newInstance();
79          parserFactory.setNamespaceAware(true);
80          final SAXParser parser = parserFactory.newSAXParser();
81          parser.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
82          parser.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
83          final XMLReader xmlreader = parser.getXMLReader();
84          if (dtds != null) {
85              final DtdResolver dtdResolver = new DtdResolver();
86              dtdResolver.setDtds(dtds);
87              xmlreader.setEntityResolver(dtdResolver);
88          }
89          final InputSource inputSource = new InputSource(inputStream);
90          return new SAXSource(xmlreader, inputSource);
91  
92      }
93  }