Search in sources :

Example 6 with ComponentModel

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

the class CamelAddEndpointIntention method findCamelComponentNamesInArtifact.

private static List<String> findCamelComponentNamesInArtifact(Set<String> artifactIds, boolean consumerOnly, Project project) {
    List<String> names = new ArrayList<>();
    CamelCatalog camelCatalog = ServiceManager.getService(project, CamelCatalogService.class).get();
    for (String name : camelCatalog.findComponentNames()) {
        String json = camelCatalog.componentJSonSchema(name);
        ComponentModel model = JsonMapper.generateComponentModel(json);
        if (artifactIds.contains(model.getArtifactId())) {
            boolean onlyConsume = model.isConsumerOnly();
            boolean onlyProduce = model.isProducerOnly();
            boolean both = !onlyConsume && !onlyProduce;
            if (both) {
                names.add(name);
            } else if (consumerOnly && onlyConsume) {
                names.add(name);
            } else if (!consumerOnly && onlyProduce) {
                names.add(name);
            }
        }
    }
    // sort
    Collections.sort(names);
    return names;
}
Also used : CamelCatalog(org.apache.camel.catalog.CamelCatalog) CamelCatalogService(com.github.cameltooling.idea.service.CamelCatalogService) ArrayList(java.util.ArrayList) ComponentModel(org.apache.camel.tooling.model.ComponentModel)

Example 7 with ComponentModel

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

the class CqCatalog method serialize.

static void serialize(final Path catalogPath, ArtifactModel<?> model) {
    final Path out = catalogPath.resolve(model.getKind() + "s").resolve(model.getName() + ".json");
    try {
        Files.createDirectories(out.getParent());
    } catch (IOException e) {
        throw new RuntimeException("Could not create " + out.getParent(), e);
    }
    String rawJson;
    switch(Kind.valueOf(model.getKind())) {
        case component:
            rawJson = JsonMapper.createParameterJsonSchema((ComponentModel) model);
            break;
        case language:
            rawJson = JsonMapper.createParameterJsonSchema((LanguageModel) model);
            break;
        case dataformat:
            rawJson = JsonMapper.createParameterJsonSchema((DataFormatModel) model);
            break;
        case other:
            rawJson = JsonMapper.createJsonSchema((OtherModel) model);
            break;
        default:
            throw new IllegalStateException("Cannot serialize kind " + model.getKind());
    }
    try {
        Files.write(out, rawJson.getBytes(StandardCharsets.UTF_8));
    } catch (IOException e) {
        throw new RuntimeException("Could not write to " + out, e);
    }
}
Also used : Path(java.nio.file.Path) DataFormatModel(org.apache.camel.tooling.model.DataFormatModel) ComponentModel(org.apache.camel.tooling.model.ComponentModel) LanguageModel(org.apache.camel.tooling.model.LanguageModel) IOException(java.io.IOException) OtherModel(org.apache.camel.tooling.model.OtherModel)

Example 8 with ComponentModel

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

the class PrepareCatalogQuarkusMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    final Path catalogPath = catalogBaseDir.toPath().resolve(CqCatalog.CQ_CATALOG_DIR);
    final Map<String, Set<String>> schemesByKind = new LinkedHashMap<>();
    CqCatalog.kinds().forEach(kind -> schemesByKind.put(kind.name(), new TreeSet<>()));
    final CqCatalog catalog = CqCatalog.findFirstFromClassPath();
    if (extendClassPathCatalog) {
        catalog.store(catalogBaseDir.toPath());
        catalog.models().forEach(model -> schemesByKind.get(model.getKind()).add(model.getName()));
    }
    findExtensions().forEach(ext -> {
        final String artifactIdBase = ext.getArtifactIdBase();
        final Path schemaFile = ext.getExtensionDir().resolve("component/src/generated/resources/org/apache/camel/component/" + artifactIdBase + "/" + artifactIdBase + ".json").toAbsolutePath().normalize();
        if (Files.isRegularFile(schemaFile)) {
            try {
                final String schema = new String(Files.readAllBytes(schemaFile), StandardCharsets.UTF_8);
                final String capBase = artifactIdBase.substring(0, 1).toUpperCase() + artifactIdBase.substring(1);
                getLog().debug("Adding an extra component " + artifactIdBase + " " + "org.apache.camel.component." + artifactIdBase + "." + capBase + "Component " + schema);
                catalog.addComponent(artifactIdBase, "org.apache.camel.component." + artifactIdBase + "." + capBase + "Component", schema);
            } catch (IOException e) {
                throw new RuntimeException("Could not read " + schemaFile, e);
            }
        }
    });
    findExtensions().forEach(extPath -> {
        final String artifactIdBase = extPath.getArtifactIdBase();
        final List<ArtifactModel<?>> models = catalog.filterModels(artifactIdBase).collect(Collectors.toList());
        final Path runtimePomXmlPath = extPath.getExtensionDir().resolve("runtime/pom.xml").toAbsolutePath().normalize();
        final CamelQuarkusExtension ext = CamelQuarkusExtension.read(runtimePomXmlPath);
        final boolean nativeSupported = ext.isNativeSupported();
        if (models.isEmpty()) {
            final ArtifactModel<?> model;
            final Kind extKind = ext.getKind();
            if (extKind == Kind.component) {
                model = new ComponentModel();
            } else if (extKind == Kind.language) {
                model = new LanguageModel();
            } else if (extKind == Kind.dataformat) {
                model = new DataFormatModel();
            } else {
                model = new OtherModel();
            }
            final String name = ext.getRuntimeArtifactId().replace("camel-quarkus-", "");
            model.setName(name);
            final String title = ext.getName().orElseThrow(() -> new RuntimeException("name is missing in " + ext.getRuntimePomXmlPath()));
            model.setTitle(title);
            model.setDescription(ext.getDescription().orElseThrow(() -> new RuntimeException("description is missing in " + ext.getRuntimePomXmlPath())));
            model.setDeprecated(CqUtils.isDeprecated(title, models, ext.isDeprecated()));
            model.setLabel(ext.getLabel().orElse("quarkus"));
            update(model, ext, nativeSupported);
            CqCatalog.serialize(catalogPath, model);
            schemesByKind.get(model.getKind()).add(model.getName());
        } else {
            for (ArtifactModel<?> model : models) {
                update(model, ext, nativeSupported);
                CqCatalog.serialize(catalogPath, model);
                schemesByKind.get(model.getKind()).add(model.getName());
            }
        }
    });
    CqCatalog.kinds().forEach(kind -> {
        final Path newCatalog = catalogPath.resolve(kind.name() + "s.properties");
        try {
            Files.createDirectories(newCatalog.getParent());
            Files.write(newCatalog, schemesByKind.get(kind.name()).stream().collect(Collectors.joining("\n")).getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            throw new RuntimeException("Could not write to " + newCatalog);
        }
    });
}
Also used : Path(java.nio.file.Path) Set(java.util.Set) TreeSet(java.util.TreeSet) IOException(java.io.IOException) OtherModel(org.apache.camel.tooling.model.OtherModel) LinkedHashMap(java.util.LinkedHashMap) DataFormatModel(org.apache.camel.tooling.model.DataFormatModel) ArtifactModel(org.apache.camel.tooling.model.ArtifactModel) TreeSet(java.util.TreeSet) Kind(org.apache.camel.catalog.Kind) ComponentModel(org.apache.camel.tooling.model.ComponentModel) LanguageModel(org.apache.camel.tooling.model.LanguageModel)

Example 9 with ComponentModel

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

the class CamelDocumentationProvider method getDocumentationElementForLookupItem.

@Nullable
@Override
public PsiElement getDocumentationElementForLookupItem(PsiManager psiManager, Object object, PsiElement element) {
    // we only support literal - string types where Camel endpoints can be specified
    if (!(object instanceof String)) {
        return null;
    }
    String lookup = object.toString();
    // must be a Camel component
    String componentName = asComponentName(lookup);
    if (componentName == null) {
        return null;
    }
    // unescape xml &
    lookup = lookup.replaceAll("&amp;", "&");
    // get last option from lookup line
    int pos = Math.max(lookup.lastIndexOf("&"), lookup.lastIndexOf("?"));
    if (pos > 0) {
        String option = lookup.substring(pos + 1);
        // if the option has a value then drop that
        pos = option.indexOf("=");
        if (pos != -1) {
            option = option.substring(0, pos);
        }
        LOG.debug("getDocumentationElementForLookupItem: " + option);
        // find its real option name and documentation which we want to show in the quick doc window
        if (option.endsWith(".")) {
            CamelCatalog camelCatalog = ServiceManager.getService(psiManager.getProject(), CamelCatalogService.class).get();
            String json = camelCatalog.componentJSonSchema(componentName);
            if (json == null) {
                return null;
            }
            ComponentModel component = JsonMapper.generateComponentModel(json);
            final String prefixOption = option;
            // find the line with this prefix as prefix and multivalue
            ComponentModel.EndpointOptionModel endpointOption = component.getEndpointOptions().stream().filter(o -> o.isMultiValue() && prefixOption.equals(o.getPrefix())).findFirst().orElse(null);
            // use the real option name instead of the prefix
            if (endpointOption != null) {
                option = endpointOption.getName();
            }
        }
        return new DocumentationElement(psiManager, element.getLanguage(), element, option, componentName);
    }
    return null;
}
Also used : CamelCatalog(org.apache.camel.catalog.CamelCatalog) CamelCatalogService(com.github.cameltooling.idea.service.CamelCatalogService) ComponentModel(org.apache.camel.tooling.model.ComponentModel) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with ComponentModel

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

the class CamelDocumentationProvider method generateCamelEndpointOptionDocumentation.

/**
 * Generates documentation for the endpoint option.
 * @param componentName the name of the Camel component
 * @param option the name of the Camel component option to generate documentation for
 * @param project the current project
 * @return a String representing the HTML documentation
 */
