|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2017 Bernhard Grünewaldt |
| 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 all |
| 14 | + * 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 THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | +package io.codeclou.java.xml.grepper; |
| 25 | + |
| 26 | +import org.apache.commons.cli.CommandLine; |
| 27 | +import org.apache.commons.cli.CommandLineParser; |
| 28 | +import org.apache.commons.cli.DefaultParser; |
| 29 | +import org.apache.commons.cli.Options; |
| 30 | +import org.w3c.dom.Document; |
| 31 | +import org.xml.sax.SAXException; |
| 32 | + |
| 33 | +import javax.xml.parsers.DocumentBuilder; |
| 34 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 35 | +import javax.xml.parsers.ParserConfigurationException; |
| 36 | +import javax.xml.xpath.XPath; |
| 37 | +import javax.xml.xpath.XPathConstants; |
| 38 | +import javax.xml.xpath.XPathExpressionException; |
| 39 | +import javax.xml.xpath.XPathFactory; |
| 40 | +import java.io.File; |
| 41 | +import java.io.FileInputStream; |
| 42 | +import java.io.FileNotFoundException; |
| 43 | +import java.io.IOException; |
| 44 | + |
| 45 | +public class XmlGrepper { |
| 46 | + |
| 47 | + private CommandLineParser parser = new DefaultParser(); |
| 48 | + private Options options = new Options(); |
| 49 | + private Boolean hasCmdLineParameterErrors = false; |
| 50 | + private Boolean hasFileNotFoundErrors = false; |
| 51 | + |
| 52 | + protected String parseXml(String xpathExpression, File xmlFile) throws FileNotFoundException, ParserConfigurationException, SAXException, IOException, XPathExpressionException { |
| 53 | + FileInputStream fileIS = new FileInputStream(xmlFile); |
| 54 | + DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); |
| 55 | + DocumentBuilder builder = builderFactory.newDocumentBuilder(); |
| 56 | + Document xmlDocument = builder.parse(fileIS); |
| 57 | + XPath xPath = XPathFactory.newInstance().newXPath(); |
| 58 | + return (String) xPath.compile(xpathExpression).evaluate(xmlDocument, XPathConstants.STRING); |
| 59 | + } |
| 60 | + |
| 61 | + protected void run(String[] args) throws Exception { |
| 62 | + // |
| 63 | + // ONLY OUTPUT STUFF ON ERROR! |
| 64 | + // |
| 65 | + options.addOption("f", "file", true, "the XML file you want to grep"); |
| 66 | + options.addOption("x", "xpath", true, "the xpath expression"); |
| 67 | + CommandLine cmd = this.parser.parse(options, args); |
| 68 | + if (!cmd.hasOption("file")) { |
| 69 | + System.out.println("\033[31;1mError >> Please specify file with -f\033[0m"); |
| 70 | + hasCmdLineParameterErrors = true; |
| 71 | + } |
| 72 | + if (!cmd.hasOption("xpath")) { |
| 73 | + System.out.println("\033[31;1mError >> Please specify xpath expression with -x\033[0m"); |
| 74 | + hasCmdLineParameterErrors = true; |
| 75 | + } |
| 76 | + if (!hasCmdLineParameterErrors) { |
| 77 | + File inputFile = new File(cmd.getOptionValue("file")); |
| 78 | + if (!inputFile.isFile()) { |
| 79 | + hasFileNotFoundErrors = true; |
| 80 | + System.out.println("\033[31;1mError >> Input file not readable\033[0m"); |
| 81 | + System.out.println(inputFile.getAbsolutePath()); |
| 82 | + } |
| 83 | + if (!hasFileNotFoundErrors) { |
| 84 | + try { |
| 85 | + String result = this.parseXml(cmd.getOptionValue("xpath"), inputFile); |
| 86 | + System.out.println(result); |
| 87 | + } catch (Exception e) { |
| 88 | + System.out.println("\033[31;1mError >> Parse Exception\033[0m"); |
| 89 | + System.out.println(e.getMessage()); |
| 90 | + System.exit(1); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments