Search in sources :

Example 1 with ComponentModel

use of org.apache.camel.tooling.model.ComponentModel in project camel-k-runtime by apache.

the class GenerateYamlEndpointsSchema method execute.

@Override
public void execute() throws MojoFailureException {
    final ObjectMapper mapper = new ObjectMapper();
    final ObjectNode root = mapper.createObjectNode();
    // Schema
    root.put("$schema", "http://json-schema.org/draft-04/schema#");
    root.put("type", "object");
    // Schema sections
    this.definitions = root.putObject("definitions");
    final CamelCatalog catalog = new DefaultCamelCatalog();
    for (String componentName : catalog.findComponentNames()) {
        ComponentModel component = catalog.componentModel(componentName);
        if (!definitions.has(component.getScheme())) {
            ObjectNode node = definitions.putObject(component.getScheme());
            node.put("type", "object");
            processEndpointOption(node, component.getEndpointPathOptions());
            processEndpointOption(node, component.getEndpointParameterOptions());
        }
        if (component.getAlternativeSchemes() != null) {
            for (String scheme : component.getAlternativeSchemes().split(",")) {
                if (!definitions.has(scheme)) {
                    definitions.putObject(scheme).put("type", "object").put("$ref", "#/definitions/" + component.getScheme());
                }
            }
        }
    }
    try {
        ToolingSupport.mkparents(outputFile);
        mapper.writerWithDefaultPrettyPrinter().writeValue(outputFile, root);
    } catch (IOException e) {
        throw new MojoFailureException(e.getMessage(), e);
    }
}
Also used : DefaultCamelCatalog(org.apache.camel.catalog.DefaultCamelCatalog) CamelCatalog(org.apache.camel.catalog.CamelCatalog) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ComponentModel(org.apache.camel.tooling.model.ComponentModel) MojoFailureException(org.apache.maven.plugin.MojoFailureException) DefaultCamelCatalog(org.apache.camel.catalog.DefaultCamelCatalog) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with ComponentModel

use of org.apache.camel.tooling.model.ComponentModel in project camel-kafka-connector by apache.

the class CamelKafkaConnectorCreateMojo method generateAndWritePom.

