generated from maciejwalkowiak/java-cli-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7008e7e
commit d82e3a8
Showing
15 changed files
with
260 additions
and
66 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
output/ | ||
target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
!**/src/main/**/target/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 5 additions & 3 deletions
8
src/main/java/org/example/Main.java → src/main/java/com/curehunter/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...icocli/PropertiesFileVersionProvider.java → ...icocli/PropertiesFileVersionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package org.example.picocli; | ||
package com.curehunter.picocli; | ||
|
||
import java.io.InputStream; | ||
import java.net.URL; | ||
|
122 changes: 122 additions & 0 deletions
122
src/main/java/com/curehunter/transform/TransformAll.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package com.curehunter.transform; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.zip.GZIPInputStream; | ||
|
||
import javax.xml.parsers.SAXParserFactory; | ||
import javax.xml.transform.Transformer; | ||
import javax.xml.transform.TransformerFactory; | ||
import javax.xml.transform.stream.StreamResult; | ||
import javax.xml.transform.stream.StreamSource; | ||
|
||
import com.curehunter.utils.FileIterator; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.concurrent.Callable; | ||
|
||
import org.apache.commons.io.filefilter.WildcardFileFilter; | ||
import picocli.CommandLine.Command; | ||
import picocli.CommandLine.Option; | ||
|
||
@Command(name = "transform") | ||
public class TransformAll implements Callable<Integer> { | ||
|
||
@Option(names = { "-h", "--help" }, usageHelp = true, description = "display this help and exit") | ||
boolean help; | ||
|
||
@Option(names = { "-i", "--in" }, description = "directory to search for files, default: ./data") | ||
private Path inputDir = Paths.get(System.getProperty("user.dir")+"/data"); | ||
|
||
@Option(names = { "-f", "--filter" }, description = "filter files with wildcard pattern, default: *.xml.gz", defaultValue = "*.xml.gz") | ||
private String filterPattern; | ||
|
||
@Option(names = { "-o", "--out" }, description = "output files to directory, default: ./output") | ||
private Path outputDir = Paths.get(System.getProperty("user.dir")+"/output"); | ||
|
||
@Option(names = { "-x", "--xsl" }, description = "XSL file to use for transformation, default: ./xsl/medlineCitationTSV.xsl") | ||
private Path xslFile = Paths.get(System.getProperty("user.dir")+"/xsl/medlineCitationTSV.xsl"); | ||
|
||
@Option(names = { "-e", "--outExt" }, description = "extension to append to output files, default: .out", defaultValue = ".out") | ||
private String outputExtension; | ||
|
||
@Option(names = { "-w", "--workers" }, description = "number of worker threads, files to process simultaneously, default: 4", defaultValue = "4") | ||
private int workerThreads; | ||
|
||
@Override | ||
public Integer call() { | ||
long startTime = System.currentTimeMillis(); | ||
FileIterator workerData = new FileIterator(inputDir.toFile()); | ||
WildcardFileFilter filter = new WildcardFileFilter(filterPattern); | ||
try { | ||
System.out.println("in=" + inputDir.toFile().getCanonicalPath() + " out=" | ||
+ outputDir.toFile().getCanonicalPath() | ||
+ " xsl=" + xslFile.toFile().getCanonicalPath()); | ||
ExecutorService exec = Executors.newFixedThreadPool(workerThreads); | ||
for (int n = 0; n < workerThreads; n++) { | ||
exec.execute(new Worker(workerData, xslFile.toFile(), outputDir.toFile(), | ||
outputExtension, filter)); | ||
} | ||
exec.shutdown(); | ||
exec.awaitTermination(1, TimeUnit.DAYS); | ||
System.out.println("Processing complete in " + (System.currentTimeMillis() - startTime) + "ms"); | ||
return 0; | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
System.out.println("Processing complete in " + (System.currentTimeMillis() - startTime) + "ms"); | ||
return 1; | ||
} | ||
|
||
static class Worker implements Runnable { | ||
FileIterator workerData; | ||
File xslt; | ||
File outputDir; | ||
String outputExtension; | ||
WildcardFileFilter filter; | ||
|
||
public Worker(FileIterator workerData, File xslt, File outputDir, String outputExtension, | ||
WildcardFileFilter filter) { | ||
this.workerData = workerData; | ||
this.xslt = xslt; | ||
this.outputDir = outputDir; | ||
this.outputExtension = outputExtension; | ||
this.filter = filter; | ||
} | ||
|
||
public void run() { | ||
SAXParserFactory parserFactory = SAXParserFactory.newInstance(); | ||
TransformerFactory transformerFactory = TransformerFactory.newInstance(); | ||
System.out.println("transformerFactory=" + transformerFactory + " parserFactory=" + parserFactory); | ||
try { | ||
Transformer transformer = transformerFactory.newTransformer(new StreamSource(xslt)); | ||
File f = null; | ||
while ((f = workerData.next()) != null) { | ||
if (filter.accept(f)) { | ||
System.out.println("start transforming file=" + f.getCanonicalPath()); | ||
try { | ||
transformer.transform(new StreamSource( | ||
f.getCanonicalPath().toLowerCase().endsWith(".gz") | ||
? new GZIPInputStream(new FileInputStream(f)) | ||
: new FileInputStream(f)), | ||
new StreamResult(new File(outputDir, f.getName() + outputExtension))); | ||
System.out.println("done transforming file=" + f.getCanonicalPath()); | ||
} catch (Throwable e) { | ||
System.err.printf("error transforming file=" + f.getCanonicalPath(), e); | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.curehunter.utils; | ||
|
||
import java.io.File; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.Iterator; | ||
|
||
/** | ||
* Iterate files in a directory, returning each file only once. | ||
*/ | ||
public class FileIterator implements Iterator<File> { | ||
|
||
private File parentDir; | ||
private Collection<File> seenFiles = new HashSet<File>(); | ||
|
||
public FileIterator(File parentDir) { | ||
this.parentDir = parentDir; | ||
} | ||
|
||
public boolean hasNext() { | ||
synchronized (this.seenFiles) { | ||
return (nextInternal() != null); | ||
} | ||
} | ||
|
||
public File next() { | ||
synchronized (this.seenFiles) { | ||
File next = nextInternal(); | ||
this.seenFiles.add(next); | ||
return next; | ||
} | ||
} | ||
|
||
private File nextInternal() { | ||
File childFiles[] = this.parentDir.listFiles(); | ||
if (childFiles != null) { | ||
Arrays.sort(childFiles); | ||
for (int n = 0; n < childFiles.length; n++) { | ||
if (!this.seenFiles.contains(childFiles[n]) && childFiles[n].isFile()) { | ||
return childFiles[n]; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public void remove() { | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
3 changes: 2 additions & 1 deletion
3
src/test/java/org/example/MainTests.java → src/test/java/com/curehunter/MainTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.