forked from yegor256/cactoos
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
3 changed files
with
62 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.cactoos.io; | ||
|
||
import javax.xml.transform.Result; | ||
import javax.xml.transform.Transformer; | ||
import javax.xml.transform.TransformerException; | ||
import javax.xml.transform.TransformerFactory; | ||
import javax.xml.transform.stream.StreamResult; | ||
import javax.xml.transform.stream.StreamSource; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
|
||
public class XsltTransformer { | ||
private final Path input; | ||
private final Path output; | ||
private final Transformer transformer; | ||
|
||
XsltTransformer(Path input, Path output, Transformer transformer) { | ||
this.input = input; | ||
this.output = output; | ||
this.transformer = transformer; | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
final Transformer transformer = TransformerFactory | ||
.newInstance() | ||
.newTransformer(new StreamSource(Paths.get("/my/directory/file.xsl").toFile())); | ||
new XsltTransformer( | ||
Paths.get("/my/directory"), | ||
Paths.get("/my/output"), | ||
transformer | ||
).run(); | ||
} | ||
|
||
private Path transform(Path file) { | ||
final StreamSource resource = new StreamSource(file.toFile()); | ||
final Path output = this.resolveOutput(file); | ||
final Result result = new StreamResult( | ||
output.toFile() | ||
); | ||
try { | ||
this.transformer.transform(resource, result); | ||
return output; | ||
} catch (TransformerException ex) { | ||
throw new IllegalStateException(ex); | ||
} | ||
} | ||
|
||
private Path resolveOutput(Path file) { | ||
return this.output.resolve(this.input.relativize(file)); | ||
} | ||
|
||
public void run() throws IOException { | ||
Files.walk(this.input) | ||
.filter(file -> file.getFileName().endsWith(".xml")) | ||
.map(this::transform) | ||
.forEach(System.out::println); | ||
} | ||
} |
Empty file.