|
| 1 | +/* |
| 2 | + * This file is part of git-commit-id-plugin-core by Konrad 'ktoso' Malawski <konrad.malawski@java.pl> |
| 3 | + * |
| 4 | + * git-commit-id-plugin-core is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Lesser General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * git-commit-id-plugin-core is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public License |
| 15 | + * along with git-commit-id-plugin-core. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package pl.project13.core.util; |
| 19 | + |
| 20 | +import nu.studer.java.util.OrderedProperties; |
| 21 | +import pl.project13.core.CannotReadFileException; |
| 22 | + |
| 23 | +import javax.annotation.Nonnull; |
| 24 | +import javax.xml.XMLConstants; |
| 25 | +import javax.xml.stream.*; |
| 26 | +import java.io.*; |
| 27 | +import java.nio.charset.Charset; |
| 28 | +import java.nio.charset.StandardCharsets; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.Properties; |
| 31 | + |
| 32 | +public class XmlManager { |
| 33 | + public static void dumpXml(OutputStream outputStream, OrderedProperties sortedLocalProperties, Charset sourceCharset) throws IOException { |
| 34 | + /* |
| 35 | + TODO get rid of custom indents and use |
| 36 | + https://ewernli.com/2009/06/18/stax-pretty-printer/ |
| 37 | + https://stackoverflow.com/a/38371920 |
| 38 | + */ |
| 39 | + XMLOutputFactory fac = XMLOutputFactory.newInstance(); |
| 40 | + |
| 41 | + try (Writer outputWriter = new OutputStreamWriter(outputStream, sourceCharset)) { |
| 42 | + XMLStreamWriter writer = fac.createXMLStreamWriter(outputWriter); |
| 43 | + // <?xml version="1.0" encoding="UTF-8"?> |
| 44 | + writer.writeStartDocument(StandardCharsets.UTF_8.toString(), "1.0"); |
| 45 | + |
| 46 | + writer.writeStartElement("gitCommitIdPlugin"); |
| 47 | + writer.writeCharacters("\n"); // indents |
| 48 | + |
| 49 | + for (Map.Entry e : sortedLocalProperties.entrySet()) { |
| 50 | + writer.writeCharacters(" "); // indents |
| 51 | + // <property key="git.branch" value="master"/> |
| 52 | + writer.writeEmptyElement("property"); |
| 53 | + writer.writeAttribute("key", e.getKey().toString()); |
| 54 | + writer.writeAttribute("value", e.getValue().toString()); |
| 55 | + writer.writeCharacters("\n"); // indents |
| 56 | + } |
| 57 | + writer.writeCharacters("\n"); // indents |
| 58 | + writer.writeEndElement(); // </gitCommitIdPlugin> |
| 59 | + |
| 60 | + writer.writeEndDocument(); |
| 61 | + writer.flush(); |
| 62 | + writer.close(); |
| 63 | + } catch (XMLStreamException e) { |
| 64 | + throw new RuntimeException(e); |
| 65 | + } |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + public static Properties readXmlProperties(@Nonnull File xmlFile, Charset sourceCharset) throws CannotReadFileException { |
| 70 | + Properties retVal = new Properties(); |
| 71 | + |
| 72 | + try (FileInputStream fis = new FileInputStream(xmlFile)) { |
| 73 | + try (InputStreamReader reader = new InputStreamReader(fis, sourceCharset)) { |
| 74 | + XMLInputFactory factory = XMLInputFactory.newInstance(); |
| 75 | + |
| 76 | + // https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html#Java |
| 77 | + // factory.setProperty("http://apache.org/xml/features/disallow-doctype-decl", true); // not supported :/ |
| 78 | + // https://docs.oracle.com/en/java/javase/11/security/java-api-xml-processing-jaxp-security-guide.html#JSSEC-GUID-88B04BE2-35EF-4F61-B4FA-57A0E9102342 |
| 79 | + // Feature for Secure Processing (FSP) |
| 80 | + // factory.setProperty(XMLConstants.FEATURE_SECURE_PROCESSING, true); // not supported :/ |
| 81 | + // https://rules.sonarsource.com/java/RSPEC-2755 |
| 82 | + // to be compliant, completely disable DOCTYPE declaration: |
| 83 | + factory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE); |
| 84 | + // or completely disable external entities declarations: |
| 85 | + factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE); |
| 86 | + // or prohibit the use of all protocols by external entities: |
| 87 | + factory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); |
| 88 | + factory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); |
| 89 | + |
| 90 | + // Other things we don't need |
| 91 | + factory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE); |
| 92 | + factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE); |
| 93 | + factory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE); |
| 94 | + |
| 95 | + XMLStreamReader xmlReader = factory.createXMLStreamReader(reader); |
| 96 | + while (xmlReader.hasNext()) { |
| 97 | + if (xmlReader.next() == XMLStreamConstants.START_ELEMENT) { |
| 98 | + if (xmlReader.getLocalName().equals("property")) { |
| 99 | + String key = xmlReader.getAttributeValue(null, "key"); |
| 100 | + String value = xmlReader.getAttributeValue(null, "value"); |
| 101 | + retVal.setProperty(key, value); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } catch (XMLStreamException ex) { |
| 106 | + throw new CannotReadFileException(ex); |
| 107 | + } |
| 108 | + } catch (IOException e) { |
| 109 | + throw new CannotReadFileException(e); |
| 110 | + } |
| 111 | + return retVal; |
| 112 | + } |
| 113 | +} |
0 commit comments