cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1188778 - in /cxf/branches/2.4.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/codegen/ tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/ tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/ ...
Date Tue, 25 Oct 2011 16:45:54 GMT
Author: sergeyb
Date: Tue Oct 25 16:45:53 2011
New Revision: 1188778

URL: http://svn.apache.org/viewvc?rev=1188778&view=rev
Log:
Merged revisions 1188773 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1188773 | sergeyb | 2011-10-25 17:39:33 +0100 (Tue, 25 Oct 2011) | 1 line
  
  [CXF-3881] Optionally generating enums
........

Modified:
    cxf/branches/2.4.x-fixes/   (props changed)
    cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/codegen/SourceGenerator.java
    cxf/branches/2.4.x-fixes/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/WadlToolConstants.java
    cxf/branches/2.4.x-fixes/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainer.java
    cxf/branches/2.4.x-fixes/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/jaxrs-toolspec.xml
    cxf/branches/2.4.x-fixes/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java

Propchange: cxf/branches/2.4.x-fixes/
------------------------------------------------------------------------------
    svn:mergeinfo = /cxf/trunk:1188773

Propchange: cxf/branches/2.4.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/codegen/SourceGenerator.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/codegen/SourceGenerator.java?rev=1188778&r1=1188777&r2=1188778&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/codegen/SourceGenerator.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/codegen/SourceGenerator.java
Tue Oct 25 16:45:53 2011
@@ -147,6 +147,8 @@ public class SourceGenerator {
     private String resourcePackageName;
     private String resourceName;
     private String wadlPath;
+    private String wadlNamespace = WadlGenerator.WADL_NS;
+    private boolean generateEnums;
     
     private Map<String, String> properties; 
     
@@ -172,6 +174,18 @@ public class SourceGenerator {
         supportMultipleXmlReps = support;
     }
     
+    public void setWadlNamespace(String ns) {
+        this.wadlNamespace = ns;
+    }
+    
+    public String getWadlNamespace() {
+        return wadlNamespace;
+    }
+    
+    public void setGenerateEnums(boolean generate) {
+        this.generateEnums = generate;
+    }
+    
     private String getClassPackageName(String wadlPackageName) {
         if (resourcePackageName != null) {
             return resourcePackageName;
@@ -231,13 +245,13 @@ public class SourceGenerator {
         for (int i = 0; i < resourceEls.size(); i++) {
             Element resource = getResourceElement(app, resourceEls.get(i), gInfo, typeClassNames,

                                                   resourceEls.get(i).getAttribute("type"),
src);
-            writeResourceClass(app, resource, 
-                               new ContextInfo(typeClassNames, gInfo, generateInterfaces),

-                               src, true);
+            writeResourceClass(resource, 
+                               new ContextInfo(app, src, typeClassNames, gInfo, generateInterfaces),

+                               true);
             if (generateInterfaces && generateImpl) {
-                writeResourceClass(app, resource, 
-                                   new ContextInfo(typeClassNames, gInfo, false), 
-                                   src, true);
+                writeResourceClass(resource, 
+                                   new ContextInfo(app, src, typeClassNames, gInfo, false),

+                                   true);
             }
             if (resourceName != null) {
                 break;
@@ -349,9 +363,9 @@ public class SourceGenerator {
         
     }
     
-    private void writeResourceClass(Application app, Element rElement,
+    private void writeResourceClass(Element rElement,
                                     ContextInfo info, 
-                                    File src, boolean isRoot) {
+                                    boolean isRoot) {
         String resourceId = resourceName != null 
             ? resourceName : rElement.getAttribute("id");
         if (resourceId.length() == 0) {
@@ -404,29 +418,29 @@ public class SourceGenerator {
         writeImplementsInterface(sbCode, qname.getLocalPart(), info.isInterfaceGenerated());
             
         sbCode.append(" {" + getLineSep() + getLineSep());
         
-        writeMethods(rElement, imports, sbCode, info, resourceId, isRoot, "");
+        writeMethods(rElement, classPackage, imports, sbCode, info, resourceId, isRoot, "");
         
         sbCode.append("}");
         writeImports(sbImports, imports, classPackage);
         
-        createJavaSourceFile(src, new QName(classPackage, className), sbCode, sbImports);
+        createJavaSourceFile(info.getSrcDir(), new QName(classPackage, className), sbCode,
sbImports, true);
         
-        writeSubresourceClasses(app, rElement, info, src, isRoot, resourceId);
+        writeSubresourceClasses(rElement, info, isRoot, resourceId);
     }
     
-    private void writeSubresourceClasses(Application app, Element rElement, ContextInfo info,

-            File src, boolean isRoot, String resourceId) {
+    private void writeSubresourceClasses(Element rElement, ContextInfo info, 
+                                         boolean isRoot, String resourceId) {
 
         List<Element> childEls = getWadlElements(rElement, "resource");
         for (Element subEl : childEls) {
             String id = subEl.getAttribute("id");
             if (id.length() > 0 && !resourceId.equals(id) && !id.startsWith("{java")
                 && !id.startsWith("java")) {
-                Element subElement = getResourceElement(app, subEl, info.getGrammarInfo(),

-                    info.getTypeClassNames(), subEl.getAttribute("type"), src);
-                writeResourceClass(app, subElement, info, src, false);
+                Element subElement = getResourceElement(info.getApp(), subEl, info.getGrammarInfo(),

+                    info.getTypeClassNames(), subEl.getAttribute("type"), info.getSrcDir());
+                writeResourceClass(subElement, info, false);
             }
-            writeSubresourceClasses(app, subEl, info, src, false, id);
+            writeSubresourceClasses(subEl, info, false, id);
         }
     }
     
@@ -487,17 +501,20 @@ public class SourceGenerator {
             + getLineSep() + " * Created by Apache CXF WadlToJava code generator"
             + getLineSep() + "**/";
     }
-    
-    private void writeMethods(Element rElement,  
-                              Set<String> imports, StringBuilder sbCode, 
+    //CHECKSTYLE:OFF
+    private void writeMethods(Element rElement,
+                              String classPackage,
+                              Set<String> imports, 
+                              StringBuilder sbCode, 
                               ContextInfo info,
                               String resourceId,
                               boolean isRoot,
                               String currentPath) {
+    //CHECKSTYLE:ON    
         List<Element> methodEls = getWadlElements(rElement, "method");
        
         for (Element methodEl : methodEls) {
-            writeResourceMethod(methodEl, imports, sbCode, info, isRoot, currentPath);  
 
+            writeResourceMethod(methodEl, classPackage, imports, sbCode, info, isRoot, currentPath);
   
         }
         
         List<Element> childEls = getWadlElements(rElement, "resource");
@@ -506,9 +523,9 @@ public class SourceGenerator {
             String newPath = (currentPath + path).replace("//", "/");
             String id = childEl.getAttribute("id");
             if (id.length() == 0) {
-                writeMethods(childEl, imports, sbCode, info, id, false, newPath);
+                writeMethods(childEl, classPackage, imports, sbCode, info, id, false, newPath);
             } else {
-                writeResourceMethod(childEl, imports, sbCode, info, false, newPath);
+                writeResourceMethod(childEl, classPackage, imports, sbCode, info, false,
newPath);
             }
         }
     }
@@ -550,7 +567,8 @@ public class SourceGenerator {
         }
     }
     
-    private void writeResourceMethod(Element methodEl, 
+    private void writeResourceMethod(Element methodEl,
+                                     String classPackage,
                                      Set<String> imports,
                                      StringBuilder sbCode,
                                      ContextInfo info,
@@ -636,7 +654,7 @@ public class SourceGenerator {
                         !isRoot && !isResourceElement && resourceEl.getAttribute("id").length()
> 0);
             
             Element repElement = getActualRepElement(allRequestReps, inXmlRep); 
-            writeRequestTypes(firstRequestEl, repElement, inParamElements, 
+            writeRequestTypes(firstRequestEl, classPackage, repElement, inParamElements,

                     jaxpSourceRequired, sbCode, imports, info);
             sbCode.append(")");
             if (info.isInterfaceGenerated()) {
@@ -714,7 +732,7 @@ public class SourceGenerator {
             return xmlElement;
         }
         for (Element el : repElements) {
-            Element param = DOMUtils.getFirstChildWithName(el, WadlGenerator.WADL_NS, "param");
+            Element param = DOMUtils.getFirstChildWithName(el, getWadlNamespace(), "param");
             if (param != null) {
                 return el;
             }
@@ -775,15 +793,16 @@ public class SourceGenerator {
         }
         sbCode.append(localName).append(" ");
     }
-        
+    //CHECKSTYLE:OFF    
     private void writeRequestTypes(Element requestEl,
+                                   String classPackage,
                                    Element repElement,
                                    List<Element> inParamEls, 
                                    boolean jaxpRequired,
                                    StringBuilder sbCode, 
                                    Set<String> imports, 
                                    ContextInfo info) {
-        
+    //CHECKSTYLE:ON    
         boolean form = false;
         boolean formParamsAvailable = false;
         if (requestEl != null) {
@@ -801,6 +820,15 @@ public class SourceGenerator {
                 paramAnn = FormParam.class; 
             } 
             String name = paramEl.getAttribute("name");
+            boolean enumCreated = false;
+            if (generateEnums) {
+                List<Element> options =
+                    DOMUtils.findAllElementsByTagNameNS(paramEl, getWadlNamespace(), "option");
+                if (options.size() > 0) {
+                    generateEnumClass(getTypicalClassName(name), options, info.getSrcDir(),
classPackage);
+                    enumCreated = true;
+                }
+            }
             if (writeAnnotations(info.isInterfaceGenerated())) {
                 writeAnnotation(sbCode, imports, paramAnn, name, false, false);
                 sbCode.append(" ");
@@ -821,7 +849,9 @@ public class SourceGenerator {
                 addImport(imports, List.class.getName());
                 type = "List<" + type + ">";
             }
-            sbCode.append(type).append(" ").append(name.replaceAll("[\\.\\-]", "_"));
+            String paramName = enumCreated ? getTypicalClassName(name) 
+                                           : name.replaceAll("[\\.\\-]", "_");
+            sbCode.append(type).append(" ").append(paramName);
             if (i + 1 < inParamEls.size()) {
                 sbCode.append(", ");
                 if (i + 1 >= 4 && ((i + 1) % 4) == 0) {
@@ -856,9 +886,40 @@ public class SourceGenerator {
         }
     }
     
+    private void generateEnumClass(String clsName, List<Element> options, File src,
String classPackage) {
+        StringBuilder sbImports = new StringBuilder();
+        StringBuilder sbCode = new StringBuilder();
+        sbImports.append(getClassComment()).append(getLineSep());
+        sbImports.append("package " + classPackage)
+            .append(";").append(getLineSep()).append(getLineSep());
+        
+        sbCode.append("public enum " + clsName);
+        sbCode.append(" {" + getLineSep());
+        
+        for (int i = 0; i < options.size(); i++) {
+            String value = options.get(i).getAttribute("value");
+            sbCode.append(TAB).append(value.toUpperCase());
+            if (i + 1 < options.size()) {
+                sbCode.append(",");
+            }
+            sbCode.append(getLineSep());
+        }
+        sbCode.append("}");
+        createJavaSourceFile(src, new QName(classPackage, clsName), sbCode, sbImports, false);
+    }
+    
+    private String getTypicalClassName(String name) { 
+        String theName = name.toUpperCase();
+        if (theName.length() == 1) {
+            return theName;
+        } else {
+            return theName.substring(0, 1) + theName.substring(1).toLowerCase();
+        }
+    }
+    
     private List<Element> getWadlElements(Element parent, String name) {
         List<Element> elements = parent != null 
-            ? DOMUtils.getChildrenWithName(parent, WadlGenerator.WADL_NS, name)
+            ? DOMUtils.getChildrenWithName(parent, getWadlNamespace(), name)
             : CastUtils.cast(Collections.emptyList(), Element.class);
         if (!"resource".equals(name)) {    
             for (int i = 0; i < elements.size(); i++) {
@@ -911,7 +972,7 @@ public class SourceGenerator {
                 }
             }
         } else {
-            Element param = DOMUtils.getFirstChildWithName(repElement, WadlGenerator.WADL_NS,
"param");
+            Element param = DOMUtils.getFirstChildWithName(repElement, getWadlNamespace(),
"param");
             if (param != null) {
                 return getPrimitiveType(param);
             }
@@ -978,11 +1039,14 @@ public class SourceGenerator {
         sbCode.append(getLineSep()).append(TAB);
     }
     
-    private void createJavaSourceFile(File src, QName qname, StringBuilder sbCode, StringBuilder
sbImports) {
+    private void createJavaSourceFile(File src, QName qname, StringBuilder sbCode, StringBuilder
sbImports,
+                                      boolean serviceClass) {
         String content = sbImports.toString() + getLineSep() + sbCode.toString();
         
         String namespace = qname.getNamespaceURI();
-        generatedServiceClasses.add(namespace + "." + qname.getLocalPart());
+        if (serviceClass) {
+            generatedServiceClasses.add(namespace + "." + qname.getLocalPart());
+        }
         
         namespace = namespace.replace(".", getFileSep());
         
@@ -1302,11 +1366,25 @@ public class SourceGenerator {
         private Set<String> typeClassNames;
         private GrammarInfo gInfo;
         private Set<String> resourceClassNames = new HashSet<String>();
+        private Application rootApp;
+        private File srcDir;
         
-        public ContextInfo(Set<String> typeClassNames, GrammarInfo gInfo, boolean interfaceIsGenerated)
{
+        public ContextInfo(Application rootApp,
+                           File srcDir,
+                           Set<String> typeClassNames, 
+                           GrammarInfo gInfo, 
+                           boolean interfaceIsGenerated) {
             this.interfaceIsGenerated = interfaceIsGenerated;
             this.typeClassNames = typeClassNames;
             this.gInfo = gInfo;
+            this.rootApp = rootApp;
+            this.srcDir = srcDir;
+        }
+        public Application getApp() {
+            return rootApp;
+        }
+        public File getSrcDir() {
+            return srcDir;
         }
         public boolean isInterfaceGenerated() {
             return interfaceIsGenerated;

Modified: cxf/branches/2.4.x-fixes/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/WadlToolConstants.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/WadlToolConstants.java?rev=1188778&r1=1188777&r2=1188778&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/WadlToolConstants.java
(original)
+++ cxf/branches/2.4.x-fixes/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/WadlToolConstants.java
Tue Oct 25 16:45:53 2011
@@ -44,6 +44,9 @@ public final class WadlToolConstants {
     
     public static final String CFG_NO_ADDRESS_BINDING = ToolConstants.CFG_NO_ADDRESS_BINDING;
     
+    public static final String CFG_WADL_NAMESPACE = "wadlNamespace";
+    public static final String CFG_GENERATE_ENUMS = "generateEnums";
+    
     public static final String CFG_WADLURL = "wadl";
     
     

Modified: cxf/branches/2.4.x-fixes/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainer.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainer.java?rev=1188778&r1=1188777&r2=1188778&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainer.java
(original)
+++ cxf/branches/2.4.x-fixes/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainer.java
Tue Oct 25 16:45:53 2011
@@ -117,6 +117,11 @@ public class JAXRSContainer extends Abst
         sg.setPackageName((String)context.get(WadlToolConstants.CFG_PACKAGENAME));
         sg.setResourceName((String)context.get(WadlToolConstants.CFG_RESOURCENAME));
 
+        String wadlNs = (String)context.get(WadlToolConstants.CFG_WADL_NAMESPACE);
+        if (wadlNs != null) {
+            sg.setWadlNamespace(wadlNs);
+        }
+        
         sg.setSupportMultipleXmlReps(context.optionSet(WadlToolConstants.CFG_MULTIPLE_XML_REPS));
         // set the base path
         sg.setWadlPath(wadlURL);
@@ -137,6 +142,10 @@ public class JAXRSContainer extends Abst
         // TODO: consider introducing an option too for users be able to
         //       supply custom type mappings
         sg.setSchemaTypesMap(DEFAULT_TYPES_MAP);
+
+        if (context.optionSet(WadlToolConstants.CFG_GENERATE_ENUMS)) {
+            sg.setGenerateEnums(true);
+        }
         
         // generate
         String codeType = context.optionSet(WadlToolConstants.CFG_TYPES)

Modified: cxf/branches/2.4.x-fixes/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/jaxrs-toolspec.xml
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/jaxrs-toolspec.xml?rev=1188778&r1=1188777&r2=1188778&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/jaxrs-toolspec.xml
(original)
+++ cxf/branches/2.4.x-fixes/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/jaxrs-toolspec.xml
Tue Oct 25 16:45:53 2011
@@ -32,6 +32,16 @@ Examples:
 
     <usage>
         <optionGroup id="options">
+            <option id="wadlns">
+                <annotation>
+                    Specify the WADL namespace.
+                </annotation>
+                <switch>wadlns</switch>
+                <associatedArgument placement="afterSpace">
+                    <annotation>wadl-namespace</annotation>
+                </associatedArgument>
+            </option>
+            
             <option id="packagename">
                 <annotation>
                     Specifies the java package name to use for the generated code
@@ -124,11 +134,18 @@ Examples:
             <option id="impl" maxOccurs="1">
                 <annotation>
                     Specifies that a dummy service implementation is generated.
-                    
                 </annotation>
                 <switch>impl</switch>
             </option>
             
+            <option id="generateEnums" maxOccurs="1">
+                <annotation>
+                    Specifies that Enum classes can be generated to represent
+                    parameters with multiple options
+                </annotation>
+                <switch>generateEnums</switch>
+            </option>
+            
             <option id="noAddressBinding" maxOccurs="1">
                 <annotation>
                     Specifies that the generator should not use the address jaxb binding
file to map wsa:EndpointReferenceType 

Modified: cxf/branches/2.4.x-fixes/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java?rev=1188778&r1=1188777&r2=1188778&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java
(original)
+++ cxf/branches/2.4.x-fixes/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java
Tue Oct 25 16:45:53 2011
@@ -41,7 +41,7 @@ public class JAXRSContainerTest extends 
             context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
             context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/bookstore.xml"));
             context.put(WadlToolConstants.CFG_COMPILE, "true");
-
+            
             container.setContext(context);
             container.execute();
 



Mime
View raw message