private void generateAndWritePom(String sanitizedName, File directory) throws Exception {
    // create initial connector pom
    ComponentModel cm = JsonMapper.generateComponentModel(componentJson);
    getLog().info("Creating a new pom.xml for the connector from scratch");
    Template pomTemplate = MavenUtils.getTemplate(rm.getResourceAsFile(initialPomTemplate));
    Map<String, String> props = new HashMap<>();
    props.put("version", project.getVersion());
    props.put("dependencyId", cm.getArtifactId());
    props.put("dependencyGroup", cm.getGroupId());
    props.put("componentName", name);
    props.put("componentSanitizedName", sanitizedName);
    props.put("componentDescription", name);
    try {
        Document pom = MavenUtils.createCrateXmlDocumentFromTemplate(pomTemplate, props);
        // Write the connector pom
        File pomFile = new File(directory, "pom.xml");
        writeXmlFormatted(pom, pomFile, getLog());
    } catch (Exception e) {
        getLog().error("Cannot create pom.xml file from Template: " + pomTemplate + " with properties: " + props, e);
        throw e;
    }
}
Also used : HashMap(java.util.HashMap) ComponentModel(org.apache.camel.tooling.model.ComponentModel) Document(org.w3c.dom.Document) File(java.io.File) IOException(java.io.IOException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Template(freemarker.template.Template)

Example 3 with ComponentModel

use of org.apache.camel.tooling.model.ComponentModel in project camel-quarkus by apache.

the class CqCatalog method toCamelDocsModel.

public static ArtifactModel<?> toCamelDocsModel(ArtifactModel<?> m) {
    if ("imap".equals(m.getName())) {
        final ComponentModel clone = (ComponentModel) CqUtils.cloneArtifactModel(m);
        clone.setName("mail");
        clone.setTitle("Mail");
        return clone;
    }
    if (m.getName().startsWith("bindy")) {
        final DataFormatModel clone = (DataFormatModel) CqUtils.cloneArtifactModel(m);
        clone.setName("bindy");
        return clone;
    }
    return m;
}
Also used : DataFormatModel(org.apache.camel.tooling.model.DataFormatModel) ComponentModel(org.apache.camel.tooling.model.ComponentModel)

Example 4 with ComponentModel

use of org.apache.camel.tooling.model.ComponentModel in project camel-idea-plugin by camel-tooling.

the class CamelDocumentationProvider method generateCamelComponentDocumentation.

private String generateCamelComponentDocumentation(String componentName, String val, int wrapLength, Project project) {
    // it is a known Camel component
    CamelCatalog camelCatalog = ServiceManager.getService(project, CamelCatalogService.class).get();
    String json = camelCatalog.componentJSonSchema(componentName);
    if (json == null) {
        return null;
    }
    ComponentModel component = JsonMapper.generateComponentModel(json);
    // camel catalog expects &amp; as & when it parses so replace all &amp; as &
    String camelQuery = val;
    camelQuery = camelQuery.replaceAll("&amp;", "&");
    // strip up ending incomplete parameter
    if (camelQuery.endsWith("&") || camelQuery.endsWith("?")) {
        camelQuery = camelQuery.substring(0, camelQuery.length() - 1);
    }
    Map<String, String> existing = null;
    try {
        existing = camelCatalog.endpointProperties(camelQuery);
    } catch (Throwable e) {
        LOG.warn("Error parsing Camel endpoint properties with url: " + camelQuery, e);
    }
    StringBuilder options = new StringBuilder();
    if (existing != null && !existing.isEmpty()) {
        JsonObject jsonObject;
        try {
            jsonObject = (JsonObject) Jsoner.deserialize(json);
        } catch (DeserializationException e) {
            throw new RuntimeException(e);
        }
        Map<String, JsonObject> properties = jsonObject.getMap("properties");
        for (Map.Entry<String, String> entry : existing.entrySet()) {
            String name = entry.getKey();
            String value = entry.getValue();
            JsonObject row = properties.get(name);
            if (row != null) {
                String kind = row.getString("kind");
                String deprecated = row.getString("deprecated");
                String line;
                if ("path".equals(kind)) {
                    line = value + "<br/>";
                } else {
                    if ("true".equals(deprecated)) {
                        line = "<s>" + name + "</s>=" + value + "<br/>";
                    } else {
                        line = name + "=" + value + "<br/>";
                    }
                }
                options.append("<br/>");
                options.append("<b>").append(line).append("</b>");
                String summary = row.getString("description");
                // the text looks a bit weird when using single /
                summary = summary.replace('/', ' ');
                options.append(wrapText(summary, wrapLength)).append("<br/>");
            }
        }
    }
    // append any lenient options as well
    Map<String, String> extra = null;
    try {
        extra = camelCatalog.endpointLenientProperties(camelQuery);
    } catch (Throwable e) {
        LOG.warn("Error parsing Camel endpoint properties with url: " + camelQuery, e);
    }
    if (extra != null && !extra.isEmpty()) {
        for (Map.Entry<String, String> entry : extra.entrySet()) {
            String name = entry.getKey();
            String value = entry.getValue();
            String line = name + "=" + value + "<br/>";
            options.append("<br/>");
            options.append("<b>").append(line).append("</b>");
            String summary = "This option is a custom option that is not part of the Camel component";
            options.append(wrapText(summary, wrapLength)).append("<br/>");
        }
    }
    StringBuilder sb = new StringBuilder();
    if (component.isDeprecated()) {
        sb.append("<b><s>").append(component.getTitle()).append(" Component (deprecated)</s></b><br/>");
    } else {
        sb.append("<b>").append(component.getTitle()).append(" Component</b><br/>");
    }
    sb.append(wrapText(component.getDescription(), wrapLength)).append("<br/><br/>");
    if (component.getDeprecatedSince() != null) {
        sb.append("<b>Deprecated Since:</b> <tt>").append(component.getDeprecatedSince()).append("</tt><br/>");
    }
    sb.append("<b>Since:</b> <tt>").append(component.getFirstVersionShort()).append("</tt><br/>");
    if (component.getSupportLevel() != null) {
        sb.append("<b>Support Level:</b> <tt>").append(component.getSupportLevel()).append("</tt><br/>");
    }
    String g = component.getGroupId();
    String a = component.getArtifactId();
    String v = component.getVersion();
    if (g != null && a != null && v != null) {
        sb.append("<b>Maven:</b> <tt>").append(g).append(":").append(a).append(":").append(v).append("</tt><br/>");
    }
    sb.append("<b>Syntax:</b> <tt>").append(component.getSyntax()).append("?options</tt><br/>");
    sb.append("<p/>");
    sb.append("<br/>");
    // indent the endpoint url with 5 spaces and wrap it by url separator
    String wrapped = wrapSeparator(val, "&", "<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", 100);
    sb.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>").append(wrapped).append("</b><br/>");
    if (options.length() > 0) {
        sb.append(options);
    }
    return sb.toString();
}
Also used : CamelCatalog(org.apache.camel.catalog.CamelCatalog) JsonObject(org.apache.camel.util.json.JsonObject) DeserializationException(org.apache.camel.util.json.DeserializationException) CamelCatalogService(com.github.cameltooling.idea.service.CamelCatalogService) ComponentModel(org.apache.camel.tooling.model.ComponentModel) Map(java.util.Map)

Example 5 with ComponentModel

use of org.apache.camel.tooling.model.ComponentModel in project camel-idea-plugin by camel-tooling.

the class CamelSmartCompletionEndpointOptions method addSmartCompletionSuggestionsQueryParameters.

public static List<LookupElement> addSmartCompletionSuggestionsQueryParameters(final String[] query, final ComponentModel component, final Map<String, String> existing, final boolean xmlMode, final PsiElement element, final Editor editor) {
    final List<LookupElement> answer = new ArrayList<>();
    String queryAtPosition = query[2];
    if (xmlMode) {
        queryAtPosition = queryAtPosition.replace("&amp;", "&");
    }
    final List<ComponentModel.EndpointOptionModel> options = component.getEndpointOptions();
    // sort the options A..Z which is easier to users to understand
    options.sort((o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName()));
    queryAtPosition = removeUnknownOption(queryAtPosition, existing, element);
    for (final ComponentModel.EndpointOptionModel option : options) {
        if ("parameter".equals(option.getKind())) {
            final String name = option.getName();
            // if we are consumer only, then any option that has producer in the label should be skipped (as its only for producer)
            final boolean consumerOnly = getCamelIdeaUtils().isConsumerEndpoint(element);
            if (consumerOnly && option.getLabel() != null && option.getLabel().contains("producer")) {
                continue;
            }
            // if we are producer only, then any option that has consume in the label should be skipped (as its only for consumer)
            final boolean producerOnly = getCamelIdeaUtils().isProducerEndpoint(element);
            if (producerOnly && option.getLabel() != null && option.getLabel().contains("consumer")) {
                continue;
            }
            // only add if not already used (or if the option is multi valued then it can have many)
            final String old = existing != null ? existing.get(name) : "";
            if (option.isMultiValue() || existing == null || old == null || old.isEmpty()) {
                // no tail for prefix, otherwise use = to setup for value
                final String key = option.getPrefix() != null ? option.getPrefix() : name;
                // the lookup should prepare for the new option
                String lookup;
                final String concatQuery = query[0];
                if (!concatQuery.contains("?")) {
                    // none existing options so we need to start with a ? mark
                    lookup = queryAtPosition + "?" + key;
                } else {
                    if (!queryAtPosition.endsWith("&") && !queryAtPosition.endsWith("?")) {
                        lookup = queryAtPosition + "&" + key;
                    } else {
                        // there is already either an ending ? or &
                        lookup = queryAtPosition + key;
                    }
                }
                if (xmlMode) {
                    lookup = lookup.replace("&", "&amp;");
                }
                LookupElementBuilder builder = LookupElementBuilder.create(lookup);
                final String suffix = query[1];
                builder = addInsertHandler(editor, builder, suffix);
                // only show the option in the UI
                builder = builder.withPresentableText(name);
                // we don't want to highlight the advanced options which should be more seldom in use
                final boolean advanced = option.getGroup().contains("advanced");
                builder = builder.withBoldness(!advanced);
                if (!option.getJavaType().isEmpty()) {
                    builder = builder.withTypeText(option.getJavaType(), true);
                }
                if (option.isDeprecated()) {
                    // mark as deprecated
                    builder = builder.withStrikeoutness(true);
                }
                // add icons for various options
                if (option.isRequired()) {
                    builder = builder.withIcon(AllIcons.Toolwindows.ToolWindowFavorites);
                } else if (option.isSecret()) {
                    builder = builder.withIcon(AllIcons.Nodes.SecurityRole);
                } else if (option.isMultiValue()) {
                    builder = builder.withIcon(AllIcons.General.ArrowRight);
                } else if (option.getEnums() != null) {
                    builder = builder.withIcon(AllIcons.Nodes.Enum);
                } else if ("object".equals(option.getType())) {
                    builder = builder.withIcon(AllIcons.Nodes.Class);
                }
                answer.add(builder.withAutoCompletionPolicy(AutoCompletionPolicy.GIVE_CHANCE_TO_OVERWRITE));
            }
        }
    }
    return answer;
}
Also used : ArrayList(java.util.ArrayList) ComponentModel(org.apache.camel.tooling.model.ComponentModel) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PrioritizedLookupElement(com.intellij.codeInsight.completion.PrioritizedLookupElement)

Aggregations

ComponentModel (org.apache.camel.tooling.model.ComponentModel)15 CamelCatalog (org.apache.camel.catalog.CamelCatalog)7 CamelCatalogService (com.github.cameltooling.idea.service.CamelCatalogService)6 IOException (java.io.IOException)4 LookupElement (com.intellij.codeInsight.lookup.LookupElement)3 PsiElement (com.intellij.psi.PsiElement)3 Map (java.util.Map)3 DataFormatModel (org.apache.camel.tooling.model.DataFormatModel)3 CamelIdeaUtils (com.github.cameltooling.idea.util.CamelIdeaUtils)2 IdeaUtils (com.github.cameltooling.idea.util.IdeaUtils)2 PrioritizedLookupElement (com.intellij.codeInsight.completion.PrioritizedLookupElement)2 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)2 ServiceManager (com.intellij.openapi.components.ServiceManager)2 Editor (com.intellij.openapi.editor.Editor)2 Project (com.intellij.openapi.project.Project)2 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 LanguageModel (org.apache.camel.tooling.model.LanguageModel)2 OtherModel (org.apache.camel.tooling.model.OtherModel)2