|
| 1 | +/** |
| 2 | + * commons - Enables analysis of Swift and Objective-C projects into SonarQube. |
| 3 | + * Copyright © 2015 Backelite (${email}) |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Lesser General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +package com.backelite.sonarqube.commons.surefire; |
| 19 | + |
| 20 | +import com.ctc.wstx.stax.WstxInputFactory; |
| 21 | +import org.codehaus.staxmate.SMInputFactory; |
| 22 | +import org.codehaus.staxmate.in.SMHierarchicCursor; |
| 23 | + |
| 24 | +import javax.xml.stream.XMLInputFactory; |
| 25 | +import javax.xml.stream.XMLResolver; |
| 26 | +import javax.xml.stream.XMLStreamException; |
| 27 | +import java.io.*; |
| 28 | + |
| 29 | +public class StaxParser { |
| 30 | + private SMInputFactory inf; |
| 31 | + private StaxParser.XmlStreamHandler streamHandler; |
| 32 | + |
| 33 | + public StaxParser(StaxParser.XmlStreamHandler streamHandler) { |
| 34 | + this.streamHandler = streamHandler; |
| 35 | + XMLInputFactory xmlFactory = XMLInputFactory.newInstance(); |
| 36 | + if (xmlFactory instanceof WstxInputFactory) { |
| 37 | + WstxInputFactory wstxInputfactory = (WstxInputFactory) xmlFactory; |
| 38 | + wstxInputfactory.configureForLowMemUsage(); |
| 39 | + wstxInputfactory.getConfig().setUndeclaredEntityResolver(new StaxParser.UndeclaredEntitiesXMLResolver()); |
| 40 | + } |
| 41 | + |
| 42 | + xmlFactory.setProperty("javax.xml.stream.isValidating", false); |
| 43 | + xmlFactory.setProperty("javax.xml.stream.supportDTD", false); |
| 44 | + xmlFactory.setProperty("javax.xml.stream.isNamespaceAware", false); |
| 45 | + this.inf = new SMInputFactory(xmlFactory); |
| 46 | + } |
| 47 | + |
| 48 | + public void parse(File xmlFile) throws XMLStreamException { |
| 49 | + try(InputStream input = new FileInputStream(xmlFile)) { |
| 50 | + this.parse(input); |
| 51 | + } catch (FileNotFoundException ex) { |
| 52 | + throw new XMLStreamException(ex); |
| 53 | + } catch (IOException ex){ |
| 54 | + throw new XMLStreamException(ex); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public void parse(InputStream xml) throws XMLStreamException { |
| 59 | + SMHierarchicCursor rootCursor = this.inf.rootElementCursor(xml); |
| 60 | + try { |
| 61 | + this.streamHandler.stream(rootCursor); |
| 62 | + } finally { |
| 63 | + rootCursor.getStreamReader().closeCompletely(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public interface XmlStreamHandler { |
| 68 | + void stream(SMHierarchicCursor var1) throws XMLStreamException; |
| 69 | + } |
| 70 | + |
| 71 | + private static class UndeclaredEntitiesXMLResolver implements XMLResolver { |
| 72 | + private UndeclaredEntitiesXMLResolver() { } |
| 73 | + |
| 74 | + public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) throws XMLStreamException { |
| 75 | + if (namespace != null && namespace.toLowerCase().startsWith("u") && namespace.length() == 5) { |
| 76 | + int unicodeCharHexValue = Integer.parseInt(namespace.substring(1), 16); |
| 77 | + if (Character.isDefined(unicodeCharHexValue)) { |
| 78 | + namespace = new String(new char[]{(char) unicodeCharHexValue}); |
| 79 | + } |
| 80 | + } |
| 81 | + return namespace; |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments