1
1
package com .amazon .tools .cli ;
2
2
3
3
4
+ import com .amazon .ion .IonEncodingVersion ;
4
5
import com .amazon .ion .IonReader ;
5
6
import com .amazon .ion .IonWriter ;
6
7
import com .amazon .ion .system .IonReaderBuilder ;
@@ -42,8 +43,12 @@ public static void main(String[] args) {
42
43
System .exit (commandLine .execute (args ));
43
44
}
44
45
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
+
45
50
@ 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)." ,
47
52
paramLabel = "<format>" ,
48
53
scope = CommandLine .ScopeType .INHERIT )
49
54
OutputFormat outputFormat ;
@@ -57,17 +62,13 @@ public static void main(String[] args) {
57
62
mixinStandardHelpOptions = true )
58
63
int cat ( @ Parameters (paramLabel = "FILE" ) File ... files ) {
59
64
60
- if (outputFormat == OutputFormat .EVENTS ) {
61
- System .err .println ("'events' output format is not supported" );
62
- return CommandLine .ExitCode .USAGE ;
63
- }
64
65
65
66
//TODO: Handle stream cutoff- java.io.IOException: Broken pipe
66
67
//TODO: This is not resilient to problems with a single file. Should it be?
67
68
try (InputStream in = getInputStream (files );
68
69
IonReader reader = IonReaderBuilder .standard ().build (in );
69
70
OutputStream out = getOutputStream (outputFile );
70
- IonWriter writer = outputFormat . createIonWriter ( out )) {
71
+ IonWriter writer = getWriter ( ionVersion , outputFormat , out )) {
71
72
// getInputStream will look for stdin if we don't supply
72
73
writer .writeValues (reader );
73
74
} catch (IOException e ) {
@@ -111,4 +112,48 @@ private static FileOutputStream getOutputStream(File outputFile) throws IOExcept
111
112
// non-line-buffered stdout, or the requested file output
112
113
return outputFile == null ? new FileOutputStream (FileDescriptor .out ) : new FileOutputStream (outputFile );
113
114
}
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
+ }
114
159
}
0 commit comments