cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1101851 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/codegen/SourceGenerator.java tools/wadlto/jaxrs/src/test/resources/wadl/singleResource.xml
Date Wed, 11 May 2011 12:08:46 GMT
Author: sergeyb
Date: Wed May 11 12:08:46 2011
New Revision: 1101851

URL: http://svn.apache.org/viewvc?rev=1101851&view=rev
Log:
[CXF-3498] Making sure root resource path is not duplicated and a parameter representing request
payload is added last

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/codegen/SourceGenerator.java
    cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/singleResource.xml

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/codegen/SourceGenerator.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/codegen/SourceGenerator.java?rev=1101851&r1=1101850&r2=1101851&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/codegen/SourceGenerator.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/codegen/SourceGenerator.java
Wed May 11 12:08:46 2011
@@ -178,9 +178,9 @@ public class SourceGenerator {
         GrammarInfo gInfo = getGrammarInfo(appElement, schemaElements);
         for (int i = 0; i < resourceEls.size(); i++) {
             Element resource = resourceEls.get(i);
-            writeResourceClass(resource, typeClassNames, gInfo, src, generateInterfaces);
+            writeResourceClass(resource, typeClassNames, gInfo, src, true, generateInterfaces);
             if (generateInterfaces && generateImpl) {
-                writeResourceClass(resource, typeClassNames, gInfo, src, false);
+                writeResourceClass(resource, typeClassNames, gInfo, src, true, false);
             }
             if (resourceName != null) {
                 break;
@@ -227,15 +227,13 @@ public class SourceGenerator {
     }
     
     private void writeResourceClass(Element rElement, Set<String> typeClassNames, 
-                                    GrammarInfo gInfo, File src, boolean interfaceIsGenerated)
{
+                                    GrammarInfo gInfo, File src, boolean isRoot,
+                                    boolean interfaceIsGenerated) {
         String resourceId = resourceName != null 
             ? resourceName : rElement.getAttribute("id");
         if (resourceId.length() == 0) {
             resourceId = DEFAULT_RESOURCE_NAME;
         }
-        String path = rElement.getAttribute("path");
-        
-        
         QName qname = JAXRSUtils.convertStringToQName(resourceId);
         if (getSchemaClassName(PackageUtils.getPackageNameByNameSpaceURI(qname.getNamespaceURI()),

                                gInfo, qname.getLocalPart(), typeClassNames) != null) {
@@ -253,7 +251,8 @@ public class SourceGenerator {
         sbImports.append("package " + classPackage)
             .append(";").append(getLineSep()).append(getLineSep());
         
-        if (writeAnnotations(interfaceIsGenerated)) {
+        if (isRoot && writeAnnotations(interfaceIsGenerated)) {
+            String path = rElement.getAttribute("path");
             writeAnnotation(sbCode, imports, Path.class, path, true, false);
         }
                 
@@ -261,16 +260,16 @@ public class SourceGenerator {
         writeImplementsInterface(sbCode, qname.getLocalPart(), interfaceIsGenerated);   
          
         sbCode.append(" {" + getLineSep() + getLineSep());
         
-        writeMethods(rElement, imports, sbCode, typeClassNames, gInfo, interfaceIsGenerated);
+        writeMethods(rElement, imports, sbCode, typeClassNames, gInfo, isRoot, interfaceIsGenerated);
         
         List<Element> childEls = DOMUtils.getChildrenWithName(rElement, 
             WadlGenerator.WADL_NS, "resource");
         for (Element childEl : childEls) {
             if (childEl.getAttribute("id").length() == 0) {
-                writeMethods(childEl, imports, sbCode, typeClassNames, gInfo, interfaceIsGenerated);
+                writeMethods(childEl, imports, sbCode, typeClassNames, gInfo, false, interfaceIsGenerated);
             } else {
-                writeResourceMethod(childEl, childEl, imports, sbCode, typeClassNames, gInfo,

-                                    interfaceIsGenerated);
+                writeResourceMethod(childEl, imports, sbCode, typeClassNames, gInfo, 
+                                    false, interfaceIsGenerated);
             }
         }
         sbCode.append("}");
@@ -281,7 +280,7 @@ public class SourceGenerator {
         for (Element subEl : childEls) {
             String id = subEl.getAttribute("id");
             if (id.length() > 0 && !resourceId.equals(id) && !id.startsWith("{java"))
{
-                writeResourceClass(subEl, typeClassNames, gInfo, src, interfaceIsGenerated);
+                writeResourceClass(subEl, typeClassNames, gInfo, src, false, interfaceIsGenerated);
             }
         }
     }
@@ -316,20 +315,21 @@ public class SourceGenerator {
     
     private String getClassComment() {
         return "/**"
-            + getLineSep() + " * Generated by Apache CXF"
+            + getLineSep() + " * Created by Apache CXF WadlToJava code generator"
             + getLineSep() + "**/";
     }
     
     private void writeMethods(Element rElement,  
                               Set<String> imports, StringBuilder sbCode, 
                               Set<String> typeClassNames, GrammarInfo gInfo,
+                              boolean isRoot,
                               boolean interfaceIsGenerated) {
         List<Element> methodEls = DOMUtils.getChildrenWithName(rElement, 
             WadlGenerator.WADL_NS, "method");
        
         for (Element methodEl : methodEls) {
-            writeResourceMethod(rElement, methodEl, imports, sbCode, typeClassNames, gInfo,

-                                interfaceIsGenerated);    
+            writeResourceMethod(methodEl, imports, sbCode, typeClassNames, gInfo, 
+                                isRoot, interfaceIsGenerated);    
         }
     }
     
@@ -370,14 +370,19 @@ public class SourceGenerator {
         }
     }
     
-    private void writeResourceMethod(Element resourceEl, Element methodEl, 
+    private void writeResourceMethod(Element methodEl, 
                                      Set<String> imports, StringBuilder sbCode, 
                                      Set<String> typeClassNames, GrammarInfo gInfo,
+                                     boolean isRoot,
                                      boolean interfaceIsGenerated) {
+        Element resourceEl = "resource".equals(methodEl.getLocalName()) 
+            ? methodEl : (Element)methodEl.getParentNode();
+        
         String methodName = methodEl.getAttribute("name");
+        String methodNameLowerCase = methodName.toLowerCase();
         String id = methodEl.getAttribute("id");
         if (id.length() == 0) {
-            id = methodName.toLowerCase();
+            id = methodNameLowerCase;
         }
         
         List<Element> responseEls = DOMUtils.getChildrenWithName(methodEl, 
@@ -389,21 +394,20 @@ public class SourceGenerator {
         if (writeAnnotations(interfaceIsGenerated)) {
             sbCode.append(TAB);
             
-            if (methodName.length() > 0) {
-                if (HTTP_METHOD_ANNOTATIONS.containsKey(methodName.toLowerCase())) {
+            if (methodNameLowerCase.length() > 0) {
+                if (HTTP_METHOD_ANNOTATIONS.containsKey(methodNameLowerCase)) {
                     writeAnnotation(sbCode, imports, 
-                                    HTTP_METHOD_ANNOTATIONS.get(methodName.toLowerCase()),
null, true, true);
+                                    HTTP_METHOD_ANNOTATIONS.get(methodNameLowerCase), null,
true, true);
                 } else {
                     // TODO : write a custom annotation class name based on HttpMethod  
 
                 }
-                
                 writeFormatAnnotations(requestEls, sbCode, imports, true);
                 writeFormatAnnotations(responseEls, sbCode, imports, false);
             }
-            
-            String path = resourceEl.getAttribute("path");
-            writeAnnotation(sbCode, imports, Path.class, path, true, true);
-            
+            if (!isRoot) {
+                String path = resourceEl.getAttribute("path");
+                writeAnnotation(sbCode, imports, Path.class, path, true, true);
+            }
         } else {
             sbCode.append(getLineSep()).append(TAB);
         }
@@ -412,7 +416,7 @@ public class SourceGenerator {
             sbCode.append("public ");
         }
         boolean responseTypeAvailable = true;
-        if (methodName.length() > 0) {
+        if (methodNameLowerCase.length() > 0) {
             responseTypeAvailable = writeResponseType(responseEls, sbCode, imports, typeClassNames,
gInfo);
             sbCode.append(id);
         } else {
@@ -508,34 +512,23 @@ public class SourceGenerator {
                                    Set<String> typeClassNames, 
                                    GrammarInfo gInfo,
                                    boolean interfaceIsGenerated) {
+        
         boolean form = false;
+        boolean formParamsAvailbale = false;
         if (requestEls.size() == 1 && inParamEls.size() == 0) {
             inParamEls.addAll(DOMUtils.getChildrenWithName(requestEls.get(0), 
                  WadlGenerator.WADL_NS, "param"));
+            int currentSize = inParamEls.size();
             form = addFormParameters(inParamEls, requestEls.get(0));
+            formParamsAvailbale = currentSize < inParamEls.size(); 
         }
-        
-        String elementName = null;
-        
-        List<Element> repElements = requestEls.size() == 1 
-            ? DOMUtils.getChildrenWithName(requestEls.get(0), WadlGenerator.WADL_NS, "representation")
-            : CastUtils.cast(Collections.emptyList(), Element.class);
-        if (repElements.size() > 0) {    
-            elementName = getElementRefName(repElements, typeClassNames, gInfo, imports);
-        }
-        if (elementName != null) {
-            sbCode.append(elementName).append(" ").append(elementName.toLowerCase());
-            if (inParamEls.size() > 0) {
-                sbCode.append(", ");
-            }
-        } else if (inParamEls.size() == 0) {
-            if (form) {
-                addImport(imports, MultivaluedMap.class.getName());
-                sbCode.append("MultivaluedMap map");
-            }
-            return;
-        }
+                  
+        if (form && !formParamsAvailbale) {
+            addImport(imports, MultivaluedMap.class.getName());
+            sbCode.append("MultivaluedMap map");
+        } 
         for (int i = 0; i < inParamEls.size(); i++) {
+    
             Element paramEl = inParamEls.get(i);
 
             String name = paramEl.getAttribute("name");
@@ -558,6 +551,23 @@ public class SourceGenerator {
                 }
             }
         }
+        if (!form) {
+            String elementName = null;
+            
+            List<Element> repElements = requestEls.size() == 1 
+                ? DOMUtils.getChildrenWithName(requestEls.get(0), WadlGenerator.WADL_NS,
"representation")
+                : CastUtils.cast(Collections.emptyList(), Element.class);
+            if (repElements.size() > 0) {    
+                elementName = getElementRefName(repElements, typeClassNames, gInfo, imports);
+            }
+            if (elementName != null) {
+                if (inParamEls.size() > 0) {
+                    sbCode.append(", ");
+                }
+                sbCode.append(elementName).append(" ").append(elementName.toLowerCase());
+            }
+        }
+        
     }
     
     private String getPrimitiveType(Element paramEl) {

Modified: cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/singleResource.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/singleResource.xml?rev=1101851&r1=1101850&r2=1101851&view=diff
==============================================================================
--- cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/singleResource.xml (original)
+++ cxf/trunk/tools/wadlto/jaxrs/src/test/resources/wadl/singleResource.xml Wed May 11 12:08:46
2011
@@ -1,8 +1,8 @@
 <application xmlns="http://wadl.dev.java.net/2009/02" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:prefix1="http://superbooks">
 
  <resources base="http://localhost:8080/baz">
-   <param name="id" style="template" type="xs:long"/>
    <resource path="/bookstore/{id}">
+     <param name="id" style="template" type="xs:long"/>
      <method name="GET">
       <request/>
       <response>



Mime
View raw message