public static PropertyDTO createInputDTO(InputComponent<?, ?> input) { String name = input.getName(); String description = input.getDescription(); String label = input.getLabel(); String requiredMessage = input.getRequiredMessage(); char shortNameChar = input.getShortName(); String shortName = Character.toString(shortNameChar); Object value = convertValueToSafeJson(input.getValueConverter(), InputComponents.getValueFor(input)); Class<?> valueType = input.getValueType(); String javaType = null; if (valueType != null) { boolean enabled = input.isEnabled(); boolean required = input.isRequired(); List<Object> enumValues = new ArrayList<>(); if (input instanceof SelectComponent) {
public static void putComponentValuesInAttributeMap(UIExecutionContext context, List<InputComponent> inputComponents) { if (inputComponents != null) { Map<Object, Object> attributeMap = context.getUIContext().getAttributeMap(); for (InputComponent inputComponent : inputComponents) { String name = inputComponent.getName(); Object value = inputComponent.getValue(); if (value != null) { attributeMap.put(name, value); } } } }
/** * Returns the label for this component * * @param input the input component * @param addColon should a colon be added in the end of the label ? * @return the label with a colon in the end if addColon is true */ public static String getLabelFor(InputComponent<?, ?> input, boolean addColon) { String label = input.getLabel(); // If no label was provided, use name if (label == null) { label = input.getName(); } // if a colon is required, add it if (addColon && !label.endsWith(COLON)) { label += COLON; } return label; }
/** * Called by {@link AbstractShellInteraction#promptRequiredMissingValues(ShellImpl)} */ protected void promptRequiredMissingValues(ShellImpl shell, Iterable<InputComponent<?, ?>> inputs) throws InterruptedException { ShellUIPromptImpl prompt = shell.createPrompt(context); for (InputComponent<?, ?> input : inputs) { if (input.isEnabled() && !input.isDeprecated()) { boolean requiredInputMissing = input.isRequired() && !(input.hasDefaultValue() || input.hasValue()); Object obj = prompt.promptValueFrom(input); if (obj == null && requiredInputMissing) { // No value returned. Just stop testing other inputs break; } } } }
if ("arguments".equals(input.getName())) if (input.getShortName() != InputComponents.DEFAULT_SHORT_NAME) result.append("-").append(input.getShortName()).append(", "); else result.append(" "); result.append("--").append(commandLineUtil.toOptionName(input.getName())).append("*"); result.append(OperatingSystemUtils.getLineSeparator()); result.append(" "); if (!input.getName().equals(input.getLabel())) result.append(input.getLabel() == null ? "" : input.getLabel()); if (input.getLabel() != null && input.getDescription() != null) result.append(" - "); result.append(input.getDescription() == null ? "" : input.getDescription()); result.append(" [").append(input.getValueType().getSimpleName()).append("]"); if (input.isRequired()) result.append(" (*required*)"); if (input.hasDefaultValue() && input.hasValue()) Object value = input.getValue(); if (value != null) else if (input.hasDefaultValue())
final boolean isMultiple = input instanceof ManyValued; final boolean hasValue = (!InputType.CHECKBOX.equals(InputComponents.getInputType(input)) && !Boolean.class .isAssignableFrom(input.getValueType()) && !boolean.class.isAssignableFrom(input.getValueType())); try optionBuilder.name(toOptionName(inputName)) .addDefaultValue(Objects.toString(defaultValue, null)) .description(input.getLabel()) .hasMultipleValues(isMultiple) .hasValue(hasValue) .type(input.getValueType()) .valueSeparator(' ') .activator(cmd -> input.isEnabled()) .completer(OptionCompleterFactory.getCompletionFor( input, shellContext, converterFactory)); if (input.isRequired() && !input.hasDefaultValue() && !input.hasValue()) if (input.isDeprecated()) if (input.getShortName() != InputComponents.DEFAULT_SHORT_NAME) optionBuilder.shortName(input.getShortName()); if (ARGUMENTS_INPUT_NAME.equals(input.getName()))
.add("name", input.getName()) .add("shortName", String.valueOf(input.getShortName())) .add("valueType", input.getValueType().getName()) .add("inputType", InputComponents.getInputType(input)) .add("enabled", input.isEnabled()) .add("required", input.isRequired()) .add("deprecated", input.isDeprecated()) .add("label", InputComponents.getLabelFor(input, false)); addOptional(objBuilder, "description", input.getDescription()); addOptional(objBuilder, "note", input.getNote()); Converter<Object, String> inputConverter = null; if (input instanceof SelectComponent) .getConverter(input.getValueType(), String.class);
for (InputComponent<?, ?> input : inputs) if (input.getShortName() != InputComponents.DEFAULT_SHORT_NAME && Boolean.class.equals(input.getValueType())) result.append(input.getShortName()); for (InputComponent<?, ?> input : inputs) if ("arguments".equals(input.getName())) else if (input.isRequired()) if (input.getShortName() != InputComponents.DEFAULT_SHORT_NAME) result.append("-").append(input.getShortName()).append(" "); result.append("--").append(commandLineUtil.toOptionName(input.getName())).append("] "); result.append(input.getValueType().getSimpleName()).append(" "); result.append("[ ... ").append(arguments.getLabel() == null ? " args" : arguments.getLabel()).append("] "); result.append(arguments.getValueType().getSimpleName()).append(" ");
/** * Validate if an required and enabled input has a value. If not, return the error message. * * @param input * @return */ public static String validateRequired(final InputComponent<?, ?> input) { String requiredMessage = null; if (input.isEnabled() && input.isRequired() && !InputComponents.hasValue(input)) { requiredMessage = input.getRequiredMessage(); if (Strings.isNullOrEmpty(requiredMessage)) { String labelValue = input.getLabel() == null ? input.getName() : input.getLabel(); // Chop : off if (labelValue.endsWith(COLON)) { labelValue = labelValue.substring(0, labelValue.length() - 1); } requiredMessage = labelValue + " must be specified."; } } return requiredMessage; }
String description = input.getDescription(); if (!Strings.isNullOrEmpty(description)) if (InputType.SECRET.equals(inputType)) value = promptInternal(label + description + defaultValueDescription + ": ", false, input.isRequired()); else if (input.getValueType() == Boolean.class) Boolean defaultValue = (Boolean) input.getValue(); value = promptBooleanInternal(label + description, defaultValue == null || defaultValue.booleanValue()); value = promptInternal(label + description + defaultValueDescription + ": ", true, input.isRequired());
/** * Returns the value stored in this {@link InputComponent}. <code>null</code> if the component is null */ public static Object getValueFor(InputComponent<?, ?> component) { return (component == null) ? null : component.getValue(); }
public static void populateController(Map<String, String> requestedInputs, CommandController controller, ConverterFactory converterFactory) { Map<String, InputComponent<?, ?>> inputs = controller.getInputs(); Set<String> inputKeys = new HashSet<>(inputs.keySet()); if (requestedInputs != null) { inputKeys.retainAll(requestedInputs.keySet()); Set<Map.Entry<String, InputComponent<?, ?>>> entries = inputs.entrySet(); for (Map.Entry<String, InputComponent<?, ?>> entry : entries) { String key = entry.getKey(); InputComponent<?, ?> component = entry.getValue(); String textValue = requestedInputs.get(key); Object value = textValue; if (component != null && textValue != null) { Converter<String, ?> valueConverter = component.getValueConverter(); if (valueConverter != null) { value = valueConverter.convert(textValue); } else { Class<?> valueType = component.getValueType(); if (valueType.isEnum()) { Class<? extends Enum> enumType = (Class<? extends Enum>) valueType; value = Enum.valueOf(enumType, textValue); } } InputComponents.setValueFor(converterFactory, component, value); } else { controller.setValueFor(key, value); } Object actual = controller.getValueFor(key); } } }
if (!input.isEnabled()) if (Resource.class.isAssignableFrom(input.getValueType())) if (Resource.class.isAssignableFrom(input.getValueType()))
/** * Returns if there is a value set for this {@link InputComponent} */ public static boolean hasValue(InputComponent<?, ?> input) { boolean ret; Object value = InputComponents.getValueFor(input); if (value == null) { ret = false; } else if (value instanceof String && value.toString().isEmpty()) { ret = false; } else if (!input.getValueType().isInstance(value) && value instanceof Iterable && !((Iterable) value).iterator().hasNext()) { ret = false; } else { ret = true; } return ret; }
public UIMessageDTO(UIMessage message) { this.description = message.getDescription(); this.inputName = message.getSource().getName(); this.severity = message.getSeverity(); }
String inputType = component.getFacet(HintsFacet.class).getInputType(); OptionCompleter<CompleterInvocation> strategy = null; else if (Resource.class.isAssignableFrom(component.getValueType()))
private Object getValueForInput(ConfigurationOption option, InputComponent<?, ?> input) { Object value = input.getValue(); if (value == null) { return value; } if (value instanceof Resource<?>) { Resource<?> resourceResolved = getResourceResolved((Resource<?>) value); return resourceResolved.getUnderlyingResourceObject(); } if (option.getType() == Path.class) { // these have to be converted Set<Path> paths = new LinkedHashSet<>(); if (value instanceof List) { for (Object path : (List) value) { if (path instanceof File) path = ((File) path).toPath(); paths.add((Path) path); } } value = paths; } return value; }
Class<Object> targetType = (Class<Object>) input.getValueType(); if (!targetType.isAssignableFrom(sourceType)) Converter<String, Object> valueConverter = (Converter<String, Object>) input.getValueConverter(); if (valueConverter != null) Converter<String, Object> valueConverter = (Converter<String, Object>) input.getValueConverter(); if (valueConverter != null && value instanceof String)
if (value instanceof Iterable && !input.getValueType().isInstance(value))