cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1328894 - in /cxf/trunk/tools/wadlto/jaxrs/src: main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java
Date Sun, 22 Apr 2012 16:28:17 GMT
Author: sergeyb
Date: Sun Apr 22 16:28:17 2012
New Revision: 1328894

URL: http://svn.apache.org/viewvc?rev=1328894&view=rev
Log:
[CXF-4256] Some updates to the code generating enum classes

Modified:
    cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
    cxf/trunk/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java

Modified: cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java?rev=1328894&r1=1328893&r2=1328894&view=diff
==============================================================================
--- cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
(original)
+++ cxf/trunk/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
Sun Apr 22 16:28:17 2012
@@ -905,7 +905,7 @@ public class SourceGenerator {
             .append(";").append(getLineSep()).append(getLineSep());
         
         sbCode.append("public enum " + clsName);
-        sbCode.append(" {" + getLineSep());
+        openBlock(sbCode);
         
         for (int i = 0; i < options.size(); i++) {
             String value = options.get(i).getAttribute("value");
@@ -920,31 +920,53 @@ public class SourceGenerator {
         }
         
         sbCode.append(TAB).append("private String value;").append(getLineSep());
-        sbCode.append(TAB).append("private ").append(clsName).append("(String v) ").append("{")
-            .append(getLineSep());
-        sbCode.append(TAB).append(TAB).append("this.value = v;").append(getLineSep());
-        sbCode.append(TAB).append("}").append(getLineSep());
-        
-        sbCode.append(TAB).append("public static ").append(clsName).append(" fromString(String
value) ")
-            .append("{").append(getLineSep());
-        sbCode.append(TAB).append(TAB);
-        sbCode.append("if (").append("value").append(" != null) {").append(getLineSep());
-        sbCode.append(TAB).append(TAB).append(TAB);
-        sbCode.append("for (").append(clsName).append(" v : ").append(clsName).append(".values())
{")
-            .append(getLineSep());
-        sbCode.append(TAB).append(TAB).append(TAB).append(TAB);
-        sbCode.append("if (value.equalsIgnoreCase(v.value)) {").append(getLineSep());
-        sbCode.append(TAB).append(TAB).append(TAB).append(TAB).append(TAB).append("return
v;")
-            .append(getLineSep());
-        sbCode.append(TAB).append(TAB).append(TAB).append(TAB).append("}").append(getLineSep());
-        sbCode.append(TAB).append(TAB).append(TAB).append("}").append(getLineSep());
-        sbCode.append(TAB).append(TAB).append("}").append(getLineSep());
-        sbCode.append(TAB).append(TAB).append("return null;").append(getLineSep());
-        sbCode.append(TAB).append("}").append(getLineSep());
+        sbCode.append(TAB).append("private ").append(clsName).append("(String v)");
+        openBlock(sbCode);
+        tab(sbCode, 2).append("this.value = v;").append(getLineSep());
+        tabCloseBlock(sbCode, 1);
+        
+        sbCode.append(TAB).append("public static ")
+            .append(clsName).append(" fromString(String value)");
+        openBlock(sbCode);    
+        tab(sbCode, 2);
+        sbCode.append("if (").append("value").append(" != null)");
+        openBlock(sbCode);
+        tab(sbCode, 3);
+        sbCode.append("for (").append(clsName).append(" v : ")
+            .append(clsName).append(".values())");
+        openBlock(sbCode);    
+        tab(sbCode, 4);
+        sbCode.append("if (value.equalsIgnoreCase(v.value))");
+        openBlock(sbCode);
+        tab(sbCode, 5);
+        sbCode.append("return v;").append(getLineSep());
+        tabCloseBlock(sbCode, 4);
+        tabCloseBlock(sbCode, 3);
+        tabCloseBlock(sbCode, 2);
+        tab(sbCode, 2);
+        sbCode.append("throw new IllegalArgumentException();").append(getLineSep());
+        tabCloseBlock(sbCode, 1);
         sbCode.append("}");
         createJavaSourceFile(src, new QName(classPackage, clsName), sbCode, sbImports, false);
     }
     
+    private static StringBuilder tab(StringBuilder sb, int count) {
+        for (int i = 0; i < count; i++) {
+            sb.append(TAB);
+        }
+        return sb;
+    }
+    
+    private StringBuilder tabCloseBlock(StringBuilder sb, int count) {
+        tab(sb, count).append("}").append(getLineSep());
+        return sb;
+    }
+    
+    private StringBuilder openBlock(StringBuilder sb) {
+        sb.append(" {").append(getLineSep());
+        return sb;
+    }
+    
     private String getTypicalClassName(String name) { 
         String theName = name.toUpperCase();
         if (theName.length() == 1) {

Modified: cxf/trunk/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java?rev=1328894&r1=1328893&r2=1328894&view=diff
==============================================================================
--- cxf/trunk/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java
(original)
+++ cxf/trunk/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java
Sun Apr 22 16:28:17 2012
@@ -32,9 +32,42 @@ import org.junit.Test;
 
 public class JAXRSContainerTest extends ProcessorTestBase {
 
+    public enum Theid {
+        ONE("one"),
+        TWO("two");
+        private String value;
+        private Theid(String v) {
+            this.value = v;
+        }
+        public static Theid fromString(String value) {
+            if (value != null) {
+                for (Theid v : Theid.values()) {
+                    if (value.equalsIgnoreCase(v.value)) {
+                        return v;
+                    }
+                }
+            }
+            throw new IllegalArgumentException();
+        }
+    }
+    
+    @Test    
+    public void test1() {
+        try {
+        assertSame(Theid.ONE, Theid.fromString("on"));
+        fail();
+        } catch (IllegalArgumentException ex) {
+            
+        }
+    }
+    
+    
     @Test    
     public void testCodeGenInterfaces() {
         try {
+            
+            
+            
             JAXRSContainer container = new JAXRSContainer(null);
 
             ToolContext context = new ToolContext();



Mime
View raw message