Search in sources :

Example 1 with DeserializationException

use of org.apache.camel.util.json.DeserializationException in project camel-idea-plugin by camel-tooling.

the class CamelDocumentationProvider method generateCamelComponentDocumentation.

private String generateCamelComponentDocumentation(String componentName, String val, int wrapLength, Project project) {
    // it is a known Camel component
    CamelCatalog camelCatalog = ServiceManager.getService(project, CamelCatalogService.class).get();
    String json = camelCatalog.componentJSonSchema(componentName);
    if (json == null) {
        return null;
    }
    ComponentModel component = JsonMapper.generateComponentModel(json);
    // camel catalog expects & as & when it parses so replace all & as &
    String camelQuery = val;
    camelQuery = camelQuery.replaceAll("&", "&");
    // strip up ending incomplete parameter
    if (camelQuery.endsWith("&") || camelQuery.endsWith("?")) {
        camelQuery = camelQuery.substring(0, camelQuery.length() - 1);
    }
    Map<String, String> existing = null;
    try {
        existing = camelCatalog.endpointProperties(camelQuery);
    } catch (Throwable e) {
        LOG.warn("Error parsing Camel endpoint properties with url: " + camelQuery, e);
    }
    StringBuilder options = new StringBuilder();
    if (existing != null && !existing.isEmpty()) {
        JsonObject jsonObject;
        try {
            jsonObject = (JsonObject) Jsoner.deserialize(json);
        } catch (DeserializationException e) {
            throw new RuntimeException(e);
        }
        Map<String, JsonObject> properties = jsonObject.getMap("properties");
        for (Map.Entry<String, String> entry : existing.entrySet()) {
            String name = entry.getKey();
            String value = entry.getValue();
            JsonObject row = properties.get(name);
            if (row != null) {
                String kind = row.getString("kind");
                String deprecated = row.getString("deprecated");
                String line;
                if ("path".equals(kind)) {
                    line = value + "<br/>";
                } else {
                    if ("true".equals(deprecated)) {
                        line = "<s>" + name + "</s>=" + value + "<br/>";
                    } else {
                        line = name + "=" + value + "<br/>";
                    }
                }
                options.append("<br/>");
                options.append("<b>").append(line).append("</b>");
                String summary = row.getString("description");
                // the text looks a bit weird when using single /
                summary = summary.replace('/', ' ');
                options.append(wrapText(summary, wrapLength)).append("<br/>");
            }
        }
    }
    // append any lenient options as well
    Map<String, String> extra = null;
    try {
        extra = camelCatalog.endpointLenientProperties(camelQuery);
    } catch (Throwable e) {
        LOG.warn("Error parsing Camel endpoint properties with url: " + camelQuery, e);
    }
    if (extra != null && !extra.isEmpty()) {
        for (Map.Entry<String, String> entry : extra.entrySet()) {
            String name = entry.getKey();
            String value = entry.getValue();
            String line = name + "=" + value + "<br/>";
            options.append("<br/>");
            options.append("<b>").append(line).append("</b>");
            String summary = "This option is a custom option that is not part of the Camel component";
            options.append(wrapText(summary, wrapLength)).append("<br/>");
        }
    }
    StringBuilder sb = new StringBuilder();
    if (component.isDeprecated()) {
        sb.append("<b><s>").append(component.getTitle()).append(" Component (deprecated)</s></b><br/>");
    } else {
        sb.append("<b>").append(component.getTitle()).append(" Component</b><br/>");
    }
    sb.append(wrapText(component.getDescription(), wrapLength)).append("<br/><br/>");
    if (component.getDeprecatedSince() != null) {
        sb.append("<b>Deprecated Since:</b> <tt>").append(component.getDeprecatedSince()).append("</tt><br/>");
    }
    sb.append("<b>Since:</b> <tt>").append(component.getFirstVersionShort()).append("</tt><br/>");
    if (component.getSupportLevel() != null) {
        sb.append("<b>Support Level:</b> <tt>").append(component.getSupportLevel()).append("</tt><br/>");
    }
    String g = component.getGroupId();
    String a = component.getArtifactId();
    String v = component.getVersion();
    if (g != null && a != null && v != null) {
        sb.append("<b>Maven:</b> <tt>").append(g).append(":").append(a).append(":").append(v).append("</tt><br/>");
    }
    sb.append("<b>Syntax:</b> <tt>").append(component.getSyntax()).append("?options</tt><br/>");
    sb.append("<p/>");
    sb.append("<br/>");
    // indent the endpoint url with 5 spaces and wrap it by url separator
    String wrapped = wrapSeparator(val, "&", "<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", 100);
    sb.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>").append(wrapped).append("</b><br/>");
    if (options.length() > 0) {
        sb.append(options);
    }
    return sb.toString();
}
Also used : CamelCatalog(org.apache.camel.catalog.CamelCatalog) JsonObject(org.apache.camel.util.json.JsonObject) DeserializationException(org.apache.camel.util.json.DeserializationException) CamelCatalogService(com.github.cameltooling.idea.service.CamelCatalogService) ComponentModel(org.apache.camel.tooling.model.ComponentModel) Map(java.util.Map)

Aggregations

CamelCatalogService (com.github.cameltooling.idea.service.CamelCatalogService)1 Map (java.util.Map)1 CamelCatalog (org.apache.camel.catalog.CamelCatalog)1 ComponentModel (org.apache.camel.tooling.model.ComponentModel)1 DeserializationException (org.apache.camel.util.json.DeserializationException)1 JsonObject (org.apache.camel.util.json.JsonObject)1