1
2
3
4
5
6 package gov.nist.secauto.metaschema.cli.commands;
7
8 import gov.nist.secauto.metaschema.cli.processor.CLIProcessor.CallingContext;
9 import gov.nist.secauto.metaschema.cli.processor.ExitCode;
10 import gov.nist.secauto.metaschema.cli.processor.command.AbstractCommandExecutor;
11 import gov.nist.secauto.metaschema.cli.processor.command.AbstractTerminalCommand;
12 import gov.nist.secauto.metaschema.cli.processor.command.CommandExecutionException;
13 import gov.nist.secauto.metaschema.cli.processor.command.ExtraArgument;
14 import gov.nist.secauto.metaschema.core.util.AutoCloser;
15 import gov.nist.secauto.metaschema.core.util.ObjectUtils;
16 import gov.nist.secauto.metaschema.databind.IBindingContext;
17 import gov.nist.secauto.metaschema.databind.io.Format;
18 import gov.nist.secauto.metaschema.databind.io.IBoundLoader;
19
20 import org.apache.commons.cli.CommandLine;
21 import org.apache.commons.cli.Option;
22 import org.apache.logging.log4j.LogManager;
23 import org.apache.logging.log4j.Logger;
24
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.OutputStreamWriter;
28 import java.io.Writer;
29 import java.net.URI;
30 import java.nio.charset.StandardCharsets;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.nio.file.StandardOpenOption;
34 import java.util.Collection;
35 import java.util.List;
36
37 import edu.umd.cs.findbugs.annotations.NonNull;
38
39
40
41
42 public abstract class AbstractConvertSubcommand
43 extends AbstractTerminalCommand {
44 private static final Logger LOGGER = LogManager.getLogger(AbstractConvertSubcommand.class);
45
46 @NonNull
47 private static final String COMMAND = "convert";
48 @NonNull
49 private static final List<ExtraArgument> EXTRA_ARGUMENTS = ObjectUtils.notNull(List.of(
50 ExtraArgument.newInstance("source-file-or-URL", true),
51 ExtraArgument.newInstance("destination-file", false)));
52
53 @Override
54 public String getName() {
55 return COMMAND;
56 }
57
58 @Override
59 public Collection<? extends Option> gatherOptions() {
60 return ObjectUtils.notNull(List.of(
61 MetaschemaCommands.OVERWRITE_OPTION,
62 MetaschemaCommands.TO_OPTION));
63 }
64
65 @Override
66 public List<ExtraArgument> getExtraArguments() {
67 return EXTRA_ARGUMENTS;
68 }
69
70
71
72
73
74 protected abstract static class AbstractConversionCommandExecutor
75 extends AbstractCommandExecutor {
76
77
78
79
80
81
82
83
84
85 protected AbstractConversionCommandExecutor(
86 @NonNull CallingContext callingContext,
87 @NonNull CommandLine commandLine) {
88 super(callingContext, commandLine);
89 }
90
91
92
93
94
95
96
97
98 @NonNull
99 protected abstract IBindingContext getBindingContext() throws CommandExecutionException;
100
101 @SuppressWarnings({
102 "PMD.OnlyOneReturn",
103 "PMD.CyclomaticComplexity", "PMD.CognitiveComplexity"
104 })
105 @Override
106 public void execute() throws CommandExecutionException {
107 CommandLine cmdLine = getCommandLine();
108
109 List<String> extraArgs = cmdLine.getArgList();
110
111 Path destination = null;
112 if (extraArgs.size() > 1) {
113 destination = MetaschemaCommands.handleDestination(ObjectUtils.requireNonNull(extraArgs.get(1)), cmdLine);
114 }
115
116 @SuppressWarnings("synthetic-access")
117 URI source = MetaschemaCommands.handleSource(
118 ObjectUtils.requireNonNull(extraArgs.get(0)),
119 ObjectUtils.notNull(getCurrentWorkingDirectory().toUri()));
120
121 Format toFormat = MetaschemaCommands.getFormat(cmdLine, MetaschemaCommands.TO_OPTION);
122
123 IBindingContext bindingContext = getBindingContext();
124
125 try {
126 IBoundLoader loader = bindingContext.newBoundLoader();
127 if (LOGGER.isInfoEnabled()) {
128 LOGGER.info("Converting '{}'.", source);
129 }
130
131 if (destination == null) {
132
133 try (OutputStreamWriter writer
134 = new OutputStreamWriter(AutoCloser.preventClose(System.out), StandardCharsets.UTF_8)) {
135 handleConversion(source, toFormat, writer, loader);
136 }
137 } else {
138 try (Writer writer = Files.newBufferedWriter(
139 destination,
140 StandardCharsets.UTF_8,
141 StandardOpenOption.CREATE,
142 StandardOpenOption.WRITE,
143 StandardOpenOption.TRUNCATE_EXISTING)) {
144 assert writer != null;
145 handleConversion(source, toFormat, writer, loader);
146 }
147 }
148 } catch (IllegalArgumentException ex) {
149 throw new CommandExecutionException(ExitCode.PROCESSING_ERROR, ex);
150 } catch (IOException ex) {
151 throw new CommandExecutionException(ExitCode.IO_ERROR, ex);
152 }
153 if (destination != null && LOGGER.isInfoEnabled()) {
154 LOGGER.info("Generated {} file: {}", toFormat.toString(), destination);
155 }
156 }
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174 protected abstract void handleConversion(
175 @NonNull URI source,
176 @NonNull Format toFormat,
177 @NonNull Writer writer,
178 @NonNull IBoundLoader loader) throws FileNotFoundException, IOException;
179 }
180 }