You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by dh...@apache.org on 2014/06/10 21:51:45 UTC

[17/35] git commit: Updated ApiComponentGeneratorMojo to generate ApiName enumeration

Updated ApiComponentGeneratorMojo to generate ApiName enumeration


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

Branch: refs/heads/master
Commit: dd70b99d643b5257ca90694338df5f9cc50a02fd
Parents: a38476c
Author: Dhiraj Bokde <dh...@yahoo.com>
Authored: Mon Jun 2 15:47:54 2014 -0700
Committer: Dhiraj Bokde <dh...@yahoo.com>
Committed: Tue Jun 10 12:48:32 2014 -0700

----------------------------------------------------------------------
 .../camel/maven/ApiComponentGeneratorMojo.java  | 53 +++++++++++++++---
 .../src/main/resources/api-collection.vm        |  4 +-
 .../src/main/resources/api-name-enum.vm         | 56 ++++++++++++++++++++
 3 files changed, 103 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/dd70b99d/tooling/maven/camel-component-util-maven-plugin/src/main/java/org/apache/camel/maven/ApiComponentGeneratorMojo.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-component-util-maven-plugin/src/main/java/org/apache/camel/maven/ApiComponentGeneratorMojo.java b/tooling/maven/camel-component-util-maven-plugin/src/main/java/org/apache/camel/maven/ApiComponentGeneratorMojo.java
index b967817..d0d4b97 100644
--- a/tooling/maven/camel-component-util-maven-plugin/src/main/java/org/apache/camel/maven/ApiComponentGeneratorMojo.java
+++ b/tooling/maven/camel-component-util-maven-plugin/src/main/java/org/apache/camel/maven/ApiComponentGeneratorMojo.java
@@ -42,33 +42,70 @@ public class ApiComponentGeneratorMojo extends AbstractGeneratorMojo {
             throw new MojoExecutionException("One or more API proxies are required");
         }
 
-        // TODO generate Component classes
         // generate ApiCollection
-        mergeTemplate(getApiCollectionContext(), getApiCollectionFile(), "/api-collection.vm");
+        mergeTemplate(getApiContext(), getApiCollectionFile(), "/api-collection.vm");
+
+        // generate ApiName
+        mergeTemplate(getApiContext(), getApiNameFile(), "/api-name-enum.vm");
     }
 
