-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunTransform.java
More file actions
54 lines (52 loc) · 2.32 KB
/
Copy pathRunTransform.java
File metadata and controls
54 lines (52 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Run an XSLT 2.0 stylesheet via the Saxon s9api (native Java, no JAXB).
//
// Standalone & cross-platform — run from the repo root with the Maven Wrapper:
// ./mvnw -q -pl XSLT_Transformations/invocation compile exec:java \
// -Dexec.args="<stylesheet.xslt> <input.xml> <output> [k=v ...]"
// Exit: 0 success, 2 setup error.
//
// Dependencies (from Maven Central, see pom.xml): Saxon-HE + xmlresolver
// (+ its -data artifact); no other libraries.
//
// This is a generic, FundsXML-agnostic invocation wrapper: it applies any
// stylesheet to any XML and passes through string parameters (k=v) — the
// FundsXML specifics live in the .xslt files it runs. The repo's stylesheets
// are XSLT 2.0; Saxon supplies the engine. s9api is Saxon's idiomatic Java
// API — no JAXB involved (a full data binding would be needless here).
import java.io.File;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.Serializer;
import net.sf.saxon.s9api.XdmAtomicValue;
import net.sf.saxon.s9api.XsltCompiler;
import net.sf.saxon.s9api.XsltExecutable;
import net.sf.saxon.s9api.XsltTransformer;
public class RunTransform {
public static void main(String[] args) {
if (args.length < 3) {
System.err.println("usage: RunTransform <xslt> <input.xml> <output> [k=v ...]");
System.exit(2);
}
try {
Processor proc = new Processor(false);
XsltCompiler comp = proc.newXsltCompiler();
XsltExecutable exe = comp.compile(new javax.xml.transform.stream.StreamSource(new File(args[0])));
XsltTransformer t = exe.load();
t.setSource(new javax.xml.transform.stream.StreamSource(new File(args[1])));
for (int i = 3; i < args.length; i++) {
int eq = args[i].indexOf('=');
if (eq > 0) {
t.setParameter(new QName(args[i].substring(0, eq)),
new XdmAtomicValue(args[i].substring(eq + 1)));
}
}
Serializer out = proc.newSerializer(new File(args[2]));
t.setDestination(out);
t.transform();
System.out.println("wrote " + args[2]);
} catch (Exception e) {
System.err.println("error: " + e.getMessage());
System.exit(2);
}
}
}