View Javadoc
1   /*
2    * The MIT License (MIT)
3    *
4    * Copyright (c) 2013 - 2023, Tapio Rautonen
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy
7    * of this software and associated documentation files (the "Software"), to deal
8    * in the Software without restriction, including without limitation the rights
9    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10   * copies of the Software, and to permit persons to whom the Software is
11   * furnished to do so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in
14   * all copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22   * THE SOFTWARE.
23   */
24  package org.eluder.coveralls.maven.plugin.parser;
25  
26  import org.codehaus.plexus.util.ReaderFactory;
27  import org.codehaus.plexus.util.xml.XmlStreamReader;
28  import org.eluder.coveralls.maven.plugin.CoverageParser;
29  import org.eluder.coveralls.maven.plugin.ProcessingException;
30  import org.eluder.coveralls.maven.plugin.domain.Source;
31  import org.eluder.coveralls.maven.plugin.source.SourceCallback;
32  import org.eluder.coveralls.maven.plugin.source.SourceLoader;
33  
34  import javax.xml.stream.FactoryConfigurationError;
35  import javax.xml.stream.XMLInputFactory;
36  import javax.xml.stream.XMLStreamConstants;
37  import javax.xml.stream.XMLStreamException;
38  import javax.xml.stream.XMLStreamReader;
39  import java.io.File;
40  import java.io.IOException;
41  import java.io.Reader;
42  
43  public abstract class AbstractXmlEventParser implements CoverageParser {
44  
45      private final File coverageFile;
46      private final SourceLoader sourceLoader;
47  
48      protected AbstractXmlEventParser(final File coverageFile, final SourceLoader sourceLoader) {
49          this.coverageFile = coverageFile;
50          this.sourceLoader = sourceLoader;
51      }
52  
53      @Override
54      public final void parse(final SourceCallback callback) throws ProcessingException, IOException {
55          XMLStreamReader xml = null;
56          try (XmlStreamReader reader = ReaderFactory.newXmlReader(coverageFile)) {
57              xml = createEventReader(reader);
58              while (xml.hasNext()) {
59                  xml.next();
60                  onEvent(xml, callback);
61              }
62          } catch (XMLStreamException ex) {
63              throw new ProcessingException(ex);
64          } finally {
65              close(xml);
66          }
67      }
68  
69      @Override
70      public final File getCoverageFile() {
71          return coverageFile;
72      }
73  
74      protected XMLStreamReader createEventReader(final Reader reader) throws ProcessingException {
75          try {
76              XMLInputFactory xmlif = XMLInputFactory.newInstance();
77              xmlif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
78              xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);
79              xmlif.setProperty(XMLInputFactory.IS_VALIDATING, false);
80              return xmlif.createXMLStreamReader(reader);
81          } catch (FactoryConfigurationError ex) {
82              throw new IllegalArgumentException(ex);
83          } catch (XMLStreamException ex) {
84              throw new ProcessingException(ex);
85          }
86      }
87  
88      private void close(final XMLStreamReader xml) throws ProcessingException {
89          if (xml != null) {
90              try {
91                  xml.close();
92              } catch (XMLStreamException ex) {
93                  throw new ProcessingException(ex);
94              }
95          }
96      }
97  
98      protected abstract void onEvent(final XMLStreamReader xml, SourceCallback callback) throws XMLStreamException, ProcessingException, IOException;
99  
100     protected final Source loadSource(final String sourceFile) throws IOException {
101         return sourceLoader.load(sourceFile);
102     }
103 
104     protected final boolean isStartElement(final XMLStreamReader xml, final String name) {
105         return (XMLStreamConstants.START_ELEMENT == xml.getEventType() && xml.getLocalName().equals(name));
106     }
107 
108     protected final boolean isEndElement(final XMLStreamReader xml, final String name) {
109         return (XMLStreamConstants.END_ELEMENT == xml.getEventType() && xml.getLocalName().equals(name));
110     }
111 }