Search in sources :

Example 11 with ComponentModel

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

the class CamelDocumentationProvider method externalUrl.

private static String externalUrl(Project project, String val) {
    String url = null;
    String name = asComponentName(val);
    CamelCatalog camelCatalog = ServiceManager.getService(project, CamelCatalogService.class).get();
    if (name != null && camelCatalog.findComponentNames().contains(name)) {
        String json = camelCatalog.componentJSonSchema(name);
        ComponentModel component = JsonMapper.generateComponentModel(json);
        String version = component.getVersion();
        if (version.startsWith("2")) {
            version = "2.x";
        } else if (version.startsWith("3.4")) {
            // LTS
            version = "3.4.x";
        } else if (version.startsWith("3.7")) {
            // LTS
            version = "3.7.x";
        } else {
            version = "latest";
        }
        if ("other".equals(component.getKind())) {
            url = String.format("https://camel.apache.org/components/%s/others/%s.html", version, component.getName());
        } else if ("component".equals(component.getKind())) {
            url = String.format("https://camel.apache.org/components/%s/%s-component.html", version, component.getScheme());
        } else {
            url = String.format("https://camel.apache.org/components/%s/%ss/%s-%s.html", version, component.getKind(), component.getName(), component.getKind());
        }
    }
    return url;
}
Also used : CamelCatalog(org.apache.camel.catalog.CamelCatalog) CamelCatalogService(com.github.cameltooling.idea.service.CamelCatalogService) ComponentModel(org.apache.camel.tooling.model.ComponentModel)

Example 12 with ComponentModel

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

the class CamelSmartCompletionEndpointOptions method addSmartCompletionContextPathEnumSuggestions.

private static List<LookupElement> addSmartCompletionContextPathEnumSuggestions(final String val, final ComponentModel component, final Map<String, String> existing) {
    final List<LookupElement> answer = new ArrayList<>();
    double priority = 100.0d;
    // lets help the suggestion list if we are editing the context-path and only have 1 enum type option
    // and the option has not been in use yet, then we can populate the list with the enum values.
    final long enums = component.getEndpointOptions().stream().filter(o -> "path".equals(o.getKind()) && o.getEnums() != null).count();
    if (enums == 1) {
        for (final ComponentModel.EndpointOptionModel option : component.getEndpointOptions()) {
            // only add support for enum in the context-path smart completion
            if ("path".equals(option.getKind()) && option.getEnums() != null) {
                final String name = option.getName();
                // only add if not already used
                final String old = existing != null ? existing.get(name) : "";
                if (existing == null || old == null || old.isEmpty()) {
                    // add all enum as choices
                    for (final String choice : option.getEnums()) {
                        final String key = choice;
                        final String lookup = val + key;
                        LookupElementBuilder builder = LookupElementBuilder.create(lookup);
                        // only show the option in the UI
                        builder = builder.withPresentableText(choice);
                        // lets use the option name as the type so its visible
                        builder = builder.withTypeText(name, true);
                        builder = builder.withIcon(AllIcons.Nodes.Enum);
                        if (option.isDeprecated()) {
                            // mark as deprecated
                            builder = builder.withStrikeoutness(true);
                        }
                        // its an enum so always auto complete the choices
                        LookupElement element = builder.withAutoCompletionPolicy(AutoCompletionPolicy.ALWAYS_AUTOCOMPLETE);
                        // they should be in the exact order
                        element = PrioritizedLookupElement.withPriority(element, priority);
                        priority -= 1.0d;
                        answer.add(element);
                    }
                }
            }
        }
    }
    return answer;
}
Also used : IdeaUtils(com.github.cameltooling.idea.util.IdeaUtils) EditorModificationUtil(com.intellij.openapi.editor.EditorModificationUtil) Lookup(com.intellij.codeInsight.lookup.Lookup) AllIcons(com.intellij.icons.AllIcons) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) LookupElement(com.intellij.codeInsight.lookup.LookupElement) CamelIdeaUtils(com.github.cameltooling.idea.util.CamelIdeaUtils) PrioritizedLookupElement(com.intellij.codeInsight.completion.PrioritizedLookupElement) Editor(com.intellij.openapi.editor.Editor) ArrayList(java.util.ArrayList) AutoCompletionPolicy(com.intellij.codeInsight.lookup.AutoCompletionPolicy) ComponentModel(org.apache.camel.tooling.model.ComponentModel) List(java.util.List) ServiceManager(com.intellij.openapi.components.ServiceManager) CamelPreferenceService(com.github.cameltooling.idea.service.CamelPreferenceService) Map(java.util.Map) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull) PropertiesFileImpl(com.intellij.lang.properties.psi.impl.PropertiesFileImpl) 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)

