use of org.apache.camel.tools.apt.helper.CollectionStringBuffer in project camel by apache.
the class EndpointAnnotationProcessor method createParameterJsonSchema.
public String createParameterJsonSchema(ComponentModel componentModel, Set<ComponentOption> componentOptions, Set<EndpointPath> endpointPaths, Set<EndpointOption> endpointOptions, String[] schemes) {
StringBuilder buffer = new StringBuilder("{");
// component model
buffer.append("\n \"component\": {");
buffer.append("\n \"kind\": \"").append("component").append("\",");
buffer.append("\n \"scheme\": \"").append(componentModel.getScheme()).append("\",");
if (!Strings.isNullOrEmpty(componentModel.getExtendsScheme())) {
buffer.append("\n \"extendsScheme\": \"").append(componentModel.getExtendsScheme()).append("\",");
}
// the first scheme is the regular so only output if there is alternatives
if (schemes != null && schemes.length > 1) {
CollectionStringBuffer csb = new CollectionStringBuffer(",");
for (String altScheme : schemes) {
csb.append(altScheme);
}
buffer.append("\n \"alternativeSchemes\": \"").append(csb.toString()).append("\",");
}
buffer.append("\n \"syntax\": \"").append(componentModel.getSyntax()).append("\",");
if (componentModel.getAlternativeSyntax() != null) {
buffer.append("\n \"alternativeSyntax\": \"").append(componentModel.getAlternativeSyntax()).append("\",");
}
buffer.append("\n \"title\": \"").append(componentModel.getTitle()).append("\",");
buffer.append("\n \"description\": \"").append(componentModel.getDescription()).append("\",");
buffer.append("\n \"label\": \"").append(getOrElse(componentModel.getLabel(), "")).append("\",");
buffer.append("\n \"deprecated\": ").append(componentModel.isDeprecated()).append(",");
buffer.append("\n \"async\": ").append(componentModel.isAsync()).append(",");
buffer.append("\n \"consumerOnly\": ").append(componentModel.isConsumerOnly()).append(",");
buffer.append("\n \"producerOnly\": ").append(componentModel.isProducerOnly()).append(",");
buffer.append("\n \"lenientProperties\": ").append(componentModel.isLenientProperties()).append(",");
buffer.append("\n \"javaType\": \"").append(componentModel.getJavaType()).append("\",");
if (componentModel.getFirstVersion() != null) {
buffer.append("\n \"firstVersion\": \"").append(componentModel.getFirstVersion()).append("\",");
}
buffer.append("\n \"groupId\": \"").append(componentModel.getGroupId()).append("\",");
buffer.append("\n \"artifactId\": \"").append(componentModel.getArtifactId()).append("\",");
if (componentModel.getVerifiers() != null) {
buffer.append("\n \"verifiers\": \"").append(componentModel.getVerifiers()).append("\",");
}
buffer.append("\n \"version\": \"").append(componentModel.getVersionId()).append("\"");
buffer.append("\n },");
// and component properties
buffer.append("\n \"componentProperties\": {");
boolean first = true;
for (ComponentOption entry : componentOptions) {
if (first) {
first = false;
} else {
buffer.append(",");
}
buffer.append("\n ");
// either we have the documentation from this apt plugin or we need help to find it from extended component
String doc = entry.getDocumentationWithNotes();
if (Strings.isNullOrEmpty(doc)) {
doc = DocumentationHelper.findComponentJavaDoc(componentModel.getScheme(), componentModel.getExtendsScheme(), entry.getName());
}
// as its json we need to sanitize the docs
doc = sanitizeDescription(doc, false);
Boolean required = entry.getRequired() != null ? Boolean.valueOf(entry.getRequired()) : null;
String defaultValue = entry.getDefaultValue();
if (Strings.isNullOrEmpty(defaultValue) && "boolean".equals(entry.getType())) {
// fallback as false for boolean types
defaultValue = "false";
}
// component options do not have prefix
String optionalPrefix = "";
String prefix = "";
boolean multiValue = false;
boolean asPredicate = false;
buffer.append(JsonSchemaHelper.toJson(entry.getName(), entry.getDisplayName(), "property", required, entry.getType(), defaultValue, doc, entry.isDeprecated(), entry.isSecret(), entry.getGroup(), entry.getLabel(), entry.isEnumType(), entry.getEnums(), false, null, asPredicate, optionalPrefix, prefix, multiValue));
}
buffer.append("\n },");
buffer.append("\n \"properties\": {");
first = true;
// sort the endpoint options in the standard order we prefer
List<EndpointPath> paths = new ArrayList<EndpointPath>();
paths.addAll(endpointPaths);
Collections.sort(paths, EndpointHelper.createPathComparator(componentModel.getSyntax()));
// include paths in the top
for (EndpointPath entry : paths) {
String label = entry.getLabel();
if (label != null) {
// skip options which are either consumer or producer labels but the component does not support them
if (label.contains("consumer") && componentModel.isProducerOnly()) {
continue;
} else if (label.contains("producer") && componentModel.isConsumerOnly()) {
continue;
}
}
if (first) {
first = false;
} else {
buffer.append(",");
}
buffer.append("\n ");
// either we have the documentation from this apt plugin or we need help to find it from extended component
String doc = entry.getDocumentation();
if (Strings.isNullOrEmpty(doc)) {
doc = DocumentationHelper.findEndpointJavaDoc(componentModel.getScheme(), componentModel.getExtendsScheme(), entry.getName());
}
// as its json we need to sanitize the docs
doc = sanitizeDescription(doc, false);
Boolean required = entry.getRequired() != null ? Boolean.valueOf(entry.getRequired()) : null;
String defaultValue = entry.getDefaultValue();
if (Strings.isNullOrEmpty(defaultValue) && "boolean".equals(entry.getType())) {
// fallback as false for boolean types
defaultValue = "false";
}
// @UriPath options do not have prefix
String optionalPrefix = "";
String prefix = "";
boolean multiValue = false;
boolean asPredicate = false;
buffer.append(JsonSchemaHelper.toJson(entry.getName(), entry.getDisplayName(), "path", required, entry.getType(), defaultValue, doc, entry.isDeprecated(), entry.isSecret(), entry.getGroup(), entry.getLabel(), entry.isEnumType(), entry.getEnums(), false, null, asPredicate, optionalPrefix, prefix, multiValue));
}
// sort the endpoint options in the standard order we prefer
List<EndpointOption> options = new ArrayList<EndpointOption>();
options.addAll(endpointOptions);
Collections.sort(options, EndpointHelper.createGroupAndLabelComparator());
// and then regular parameter options
for (EndpointOption entry : options) {
String label = entry.getLabel();
if (label != null) {
// skip options which are either consumer or producer labels but the component does not support them
if (label.contains("consumer") && componentModel.isProducerOnly()) {
continue;
} else if (label.contains("producer") && componentModel.isConsumerOnly()) {
continue;
}
}
if (first) {
first = false;
} else {
buffer.append(",");
}
buffer.append("\n ");
// either we have the documentation from this apt plugin or we need help to find it from extended component
String doc = entry.getDocumentationWithNotes();
if (Strings.isNullOrEmpty(doc)) {
doc = DocumentationHelper.findEndpointJavaDoc(componentModel.getScheme(), componentModel.getExtendsScheme(), entry.getName());
}
// as its json we need to sanitize the docs
doc = sanitizeDescription(doc, false);
Boolean required = entry.getRequired() != null ? Boolean.valueOf(entry.getRequired()) : null;
String defaultValue = entry.getDefaultValue();
if (Strings.isNullOrEmpty(defaultValue) && "boolean".equals(entry.getType())) {
// fallback as false for boolean types
defaultValue = "false";
}
String optionalPrefix = entry.getOptionalPrefix();
String prefix = entry.getPrefix();
boolean multiValue = entry.isMultiValue();
boolean asPredicate = false;
buffer.append(JsonSchemaHelper.toJson(entry.getName(), entry.getDisplayName(), "parameter", required, entry.getType(), defaultValue, doc, entry.isDeprecated(), entry.isSecret(), entry.getGroup(), entry.getLabel(), entry.isEnumType(), entry.getEnums(), false, null, asPredicate, optionalPrefix, prefix, multiValue));
}
buffer.append("\n }");
buffer.append("\n}\n");
return buffer.toString();
}
Aggregations