CAMEL-8197 Added maven plagin to inject EIP documentation into the spring and blueprint XML DSL.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/5279296d Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/5279296d Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/5279296d Branch: refs/heads/master Commit: 5279296d5b082cae02c84826bd145d449b7fe6a2 Parents: 0eb1545 Author: nkukhar <kukha...@gmail.com> Authored: Sun Feb 8 23:19:06 2015 -0800 Committer: Claus Ibsen <davscl...@apache.org> Committed: Tue Feb 10 09:50:07 2015 +0100 ---------------------------------------------------------------------- .../pom.xml | 139 +++++++++++++++++++ .../camel/maven/CamelSpringNamespace.java | 44 ++++++ .../java/org/apache/camel/maven/Constants.java | 36 +++++ .../camel/maven/DocumentationEnricher.java | 95 +++++++++++++ .../java/org/apache/camel/maven/DomParser.java | 42 ++++++ .../maven/EipDocumentationGeneratorMojo.java | 135 ++++++++++++++++++ .../org/apache/camel/maven/PackageHelper.java | 64 +++++++++ 7 files changed, 555 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/5279296d/tooling/maven/camel-eip-documentation-enricher-maven-plugin/pom.xml ---------------------------------------------------------------------- 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 0000000..74e3f53 --- /dev/null +++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/pom.xml @@ -0,0 +1,139 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.camel</groupId> + <artifactId>maven-plugins</artifactId> + <version>2.15-SNAPSHOT</version> + </parent> + + <artifactId>camel-eip-documentation-enricher-maven-plugin</artifactId> + <packaging>maven-plugin</packaging> + <name>Camel :: Maven Plugins :: Camel EIP Documentation Enricher Plugin</name> + <description>Adds documentation to camel xsd schema</description> + <dependencies> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-core</artifactId> + </dependency> + + <dependency> + <groupId>org.apache.maven.reporting</groupId> + <artifactId>maven-reporting-api</artifactId> + </dependency> + <dependency> + <groupId>org.apache.maven.reporting</groupId> + <artifactId>maven-reporting-impl</artifactId> + </dependency> + <dependency> + <groupId>org.codehaus.plexus</groupId> + <artifactId>plexus-utils</artifactId> + </dependency> + <dependency> + <groupId>org.apache.maven.plugin-tools</groupId> + <artifactId>maven-plugin-annotations</artifactId> + <version>3.3</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-plugin-descriptor</artifactId> + <version>2.2.1</version> + <exclusions> + <exclusion> + <groupId>log4j</groupId> + <artifactId>log4j</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-artifact</artifactId> + <version>2.2.1</version> + </dependency> + + <!-- add some logging to the classpath --> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>log4j-over-slf4j</artifactId> + <version>${slf4j-version}</version> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-simple</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <!-- Camel annotations in provided scope to avoid compile errors in IDEs --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>spi-annotations</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> + </dependencies> + + <build> + <pluginManagement> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-plugin-plugin</artifactId> + <version>3.3</version> + <configuration> + <!-- see http://jira.codehaus.org/browse/MNG-5346 --> + <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound> + </configuration> + <executions> + <execution> + <id>mojo-descriptor</id> + <goals> + <goal>descriptor</goal> + </goals> + </execution> + <execution> + <id>help-goal</id> + <goals> + <goal>helpmojo</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </pluginManagement> + <plugins> + </plugins> + </build> + +</project> http://git-wip-us.apache.org/repos/asf/camel/blob/5279296d/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/CamelSpringNamespace.java ---------------------------------------------------------------------- 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 0000000..d64d0b9 --- /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"); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/5279296d/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/Constants.java ---------------------------------------------------------------------- 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 0000000..18a9abf --- /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"; +} http://git-wip-us.apache.org/repos/asf/camel/blob/5279296d/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DocumentationEnricher.java ---------------------------------------------------------------------- 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 0000000..5a17dd7 --- /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<String, File> 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<Map<String, String>> rows = JsonSchemaHelper.parseJsonSchema + (Constants.MODEL_ATTRIBUTE_NAME, PackageHelper.fileToString(jsonFile), false); + for (Map<String, String> 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<Map<String, String>> rows = JsonSchemaHelper.parseJsonSchema + (Constants.PROPERTIES_ATTRIBUTE_NAME, PackageHelper.fileToString(jsonFile), true); + for (Map<String, String> 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; + } + } + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/5279296d/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/DomParser.java ---------------------------------------------------------------------- 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 0000000..4d447b9 --- /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); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/5279296d/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/EipDocumentationGeneratorMojo.java ---------------------------------------------------------------------- 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 0000000..1a8b5d8 --- /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<String, File> 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<String, String> typeToNameMap = buildTypeToNameMap(elementsAndTypes); + for (Map.Entry<String, String> 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<String, String> buildTypeToNameMap(NodeList elementsAndTypes) { + Map<String, String> 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); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/5279296d/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/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 0000000..c99ca10 --- /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<String, File> findJsonFiles(File rootDir) { + Map<String, File> results = new HashMap<>(); + findJsonFiles0(rootDir, results, new CamelComponentsModelFilter()); + return results; + } + + private static void findJsonFiles0(File dir, Map<String, File> 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); + } + } +}