From 8a11d3dbfe59068e65751de7fee0c0b1eac88d5f Mon Sep 17 00:00:00 2001 From: nkukhar Date: Sun, 8 Feb 2015 23:19:06 -0800 Subject: [PATCH 1/7] CAMEL-8197 Added maven plagin to inject EIP documentation into the spring and blueprint XML DSL. --- .../pom.xml | 139 ++++++++++++++++++ .../camel/maven/CamelSpringNamespace.java | 44 ++++++ .../org/apache/camel/maven/Constants.java | 36 +++++ .../camel/maven/DocumentationEnricher.java | 95 ++++++++++++ .../org/apache/camel/maven/DomParser.java | 42 ++++++ .../maven/EipDocumentationGeneratorMojo.java | 135 +++++++++++++++++ .../org/apache/camel/maven/PackageHelper.java | 64 ++++++++ 7 files changed, 555 insertions(+) create mode 100644 tooling/maven/camel-eip-documentation-enricher-maven-plugin/pom.xml create mode 100644 tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/CamelSpringNamespace.java create mode 100644 tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/Constants.java create mode 100644 tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DocumentationEnricher.java create mode 100644 tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DomParser.java create mode 100644 tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/EipDocumentationGeneratorMojo.java create mode 100644 tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/PackageHelper.java diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/pom.xml b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/pom.xml new file mode 100644 index 0000000000000..74e3f534590b5 --- /dev/null +++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/pom.xml @@ -0,0 +1,139 @@ + + + + 4.0.0 + + + org.apache.camel + maven-plugins + 2.15-SNAPSHOT + + + camel-eip-documentation-enricher-maven-plugin + maven-plugin + Camel :: Maven Plugins :: Camel EIP Documentation Enricher Plugin + Adds documentation to camel xsd schema + + + org.apache.camel + camel-core + + + + org.apache.maven.reporting + maven-reporting-api + + + org.apache.maven.reporting + maven-reporting-impl + + + org.codehaus.plexus + plexus-utils + + + org.apache.maven.plugin-tools + maven-plugin-annotations + 3.3 + provided + + + org.apache.maven + maven-plugin-descriptor + 2.2.1 + + + log4j + log4j + + + + + org.apache.maven + maven-artifact + 2.2.1 + + + + + org.slf4j + slf4j-api + + + org.slf4j + log4j-over-slf4j + ${slf4j-version} + + + org.slf4j + slf4j-simple + test + + + + org.apache.camel + camel-test + test + + + junit + junit + test + + + + org.apache.camel + spi-annotations + ${project.version} + provided + + + + + + + + org.apache.maven.plugins + maven-plugin-plugin + 3.3 + + + true + + + + mojo-descriptor + + descriptor + + + + help-goal + + helpmojo + + + + + + + + + + + diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/CamelSpringNamespace.java b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/CamelSpringNamespace.java new file mode 100644 index 0000000000000..d64d0b92ac784 --- /dev/null +++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/CamelSpringNamespace.java @@ -0,0 +1,44 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.maven; + +import javax.xml.namespace.NamespaceContext; +import java.util.Iterator; + +public class CamelSpringNamespace implements NamespaceContext { + + @Override + public String getNamespaceURI(String prefix) { + if (prefix == null){ + throw new IllegalArgumentException("The prefix cannot be null."); + } + if ("xs".equals(prefix)){ + return "http://www.w3.org/2001/XMLSchema"; + } + return null; + } + + @Override + public String getPrefix(String namespaceURI) { + throw new UnsupportedOperationException("Operation not supported"); + } + + @Override + public Iterator getPrefixes(String namespaceURI) { + throw new UnsupportedOperationException("Operation not supported"); + } +} diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/Constants.java b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/Constants.java new file mode 100644 index 0000000000000..18a9abf18cd0f --- /dev/null +++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/Constants.java @@ -0,0 +1,36 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.maven; + +public final class Constants { + private Constants(){} + + // Camel core constants. + public static final String PATH_TO_MODEL_DIR = "target/classes/org/apache/camel/model"; + + // XML constants. + public static final String NAME_ATTRIBUTE_NAME = "name"; + public static final String TYPE_ATTRIBUTE_NAME = "type"; + public static final String XS_ANNOTATION_ELEMENT_NAME = "xs:annotation"; + public static final String XS_DOCUMENTATION_ELEMENT_NAME = "xs:documentation"; + + // Json files constants. + public static final String PROPERTIES_ATTRIBUTE_NAME = "properties"; + public static final String JSON_SUFIX = ".json"; + public static final String DESCRIPTION_ATTRIBUTE_NAME = "description"; + public static final String MODEL_ATTRIBUTE_NAME = "model"; +} diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DocumentationEnricher.java b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DocumentationEnricher.java new file mode 100644 index 0000000000000..5a17dd7f0b36a --- /dev/null +++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DocumentationEnricher.java @@ -0,0 +1,95 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.maven; + +import org.apache.camel.util.JsonSchemaHelper; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Map; + +public class DocumentationEnricher { + public void enrichTopLevelElementsDocumentation(Document document, + NodeList elements, + Map jsonFiles) + throws IOException { + for (int i = 0; i < elements.getLength(); i++) { + Element item = (Element) elements.item(i); + String name = item.getAttribute(Constants.NAME_ATTRIBUTE_NAME); + if (jsonFiles.containsKey(name)) { + addDocumentation(document, item, jsonFiles.get(name)); + } + } + } + + private void addDocumentation(Document document, Element item, File jsonFile) + throws IOException { + List> rows = JsonSchemaHelper.parseJsonSchema + (Constants.MODEL_ATTRIBUTE_NAME, PackageHelper.fileToString(jsonFile), false); + for (Map row : rows) { + if (row.containsKey(Constants.DESCRIPTION_ATTRIBUTE_NAME)) { + String descriptionText = row.get(Constants.DESCRIPTION_ATTRIBUTE_NAME); + addDocumentation(document, item, descriptionText); + break; + } + } + } + + private void addDocumentation(Document document, Element item, String textContent) { + Element annotation = document.createElement(Constants.XS_ANNOTATION_ELEMENT_NAME); + Element documentation = document.createElement(Constants.XS_DOCUMENTATION_ELEMENT_NAME); + documentation.setAttribute("xml:lang", "en"); + documentation.setTextContent(textContent); + annotation.appendChild(documentation); + if (item.getFirstChild() != null){ + item.insertBefore(annotation, item.getFirstChild()); + } else { + item.appendChild(annotation); + } + } + + public void enrichTypeAttributesDocumentation(Document document, + NodeList attributeElements, + File jsonFile) throws IOException { + for (int j = 0; j < attributeElements.getLength(); j++) { + Element item = (Element) attributeElements.item(j); + addAttributeDocumentation(item, jsonFile, document); + } + } + + private void addAttributeDocumentation(Element item, + File jsonFile, + Document document) + throws IOException { + List> rows = JsonSchemaHelper.parseJsonSchema + (Constants.PROPERTIES_ATTRIBUTE_NAME, PackageHelper.fileToString(jsonFile), true); + for (Map row : rows) { + if (item.getAttribute(Constants.NAME_ATTRIBUTE_NAME) + .equals(row.get(Constants.NAME_ATTRIBUTE_NAME))){ + String descriptionText = row.get(Constants.DESCRIPTION_ATTRIBUTE_NAME); + if (descriptionText != null) { + addDocumentation(document, item, descriptionText); + break; + } + } + } + } +} diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DomParser.java b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DomParser.java new file mode 100644 index 0000000000000..4d447b99593bf --- /dev/null +++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DomParser.java @@ -0,0 +1,42 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.maven; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; + +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpressionException; + +public class DomParser { + private final Logger logger = LoggerFactory.getLogger(DomParser.class); + + + public NodeList findElementsAndTypes(Document document, XPath xPath) throws XPathExpressionException { + return (NodeList) xPath.compile("/xs:schema/xs:element") + .evaluate(document, XPathConstants.NODESET); + } + + public NodeList findAttributesElements(Document document, XPath xPath, String name) throws XPathExpressionException { + return (NodeList) xPath.compile( + "/xs:schema/xs:complexType[@name='" + name + "']//xs:attribute") + .evaluate(document, XPathConstants.NODESET); + } +} diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/EipDocumentationGeneratorMojo.java b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/EipDocumentationGeneratorMojo.java new file mode 100644 index 0000000000000..1a8b5d834aa59 --- /dev/null +++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/EipDocumentationGeneratorMojo.java @@ -0,0 +1,135 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.maven; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.annotations.ResolutionScope; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +import javax.xml.namespace.NamespaceContext; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.*; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathFactory; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +@Mojo(name = "eip-documentation-enricher", requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, requiresProject = true, + defaultPhase = LifecyclePhase.PACKAGE) +public class EipDocumentationGeneratorMojo extends AbstractMojo { + private final Logger logger = LoggerFactory.getLogger(EipDocumentationGeneratorMojo.class); + + /** + * Project's source directory as specified in the POM. + */ + @Parameter(required = true) + File inputCamelSchemaFile; + + @Parameter(required = true) + File outputCamelSchemaFile; + +// @Parameter(defaultValue = "${project.build.directory}/../../../..//camel-core") + @Parameter(defaultValue = "${project.build.directory}/../../..//camel-core") + File camelCoreDir; + + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + File rootDir = new File(camelCoreDir, Constants.PATH_TO_MODEL_DIR); + DomParser domParser = new DomParser(); + DocumentationEnricher documentationEnricher = new DocumentationEnricher(); + Map jsonFiles = PackageHelper.findJsonFiles(rootDir); + XPath xPath = buildXPath(new CamelSpringNamespace()); + try { + Document document = buildNamespaceAwareDocument(inputCamelSchemaFile); + NodeList elementsAndTypes = domParser.findElementsAndTypes(document, xPath); + documentationEnricher.enrichTopLevelElementsDocumentation + (document, elementsAndTypes, jsonFiles); + Map typeToNameMap = buildTypeToNameMap(elementsAndTypes); + for (Map.Entry entry : typeToNameMap.entrySet()) { + NodeList attributeElements = domParser.findAttributesElements(document, xPath, entry.getKey()); + if (jsonFiles.containsKey(entry.getValue())){ + documentationEnricher.enrichTypeAttributesDocumentation + (document, attributeElements, jsonFiles.get(entry.getValue())); + } + } + saveToFile(document, outputCamelSchemaFile, buildTransformer()); + } catch (Exception e) { + getLog().error(e); + } + } + + private Map buildTypeToNameMap(NodeList elementsAndTypes) { + Map typeToNameMap = new HashMap<>(); + for (int i = 0; i < elementsAndTypes.getLength(); i++) { + Element item = (Element) elementsAndTypes.item(i); + String name = item.getAttribute(Constants.NAME_ATTRIBUTE_NAME); + String type = item.getAttribute(Constants.TYPE_ATTRIBUTE_NAME); + if (name != null && type != null) { + type = type.replaceAll("tns:", ""); + logger.debug("Putting attributes type:'{}', name:'{}'", name, type); + typeToNameMap.put(type, name); + } + } + return typeToNameMap; + } + + private XPath buildXPath(NamespaceContext namespaceContext) { + XPath xPath = XPathFactory.newInstance().newXPath(); + xPath.setNamespaceContext(namespaceContext); + return xPath; + } + + private Transformer buildTransformer() throws TransformerConfigurationException { + Transformer transformer = + TransformerFactory.newInstance().newTransformer(); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + transformer.setOutputProperty( + "{http://xml.apache.org/xslt}indent-amount", "2"); + return transformer; + } + + public Document buildNamespaceAwareDocument(File xml) throws ParserConfigurationException, IOException, SAXException { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + DocumentBuilder builder = factory.newDocumentBuilder(); + return builder.parse(xml); + } + + private void saveToFile(Document document, File outputFile, Transformer transformer) throws IOException, TransformerException { + StreamResult result = + new StreamResult(new FileOutputStream(outputFile)); + DOMSource source = new DOMSource(document); + transformer.transform(source, result); + } +} diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/PackageHelper.java b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/PackageHelper.java new file mode 100644 index 0000000000000..c99ca10fb3d96 --- /dev/null +++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/PackageHelper.java @@ -0,0 +1,64 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.maven; + +import java.io.*; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +public class PackageHelper { + private PackageHelper() { + } + + public static String fileToString(File file) throws IOException { + byte[] encoded = Files.readAllBytes(Paths.get(file.toURI())); + return new String(encoded, Charset.defaultCharset()); + } + + public static Map findJsonFiles(File rootDir) { + Map results = new HashMap<>(); + findJsonFiles0(rootDir, results, new CamelComponentsModelFilter()); + return results; + } + + private static void findJsonFiles0(File dir, Map result, FileFilter filter) { + File[] files = dir.listFiles(filter); + if (files != null) { + for (File file : files) { + // skip files in root dirs as Camel does not store information there but others may do + boolean jsonFile = file.isFile() && file.getName().endsWith(Constants.JSON_SUFIX); + if (jsonFile) { + result.put(file.getName().replaceAll("\\"+ Constants.JSON_SUFIX, ""), file); + } else if (file.isDirectory()) { + findJsonFiles0(file, result, filter); + } + } + } + } + + private static class CamelComponentsModelFilter implements FileFilter { + @Override + public boolean accept(File pathname) { + return pathname.isDirectory() || + pathname.getName().endsWith(Constants.JSON_SUFIX); + } + } +} From 9632d88b5a1f9706febd31d3127604a43e55c1f0 Mon Sep 17 00:00:00 2001 From: nkukhar Date: Sun, 8 Feb 2015 23:20:51 -0800 Subject: [PATCH 2/7] Minor refactoring --- components/camel-spring/pom.xml | 19 +++++++++++++++++-- .../org/apache/camel/maven/DomParser.java | 4 ---- .../maven/EipDocumentationGeneratorMojo.java | 8 +++----- .../org/apache/camel/maven/PackageHelper.java | 5 +++-- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/components/camel-spring/pom.xml b/components/camel-spring/pom.xml index e6e4cccb21d7b..4714cabc42522 100644 --- a/components/camel-spring/pom.xml +++ b/components/camel-spring/pom.xml @@ -377,9 +377,24 @@ - + + org.apache.camel + camel-eip-documentation-enricher-maven-plugin + + + eip-documentation-enricher + package + + eip-documentation-enricher + + + ${project.build.directory}/schema/camel-spring.xsd + ${project.build.directory}/schema/camel-spring.xsd + + + + - diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DomParser.java b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DomParser.java index 4d447b99593bf..6149421a7aa90 100644 --- a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DomParser.java +++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DomParser.java @@ -16,8 +16,6 @@ */ package org.apache.camel.maven; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; @@ -26,8 +24,6 @@ import javax.xml.xpath.XPathExpressionException; public class DomParser { - private final Logger logger = LoggerFactory.getLogger(DomParser.class); - public NodeList findElementsAndTypes(Document document, XPath xPath) throws XPathExpressionException { return (NodeList) xPath.compile("/xs:schema/xs:element") diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/EipDocumentationGeneratorMojo.java b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/EipDocumentationGeneratorMojo.java index 1a8b5d834aa59..7a4a0c12c2328 100644 --- a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/EipDocumentationGeneratorMojo.java +++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/EipDocumentationGeneratorMojo.java @@ -23,8 +23,6 @@ import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.ResolutionScope; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; @@ -48,7 +46,6 @@ @Mojo(name = "eip-documentation-enricher", requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, requiresProject = true, defaultPhase = LifecyclePhase.PACKAGE) public class EipDocumentationGeneratorMojo extends AbstractMojo { - private final Logger logger = LoggerFactory.getLogger(EipDocumentationGeneratorMojo.class); /** * Project's source directory as specified in the POM. @@ -59,7 +56,6 @@ public class EipDocumentationGeneratorMojo extends AbstractMojo { @Parameter(required = true) File outputCamelSchemaFile; -// @Parameter(defaultValue = "${project.build.directory}/../../../..//camel-core") @Parameter(defaultValue = "${project.build.directory}/../../..//camel-core") File camelCoreDir; @@ -97,7 +93,9 @@ private Map buildTypeToNameMap(NodeList elementsAndTypes) { String type = item.getAttribute(Constants.TYPE_ATTRIBUTE_NAME); if (name != null && type != null) { type = type.replaceAll("tns:", ""); - logger.debug("Putting attributes type:'{}', name:'{}'", name, type); + if (getLog().isDebugEnabled()) { + getLog().debug(String.format("Putting attributes type:'%s', name:'%s'", name, type)); + } typeToNameMap.put(type, name); } } diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/PackageHelper.java b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/PackageHelper.java index c99ca10fb3d96..67ec981c63eba 100644 --- a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/PackageHelper.java +++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/PackageHelper.java @@ -16,13 +16,14 @@ */ package org.apache.camel.maven; -import java.io.*; +import java.io.File; +import java.io.FileFilter; +import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; -import java.util.Set; public class PackageHelper { private PackageHelper() { From b5eb8c8caf94ce07120f82a53c54736ea0cd959b Mon Sep 17 00:00:00 2001 From: nkukhar Date: Sun, 8 Feb 2015 23:21:45 -0800 Subject: [PATCH 3/7] added missing files --- components/camel-blueprint/pom.xml | 19 +++++++++++++++++++ tooling/maven/pom.xml | 1 + 2 files changed, 20 insertions(+) diff --git a/components/camel-blueprint/pom.xml b/components/camel-blueprint/pom.xml index 674220b320982..80d746d26e85e 100644 --- a/components/camel-blueprint/pom.xml +++ b/components/camel-blueprint/pom.xml @@ -167,6 +167,25 @@ + + org.apache.camel + camel-eip-documentation-enricher-maven-plugin + + + eip-documentation-enricher + package + + eip-documentation-enricher + + + ${project.build.directory}/schema/camel-blueprint.xsd + ${project.build.directory}/schema/camel-blueprint.xsd + + + + + + diff --git a/tooling/maven/pom.xml b/tooling/maven/pom.xml index 7a14512a002a9..b203cd4adc0eb 100644 --- a/tooling/maven/pom.xml +++ b/tooling/maven/pom.xml @@ -32,6 +32,7 @@ camel-package-maven-plugin camel-maven-plugin + camel-eip-documentation-maven-plugin guice-maven-plugin camel-api-component-maven-plugin From 9745de107f6ef1e6fd7ad6f205d1d5a061aed5ef Mon Sep 17 00:00:00 2001 From: nkukhar Date: Mon, 9 Feb 2015 22:48:10 -0800 Subject: [PATCH 4/7] pom updated --- tooling/maven/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tooling/maven/pom.xml b/tooling/maven/pom.xml index b203cd4adc0eb..53d7d55647571 100644 --- a/tooling/maven/pom.xml +++ b/tooling/maven/pom.xml @@ -32,7 +32,7 @@ camel-package-maven-plugin camel-maven-plugin - camel-eip-documentation-maven-plugin + camel-eip-documentation-enricher-maven-plugin guice-maven-plugin camel-api-component-maven-plugin From a2d95fccbe896f4bbfa5c79de78126f331cb26a5 Mon Sep 17 00:00:00 2001 From: nkukhar Date: Mon, 9 Feb 2015 23:44:25 -0800 Subject: [PATCH 5/7] Unit tests added for PackageHelper class. --- .../apache/camel/maven/PackageHelperTest.java | 31 +++++++++++++++++++ .../src/test/resources/filecontent/a.txt | 1 + .../src/test/resources/json/a.json | 0 .../src/test/resources/json/b.json | 0 .../src/test/resources/json/c.txt | 0 5 files changed, 32 insertions(+) create mode 100644 tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/test/java/org/apache/camel/maven/PackageHelperTest.java create mode 100644 tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/test/resources/filecontent/a.txt create mode 100644 tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/test/resources/json/a.json create mode 100644 tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/test/resources/json/b.json create mode 100644 tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/test/resources/json/c.txt diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/test/java/org/apache/camel/maven/PackageHelperTest.java b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/test/java/org/apache/camel/maven/PackageHelperTest.java new file mode 100644 index 0000000000000..5e6f6ad745e32 --- /dev/null +++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/test/java/org/apache/camel/maven/PackageHelperTest.java @@ -0,0 +1,31 @@ +package org.apache.camel.maven; + +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + + +public class PackageHelperTest { + + @Test + public void testFileToString() throws Exception { + assertEquals("dk19i21)@+#(OR", PackageHelper.fileToString(new File( + this.getClass().getClassLoader().getResource("filecontent/a.txt").getFile()))); + } + + @Test + public void testFindJsonFiles() throws Exception { + Map jsonFiles = PackageHelper.findJsonFiles(new File( + this.getClass().getClassLoader().getResource("json").getFile())); + + assertTrue("Files a.json must be found", jsonFiles.containsKey("a")); + assertTrue("Files b.json must be found", jsonFiles.containsKey("b")); + assertFalse("File c.txt must not be found", jsonFiles.containsKey("c")); + } +} \ No newline at end of file diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/test/resources/filecontent/a.txt b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/test/resources/filecontent/a.txt new file mode 100644 index 0000000000000..5bed4907d6568 --- /dev/null +++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/test/resources/filecontent/a.txt @@ -0,0 +1 @@ +dk19i21)@+#(OR \ No newline at end of file diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/test/resources/json/a.json b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/test/resources/json/a.json new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/test/resources/json/b.json b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/test/resources/json/b.json new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/test/resources/json/c.txt b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/test/resources/json/c.txt new file mode 100644 index 0000000000000..e69de29bb2d1d From efcef74a7eec64c0569e808663e231863e1f8285 Mon Sep 17 00:00:00 2001 From: nkukhar Date: Mon, 9 Feb 2015 23:48:01 -0800 Subject: [PATCH 6/7] License and notice added --- .../src/main/resources/META-INF/LICENSE.txt | 203 ++++++++++++++++++ .../src/main/resources/META-INF/NOTICE.txt | 11 + 2 files changed, 214 insertions(+) create mode 100644 tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/resources/META-INF/LICENSE.txt create mode 100644 tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/resources/META-INF/NOTICE.txt diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/resources/META-INF/LICENSE.txt b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/resources/META-INF/LICENSE.txt new file mode 100644 index 0000000000000..6b0b1270ff0ca --- /dev/null +++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/resources/META-INF/LICENSE.txt @@ -0,0 +1,203 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/resources/META-INF/NOTICE.txt b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/resources/META-INF/NOTICE.txt new file mode 100644 index 0000000000000..2e215bf2e6b1f --- /dev/null +++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/resources/META-INF/NOTICE.txt @@ -0,0 +1,11 @@ + ========================================================================= + == NOTICE file corresponding to the section 4 d of == + == the Apache License, Version 2.0, == + == in this case for the Apache Camel distribution. == + ========================================================================= + + This product includes software developed by + The Apache Software Foundation (http://www.apache.org/). + + Please read the different LICENSE files present in the licenses directory of + this distribution. From 9b5868350a8be47f67cd8b9a1f19571a2c66b816 Mon Sep 17 00:00:00 2001 From: nkukhar Date: Mon, 9 Feb 2015 23:49:23 -0800 Subject: [PATCH 7/7] Documentation added, class/functions renaming --- .../camel/maven/CamelSpringNamespace.java | 7 ++- .../org/apache/camel/maven/Constants.java | 3 ++ .../camel/maven/DocumentationEnricher.java | 52 ++++++++++--------- .../maven/{DomParser.java => DomFinder.java} | 5 +- ...java => EipDocumentationEnricherMojo.java} | 19 +++++-- .../org/apache/camel/maven/PackageHelper.java | 6 ++- 6 files changed, 58 insertions(+), 34 deletions(-) rename tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/{DomParser.java => DomFinder.java} (94%) rename tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/{EipDocumentationGeneratorMojo.java => EipDocumentationEnricherMojo.java} (92%) diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/CamelSpringNamespace.java b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/CamelSpringNamespace.java index d64d0b92ac784..4466045a2d35e 100644 --- a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/CamelSpringNamespace.java +++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/CamelSpringNamespace.java @@ -19,14 +19,17 @@ import javax.xml.namespace.NamespaceContext; import java.util.Iterator; +/** + * Default namespace for xsd schema. + */ public class CamelSpringNamespace implements NamespaceContext { @Override public String getNamespaceURI(String prefix) { - if (prefix == null){ + if (prefix == null) { throw new IllegalArgumentException("The prefix cannot be null."); } - if ("xs".equals(prefix)){ + if ("xs".equals(prefix)) { return "http://www.w3.org/2001/XMLSchema"; } return null; diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/Constants.java b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/Constants.java index 18a9abf18cd0f..745026c2c8963 100644 --- a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/Constants.java +++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/Constants.java @@ -16,6 +16,9 @@ */ package org.apache.camel.maven; +/** + * Stores mojo related constants. + */ public final class Constants { private Constants(){} diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DocumentationEnricher.java b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DocumentationEnricher.java index 5a17dd7f0b36a..31ab995585e9d 100644 --- a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DocumentationEnricher.java +++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DocumentationEnricher.java @@ -26,7 +26,11 @@ import java.util.List; import java.util.Map; +/** + * Enriches xml document with documentation from json files. + */ public class DocumentationEnricher { + public void enrichTopLevelElementsDocumentation(Document document, NodeList elements, Map jsonFiles) @@ -35,12 +39,21 @@ public void enrichTopLevelElementsDocumentation(Document document, Element item = (Element) elements.item(i); String name = item.getAttribute(Constants.NAME_ATTRIBUTE_NAME); if (jsonFiles.containsKey(name)) { - addDocumentation(document, item, jsonFiles.get(name)); + addElementDocumentation(document, item, jsonFiles.get(name)); } } } - private void addDocumentation(Document document, Element item, File jsonFile) + public void enrichTypeAttributesDocumentation(Document document, + NodeList attributeElements, + File jsonFile) throws IOException { + for (int j = 0; j < attributeElements.getLength(); j++) { + Element item = (Element) attributeElements.item(j); + addAttributeDocumentation(item, jsonFile, document); + } + } + + private void addElementDocumentation(Document document, Element item, File jsonFile) throws IOException { List> rows = JsonSchemaHelper.parseJsonSchema (Constants.MODEL_ATTRIBUTE_NAME, PackageHelper.fileToString(jsonFile), false); @@ -53,28 +66,6 @@ private void addDocumentation(Document document, Element item, File jsonFile) } } - private void addDocumentation(Document document, Element item, String textContent) { - Element annotation = document.createElement(Constants.XS_ANNOTATION_ELEMENT_NAME); - Element documentation = document.createElement(Constants.XS_DOCUMENTATION_ELEMENT_NAME); - documentation.setAttribute("xml:lang", "en"); - documentation.setTextContent(textContent); - annotation.appendChild(documentation); - if (item.getFirstChild() != null){ - item.insertBefore(annotation, item.getFirstChild()); - } else { - item.appendChild(annotation); - } - } - - public void enrichTypeAttributesDocumentation(Document document, - NodeList attributeElements, - File jsonFile) throws IOException { - for (int j = 0; j < attributeElements.getLength(); j++) { - Element item = (Element) attributeElements.item(j); - addAttributeDocumentation(item, jsonFile, document); - } - } - private void addAttributeDocumentation(Element item, File jsonFile, Document document) @@ -92,4 +83,17 @@ private void addAttributeDocumentation(Element item, } } } + + private void addDocumentation(Document document, Element item, String textContent) { + Element annotation = document.createElement(Constants.XS_ANNOTATION_ELEMENT_NAME); + Element documentation = document.createElement(Constants.XS_DOCUMENTATION_ELEMENT_NAME); + documentation.setAttribute("xml:lang", "en"); + documentation.setTextContent(textContent); + annotation.appendChild(documentation); + if (item.getFirstChild() != null){ + item.insertBefore(annotation, item.getFirstChild()); + } else { + item.appendChild(annotation); + } + } } diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DomParser.java b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DomFinder.java similarity index 94% rename from tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DomParser.java rename to tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DomFinder.java index 6149421a7aa90..c5c0aff38f307 100644 --- a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DomParser.java +++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DomFinder.java @@ -23,7 +23,10 @@ import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; -public class DomParser { +/** + * Finds xml elements where documentation can be added. + */ +public class DomFinder { public NodeList findElementsAndTypes(Document document, XPath xPath) throws XPathExpressionException { return (NodeList) xPath.compile("/xs:schema/xs:element") diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/EipDocumentationGeneratorMojo.java b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/EipDocumentationEnricherMojo.java similarity index 92% rename from tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/EipDocumentationGeneratorMojo.java rename to tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/EipDocumentationEnricherMojo.java index 7a4a0c12c2328..4b4a967635afb 100644 --- a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/EipDocumentationGeneratorMojo.java +++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/EipDocumentationEnricherMojo.java @@ -43,37 +43,46 @@ import java.util.HashMap; import java.util.Map; +/** + * Injects EIP documentation to camel schema. + */ @Mojo(name = "eip-documentation-enricher", requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, requiresProject = true, defaultPhase = LifecyclePhase.PACKAGE) -public class EipDocumentationGeneratorMojo extends AbstractMojo { +public class EipDocumentationEnricherMojo extends AbstractMojo { /** - * Project's source directory as specified in the POM. + * Path to camel EIP schema. */ @Parameter(required = true) File inputCamelSchemaFile; + /** + * Path to camel EIP schema with enriched documentation. + */ @Parameter(required = true) File outputCamelSchemaFile; + /** + * Path to camel core project root directory. + */ @Parameter(defaultValue = "${project.build.directory}/../../..//camel-core") File camelCoreDir; @Override public void execute() throws MojoExecutionException, MojoFailureException { File rootDir = new File(camelCoreDir, Constants.PATH_TO_MODEL_DIR); - DomParser domParser = new DomParser(); + DomFinder domFinder = new DomFinder(); DocumentationEnricher documentationEnricher = new DocumentationEnricher(); Map jsonFiles = PackageHelper.findJsonFiles(rootDir); XPath xPath = buildXPath(new CamelSpringNamespace()); try { Document document = buildNamespaceAwareDocument(inputCamelSchemaFile); - NodeList elementsAndTypes = domParser.findElementsAndTypes(document, xPath); + NodeList elementsAndTypes = domFinder.findElementsAndTypes(document, xPath); documentationEnricher.enrichTopLevelElementsDocumentation (document, elementsAndTypes, jsonFiles); Map typeToNameMap = buildTypeToNameMap(elementsAndTypes); for (Map.Entry entry : typeToNameMap.entrySet()) { - NodeList attributeElements = domParser.findAttributesElements(document, xPath, entry.getKey()); + NodeList attributeElements = domFinder.findAttributesElements(document, xPath, entry.getKey()); if (jsonFiles.containsKey(entry.getValue())){ documentationEnricher.enrichTypeAttributesDocumentation (document, attributeElements, jsonFiles.get(entry.getValue())); diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/PackageHelper.java b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/PackageHelper.java index 67ec981c63eba..247009840c733 100644 --- a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/PackageHelper.java +++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/PackageHelper.java @@ -25,9 +25,11 @@ import java.util.HashMap; import java.util.Map; +/** + * Utility class to find, read json files. + */ public class PackageHelper { - private PackageHelper() { - } + private PackageHelper() {} public static String fileToString(File file) throws IOException { byte[] encoded = Files.readAllBytes(Paths.get(file.toURI()));