-
Notifications
You must be signed in to change notification settings - Fork 55
/
TranslationExample.java
94 lines (72 loc) · 2.13 KB
/
TranslationExample.java
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* Copyright (c) 2014 Villu Ruusmann
*/
package org.jpmml.model.example;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.beust.jcommander.Parameter;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.dmg.pmml.PMML;
import org.jpmml.model.jackson.JacksonUtil;
public class TranslationExample extends Example {
@Parameter (
names = {"--input"},
description = "Input PMML file",
required = true
)
private File input = null;
@Parameter (
names = {"--output"},
description = "Output JSON file",
required = true
)
private File output = null;
@Parameter (
names = {"--indent"},
description = "Indent string"
)
private String indent = null;
static
public void main(String... args) throws Exception {
execute(TranslationExample.class, args);
}
@Override
public void execute() throws Exception {
PMML pmml = unmarshalPMML(this.input);
JsonFactory jsonFactory = null;
String name = this.output.getName();
String nameExtension = null;
int dot = name.lastIndexOf('.');
if(dot > -1){
nameExtension = name.substring(dot + 1);
} // End if
if(("YAML").equalsIgnoreCase(nameExtension)){
jsonFactory = new YAMLFactory();
}
DefaultPrettyPrinter prettyPrinter = null;
if(this.indent != null){
DefaultPrettyPrinter.Indenter indenter = new DefaultIndenter(filterIndent(this.indent), "\n");
prettyPrinter = new DefaultPrettyPrinter();
prettyPrinter.indentObjectsWith(indenter);
prettyPrinter.indentArraysWith(indenter);
}
ObjectMapper mapper = JacksonUtil.createObjectMapper(jsonFactory);
try(OutputStream os = new FileOutputStream(this.output)){
ObjectWriter writer = mapper.writer(prettyPrinter);
writer.writeValue(os, pmml);
}
}
static
private String filterIndent(String indent){
if(("\\t").equals(indent)){
return "\t";
}
return indent;
}
}