Search in sources :

Example 1 with LanguageValidationResult

use of org.apache.camel.catalog.LanguageValidationResult in project camel-idea-plugin by camel-tooling.

the class CamelJSonPathAnnotator method validateText.

/**
 * Validate jsonpath expression. eg jsonpath("$.store.book[?(@.price < 10)]")
 * if the expression is not valid a error annotation is created and highlight the invalid value.
 */
void validateText(@NotNull PsiElement element, @NotNull AnnotationHolder holder, @NotNull String text) {
    // only validate if the element is jsonpath element
    if (getCamelIdeaUtils().isCamelExpression(element, "jsonpath")) {
        CamelCatalog catalogService = ServiceManager.getService(element.getProject(), CamelCatalogService.class).get();
        CamelService camelService = ServiceManager.getService(element.getProject(), CamelService.class);
        // must have camel-json library
        boolean jsonLib = camelService.containsLibrary("camel-jsonpath", false);
        if (!jsonLib) {
            camelService.showMissingJSonPathJarNotification(element.getProject());
            return;
        }
        try {
            // need to use the classloader that can load classes from the project
            ClassLoader loader = camelService.getProjectClassloader();
            if (loader != null) {
                LanguageValidationResult result;
                boolean predicate = getCamelIdeaUtils().isCamelExpressionUsedAsPredicate(element, "jsonpath");
                if (predicate) {
                    LOG.debug("Inspecting jsonpath predicate: " + text);
                    result = catalogService.validateLanguagePredicate(loader, "jsonpath", text);
                } else {
                    LOG.debug("Inspecting jsonpath expression: " + text);
                    result = catalogService.validateLanguageExpression(loader, "jsonpath", text);
                }
                if (!result.isSuccess()) {
                    String error = result.getShortError();
                    if ("[null]".equals(error)) {
                        return;
                    }
                    if (error == null) {
                        error = result.getError();
                    }
                    TextRange range = element.getTextRange();
                    if (result.getIndex() > 0) {
                        range = getAdjustedTextRange(element, range, text, result);
                    }
                    holder.newAnnotation(HighlightSeverity.ERROR, error).range(range).create();
                }
            }
        } catch (Throwable e) {
            LOG.warn("Error inspecting Camel jsonpath: " + text, e);
        }
    }
}
Also used : CamelCatalog(org.apache.camel.catalog.CamelCatalog) CamelCatalogService(com.github.cameltooling.idea.service.CamelCatalogService) LanguageValidationResult(org.apache.camel.catalog.LanguageValidationResult) CamelService(com.github.cameltooling.idea.service.CamelService) TextRange(com.intellij.openapi.util.TextRange)

Example 2 with LanguageValidationResult

use of org.apache.camel.catalog.LanguageValidationResult in project camel-idea-plugin by camel-tooling.

the class CamelSimpleAnnotator method validateText.

/**
 * Validate simple expression. eg simple("${body}")
 * if the expression is not valid a error annotation is created and highlight the invalid value.
 */
void validateText(@NotNull PsiElement element, @NotNull AnnotationHolder holder, @NotNull String text) {
    // we only want to evaluate if there is a simple function as plain text without functions dont make sense to validate
    boolean hasSimple = text.contains("${") || text.contains("$simple{");
    if (hasSimple && getCamelIdeaUtils().isCamelExpression(element, "simple")) {
        CamelCatalog catalogService = ServiceManager.getService(element.getProject(), CamelCatalogService.class).get();
        CamelService camelService = ServiceManager.getService(element.getProject(), CamelService.class);
        boolean predicate = false;
        try {
            // need to use the classloader that can load classes from the camel-core
            ClassLoader loader = camelService.getCamelCoreClassloader();
            if (loader != null) {
                LanguageValidationResult result;
                predicate = getCamelIdeaUtils().isCamelExpressionUsedAsPredicate(element, "simple");
                if (predicate) {
                    LOG.debug("Validate simple predicate: " + text);
                    result = catalogService.validateLanguagePredicate(loader, "simple", text);
                } else {
                    LOG.debug("Validate simple expression: " + text);
                    result = catalogService.validateLanguageExpression(loader, "simple", text);
                }
                if (!result.isSuccess()) {
                    String error = result.getShortError();
                    if ("[null]".equals(error)) {
                        return;
                    }
                    TextRange range = element.getTextRange();
                    if (result.getIndex() > 0) {
                        range = getAdjustedTextRange(element, range, text, result);
                    }
                    holder.newAnnotation(HighlightSeverity.ERROR, error).range(range).create();
                }
            }
        } catch (Throwable e) {
            LOG.warn("Error validating Camel simple " + (predicate ? "predicate" : "expression") + ": " + text, e);
        }
    }
}
Also used : CamelCatalog(org.apache.camel.catalog.CamelCatalog) CamelCatalogService(com.github.cameltooling.idea.service.CamelCatalogService) LanguageValidationResult(org.apache.camel.catalog.LanguageValidationResult) CamelService(com.github.cameltooling.idea.service.CamelService) TextRange(com.intellij.openapi.util.TextRange)