Example 13 with ComponentModel

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

the class CamelEndpointSmartCompletionExtension method addCompletions.

@Override
public void addCompletions(@NotNull CompletionParameters parameters, @NotNull ProcessingContext context, @NotNull CompletionResultSet resultSet, @NotNull String[] query) {
    boolean endsWithAmpQuestionMark = false;
    // it is a known Camel component
    String componentName = StringUtils.asComponentName(query[0]);
    // it is a known Camel component
    Project project = parameters.getOriginalFile().getManager().getProject();
    CamelCatalog camelCatalog = ServiceManager.getService(project, CamelCatalogService.class).get();
    String json = camelCatalog.componentJSonSchema(componentName);
    ComponentModel componentModel = JsonMapper.generateComponentModel(json);
    final PsiElement element = parameters.getPosition();
    // grab all existing parameters
    String concatQuery = query[0];
    String suffix = query[1];
    String queryAtPosition = query[2];
    String prefixValue = query[2];
    // camel catalog expects &amp; as & when it parses so replace all &amp; as &
    concatQuery = concatQuery.replaceAll("&amp;", "&");
    boolean editQueryParameters = concatQuery.contains("?");
    // strip up ending incomplete parameter
    if (queryAtPosition.endsWith("&") || queryAtPosition.endsWith("?")) {
        endsWithAmpQuestionMark = true;
        queryAtPosition = queryAtPosition.substring(0, queryAtPosition.length() - 1);
    }
    // strip up ending incomplete parameter
    if (concatQuery.endsWith("&") || concatQuery.endsWith("?")) {
        concatQuery = concatQuery.substring(0, concatQuery.length() - 1);
    }
    Map<String, String> existing = null;
    try {
        existing = camelCatalog.endpointProperties(concatQuery);
    } catch (Exception e) {
        LOG.warn("Error parsing Camel endpoint properties with url: " + queryAtPosition, e);
    }
    // are we editing an existing parameter value
    // or are we having a list of suggested parameters to choose among
    boolean caretAtEndOfLine = getIdeaUtils().isCaretAtEndOfLine(element);
    LOG.trace("Caret at end of line: " + caretAtEndOfLine);
    String[] queryParameter = getIdeaUtils().getQueryParameterAtCursorPosition(element);
    String optionValue = queryParameter[1];
    // a bit complex to figure out whether to edit the endpoint value or not
    boolean editOptionValue = false;
    if (endsWithAmpQuestionMark) {
        // should not edit value but suggest a new option instead
        editOptionValue = false;
    } else {
        if ("".equals(optionValue)) {
            // empty value so must edit
            editOptionValue = true;
        } else if (StringUtils.isNotEmpty(optionValue) && !caretAtEndOfLine) {
            // has value and cursor not at end of line so must edit
            editOptionValue = true;
        }
    }
    LOG.trace("Add new option: " + !editOptionValue);
    LOG.trace("Edit option value: " + editOptionValue);
    List<LookupElement> answer = null;
    if (editOptionValue) {
        String name = queryParameter[0].substring(1);
        ComponentModel.EndpointOptionModel endpointOption = componentModel.getEndpointOptions().stream().filter(o -> name.equals(o.getName())).findFirst().orElse(null);
        if (endpointOption != null) {
            answer = addSmartCompletionForEndpointValue(parameters.getEditor(), queryAtPosition, suffix, endpointOption, element, xmlMode);
        }
    }
    if (answer == null) {
        if (editQueryParameters) {
            // suggest a list of options for query parameters
            answer = addSmartCompletionSuggestionsQueryParameters(query, componentModel, existing, xmlMode, element, parameters.getEditor());
        } else {
            if (!resultSet.isStopped()) {
                // suggest a list of options for context-path
                answer = addSmartCompletionSuggestionsContextPath(queryAtPosition, componentModel, existing, xmlMode, element);
            }
        }
    }
    // are there any results then add them
    if (answer != null && !answer.isEmpty()) {
        resultSet.withPrefixMatcher(prefixValue).addAllElements(answer);
        resultSet.stopHere();
    }
}
Also used : CamelCatalog(org.apache.camel.catalog.CamelCatalog) LookupElement(com.intellij.codeInsight.lookup.LookupElement) Project(com.intellij.openapi.project.Project) CamelCatalogService(com.github.cameltooling.idea.service.CamelCatalogService) ComponentModel(org.apache.camel.tooling.model.ComponentModel) PsiElement(com.intellij.psi.PsiElement)

