Component docs
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/1e1e1f9f Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/1e1e1f9f Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/1e1e1f9f Branch: refs/heads/master Commit: 1e1e1f9f69269ed4fdaa40a153871c8780bda219 Parents: be75520 Author: Claus Ibsen <davscl...@apache.org> Authored: Sun Jun 14 10:06:38 2015 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Sun Jun 14 13:08:07 2015 +0200 ---------------------------------------------------------------------- .../camel/tools/apt/DocumentationHelper.java | 52 +++++++++++- .../tools/apt/EndpointAnnotationProcessor.java | 2 +- .../camel/tools/apt/JsonSchemaHelper.java | 89 ++++++++++++++++++++ 3 files changed, 141 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/1e1e1f9f/tooling/apt/src/main/java/org/apache/camel/tools/apt/DocumentationHelper.java ---------------------------------------------------------------------- diff --git a/tooling/apt/src/main/java/org/apache/camel/tools/apt/DocumentationHelper.java b/tooling/apt/src/main/java/org/apache/camel/tools/apt/DocumentationHelper.java index 93a7ae5..37e190b 100644 --- a/tooling/apt/src/main/java/org/apache/camel/tools/apt/DocumentationHelper.java +++ b/tooling/apt/src/main/java/org/apache/camel/tools/apt/DocumentationHelper.java @@ -17,17 +17,67 @@ package org.apache.camel.tools.apt; import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; +import java.util.List; +import java.util.Map; + +import static org.apache.camel.tools.apt.JsonSchemaHelper.parseJsonSchema; /** * Helper to find documentation for inherited options when a component extends another. */ public final class DocumentationHelper { - public static String findJavaDoc(String scheme, String fieldName) { + public static String findJavaDoc(String scheme, String extendsScheme, String fieldName) { + File file = jsonFile(scheme, extendsScheme); + if (file != null) { + FileInputStream fis = null; + try { + fis = new FileInputStream(file); + String json = loadText(fis); + List<Map<String, String>> rows = parseJsonSchema("properties", json, true); + + for (Map<String, String> row : rows) { + String name = row.get("name"); + String description = row.get("description"); + if (fieldName.equals(name)) { + return description; + } + } + } catch (Exception e) { + // ignore + } finally { + IOHelper.close(fis); + } + } + + // not found + return null; + } + + private static File jsonFile(String scheme, String extendsScheme) { + // TODO: scan components for each component and find component name from extendsScheme + // and then find the package name where the json file is + + if ("file".equals(extendsScheme)) { + return new File("../../camel-core/target/classes/org/apache/camel/component/file/file.json"); + } else if ("http".equals(extendsScheme)) { + return new File("../camel-http/target/classes/org/apache/camel/component/http/http.json"); + } else if ("https".equals(extendsScheme)) { + return new File("../camel-http/target/classes/org/apache/camel/component/http/https.json"); + } else if ("netty".equals(extendsScheme)) { + return new File("../camel-netty/target/classes/org/apache/camel/component/netty/netty.json"); + } else if ("netty4".equals(extendsScheme)) { + return new File("../camel-netty4/target/classes/org/apache/camel/component/netty4/netty4.json"); + } else if ("servlet".equals(extendsScheme)) { + return new File("../camel-servlet/target/classes/org/apache/camel/component/servlet/servlet.json"); + } + // not found return null; } http://git-wip-us.apache.org/repos/asf/camel/blob/1e1e1f9f/tooling/apt/src/main/java/org/apache/camel/tools/apt/EndpointAnnotationProcessor.java ---------------------------------------------------------------------- diff --git a/tooling/apt/src/main/java/org/apache/camel/tools/apt/EndpointAnnotationProcessor.java b/tooling/apt/src/main/java/org/apache/camel/tools/apt/EndpointAnnotationProcessor.java index 6cb7973..7f895ed 100644 --- a/tooling/apt/src/main/java/org/apache/camel/tools/apt/EndpointAnnotationProcessor.java +++ b/tooling/apt/src/main/java/org/apache/camel/tools/apt/EndpointAnnotationProcessor.java @@ -286,7 +286,7 @@ public class EndpointAnnotationProcessor extends AbstractAnnotationProcessor { String doc = entry.getDocumentationWithNotes(); if (Strings.isNullOrEmpty(doc)) { - doc = DocumentationHelper.findJavaDoc(componentModel.getScheme(), entry.getName()); + doc = DocumentationHelper.findJavaDoc(componentModel.getScheme(), componentModel.getExtendsScheme(), entry.getName()); } doc = sanitizeDescription(doc, false); http://git-wip-us.apache.org/repos/asf/camel/blob/1e1e1f9f/tooling/apt/src/main/java/org/apache/camel/tools/apt/JsonSchemaHelper.java ---------------------------------------------------------------------- diff --git a/tooling/apt/src/main/java/org/apache/camel/tools/apt/JsonSchemaHelper.java b/tooling/apt/src/main/java/org/apache/camel/tools/apt/JsonSchemaHelper.java index f53785b..a67e2ca 100644 --- a/tooling/apt/src/main/java/org/apache/camel/tools/apt/JsonSchemaHelper.java +++ b/tooling/apt/src/main/java/org/apache/camel/tools/apt/JsonSchemaHelper.java @@ -18,7 +18,13 @@ package org.apache.camel.tools.apt; import java.net.URI; import java.net.URL; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * A helper class for <a href="http://json-schema.org/">JSON schema</a>. @@ -26,6 +32,8 @@ import java.util.Set; final class JsonSchemaHelper { private static final String VALID_CHARS = ".-='/\\!&():;"; + private static final Pattern PATTERN = Pattern.compile("\"(.+?)\"|\\[(.+)\\]"); + private static final String QUOT = """; private JsonSchemaHelper() { } @@ -257,6 +265,87 @@ final class JsonSchemaHelper { } /** + * Parses the json schema to split it into a list or rows, where each row contains key value pairs with the metadata + * + * @param group the group to parse from such as <tt>component</tt>, <tt>componentProperties</tt>, or <tt>properties</tt>. + * @param json the json + * @return a list of all the rows, where each row is a set of key value pairs with metadata + */ + public static List<Map<String, String>> parseJsonSchema(String group, String json, boolean parseProperties) { + List<Map<String, String>> answer = new ArrayList<Map<String, String>>(); + if (json == null) { + return answer; + } + + boolean found = false; + + // parse line by line + String[] lines = json.split("\n"); + for (String line : lines) { + // we need to find the group first + if (!found) { + String s = line.trim(); + found = s.startsWith("\"" + group + "\":"); + continue; + } + + // we should stop when we end the group + if (line.equals(" },") || line.equals(" }")) { + break; + } + + // need to safe encode \" so we can parse the line + line = line.replaceAll("\"\\\\\"\"", '"' + QUOT + '"'); + + Map<String, String> row = new LinkedHashMap<String, String>(); + Matcher matcher = PATTERN.matcher(line); + + String key; + if (parseProperties) { + // when parsing properties the first key is given as name, so the first parsed token is the value of the name + key = "name"; + } else { + key = null; + } + while (matcher.find()) { + if (key == null) { + key = matcher.group(1); + } else { + String value = matcher.group(1); + if (value == null) { + value = matcher.group(2); + // its an enum so strip out " and trim spaces after comma + value = value.replaceAll("\"", ""); + value = value.replaceAll(", ", ","); + } + if (value != null) { + value = value.trim(); + // decode + value = value.replaceAll(QUOT, "\""); + value = decodeJson(value); + } + row.put(key, value); + // reset + key = null; + } + } + if (!row.isEmpty()) { + answer.add(row); + } + } + + return answer; + } + + private static String decodeJson(String value) { + // json encodes a \ as \\ so we need to decode from \\ back to \ + if ("\\\\".equals(value)) { + value = "\\"; + } + return value; + } + + /** * The default value may need to be escaped to be safe for json */ private static String safeDefaultValue(String value) {