Skip to content

Commit

Permalink
Support CamelContext as DocumentSymbol for XML DSL (#81)
Browse files Browse the repository at this point in the history
Signed-off-by: Aurélien Pupier <apupier@redhat.com>
  • Loading branch information
apupier committed Aug 6, 2018
1 parent 630e4a4 commit c472f12
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,16 @@ public DocumentSymbolProcessor(TextDocumentItem textDocumentItem) {
public CompletableFuture<List<? extends SymbolInformation>> getDocumentSymbols() {
return CompletableFuture.supplyAsync(() -> {
try {
List<SymbolInformation> symbolInformations = new ArrayList<>();
NodeList routeNodes = parserFileHelper.getRouteNodes(textDocumentItem);
if (routeNodes != null) {
return convertToSymbolInformation(routeNodes);
symbolInformations.addAll(convertToSymbolInformation(routeNodes));
}
NodeList camelContextNodes = parserFileHelper.getCamelContextNodes(textDocumentItem);
if (camelContextNodes != null) {
symbolInformations.addAll(convertToSymbolInformation(camelContextNodes));
}
return symbolInformations;
} catch (Exception e) {
LOGGER.error("Cannot determine document symbols", e);
}
Expand All @@ -75,8 +81,8 @@ private List<SymbolInformation> convertToSymbolInformation(NodeList routeNodes)
return res;
}

private String computeDisplayNameOfSymbol(Node routeNode) {
Node routeIdAttribute = routeNode.getAttributes().getNamedItem(ATTRIBUTE_ID);
private String computeDisplayNameOfSymbol(Node node) {
Node routeIdAttribute = node.getAttributes().getNamedItem(ATTRIBUTE_ID);
String displayNameOfSymbol;
if (routeIdAttribute != null) {
displayNameOfSymbol = routeIdAttribute.getNodeValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
Expand All @@ -40,8 +44,10 @@ public class ParserXMLFileHelper extends ParserFileHelper {

private static final Logger LOGGER = LoggerFactory.getLogger(ParserXMLFileHelper.class);
private static final String ATTRIBUTE_ROUTE = "route";
private static final String ATTRIBUTE_CAMEL_CONTEXT = "camelContext";
private static final String NAMESPACEURI_CAMEL_BLUEPRINT = "http://camel.apache.org/schema/blueprint";
private static final String NAMESPACEURI_CAMEL_SPRING = "http://camel.apache.org/schema/spring";
private static final List<String> DOCUMENT_SYMBOL_POSSIBLE_TYPES = Arrays.asList(ATTRIBUTE_CAMEL_CONTEXT, ATTRIBUTE_ROUTE);

public String getCamelComponentUri(String line, int characterPosition) {
int uriAttribute = line.indexOf("uri=\"");
Expand Down Expand Up @@ -78,7 +84,9 @@ private boolean hasElementFromCamelNamespace(TextDocumentItem textDocumentItem)
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
Document xmlParsed = dbf.newDocumentBuilder().parse(new ByteArrayInputStream(textDocumentItem.getText().getBytes(StandardCharsets.UTF_8)));
for (String camelNodeTag : CAMEL_POSSIBLE_TYPES) {
Set<String> interestingCamelNodeType = new HashSet<>(CAMEL_POSSIBLE_TYPES);
interestingCamelNodeType.addAll(DOCUMENT_SYMBOL_POSSIBLE_TYPES);
for (String camelNodeTag : interestingCamelNodeType) {
if(hasElementFromCamelNameSpaces(xmlParsed.getElementsByTagName(camelNodeTag))){
return true;
}
Expand Down Expand Up @@ -114,9 +122,17 @@ private boolean hasElementFromCamelNameSpaces(NodeList childNodes) {
}

public NodeList getRouteNodes(TextDocumentItem textDocumentItem) throws Exception {
return getNodesOfType(textDocumentItem, ATTRIBUTE_ROUTE);
}

public NodeList getCamelContextNodes(TextDocumentItem textDocumentItem) throws Exception {
return getNodesOfType(textDocumentItem, ATTRIBUTE_CAMEL_CONTEXT);
}

private NodeList getNodesOfType(TextDocumentItem textDocumentItem, String attributeTypeToFilter) throws Exception {
if (hasElementFromCamelNamespace(textDocumentItem)) {
Document parsedXml = XmlLineNumberParser.parseXml(new ByteArrayInputStream(textDocumentItem.getText().getBytes(StandardCharsets.UTF_8)));
return parsedXml.getElementsByTagName(ATTRIBUTE_ROUTE);
return parsedXml.getElementsByTagName(attributeTypeToFilter);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void testRoutesProvidedAsDocumentSymbol() throws Exception {
" </recipientList>\r\n" +
" </route>\n"
+ "</camelContext>\n";
List<? extends SymbolInformation> documentSymbols = testRetrieveDocumentSymbol(textTotest, 2);
List<? extends SymbolInformation> documentSymbols = testRetrieveDocumentSymbol(textTotest, 3);
SymbolInformation firstRoute = documentSymbols.get(0);
assertThat(firstRoute.getName()).isEqualTo("a route");
Position expectedStart = new Position(2, 24/* expecting 4 but seems a bug in Camel*/);
Expand All @@ -64,11 +64,71 @@ public void testRoutesProvidedAsDocumentSymbol() throws Exception {
}

@Test
public void testEmptyCamelContextReturnNoDocumentSymbol() throws Exception {
public void testEmptyCamelContextReturnCamelContextDocumentSymbol() throws Exception {
String textTotest =
"<camelContext id=\"camel\" xmlns=\"http://camel.apache.org/schema/spring\">\r\n" +
"</camelContext>\n";
testRetrieveDocumentSymbol(textTotest, 0);
testRetrieveDocumentSymbol(textTotest, 1);
}

@Test
public void test2CamelContext() throws Exception {
String textTotest =
"<beans xmlns=\"http://www.springframework.org/schema/beans\"\n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
" xsi:schemaLocation=\"\n" +
" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd\n" +
" http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd\">\n" +
"<camelContext id=\"camel\" xmlns=\"http://camel.apache.org/schema/spring\">\r\n" +
"\r\n" +
" <route id=\"a route\">\r\n" +
" <from uri=\"direct:cafe\"/>\r\n" +
" <split>\r\n" +
" <method bean=\"orderSplitter\"/>\r\n" +
" <to uri=\"direct:drink\"/>\r\n" +
" </split>\r\n" +
" </route>\r\n" +
"\r\n" +
" <route id=\"another Route\">\r\n" +
" <from uri=\"direct:drink\"/>\r\n" +
" <recipientList>\r\n" +
" <method bean=\"drinkRouter\"/>\r\n" +
" </recipientList>\r\n" +
" </route>\n"
+ "</camelContext>\n" +
"<camelContext id=\"camel2\" xmlns=\"http://camel.apache.org/schema/spring\">\r\n" +
"\r\n" +
" <route id=\"a route2\">\r\n" +
" <from uri=\"direct:cafe\"/>\r\n" +
" <split>\r\n" +
" <method bean=\"orderSplitter\"/>\r\n" +
" <to uri=\"direct:drink\"/>\r\n" +
" </split>\r\n" +
" </route>\r\n" +
"\r\n" +
" <route id=\"another Route2\">\r\n" +
" <from uri=\"direct:drink\"/>\r\n" +
" <recipientList>\r\n" +
" <method bean=\"drinkRouter\"/>\r\n" +
" </recipientList>\r\n" +
" </route>\n"
+ "</camelContext>\n"
+ "</beans>";
testRetrieveDocumentSymbol(textTotest, 6);
}

@Test
public void testEmptyRoutes() throws Exception {
String textTotest =
"<camelContext id=\"camel\" xmlns=\"http://camel.apache.org/schema/spring\">\r\n" +
"\r\n" +
" <route id=\"a route\">\r\n" +
" </route>\r\n" +
"\r\n" +
" <route id=\"another Route\">\r\n" +
" </route>\n"
+ "</camelContext>\n";
testRetrieveDocumentSymbol(textTotest, 3);
}

@Test
Expand Down Expand Up @@ -97,7 +157,7 @@ public void testRoutesWithoutId() throws Exception {
" </recipientList>\r\n" +
" </route>\n"
+ "</camelContext>\n";
testRetrieveDocumentSymbol(textTotest, 3);
testRetrieveDocumentSymbol(textTotest, 4);
}

@Test
Expand Down

0 comments on commit c472f12

Please sign in to comment.