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.DefaultExtraArgument;
14 import gov.nist.secauto.metaschema.cli.processor.command.ExtraArgument;
15 import gov.nist.secauto.metaschema.core.util.AutoCloser;
16 import gov.nist.secauto.metaschema.core.util.ObjectUtils;
17 import gov.nist.secauto.metaschema.databind.IBindingContext;
18 import gov.nist.secauto.metaschema.databind.io.Format;
19 import gov.nist.secauto.metaschema.databind.io.IBoundLoader;
20
21 import org.apache.commons.cli.CommandLine;
22 import org.apache.commons.cli.Option;
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
25
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.io.OutputStreamWriter;
29 import java.io.Writer;
30 import java.net.URI;
31 import java.nio.charset.StandardCharsets;
32 import java.nio.file.Files;
33 import java.nio.file.Path;
34 import java.nio.file.StandardOpenOption;
35 import java.util.Collection;
36 import java.util.List;
37
38 import edu.umd.cs.findbugs.annotations.NonNull;
39
40
41
42
43 public abstract class AbstractConvertSubcommand
44 extends AbstractTerminalCommand {
45 private static final Logger LOGGER = LogManager.getLogger(AbstractConvertSubcommand.class);
46
47 @NonNull
48 private static final String COMMAND = "convert";
49 @NonNull
50 private static final List<ExtraArgument> EXTRA_ARGUMENTS = ObjectUtils.notNull(List.of(
51 new DefaultExtraArgument("source-file-or-URL", true),
52 new DefaultExtraArgument("destination-file", false)));
53
54 @Override
55 public String getName() {
56 return COMMAND;
57 }
58
59 @Override
60 public Collection<? extends Option> gatherOptions() {
61 return ObjectUtils.notNull(List.of(
62 MetaschemaCommands.OVERWRITE_OPTION,
63 MetaschemaCommands.TO_OPTION));
64 }
65
66 @Override
67 public List<ExtraArgument> getExtraArguments() {
68 return EXTRA_ARGUMENTS;
69 }
70
71
72
73
74
75 protected abstract static class AbstractConversionCommandExecutor
76 extends AbstractCommandExecutor {
77
78
79
80
81
82
83
84
85
86 protected AbstractConversionCommandExecutor(
87 @NonNull CallingContext callingContext,
88 @NonNull CommandLine commandLine) {
89 super(callingContext, commandLine);
90 }
91
92
93
94
95
96
97
98
99 @NonNull
100 protected abstract IBindingContext getBindingContext() throws CommandExecutionException;
101
102 @SuppressWarnings({
103 "PMD.OnlyOneReturn",
104 "PMD.CyclomaticComplexity", "PMD.CognitiveComplexity"
105 })
106 @Override
107 public void execute() throws CommandExecutionException {
108 CommandLine cmdLine = getCommandLine();
109
110 List<String> extraArgs = cmdLine.getArgList();
111
112 Path destination = null;
113 if (extraArgs.size() > 1) {
114 destination = MetaschemaCommands.handleDestination(ObjectUtils.requireNonNull(extraArgs.get(1)), cmdLine);
115 }
116
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 }