Example 3 with LanguageValidationResult

use of org.apache.camel.catalog.LanguageValidationResult in project camel-idea-plugin by camel-tooling.

the class AbstractCamelInspection method validateJSonPath.

private void validateJSonPath(@NotNull PsiElement element, @NotNull final ProblemsHolder holder, @NotNull String text, boolean isOnTheFly) {
    CamelCatalog catalogService = ServiceManager.getService(element.getProject(), CamelCatalogService.class).get();
    CamelService camelService = ServiceManager.getService(element.getProject(), CamelService.class);
    IElementType type = element.getNode().getElementType();
    LOG.trace("Element " + element + " of type: " + type + " to inspect jsonpath: " + text);
    // must have camel-json library
    boolean jsonLib = camelService.containsLibrary("camel-jsonpath", false);
    if (!jsonLib) {
        return;
    }
    try {
        // need to use the classloader that can load classes from the project
        ClassLoader loader = camelService.getProjectClassloader();
        if (loader != null) {
            LanguageValidationResult result;
            boolean predicate = getCamelIdeaUtils().isCamelExpressionUsedAsPredicate(element, "jsonpath");
            if (predicate) {
                LOG.debug("Inspecting jsonpath predicate: " + text);
                result = catalogService.validateLanguagePredicate(loader, "jsonpath", text);
            } else {
                LOG.debug("Inspecting jsonpath expression: " + text);
                result = catalogService.validateLanguageExpression(loader, "jsonpath", text);
            }
            if (!result.isSuccess()) {
                // favor the short error message
                String msg = result.getShortError();
                if (msg == null) {
                    msg = result.getError();
                }
                if ("[null]".equals(msg)) {
                    return;
                }
                holder.registerProblem(element, msg);
            }
        }
    } catch (Throwable e) {
        LOG.warn("Error inspection Camel jsonpath: " + text, e);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) CamelCatalog(org.apache.camel.catalog.CamelCatalog) CamelCatalogService(com.github.cameltooling.idea.service.CamelCatalogService) LanguageValidationResult(org.apache.camel.catalog.LanguageValidationResult) CamelService(com.github.cameltooling.idea.service.CamelService)

Example 4 with LanguageValidationResult

use of org.apache.camel.catalog.LanguageValidationResult in project camel-idea-plugin by camel-tooling.

the class AbstractCamelInspection method validateSimple.

private void validateSimple(@NotNull PsiElement element, @NotNull final ProblemsHolder holder, @NotNull String text, boolean isOnTheFly) {
    CamelCatalog catalogService = ServiceManager.getService(element.getProject(), CamelCatalogService.class).get();
    CamelService camelService = ServiceManager.getService(element.getProject(), CamelService.class);
    IElementType type = element.getNode().getElementType();
    LOG.trace("Element " + element + " of type: " + type + " to inspect simple: " + text);
    try {
        // need to use the classloader that can load classes from the camel-core
        ClassLoader loader = camelService.getCamelCoreClassloader();
        if (loader != null) {
            LanguageValidationResult result;
            boolean predicate = getCamelIdeaUtils().isCamelExpressionUsedAsPredicate(element, "simple");
            if (predicate) {
                LOG.debug("Inspecting simple predicate: " + text);
                result = catalogService.validateLanguagePredicate(loader, "simple", text);
            } else {
                LOG.debug("Inspecting simple expression: " + text);
                result = catalogService.validateLanguageExpression(loader, "simple", text);
            }
            if (!result.isSuccess()) {
                // favor the short error message
                String msg = result.getShortError();
                if (msg == null) {
                    msg = result.getError();
                }
                if ("[null]".equals(msg)) {
                    return;
                }
                holder.registerProblem(element, msg);
            }
        }
    } catch (Throwable e) {
        LOG.warn("Error inspection Camel simple: " + text, e);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) CamelCatalog(org.apache.camel.catalog.CamelCatalog) CamelCatalogService(com.github.cameltooling.idea.service.CamelCatalogService) LanguageValidationResult(org.apache.camel.catalog.LanguageValidationResult) CamelService(com.github.cameltooling.idea.service.CamelService)

Aggregations

CamelCatalogService (com.github.cameltooling.idea.service.CamelCatalogService)4 CamelService (com.github.cameltooling.idea.service.CamelService)4 CamelCatalog (org.apache.camel.catalog.CamelCatalog)4 LanguageValidationResult (org.apache.camel.catalog.LanguageValidationResult)4 TextRange (com.intellij.openapi.util.TextRange)2 IElementType (com.intellij.psi.tree.IElementType)2