|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package com.github.cameltooling.idea.reference.endpoint.parameter; |
| 18 | + |
| 19 | +import com.github.cameltooling.idea.reference.endpoint.CamelEndpointPsiReferenceProvider; |
| 20 | +import com.github.cameltooling.idea.service.CamelCatalogService; |
| 21 | +import com.github.cameltooling.idea.util.CamelIdeaUtils; |
| 22 | +import com.github.cameltooling.idea.util.IdeaUtils; |
| 23 | +import com.intellij.openapi.project.Project; |
| 24 | +import com.intellij.openapi.util.TextRange; |
| 25 | +import com.intellij.openapi.util.text.StringUtil; |
| 26 | +import com.intellij.patterns.ElementPattern; |
| 27 | +import com.intellij.psi.PsiElement; |
| 28 | +import com.intellij.psi.PsiReference; |
| 29 | +import com.intellij.psi.PsiReferenceContributor; |
| 30 | +import com.intellij.psi.PsiReferenceRegistrar; |
| 31 | +import com.intellij.util.ProcessingContext; |
| 32 | +import org.apache.camel.catalog.CamelCatalog; |
| 33 | +import org.jetbrains.annotations.NotNull; |
| 34 | + |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.Objects; |
| 38 | + |
| 39 | +/** |
| 40 | + * Contributor for {@link EndpointParameterReference}, which is a reference from endpoint uri query parameter to its setter method in the corresponding camel class |
| 41 | + * E.g. from the 'synchronous' substring in endpoint uri "direct:abc?synchronous=true" to {@link org.apache.camel.component.direct.DirectEndpoint#setSynchronous} |
| 42 | + */ |
| 43 | +public class CamelEndpointParameterReferenceContributor extends PsiReferenceContributor { |
| 44 | + |
| 45 | + @Override |
| 46 | + public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) { |
| 47 | + List<ElementPattern<? extends PsiElement>> patterns = CamelIdeaUtils.getService().getAllowedEndpointUriLocations(); |
| 48 | + if (!patterns.isEmpty()) { |
| 49 | + CamelEndpointPsiReferenceProvider provider = createProvider(); |
| 50 | + patterns.forEach(pattern -> { |
| 51 | + registrar.registerReferenceProvider(pattern, provider); |
| 52 | + }); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @NotNull |
| 57 | + private CamelEndpointPsiReferenceProvider createProvider() { |
| 58 | + return new CamelEndpointPsiReferenceProvider() { |
| 59 | + @Override |
| 60 | + protected PsiReference[] getEndpointReferencesByElement(String endpointUri, PsiElement element, ProcessingContext context) { |
| 61 | + Project project = element.getProject(); |
| 62 | + |
| 63 | + CamelCatalogService catalogService = project.getService(CamelCatalogService.class); |
| 64 | + CamelCatalog catalog = catalogService.get(); |
| 65 | + String component = catalog.endpointComponentName(endpointUri); |
| 66 | + if (component == null) { |
| 67 | + return PsiReference.EMPTY_ARRAY; |
| 68 | + } |
| 69 | + |
| 70 | + Map<String, String> params; |
| 71 | + try { |
| 72 | + String unescapedUri = IdeaUtils.getService().isXmlLanguage(element) ? StringUtil.unescapeXmlEntities(endpointUri) : endpointUri; |
| 73 | + params = catalog.endpointProperties(unescapedUri); |
| 74 | + } catch (Exception e) { |
| 75 | + return PsiReference.EMPTY_ARRAY; |
| 76 | + } |
| 77 | + |
| 78 | + return createParameterReferences(endpointUri, element, component, params); |
| 79 | + } |
| 80 | + |
| 81 | + private PsiReference[] createParameterReferences(String endpointUri, PsiElement endpointElement, String component, Map<String, String> params) { |
| 82 | + return params.keySet().stream() |
| 83 | + .map(param -> { |
| 84 | + int paramStartIndex = endpointUri.indexOf(param + "="); |
| 85 | + if (paramStartIndex < 0) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + if (endpointElement.getText().startsWith("\"")) { |
| 89 | + paramStartIndex++; |
| 90 | + } |
| 91 | + return new EndpointParameterReference(endpointElement, component, param, new TextRange(paramStartIndex, paramStartIndex + param.length())); |
| 92 | + }) |
| 93 | + .filter(Objects::nonNull) |
| 94 | + .toArray(PsiReference[]::new); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + protected boolean isEndpoint(String endpointUri) { |
| 99 | + return true; |
| 100 | + } |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments