Skip to content

Commit d7d6a30

Browse files
author
clouless
committed
working with tests
1 parent 6e2e425 commit d7d6a30

File tree

13 files changed

+643
-2
lines changed

13 files changed

+643
-2
lines changed

.editorconfig

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
# Unix-style newlines with a newline ending every file
7+
[*]
8+
end_of_line = lf
9+
insert_final_newline = true
10+
11+
# Matches multiple files with brace expansion notation
12+
# Set default charset
13+
[*.{js,py,xml,java}]
14+
charset = utf-8
15+
16+
# 4 space indentation
17+
[*.xml]
18+
indent_style = space
19+
indent_size = 2
20+
21+
[*.java]
22+
indent_style = space
23+
indent_size = 4
24+
25+
# Tab indentation (no size specified)
26+
[Makefile]
27+
indent_style = tab
28+
29+
# Indentation override for all JS under lib directory
30+
[lib/**.js]
31+
indent_style = space
32+
indent_size = 2
33+
34+
# Matches the exact files either package.json or .travis.yml
35+
[{package.json,.travis.yml}]
36+
indent_style = space
37+
indent_size = 2

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.iml
2+
.idea
3+
target
4+
output.xml

DEVELOPMENT.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Development
2+
3+
### Build
4+
5+
```
6+
mvn package -Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true
7+
java -jar target/java-xml-grepper.jar
8+
```
9+
10+
### Testcoverage
11+
12+
[Run OpenClover](http://openclover.org/) with maven:
13+
14+
```bash
15+
mvn clean clover:setup test clover:aggregate clover:clover
16+
```
17+
18+
Now look into `target/site/clover/`

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018 codeclou.io
3+
Copyright (c) 2018 Bernhard Grünewaldt
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,71 @@
11
# java-xml-grepper
2-
You want to get value from your xml file? Then this is for you
2+
3+
You want to get value from your xml file using XPath? Then this is for you.
4+
5+
----
6+
 
7+
8+
### Is this for me?
9+
10+
If you want to grep e.g. the version of a `pom.xml` file to commandline then this is for you.
11+
12+
```xml
13+
<?xml version="1.0" encoding="UTF-8"?>
14+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
15+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
16+
<modelVersion>4.0.0</modelVersion>
17+
<groupId>io.codeclou</groupId>
18+
<artifactId>java-xml-grepper</artifactId>
19+
<version>1.4.5</version>
20+
</project>
21+
```
22+
23+
24+
----
25+
&nbsp;
26+
27+
### Usage
28+
29+
With current folder containing a `pom.xml` file and you want to print out the version on commandline.
30+
The `-x` parameter accepts XPath expressions that can be cast to String.
31+
32+
```
33+
# INSTALL
34+
curl -L -o java-xml-grepper.jar \
35+
https://github.com/codeclou/java-xml-grepper/releases/download/1.0.0/java-xml-grepper.jar
36+
37+
# RUN
38+
java -jar java-xml-grepper.jar \
39+
-i pom.xml \
40+
-x /project/version
41+
42+
# PRINTS (example)
43+
1.0.1
44+
```
45+
46+
-----
47+
&nbsp;
48+
49+
### Demo
50+
51+
```
52+
git clone https://github.com/codeclou/java-xml-grepper.git src
53+
cd src
54+
55+
curl -L -o java-xml-grepper.jar \
56+
https://github.com/codeclou/java-xml-grepper/releases/download/1.0.0/java-xml-grepper.jar
57+
58+
java -jar java-xml-grepper.jar \
59+
-i pom.xml \
60+
-x /project/version
61+
62+
# Should print the version
63+
```
64+
65+
66+
----
67+
&nbsp;
68+
69+
### License
70+
71+
[MIT](https://github.com/codeclou/java-xml-grepper/blob/master/LICENSE) © [Bernhard Grünewaldt](https://github.com/clouless)

pom.xml

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>io.codeclou</groupId>
6+
<artifactId>java-xml-grepper</artifactId>
7+
<version>1.0.0</version>
8+
<organization>
9+
<name>codeclou.io</name>
10+
<url>http://codeclou.io/</url>
11+
</organization>
12+
<licenses>
13+
<license>
14+
<name>MIT</name>
15+
<url>https://github.com/codeclou/java-xml-grepper/blob/master/LICENSE</url>
16+
</license>
17+
</licenses>
18+
<name>java-xml-grepper</name>
19+
<description>java-xml-grepper</description>
20+
<packaging>jar</packaging>
21+
<dependencies>
22+
<dependency>
23+
<groupId>commons-cli</groupId>
24+
<artifactId>commons-cli</artifactId>
25+
<version>1.4</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>junit</groupId>
29+
<artifactId>junit</artifactId>
30+
<version>4.12</version>
31+
<scope>test</scope>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.openpojo</groupId>
35+
<artifactId>openpojo</artifactId>
36+
<version>0.8.6</version>
37+
<scope>test</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.powermock</groupId>
41+
<artifactId>powermock-module-junit4</artifactId>
42+
<version>${powermock.version}</version>
43+
<scope>test</scope>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.powermock</groupId>
47+
<artifactId>powermock-api-mockito</artifactId>
48+
<version>${powermock.version}</version>
49+
<scope>test</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.mockito</groupId>
53+
<artifactId>mockito-all</artifactId>
54+
<version>1.10.19</version>
55+
<scope>test</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.powermock</groupId>
59+
<artifactId>powermock-api-mockito-common</artifactId>
60+
<version>1.7.0</version>
61+
<scope>test</scope>
62+
</dependency>
63+
</dependencies>
64+
<build>
65+
<plugins>
66+
<plugin>
67+
<groupId>org.openclover</groupId>
68+
<artifactId>clover-maven-plugin</artifactId>
69+
<version>4.2.0</version>
70+
</plugin>
71+
<plugin>
72+
<artifactId>maven-assembly-plugin</artifactId>
73+
<version>3.1.0</version>
74+
<configuration>
75+
<finalName>java-xml-grepper</finalName>
76+
<archive>
77+
<manifest>
78+
<mainClass>io.codeclou.java.xml.grepper.Main</mainClass>
79+
</manifest>
80+
</archive>
81+
<descriptorRefs>
82+
<descriptorRef>jar-with-dependencies</descriptorRef>
83+
</descriptorRefs>
84+
<appendAssemblyId>false</appendAssemblyId>
85+
</configuration>
86+
<executions>
87+
<execution>
88+
<id>make-assembly</id>
89+
<phase>package</phase>
90+
<goals>
91+
<goal>single</goal>
92+
</goals>
93+
</execution>
94+
</executions>
95+
</plugin>
96+
</plugins>
97+
</build>
98+
<properties>
99+
<powermock.version>1.7.0</powermock.version>
100+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
101+
<maven.compiler.source>1.8</maven.compiler.source>
102+
<maven.compiler.target>1.8</maven.compiler.target>
103+
</properties>
104+
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
public class Main {
27+
28+
public static void main(String [] args) throws Exception {
29+
XmlGrepper xmlGrepper = new XmlGrepper();
30+
xmlGrepper.run(args);
31+
}
32+
33+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)