use of java.util.Locale.ROOT in project beam by apache.
the class PipelineOptionsFactory method parseObjects.
/**
* Using the parsed string arguments, we convert the strings to the expected return type of the
* methods that are found on the passed-in class.
*
* <p>For any return type that is expected to be an array or a collection, we further split up
* each string on ','.
*
* <p>We special case the "runner" option. It is mapped to the class of the {@link PipelineRunner}
* based off of the {@link PipelineRunner PipelineRunners} simple class name. If the provided
* runner name is not registered via a {@link PipelineRunnerRegistrar}, we attempt to obtain the
* class that the name represents using {@link Class#forName(String)} and use the result class if
* it subclasses {@link PipelineRunner}.
*
* <p>If strict parsing is enabled, unknown options or options that cannot be converted to the
* expected java type using an {@link ObjectMapper} will be ignored.
*/
private static <T extends PipelineOptions> Map<String, Object> parseObjects(Class<T> klass, ListMultimap<String, String> options, boolean strictParsing) {
Map<String, Method> propertyNamesToGetters = Maps.newHashMap();
Cache cache = CACHE.get();
cache.validateWellFormed(klass);
@SuppressWarnings("unchecked") Iterable<PropertyDescriptor> propertyDescriptors = cache.getPropertyDescriptors(Stream.concat(getRegisteredOptions().stream(), Stream.of(klass)).collect(Collectors.toSet()));
for (PropertyDescriptor descriptor : propertyDescriptors) {
propertyNamesToGetters.put(descriptor.getName(), descriptor.getReadMethod());
}
Map<String, Object> convertedOptions = Maps.newHashMap();
for (final Map.Entry<String, Collection<String>> entry : options.asMap().entrySet()) {
try {
// Either off by one or off by two character errors.
if (!propertyNamesToGetters.containsKey(entry.getKey())) {
SortedSet<String> closestMatches = new TreeSet<>(Sets.filter(propertyNamesToGetters.keySet(), input -> StringUtils.getLevenshteinDistance(entry.getKey(), input) <= 2));
switch(closestMatches.size()) {
case 0:
throw new IllegalArgumentException(String.format("Class %s missing a property named '%s'.", klass, entry.getKey()));
case 1:
throw new IllegalArgumentException(String.format("Class %s missing a property named '%s'. Did you mean '%s'?", klass, entry.getKey(), Iterables.getOnlyElement(closestMatches)));
default:
throw new IllegalArgumentException(String.format("Class %s missing a property named '%s'. Did you mean one of %s?", klass, entry.getKey(), closestMatches));
}
}
Method method = propertyNamesToGetters.get(entry.getKey());
// Only allow empty argument values for String, String Array, and Collection<String>.
Class<?> returnType = method.getReturnType();
JavaType type = MAPPER.getTypeFactory().constructType(method.getGenericReturnType());
if ("runner".equals(entry.getKey())) {
String runner = Iterables.getOnlyElement(entry.getValue());
final Map<String, Class<? extends PipelineRunner<?>>> pipelineRunners = cache.supportedPipelineRunners;
if (pipelineRunners.containsKey(runner.toLowerCase())) {
convertedOptions.put("runner", pipelineRunners.get(runner.toLowerCase(ROOT)));
} else {
try {
Class<?> runnerClass = Class.forName(runner, true, ReflectHelpers.findClassLoader());
if (!PipelineRunner.class.isAssignableFrom(runnerClass)) {
throw new IllegalArgumentException(String.format("Class '%s' does not implement PipelineRunner. " + "Supported pipeline runners %s", runner, cache.getSupportedRunners()));
}
convertedOptions.put("runner", runnerClass);
} catch (ClassNotFoundException e) {
String msg = String.format("Unknown 'runner' specified '%s', supported pipeline runners %s", runner, cache.getSupportedRunners());
throw new IllegalArgumentException(msg, e);
}
}
} else if (isCollectionOrArrayOfAllowedTypes(returnType, type)) {
// Split any strings with ","
List<String> values = entry.getValue().stream().flatMap(input -> Arrays.stream(input.split(","))).collect(Collectors.toList());
if (values.contains("")) {
checkEmptyStringAllowed(returnType, type, method.getGenericReturnType().toString());
}
convertedOptions.put(entry.getKey(), MAPPER.convertValue(values, type));
} else if (isSimpleType(returnType, type)) {
String value = Iterables.getOnlyElement(entry.getValue());
if (value.isEmpty()) {
checkEmptyStringAllowed(returnType, type, method.getGenericReturnType().toString());
}
convertedOptions.put(entry.getKey(), MAPPER.convertValue(value, type));
} else {
String value = Iterables.getOnlyElement(entry.getValue());
if (value.isEmpty()) {
checkEmptyStringAllowed(returnType, type, method.getGenericReturnType().toString());
}
try {
convertedOptions.put(entry.getKey(), tryParseObject(value, method));
} catch (IOException e) {
throw new IllegalArgumentException("Unable to parse JSON value " + value, e);
}
}
} catch (IllegalArgumentException e) {
if (strictParsing) {
throw e;
} else {
LOG.warn("Strict parsing is disabled, ignoring option '{}' with value '{}' because {}", entry.getKey(), entry.getValue(), e.getMessage());
}
}
}
return convertedOptions;
}
use of java.util.Locale.ROOT in project component-runtime by Talend.
the class AsciidoctorService method toHtml.
public String toHtml(final String input) {
final StringBuilder builder = new StringBuilder();
try (final BufferedReader reader = new BufferedReader(new StringReader(input))) {
String line;
int lineNumber = 0;
while ((line = reader.readLine()) != null) {
lineNumber++;
if (line.startsWith("////")) {
do {
line = reader.readLine();
lineNumber++;
} while (!line.startsWith("////"));
}
if (line.startsWith("//")) {
continue;
}
if (line.startsWith(":")) {
// ignore attributes
continue;
}
if (line.startsWith("[")) {
if (line.startsWith("[source")) {
line = reader.readLine();
lineNumber++;
if (line != null && line.startsWith("----")) {
builder.append("<pre><code>\n");
do {
line = reader.readLine();
if (line == null) {
break;
}
lineNumber++;
if (line.startsWith("----")) {
break;
}
builder.append(escapeHtml4(line, false)).append("\n");
} while (true);
builder.append("</pre></code>\n");
}
}
// else ignore customizations for now
continue;
}
if (line.trim().isEmpty()) {
continue;
}
if (line.startsWith("=")) {
final int space = line.indexOf(' ');
if (space < 0) {
throw new IllegalArgumentException("line " + lineNumber + " missing space after equal(s)");
}
final String title = line.substring(space).trim();
builder.append("<h").append(space).append(" id=\"").append("_").append(title.replace(" ", "_").toLowerCase(ROOT)).append("\">").append(escapeHtml4(title, true)).append("</h").append(space).append(">\n");
} else if (line.startsWith("|===")) {
builder.append("<table class=\"tableblock frame-all grid-all spread\">\n");
boolean headersDone = false;
do {
line = reader.readLine();
if (line == null) {
break;
}
lineNumber++;
if (line.startsWith("|===")) {
break;
}
if (line.trim().isEmpty()) {
continue;
}
if (!headersDone) {
builder.append("<thead>\n");
}
final String[] columns = (line.startsWith("|") ? line.substring(1) : line).split("\\|");
builder.append("<tr>\n");
if (columns.length > 1) {
final boolean data = headersDone;
Stream.of(columns).forEach(c -> td(data, builder, c));
} else {
td(headersDone, builder, line);
do {
line = reader.readLine();
if (line == null) {
break;
}
lineNumber++;
if (line.isEmpty()) {
break;
}
line = line.startsWith("|") ? line.substring(1) : line;
td(headersDone, builder, line);
} while (true);
}
builder.append("</tr>\n");
if (!headersDone) {
builder.append("</thead>\n<tbody>\n");
headersDone = true;
}
} while (true);
builder.append("</tbody>\n</table>\n");
} else {
// paragraph
builder.append("<div class=\"paragraph\">\n<p>\n").append(escapeHtml4(line, true)).append("\n");
do {
line = reader.readLine();
if (line == null) {
break;
}
lineNumber++;
if (line.isEmpty()) {
break;
}
builder.append(escapeHtml4(line, true)).append("\n");
} while (true);
builder.append("</p>\n</div>\n");
}
}
} catch (final IOException e) {
throw new IllegalArgumentException(e);
}
return builder.toString();
}
use of java.util.Locale.ROOT in project component-runtime by Talend.
the class JsonSchemaConverter method convert.
@Override
public CompletionStage<PropertyContext> convert(final CompletionStage<PropertyContext> cs) {
return cs.thenCompose(context -> {
final JsonSchema jsonSchema = new JsonSchema();
jsonSchema.setTitle(context.getProperty().getDisplayName());
final String type = context.getProperty().getType();
switch(type.toLowerCase(ROOT)) {
case "enum":
return new EnumPropertyConverter(jsonSchema).convert(CompletableFuture.completedFuture(context)).thenCompose(c -> postHandling(context, jsonSchema, type));
case "array":
return new ArrayPropertyConverter(jsonb, jsonSchema, properties).convert(CompletableFuture.completedFuture(context)).thenCompose(c -> postHandling(context, jsonSchema, type));
default:
if (context.getProperty().getPath().endsWith("[]")) {
return CompletableFuture.completedFuture(context);
}
jsonSchema.setType(type.toLowerCase(ROOT));
jsonSchema.setRequired(properties.stream().filter(context::isDirectChild).filter(nested -> new PropertyContext(nested).isRequired()).map(SimplePropertyDefinition::getName).collect(toSet()));
return CompletableFuture.completedFuture(context).thenCompose(c -> postHandling(context, jsonSchema, type));
}
});
}
use of java.util.Locale.ROOT in project component-runtime by Talend.
the class ArrayPropertyConverter method convert.
@Override
public CompletionStage<PropertyContext> convert(final CompletionStage<PropertyContext> cs) {
return cs.thenCompose(context -> {
jsonSchema.setType(context.getProperty().getType().toLowerCase(ROOT));
final String prefix = context.getProperty().getPath() + "[]";
final List<SimplePropertyDefinition> arrayElements = properties.stream().filter(child -> child.getPath().startsWith(prefix)).collect(toList());
if (arrayElements.stream().anyMatch(e -> e.getPath().startsWith(prefix + '.'))) {
// complex object
final JsonSchema items = new JsonSchema();
items.setType("object");
items.setProperties(new HashMap<>());
jsonSchema.setItems(items);
return CompletableFuture.allOf(arrayElements.stream().map(PropertyContext::new).map(CompletableFuture::completedFuture).map(e -> new JsonSchemaConverter(jsonb, items, emptyList()).convert(e)).toArray(CompletableFuture[]::new)).thenApply(done -> context);
} else if (!arrayElements.isEmpty()) {
// primitive
final String type = arrayElements.get(0).getType();
final JsonSchema item = new JsonSchema();
item.setTitle(jsonSchema.getTitle());
item.setType("enum".equalsIgnoreCase(type) ? "string" : type.toLowerCase(ROOT));
jsonSchema.setItems(item);
}
return CompletableFuture.completedFuture(context);
});
}
Aggregations