camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [3/4] camel git commit: Minor refactoring and bug fixes.
Date Sun, 15 Mar 2015 06:39:40 GMT
Minor refactoring and bug fixes.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/c87732db
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c87732db
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c87732db

Branch: refs/heads/master
Commit: c87732dbdbce014930ab6e6d60d38b11e736ab3f
Parents: f88d6dd
Author: nkukhar <kukhar.n@gmail.com>
Authored: Sat Mar 14 01:06:46 2015 -0700
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sun Mar 15 07:38:49 2015 +0100

----------------------------------------------------------------------
 .../camel/maven/CamelSpringNamespace.java       |   4 +-
 .../java/org/apache/camel/maven/Constants.java  |   2 +
 .../maven/EipDocumentationEnricherMojo.java     | 138 +++++++++----------
 .../java/org/apache/camel/maven/XmlHelper.java  |  55 ++++++++
 4 files changed, 121 insertions(+), 78 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/c87732db/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
index 3daf1d3..ee443e3 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
@@ -29,8 +29,8 @@ public class CamelSpringNamespace implements NamespaceContext {
         if (prefix == null) {
             throw new IllegalArgumentException("The prefix cannot be null.");
         }
-        if ("xs".equals(prefix)) {
-            return "http://www.w3.org/2001/XMLSchema";
+        if (Constants.XML_SCHEMA_NAMESPACE_PREFIX.equals(prefix)) {
+            return Constants.XML_SCHEMA_NAMESPACE_URI;
         }
         return null;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/c87732db/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
index ebfd846..e26ce78 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
@@ -27,6 +27,8 @@ public final class Constants {
     public static final int WRAP_LENGTH = 80;
 
     // XML constants.
+    public static final String XML_SCHEMA_NAMESPACE_PREFIX = "xs";
+    public static final String XML_SCHEMA_NAMESPACE_URI = "http://www.w3.org/2001/XMLSchema";
     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";

http://git-wip-us.apache.org/repos/asf/camel/blob/c87732db/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/EipDocumentationEnricherMojo.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/EipDocumentationEnricherMojo.java
b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/EipDocumentationEnricherMojo.java
index db6ae1f..031e7d7 100644
--- a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/EipDocumentationEnricherMojo.java
+++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/EipDocumentationEnricherMojo.java
@@ -24,26 +24,18 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
-import javax.xml.namespace.NamespaceContext;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerConfigurationException;
 import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
 import javax.xml.xpath.XPath;
 import javax.xml.xpath.XPathExpressionException;
-import javax.xml.xpath.XPathFactory;
 
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
-import org.xml.sax.SAXException;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
@@ -63,50 +55,59 @@ public class EipDocumentationEnricherMojo extends AbstractMojo {
      * Path to camel EIP schema.
      */
     @Parameter(required = true)
-    File inputCamelSchemaFile;
+    public File inputCamelSchemaFile;
 
     /**
      * Path to camel EIP schema with enriched documentation.
      */
     @Parameter(required = true)
-    File outputCamelSchemaFile;
+    public File outputCamelSchemaFile;
 
     /**
      * Path to camel core project root directory.
      */
     @Parameter(defaultValue = "${project.build.directory}/../../..//camel-core")
-    File camelCoreDir;
+    public File camelCoreDir;
 
     @Override
     public void execute() throws MojoExecutionException, MojoFailureException {
-        Set<String> injectedTypes = new HashSet<String>();
+        validateExists(inputCamelSchemaFile, "inputCamelSchemaFile");
+        validateIsFile(inputCamelSchemaFile, "inputCamelSchemaFile");
+        validateExists(camelCoreDir, "camelCoreDir");
+        validateIsDirectory(camelCoreDir, "camelCoreDir");
+        try {
+            runPlugin();
+        } catch (Exception e) {
+            throw new MojoExecutionException("Error during plugin execution", e);
+        }
+    }
+
+    private void runPlugin() throws Exception {
         File rootDir = new File(camelCoreDir, Constants.PATH_TO_MODEL_DIR);
-        Document document = buildNamespaceAwareDocument(inputCamelSchemaFile);
-        XPath xPath = buildXPath(new CamelSpringNamespace());
+        Document document = XmlHelper.buildNamespaceAwareDocument(inputCamelSchemaFile);
+        XPath xPath = XmlHelper.buildXPath(new CamelSpringNamespace());
         DomFinder domFinder = new DomFinder(document, xPath);
         DocumentationEnricher documentationEnricher = new DocumentationEnricher(document);
         Map<String, File> jsonFiles = PackageHelper.findJsonFiles(rootDir);
-        try {
-            NodeList elementsAndTypes = domFinder.findElementsAndTypes();
-            documentationEnricher.enrichTopLevelElementsDocumentation(elementsAndTypes, jsonFiles);
-            Map<String, String> typeToNameMap = buildTypeToNameMap(elementsAndTypes);
-            for (Map.Entry<String, String> entry : typeToNameMap.entrySet()) {
-                String elementType = entry.getKey();
-                String elementName = entry.getValue();
-                if (jsonFileExistsForElement(jsonFiles, elementName)) {
-                    injectAttributesDocumentation(domFinder,
-                            documentationEnricher,
-                            jsonFiles.get(elementName),
-                            elementType,
-                            injectedTypes);
-                }
+
+        NodeList elementsAndTypes = domFinder.findElementsAndTypes();
+        documentationEnricher.enrichTopLevelElementsDocumentation(elementsAndTypes, jsonFiles);
+        Map<String, String> typeToNameMap = buildTypeToNameMap(elementsAndTypes);
+        Set<String> injectedTypes = new HashSet<String>();
+
+        for (Map.Entry<String, String> entry : typeToNameMap.entrySet()) {
+            String elementType = entry.getKey();
+            String elementName = entry.getValue();
+            if (jsonFileExistsForElement(jsonFiles, elementName)) {
+                injectAttributesDocumentation(domFinder,
+                        documentationEnricher,
+                        jsonFiles.get(elementName),
+                        elementType,
+                        injectedTypes);
             }
-            saveToFile(document, outputCamelSchemaFile, buildTransformer());
-        } catch (XPathExpressionException e) {
-            throw new MojoExecutionException("Error during documentation enrichment", e);
-        } catch (IOException e) {
-            throw new MojoExecutionException("Error during documentation enrichment", e);
         }
+
+        saveToFile(document, outputCamelSchemaFile, XmlHelper.buildTransformer());
     }
 
     private boolean jsonFileExistsForElement(Map<String, File> jsonFiles,
@@ -114,20 +115,28 @@ public class EipDocumentationEnricherMojo extends AbstractMojo {
         return jsonFiles.containsKey(elementName);
     }
 
+    /**
+     * Recursively injects documentation to complex type attributes and it's parents.
+     */
     private void injectAttributesDocumentation(DomFinder domFinder,
                                                DocumentationEnricher documentationEnricher,
                                                File jsonFile,
                                                String type,
                                                Set<String> injectedTypes) throws XPathExpressionException,
IOException {
+        if (injectedTypes.contains(type)) {
+            return;
+        }
+
+        injectedTypes.add(type);
         NodeList attributeElements = domFinder.findAttributesElements(type);
         if (attributeElements.getLength() > 0) {
             documentationEnricher.enrichTypeAttributesDocumentation(attributeElements, jsonFile);
-            injectedTypes.add(type);
-            String baseType = domFinder.findBaseType(type);
+        }
+
+        String baseType = domFinder.findBaseType(type);
+        if (baseType != null && !StringUtils.isEmpty(baseType)) {
             baseType = truncateTypeNamespace(baseType);
-            if (baseType != null && !injectedTypes.contains(baseType)) {
-                injectAttributesDocumentation(domFinder, documentationEnricher, jsonFile,
baseType, injectedTypes);
-            }
+            injectAttributesDocumentation(domFinder, documentationEnricher, jsonFile, baseType,
injectedTypes);
         }
     }
 
@@ -152,51 +161,28 @@ public class EipDocumentationEnricherMojo extends AbstractMojo {
         return baseType.replaceAll("tns:", "");
     }
 
-    private XPath buildXPath(NamespaceContext namespaceContext) {
-        XPath xPath = XPathFactory.newInstance().newXPath();
-        xPath.setNamespaceContext(namespaceContext);
-        return xPath;
+
+    private void saveToFile(Document document, File outputFile, Transformer transformer)
throws FileNotFoundException, TransformerException {
+        StreamResult result = new StreamResult(new FileOutputStream(outputFile));
+        DOMSource source = new DOMSource(document);
+        transformer.transform(source, result);
     }
 
-    private Transformer buildTransformer() throws MojoExecutionException {
-        Transformer transformer;
-        try {
-            transformer = TransformerFactory.newInstance().newTransformer();
-            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
-            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
-        } catch (TransformerConfigurationException e) {
-            throw new MojoExecutionException("Error during building transformer", e);
+    private void validateIsFile(File file, String name) throws MojoExecutionException {
+        if (!file.isFile()) {
+            throw new MojoExecutionException(name + "is not a file");
         }
-        return transformer;
     }
 
-    public Document buildNamespaceAwareDocument(File xml) throws MojoExecutionException {
-        Document result;
-        DocumentBuilder builder;
-        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-        factory.setNamespaceAware(true);
-        try {
-            builder = factory.newDocumentBuilder();
-            result =  builder.parse(xml);
-        } catch (SAXException e) {
-            throw new MojoExecutionException("Error during building a document", e);
-        } catch (ParserConfigurationException e) {
-            throw new MojoExecutionException("Error during building a document", e);
-        } catch (IOException  e) {
-            throw new MojoExecutionException("Error during building a document", e);
+    private void validateIsDirectory(File file, String name) throws MojoExecutionException
{
+        if (!file.isDirectory()) {
+            throw new MojoExecutionException(name + "is not a directory");
         }
-        return result;
     }
 
-    private void saveToFile(Document document, File outputFile, Transformer transformer)
throws MojoExecutionException {
-        try {
-            StreamResult result = new StreamResult(new FileOutputStream(outputFile));
-            DOMSource source = new DOMSource(document);
-            transformer.transform(source, result);
-        } catch (TransformerException e) {
-            throw new MojoExecutionException("Error during saving to file", e);
-        } catch (FileNotFoundException e) {
-            throw new MojoExecutionException("Error during saving to file", e);
+    private void validateExists(File file, String name) throws MojoExecutionException {
+        if (file == null || !file.exists()) {
+            throw new MojoExecutionException(name + " does not exist");
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/c87732db/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/XmlHelper.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/XmlHelper.java
b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/XmlHelper.java
new file mode 100644
index 0000000..7136c5c
--- /dev/null
+++ b/tooling/maven/camel-eip-documentation-enricher-maven-plugin/src/main/java/org/apache/camel/maven/XmlHelper.java
@@ -0,0 +1,55 @@
+/**
+ * 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.File;
+import java.io.IOException;
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathFactory;
+
+import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
+
+public final class XmlHelper {
+    private XmlHelper() { }
+
+    public static Document buildNamespaceAwareDocument(File xml) throws SAXException, ParserConfigurationException,
IOException {
+        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+        factory.setNamespaceAware(true);
+        return factory.newDocumentBuilder().parse(xml);
+    }
+
+    public static 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 static XPath buildXPath(NamespaceContext namespaceContext) {
+        XPath xPath = XPathFactory.newInstance().newXPath();
+        xPath.setNamespaceContext(namespaceContext);
+        return xPath;
+    }
+}


Mime
View raw message