Skip to content

Commit 7c73766

Browse files
committed
Use own copy of OutputFormat, support 1.1 output
1 parent 1071d93 commit 7c73766

File tree

1 file changed

+51
-6
lines changed

1 file changed

+51
-6
lines changed

ion-java-cli/src/main/java/com/amazon/tools/cli/SimpleIonCli.java

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.amazon.tools.cli;
22

33

4+
import com.amazon.ion.IonEncodingVersion;
45
import com.amazon.ion.IonReader;
56
import com.amazon.ion.IonWriter;
67
import com.amazon.ion.system.IonReaderBuilder;
@@ -42,8 +43,12 @@ public static void main(String[] args) {
4243
System.exit(commandLine.execute(args));
4344
}
4445

46+
@Option(names={"-v", "--ion-version"}, description = "Output Ion version", defaultValue = "1.0",
47+
converter = IonEncodingVersionConverter.class, scope = CommandLine.ScopeType.INHERIT)
48+
IonEncodingVersion ionVersion;
49+
4550
@Option(names={"-f", "--format", "--output-format"}, defaultValue = "pretty",
46-
description = "Output format, from the set (text | pretty | binary | none).",
51+
description = "Output format, from the set (text | pretty | binary | debug | none).",
4752
paramLabel = "<format>",
4853
scope = CommandLine.ScopeType.INHERIT)
4954
OutputFormat outputFormat;
@@ -57,17 +62,13 @@ public static void main(String[] args) {
5762
mixinStandardHelpOptions = true)
5863
int cat( @Parameters(paramLabel = "FILE") File... files) {
5964

60-
if (outputFormat == OutputFormat.EVENTS) {
61-
System.err.println("'events' output format is not supported");
62-
return CommandLine.ExitCode.USAGE;
63-
}
6465

6566
//TODO: Handle stream cutoff- java.io.IOException: Broken pipe
6667
//TODO: This is not resilient to problems with a single file. Should it be?
6768
try (InputStream in = getInputStream(files);
6869
IonReader reader = IonReaderBuilder.standard().build(in);
6970
OutputStream out = getOutputStream(outputFile);
70-
IonWriter writer = outputFormat.createIonWriter(out)) {
71+
IonWriter writer = getWriter(ionVersion, outputFormat, out)) {
7172
// getInputStream will look for stdin if we don't supply
7273
writer.writeValues(reader);
7374
} catch (IOException e) {
@@ -111,4 +112,48 @@ private static FileOutputStream getOutputStream(File outputFile) throws IOExcept
111112
// non-line-buffered stdout, or the requested file output
112113
return outputFile == null ? new FileOutputStream(FileDescriptor.out) : new FileOutputStream(outputFile);
113114
}
115+
116+
private static IonWriter getWriter(IonEncodingVersion<?,?> version, OutputFormat format, OutputStream out) {
117+
if (version == IonEncodingVersion.ION_1_0) return getWriter_1_0(format, out);
118+
if (version == IonEncodingVersion.ION_1_1) return getWriter_1_1(format, out);
119+
throw new IllegalArgumentException("Unrecognized IonEncodingVersion: " + version);
120+
}
121+
122+
private static IonWriter getWriter_1_0(OutputFormat format, OutputStream out) {
123+
switch (format) {
124+
case Pretty: return IonEncodingVersion.ION_1_0.textWriterBuilder().withPrettyPrinting().build(out);
125+
case Text: return IonEncodingVersion.ION_1_0.textWriterBuilder().build(out);
126+
case Binary: return IonEncodingVersion.ION_1_0.binaryWriterBuilder().build(out);
127+
case Debug: throw new UnsupportedOperationException("Not yet supported, pending ion-java #1005");
128+
case None: return IonEncodingVersion.ION_1_0.textWriterBuilder().build(new NoOpOutputStream());
129+
default: throw new IllegalArgumentException("Unrecognized or unsupported output format: " + format);
130+
}
131+
}
132+
133+
private static IonWriter getWriter_1_1(OutputFormat format, OutputStream out) {
134+
switch (format) {
135+
case Pretty: return IonEncodingVersion.ION_1_1.textWriterBuilder().withPrettyPrinting().build(out);
136+
case Text: return IonEncodingVersion.ION_1_1.textWriterBuilder().build(out);
137+
case Binary: return IonEncodingVersion.ION_1_1.binaryWriterBuilder().build(out);
138+
case Debug: throw new UnsupportedOperationException("Not yet supported, pending ion-java #1005");
139+
case None: return IonEncodingVersion.ION_1_1.textWriterBuilder().build(new NoOpOutputStream());
140+
default: throw new IllegalArgumentException("Unrecognized or unsupported output format: " + format);
141+
}
142+
}
143+
144+
private enum OutputFormat {
145+
Pretty, Text, Binary, Debug, None
146+
}
147+
148+
private static class IonEncodingVersionConverter implements CommandLine.ITypeConverter<IonEncodingVersion<?,?>> {
149+
150+
@Override
151+
public IonEncodingVersion<?,?> convert(String ionVersion) {
152+
switch (ionVersion) {
153+
case "1.0": return IonEncodingVersion.ION_1_0;
154+
case "1.1": return IonEncodingVersion.ION_1_1;
155+
default: throw new IllegalArgumentException("Unrecognized or unsupported Ion version: " + ionVersion);
156+
}
157+
}
158+
}
114159
}

0 commit comments

Comments
 (0)