1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.cli.processor;
7   
8   import java.util.Arrays;
9   import java.util.Collections;
10  import java.util.List;
11  
12  import edu.umd.cs.findbugs.annotations.NonNull;
13  
14  public class MessageExitStatus
15      extends AbstractExitStatus {
16    private final List<Object> messageArguments;
17  
18    /**
19     * Construct a new {@link ExitStatus} based on an array of message arguments.
20     *
21     * @param code
22     *          the exit code to use.
23     * @param messageArguments
24     *          the arguments that can be passed to a formatted string to generate
25     *          the message
26     */
27    public MessageExitStatus(@NonNull ExitCode code, @NonNull Object... messageArguments) {
28      super(code);
29      if (messageArguments.length == 0) {
30        this.messageArguments = Collections.emptyList();
31      } else {
32        this.messageArguments = Arrays.asList(messageArguments);
33      }
34    }
35  
36    @Override
37    public String getMessage() {
38      String format = lookupMessageForCode(getExitCode());
39      return String.format(format, messageArguments.toArray());
40    }
41  
42    private String lookupMessageForCode(@SuppressWarnings("unused") ExitCode ignoredExitCode) {
43      // TODO: add message bundle support
44      StringBuilder builder = new StringBuilder();
45      // builder.append(getExitCode()).append(":");
46      for (int index = 1; index <= messageArguments.size(); index++) {
47        if (index > 1) {
48          builder.append(' ');
49        }
50        builder.append("%s");
51        // builder.append(index);
52      }
53      return builder.toString();
54    }
55  }