-    private VelocityContext getApiCollectionContext() {
+    private VelocityContext getApiContext() {
         final VelocityContext context = new VelocityContext();
         context.put("componentName", componentName);
-        context.put("collectionName", getApiCollectionName());
         context.put("packageName", outPackage);
         context.put("apis", apis);
         context.put("helper", getClass());
+        context.put("collectionName", getApiCollectionName());
+        context.put("apiNameEnum", getApiNameEnum());
         return context;
     }
 
+    private String getApiCollectionName() {
+        return componentName + "ApiCollection";
+    }
+
+    private String getApiNameEnum() {
+        return componentName + "ApiName";
+    }
+
     private File getApiCollectionFile() {
-        final StringBuilder fileName = new StringBuilder();
-        fileName.append(outPackage.replaceAll("\\.", File.separator)).append(File.separator);
+        final StringBuilder fileName = getFileBuilder();
         fileName.append(getApiCollectionName()).append(".java");
         return new File(generatedSrcDir, fileName.toString());
     }
 
-    private String getApiCollectionName() {
-        return componentName + "ApiCollection";
+    private File getApiNameFile() {
+        final StringBuilder fileName = getFileBuilder();
+        fileName.append(getApiNameEnum()).append(".java");
+        return new File(generatedSrcDir, fileName.toString());
+    }
+
+    private StringBuilder getFileBuilder() {
+        final StringBuilder fileName = new StringBuilder();
+        fileName.append(outPackage.replaceAll("\\.", File.separator)).append(File.separator);
+        return fileName;
     }
 
     public static String getApiMethod(String proxyClass) {
         return proxyClass.substring(proxyClass.lastIndexOf('.') + 1) + "ApiMethod";
     }
+
+    public static String getEnumConstant(String enumValue) {
+        if (enumValue == null || enumValue.isEmpty()) {
+            return "DEFAULT";
+        }
+        StringBuilder builder = new StringBuilder();
+        if (!Character.isJavaIdentifierStart(enumValue.charAt(0))) {
+            builder.append('_');
+        }
+        for (char c : enumValue.toCharArray()) {
+            char upperCase = Character.toUpperCase(c);
+            if (!Character.isJavaIdentifierPart(upperCase)) {
+                builder.append('_');
+            } else {
+                builder.append(upperCase);
+            }
+        }
+        return builder.toString();
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/dd70b99d/tooling/maven/camel-component-util-maven-plugin/src/main/resources/api-collection.vm
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-component-util-maven-plugin/src/main/resources/api-collection.vm b/tooling/maven/camel-component-util-maven-plugin/src/main/resources/api-collection.vm
index 7529712..b7213c6 100644
--- a/tooling/maven/camel-component-util-maven-plugin/src/main/resources/api-collection.vm
+++ b/tooling/maven/camel-component-util-maven-plugin/src/main/resources/api-collection.vm
@@ -30,7 +30,7 @@ import org.apache.camel.util.component.ApiMethodHelper;
 /**
  * Camel {@link ApiCollection} for $componentName
  */
-public final class $collectionName extends ApiCollection {
+public final class $collectionName extends ApiCollection<${apiNameEnum}> {
 
     private static $collectionName collection;
 
@@ -42,7 +42,7 @@ public final class $collectionName extends ApiCollection {
         aliases.put("$alias.Key", "$alias.Value");
 #end
 #set( $apiMethod = $helper.getApiMethod($api.ProxyClass) )
-        apis.put("$api.ApiName", new ApiMethodHelper<$apiMethod>(${apiMethod}.class, aliases));
+        apis.put(${apiNameEnum}.${helper.getEnumConstant($api.ApiName)}, new ApiMethodHelper<$apiMethod>(${apiMethod}.class, aliases));
 #end
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/dd70b99d/tooling/maven/camel-component-util-maven-plugin/src/main/resources/api-name-enum.vm
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-component-util-maven-plugin/src/main/resources/api-name-enum.vm b/tooling/maven/camel-component-util-maven-plugin/src/main/resources/api-name-enum.vm
new file mode 100644
index 0000000..f4fa253
--- /dev/null
+++ b/tooling/maven/camel-component-util-maven-plugin/src/main/resources/api-name-enum.vm
@@ -0,0 +1,56 @@
+## ------------------------------------------------------------------------
+## 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.
+## ------------------------------------------------------------------------
+## api-name-enum.vm
+/*
+ * Camel ApiName Enumeration generated by camel-component-util-maven-plugin
+ * Generated on: $generatedDate
+ */
+package $packageName;
+
+import org.apache.camel.util.component.ApiName;
+
+/**
+ * Camel {@link ApiName} Enumeration for Component $componentName
+ */
+public enum $apiNameEnum implements ApiName {
+
+#foreach ( $api in $apis )
+#set ( $apiName = $api.ApiName )
+    ${helper.getEnumConstant($apiName)}("$apiName")#if ( $foreach.hasNext ),#else;#end
+
+#end
+
+    private final String name;
+
+    private ${apiNameEnum}(String name) {
+        this.name = name;
+    }
+
+    @Override
+    public String getName() {
+        return name;
+    }
+
+    public static $apiNameEnum fromValue(String value) {
+        for ($apiNameEnum api : ${apiNameEnum}.values()) {
+            if (api.name.equals(value)) {
+                return api;
+            }
+        }
+        throw new IllegalArgumentException("Invalid value " + value);
+    }
+}