Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,49 +33,51 @@
import org.jetbrains.annotations.NotNull;

/**
* Validate JSonPath expression and annotated the specific jsonpath expression to highlight the error in the editor
* Validate language expression such as JSonPath/JQ to highlight the error in the editor
*/
public class CamelJSonPathAnnotator extends AbstractCamelAnnotator {
public class CamelLanguageAnnotator extends AbstractCamelAnnotator {

private static final Logger LOG = Logger.getInstance(CamelEndpointAnnotator.class);

@Override
boolean isEnabled() {
return CamelPreferenceService.getService().isRealTimeJSonPathValidation();
return CamelPreferenceService.getService().isRealTimeJSonPathValidation() || CamelPreferenceService.getService().isRealTimeJQValidation();
}

/**
* 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.
* Validate language expression, such as jsonpath("$.store.book[?(@.price < 10)]")
* if the expression is not valid an error annotation is created and highlight the invalid value.
*/
void validateText(@NotNull PsiElement element, @NotNull AnnotationHolder holder, @NotNull String text) {

final CamelIdeaUtils camelIdeaUtils = CamelIdeaUtils.getService();
// only validate if the element is jsonpath element
if (camelIdeaUtils.isCamelExpression(element, "jsonpath")) {

boolean json = CamelPreferenceService.getService().isRealTimeJSonPathValidation() && camelIdeaUtils.isCamelExpression(element, "jsonpath");
boolean jq = CamelPreferenceService.getService().isRealTimeJQValidation() && camelIdeaUtils.isCamelExpression(element, "jq");
if (json || jq) {
Project project = element.getProject();
CamelCatalog catalogService = project.getService(CamelCatalogService.class).get();
CamelService camelService = project.getService(CamelService.class);

// must have camel-json library
boolean jsonLib = camelService.containsLibrary("camel-jsonpath", false);
if (!jsonLib) {
camelService.showMissingJSonPathJarNotification();
// must have the supporting library
String lib = json ? "camel-jsonpath" : "camel-jq";
if (!camelService.containsLibrary(lib, false)) {
camelService.showMissingLanguageJarNotification(lib);
return;
}

String lan = json ? "jsonpath" : "jq";
try {
// need to use the classloader that can load classes from the project
ClassLoader loader = camelService.getProjectClassloader();
if (loader != null) {
LanguageValidationResult result;
boolean predicate = camelIdeaUtils.isCamelExpressionUsedAsPredicate(element, "jsonpath");
boolean predicate = camelIdeaUtils.isCamelExpressionUsedAsPredicate(element, lan);
if (predicate) {
LOG.debug("Inspecting jsonpath predicate: " + text);
result = catalogService.validateLanguagePredicate(loader, "jsonpath", text);
LOG.debug("Inspecting " + lan + " predicate: " + text);
result = catalogService.validateLanguagePredicate(loader, lan, text);
} else {
LOG.debug("Inspecting jsonpath expression: " + text);
result = catalogService.validateLanguageExpression(loader, "jsonpath", text);
LOG.debug("Inspecting " + lan + " expression: " + text);
result = catalogService.validateLanguageExpression(loader, lan, text);
}
if (!result.isSuccess()) {
String error = result.getShortError();
Expand All @@ -95,7 +97,7 @@ void validateText(@NotNull PsiElement element, @NotNull AnnotationHolder holder,
}
}
} catch (Throwable e) {
LOG.warn("Error inspecting Camel jsonpath: " + text, e);
LOG.warn("Error inspecting Camel " + lan + ": " + text, e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class CamelAnnotatorPage implements SearchableConfigurable, Configurable.
private JBCheckBox highlightCustomOptionsCheckBox;
private JBCheckBox realTimeSimpleValidationCatalogCheckBox;
private JBCheckBox realTimeJSonPathValidationCatalogCheckBox;
private JBCheckBox realTimeJQValidationCatalogCheckBox;
private JBCheckBox realTimeIdReferenceTypeValidationCheckBox;
private JBCheckBox realTimeBeanMethodValidationCheckBox;
private JPanel result;
Expand All @@ -52,6 +53,7 @@ public JComponent createComponent() {
highlightCustomOptionsCheckBox = new JBCheckBox("Highlight custom endpoint options as warnings in editor");
realTimeSimpleValidationCatalogCheckBox = new JBCheckBox("Real time validation of Camel simple language in editor");
realTimeJSonPathValidationCatalogCheckBox = new JBCheckBox("Real time validation of Camel JSonPath language in editor");
realTimeJQValidationCatalogCheckBox = new JBCheckBox("Real time validation of Camel JQ language in editor");
realTimeIdReferenceTypeValidationCheckBox = new JBCheckBox("Real time type validation when referencing beans");
realTimeBeanMethodValidationCheckBox = new JBCheckBox("Real time validation of call bean method in editor");

Expand All @@ -63,6 +65,7 @@ public JComponent createComponent() {
jPanel.add(highlightCustomOptionsCheckBox, "span 2");
jPanel.add(realTimeSimpleValidationCatalogCheckBox, "span 2");
jPanel.add(realTimeJSonPathValidationCatalogCheckBox, "span 2");
jPanel.add(realTimeJQValidationCatalogCheckBox, "span 2");
jPanel.add(realTimeIdReferenceTypeValidationCheckBox, "span 2");
jPanel.add(realTimeBeanMethodValidationCheckBox, "span 2");

Expand All @@ -79,6 +82,7 @@ public void apply() {
camelPreferenceService.setHighlightCustomOptions(highlightCustomOptionsCheckBox.isSelected());
camelPreferenceService.setRealTimeSimpleValidation(realTimeSimpleValidationCatalogCheckBox.isSelected());
camelPreferenceService.setRealTimeJSonPathValidation(realTimeJSonPathValidationCatalogCheckBox.isSelected());
camelPreferenceService.setRealTimeJQValidation(realTimeJQValidationCatalogCheckBox.isSelected());
camelPreferenceService.setRealTimeIdReferenceTypeValidation(realTimeIdReferenceTypeValidationCheckBox.isSelected());
camelPreferenceService.setRealTimeBeanMethodValidationCheckBox(realTimeBeanMethodValidationCheckBox.isSelected());
}
Expand All @@ -91,6 +95,7 @@ public boolean isModified() {
|| camelPreferenceService.isHighlightCustomOptions() != highlightCustomOptionsCheckBox.isSelected()
|| camelPreferenceService.isRealTimeSimpleValidation() != realTimeSimpleValidationCatalogCheckBox.isSelected()
|| camelPreferenceService.isRealTimeJSonPathValidation() != realTimeJSonPathValidationCatalogCheckBox.isSelected()
|| camelPreferenceService.isRealTimeJQValidation() != realTimeJQValidationCatalogCheckBox.isSelected()
|| camelPreferenceService.isRealTimeIdReferenceTypeValidation() != realTimeIdReferenceTypeValidationCheckBox.isSelected()
|| camelPreferenceService.isRealTimeBeanMethodValidationCheckBox() != realTimeBeanMethodValidationCheckBox.isSelected();
return b1;
Expand All @@ -103,6 +108,7 @@ public void reset() {
highlightCustomOptionsCheckBox.setSelected(camelPreferenceService.isHighlightCustomOptions());
realTimeSimpleValidationCatalogCheckBox.setSelected(camelPreferenceService.isRealTimeSimpleValidation());
realTimeJSonPathValidationCatalogCheckBox.setSelected(camelPreferenceService.isRealTimeJSonPathValidation());
realTimeJQValidationCatalogCheckBox.setSelected(camelPreferenceService.isRealTimeJQValidation());
realTimeIdReferenceTypeValidationCheckBox.setSelected(camelPreferenceService.isRealTimeIdReferenceTypeValidation());
realTimeBeanMethodValidationCheckBox.setSelected(camelPreferenceService.isRealTimeBeanMethodValidationCheckBox());
}
Expand All @@ -113,6 +119,7 @@ public void disposeUIResources() {
highlightCustomOptionsCheckBox = null;
realTimeSimpleValidationCatalogCheckBox = null;
realTimeJSonPathValidationCatalogCheckBox = null;
realTimeJQValidationCatalogCheckBox = null;
realTimeIdReferenceTypeValidationCheckBox = null;
realTimeBeanMethodValidationCheckBox = null;

Expand Down Expand Up @@ -150,6 +157,10 @@ JBCheckBox getRealTimeJSonPathValidationCatalogCheckBox() {
return realTimeJSonPathValidationCatalogCheckBox;
}

public JBCheckBox getRealTimeJQValidationCatalogCheckBox() {
return realTimeJQValidationCatalogCheckBox;
}

JBCheckBox getRealTimeIdReferenceTypeValidationCheckBox() {
return realTimeIdReferenceTypeValidationCheckBox;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class CamelPreferenceService implements PersistentStateComponent<CamelPre
private boolean realTimeEndpointValidation = true;
private boolean realTimeSimpleValidation = true;
private boolean realTimeJSonPathValidation = true;
private boolean realTimeJQValidation = true;
private boolean realTimeIdReferenceTypeValidation = true;
private boolean realTimeBeanMethodValidationCheckBox = true;
private boolean highlightCustomOptions = true;
Expand Down Expand Up @@ -101,6 +102,14 @@ public void setRealTimeJSonPathValidation(boolean realTimeJSonPathValidation) {
this.realTimeJSonPathValidation = realTimeJSonPathValidation;
}

public boolean isRealTimeJQValidation() {
return realTimeJQValidation;
}

public void setRealTimeJQValidation(boolean realTimeJQValidation) {
this.realTimeJQValidation = realTimeJQValidation;
}

public boolean isRealTimeIdReferenceTypeValidation() {
return realTimeIdReferenceTypeValidation;
}
Expand Down Expand Up @@ -199,6 +208,7 @@ public boolean equals(Object o) {
return realTimeEndpointValidation == that.realTimeEndpointValidation
&& realTimeSimpleValidation == that.realTimeSimpleValidation
&& realTimeJSonPathValidation == that.realTimeJSonPathValidation
&& realTimeJQValidation == that.realTimeJQValidation
&& realTimeIdReferenceTypeValidation == that.realTimeIdReferenceTypeValidation
&& downloadCatalog == that.downloadCatalog
&& scanThirdPartyComponents == that.scanThirdPartyComponents
Expand All @@ -209,7 +219,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(realTimeEndpointValidation, realTimeSimpleValidation, realTimeJSonPathValidation,
return Objects.hash(realTimeEndpointValidation, realTimeSimpleValidation, realTimeJSonPathValidation, realTimeJQValidation,
realTimeIdReferenceTypeValidation, downloadCatalog, scanThirdPartyComponents,
ignorePropertyList, excludePropertyFiles);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public class CamelService implements Disposable {
private volatile boolean camelPresent;
private volatile Notification camelVersionNotification;
private volatile Notification camelMissingJSonSchemaNotification;
private volatile Notification camelMissingJSonPathJarNotification;
private volatile Notification camelMissingLanguageJarNotification;

/**
* The project in which the service is registered.
Expand Down Expand Up @@ -118,9 +118,9 @@ public synchronized void dispose() {
camelMissingJSonSchemaNotification.expire();
camelMissingJSonSchemaNotification = null;
}
if (camelMissingJSonPathJarNotification != null) {
camelMissingJSonPathJarNotification.expire();
camelMissingJSonPathJarNotification = null;
if (camelMissingLanguageJarNotification != null) {
camelMissingLanguageJarNotification.expire();
camelMissingLanguageJarNotification = null;
}
if (camelCoreClassloader != null) {
try {
Expand Down Expand Up @@ -345,12 +345,12 @@ synchronized ClassLoader getProjectCompleteClassloader() {
return projectCompleteClassloader;
}

public void showMissingJSonPathJarNotification() {
if (camelMissingJSonPathJarNotification == null) {
public void showMissingLanguageJarNotification(String lib) {
if (camelMissingLanguageJarNotification == null) {
Icon icon = CamelPreferenceService.getService().getCamelIcon();
camelMissingJSonPathJarNotification = CAMEL_NOTIFICATION_GROUP.createNotification("camel-jsonpath is not on classpath. Cannot perform real time JSonPath validation.",
camelMissingLanguageJarNotification = CAMEL_NOTIFICATION_GROUP.createNotification(lib + " is not on classpath. Cannot perform real time validation.",
NotificationType.WARNING).setImportant(true).setIcon(icon);
camelMissingJSonPathJarNotification.notify(project);
camelMissingLanguageJarNotification.notify(project);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,14 @@ public boolean isCamelExpression(PsiElement element, String language) {
methods = new String[]{"simple", "log"};
} else if ("jsonpath".equals(language)) {
methods = new String[]{"jsonpath"};
} else if ("jq".equals(language)) {
methods = new String[]{"jq"};
}
if (methods != null) {
return IdeaUtils.getService().isFromJavaMethodCall(element, true, methods);
} else {
return false;
}
return IdeaUtils.getService().isFromJavaMethodCall(element, true, methods);
}

@Override
Expand Down
10 changes: 5 additions & 5 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,18 @@
<annotator language="JAVA" implementationClass="com.github.cameltooling.idea.annotator.CamelSimpleAnnotator"/>
<annotator language="XML" implementationClass="com.github.cameltooling.idea.annotator.CamelSimpleAnnotator"/>
<annotator language="yaml" implementationClass="com.github.cameltooling.idea.annotator.CamelSimpleAnnotator"/>
<!-- annotator to validate jsonpath language -->
<annotator language="JAVA" implementationClass="com.github.cameltooling.idea.annotator.CamelJSonPathAnnotator"/>
<annotator language="XML" implementationClass="com.github.cameltooling.idea.annotator.CamelJSonPathAnnotator"/>
<annotator language="yaml" implementationClass="com.github.cameltooling.idea.annotator.CamelJSonPathAnnotator"/>
<!-- annotator to validate json/jq language(s) -->
<annotator language="JAVA" implementationClass="com.github.cameltooling.idea.annotator.CamelLanguageAnnotator"/>
<annotator language="XML" implementationClass="com.github.cameltooling.idea.annotator.CamelLanguageAnnotator"/>
<annotator language="yaml" implementationClass="com.github.cameltooling.idea.annotator.CamelLanguageAnnotator"/>
<!-- annotator to validate bean references by type -->
<annotator language="JAVA" implementationClass="com.github.cameltooling.idea.annotator.BeanReferenceTypeAnnotator"/>
<annotator language="XML" implementationClass="com.github.cameltooling.idea.annotator.BeanReferenceTypeAnnotator"/>
<!-- annotator to validate bean method calls language -->
<annotator language="JAVA" implementationClass="com.github.cameltooling.idea.annotator.CamelBeanMethodAnnotator"/>

<!-- inspection to validate endpoints -->
<localInspection displayName="Camel inspection" groupName="Camel" implementationClass="com.github.cameltooling.idea.inspection.CamelInspection"/>
<localInspection language="" displayName="Camel Inspection" groupName="Camel" implementationClass="com.github.cameltooling.idea.inspection.CamelInspection"/>

<!-- preference -->
<applicationConfigurable id="camel" groupId="language" displayName="Apache Camel" instance="com.github.cameltooling.idea.preference.CamelPreferenceEntryPage"/>
Expand Down
Loading
Loading