private String generateCamelEndpointOptionDocumentation(String componentName, String option, Project project) {
    CamelCatalog camelCatalog = ServiceManager.getService(project, CamelCatalogService.class).get();
    String json = camelCatalog.componentJSonSchema(componentName);
    if (json == null) {
        return null;
    }
    ComponentModel component = JsonMapper.generateComponentModel(json);
    ComponentModel.EndpointOptionModel endpointOption;
    if (option.endsWith(".")) {
        // find the line with this prefix as prefix and multivalue
        endpointOption = component.getEndpointOptions().stream().filter(o -> o.isMultiValue() && option.equals(o.getPrefix())).findFirst().orElse(null);
    } else {
        endpointOption = component.getEndpointOptions().stream().filter(o -> option.equals(o.getName())).findFirst().orElse(null);
    }
    if (endpointOption == null) {
        return null;
    }
    StringBuilder builder = new StringBuilder();
    if (endpointOption.isDeprecated()) {
        builder.append("<strong><s>").append(endpointOption.getName()).append("</s></strong><br/><br/>");
    } else {
        builder.append("<strong>").append(endpointOption.getName()).append("</strong><br/><br/>");
    }
    builder.append("<strong>Group: </strong>").append(endpointOption.getGroup()).append("<br/>");
    builder.append("<strong>Type: </strong>").append("<tt>").append(endpointOption.getJavaType()).append("</tt>").append("<br/>");
    boolean required = endpointOption.isRequired();
    builder.append("<strong>Required: </strong>").append(required).append("<br/>");
    if (endpointOption.getEnums() != null) {
        String values = String.join(", ", endpointOption.getEnums());
        builder.append("<strong>Possible values: </strong>").append(values).append("<br/>");
    }
    if (endpointOption.getDefaultValue() != null) {
        builder.append("<strong>Default value: </strong>").append(endpointOption.getDefaultValue()).append("<br/>");
    }
    builder.append("<br/><div>").append(endpointOption.getDescription()).append("</div>");
    return builder.toString();
}
Also used : Language(com.intellij.lang.Language) PsiTypeParameterList(com.intellij.psi.PsiTypeParameterList) IdeaUtils(com.github.cameltooling.idea.util.IdeaUtils) PsiType(com.intellij.psi.PsiType) Arrays(java.util.Arrays) Application(com.intellij.openapi.application.Application) StringUtils.asLanguageName(com.github.cameltooling.idea.util.StringUtils.asLanguageName) CamelIdeaUtils(com.github.cameltooling.idea.util.CamelIdeaUtils) PsiMethodCallExpression(com.intellij.psi.PsiMethodCallExpression) XmlToken(com.intellij.psi.xml.XmlToken) PsiExpressionList(com.intellij.psi.PsiExpressionList) PsiManager(com.intellij.psi.PsiManager) ComponentModel(org.apache.camel.tooling.model.ComponentModel) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) DocumentationProviderEx(com.intellij.lang.documentation.DocumentationProviderEx) Map(java.util.Map) XmlTokenType(com.intellij.psi.xml.XmlTokenType) StringUtils.wrapWords(com.github.cameltooling.idea.util.StringUtils.wrapWords) Logger(com.intellij.openapi.diagnostic.Logger) Jsoner(org.apache.camel.util.json.Jsoner) JsonObject(org.apache.camel.util.json.JsonObject) BrowserUtil(com.intellij.ide.BrowserUtil) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ServiceManager(com.intellij.openapi.components.ServiceManager) ApplicationManager(com.intellij.openapi.application.ApplicationManager) NotNull(org.jetbrains.annotations.NotNull) ExternalDocumentationHandler(com.intellij.lang.documentation.ExternalDocumentationHandler) CamelService(com.github.cameltooling.idea.service.CamelService) ExternalDocumentationProvider(com.intellij.lang.documentation.ExternalDocumentationProvider) Computable(com.intellij.openapi.util.Computable) PsiExpression(com.intellij.psi.PsiExpression) PsiClassType(com.intellij.psi.PsiClassType) CamelCatalogService(com.github.cameltooling.idea.service.CamelCatalogService) PsiClass(com.intellij.psi.PsiClass) CamelCatalog(org.apache.camel.catalog.CamelCatalog) PsiClassReferenceType(com.intellij.psi.impl.source.PsiClassReferenceType) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) PsiMethod(com.intellij.psi.PsiMethod) LightElement(com.intellij.psi.impl.light.LightElement) StringUtils.asComponentName(com.github.cameltooling.idea.util.StringUtils.asComponentName) Editor(com.intellij.openapi.editor.Editor) ASTNode(com.intellij.lang.ASTNode) JsonMapper(org.apache.camel.tooling.model.JsonMapper) DeserializationException(org.apache.camel.util.json.DeserializationException) StringUtils.wrapSeparator(com.github.cameltooling.idea.util.StringUtils.wrapSeparator) PsiLiteralExpression(com.intellij.psi.PsiLiteralExpression) CamelCatalog(org.apache.camel.catalog.CamelCatalog) CamelCatalogService(com.github.cameltooling.idea.service.CamelCatalogService) ComponentModel(org.apache.camel.tooling.model.ComponentModel)

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