Example 14 with ComponentModel

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

the class SpringBootAutoConfigurationMojo method executeComponents.

private void executeComponents(JarFile componentJar, Map<String, Supplier<String>> jsonFiles) throws MojoFailureException {
    // find the component names
    List<String> componentNames = findComponentNames(componentJar);
    // create auto configuration for the components
    if (!componentNames.isEmpty()) {
        getLog().debug("Found " + componentNames.size() + " components");
        List<ComponentModel> allModels = new LinkedList<>();
        for (String componentName : componentNames) {
            String json = loadComponentJson(jsonFiles, componentName);
            if (json != null) {
                ComponentModel model = JsonMapper.generateComponentModel(json);
                allModels.add(model);
            }
        }
        // Group the models by implementing classes
        Map<String, List<ComponentModel>> grModels = allModels.stream().collect(Collectors.groupingBy(ComponentModel::getJavaType));
        for (String componentClass : grModels.keySet()) {
            List<ComponentModel> compModels = grModels.get(componentClass);
            // They should be
            ComponentModel model = compModels.get(0);
            // equivalent
            List<String> aliases = compModels.stream().map(ComponentModel::getScheme).sorted().collect(Collectors.toList());
            // use springboot as sub package name so the code is not in normal
            // package so the Spring Boot JARs can be optional at runtime
            int pos = model.getJavaType().lastIndexOf(".");
            String pkg = model.getJavaType().substring(0, pos) + ".springboot";
            String overrideComponentName = null;
            if (aliases.size() > 1) {
                // determine component name when there are multiple ones
                overrideComponentName = model.getArtifactId().replace("camel-", "");
            }
            beforeGenerateComponentSource(model);
            boolean complexOptions = model.getOptions().stream().anyMatch(this::isComplexType);
            createComponentConfigurationSource(pkg, model, overrideComponentName);
            createComponentAutoConfigurationSource(pkg, model, overrideComponentName, complexOptions);
            if (complexOptions) {
                createComponentConverterSource(pkg, model);
            }
            createComponentSpringFactorySource(pkg, model);
        }
    }
}
Also used : ComponentModel(org.apache.camel.tooling.model.ComponentModel)

Example 15 with ComponentModel

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

the class CamelKafkaConnectorUpdateMojo method updateConnector.

private void updateConnector() throws Exception {
    String sanitizedName = sanitizeMavenArtifactId(name);
    // create the connector directory
    File connectorDir = new File(projectDir, "camel-" + sanitizedName + KAFKA_CONNECTORS_SUFFIX);
    if (!connectorDir.exists() || !connectorDir.isDirectory()) {
        getLog().info("Connector " + name + " can not be updated since directory " + connectorDir.getAbsolutePath() + " dose not exist.");
        throw new MojoFailureException("Directory already exists: " + connectorDir);
    }
    // create the base pom.xml
    Document pom = createBasePom(connectorDir);
    // Apply changes to the connector pom
    fixExcludedDependencies(pom);
    fixAdditionalDependencies(pom, additionalDependencies);
    fixAdditionalRepositories(pom);
    // Write the connector pom
    File pomFile = new File(connectorDir, "pom.xml");
    writeXmlFormatted(pom, pomFile, getLog());
    // write package
    Document pkg = createPackageFile();
    File pkgFile = new File(connectorDir, "src/main/assembly/package.xml");
    writeXmlFormatted(pkg, pkgFile, getLog());
    // write LICENSE, USAGE
    writeStaticFiles(connectorDir);
    // generate classes
    ComponentModel model = JsonMapper.generateComponentModel(componentJson);
    if (model.isConsumerOnly()) {
        createClasses(sanitizedName, connectorDir, model, ConnectorType.SOURCE);
    } else if (model.isProducerOnly()) {
        createClasses(sanitizedName, connectorDir, model, ConnectorType.SINK);
    } else {
        createClasses(sanitizedName, connectorDir, model, ConnectorType.SOURCE);
        createClasses(sanitizedName, connectorDir, model, ConnectorType.SINK);
    }
}
Also used : MojoFailureException(org.apache.maven.plugin.MojoFailureException) ComponentModel(org.apache.camel.tooling.model.ComponentModel) Document(org.w3c.dom.Document) File(java.io.File)

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