cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject git commit: CXF-5881 If a given wadl has a single schema with no target namespace then optionally support it
Date Wed, 16 Jul 2014 15:01:33 GMT
Repository: cxf
Updated Branches:
  refs/heads/2.7.x-fixes 7317872c3 -> 3a1327776


CXF-5881 If a given wadl has a single schema with no target namespace then optionally support
it


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

Branch: refs/heads/2.7.x-fixes
Commit: 3a13277765261ad72701a7d7eb7be9e2035bff7b
Parents: 7317872
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Wed Jul 16 15:54:43 2014 +0100
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Wed Jul 16 16:01:03 2014 +0100

----------------------------------------------------------------------
 .../cxf/tools/wadlto/jaxrs/SourceGenerator.java | 41 +++++++++++++-------
 .../tools/wadlto/jaxrs/JAXRSContainerTest.java  | 26 +++++++++++++
 .../wadl/resourceSchemaNoTargetNamespace.xml    | 30 ++++++++++++++
 3 files changed, 84 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/3a132777/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
----------------------------------------------------------------------
diff --git a/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
b/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
index a2ad31c..22e11cd 100644
--- a/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
+++ b/tools/wadlto/jaxrs/src/main/java/org/apache/cxf/tools/wadlto/jaxrs/SourceGenerator.java
@@ -401,7 +401,9 @@ public class SourceGenerator {
         for (SchemaInfo schemaEl : schemaElements) {
             populateElementTypeMap(app, schemaEl.getElement(), schemaEl.getSystemId(), elementTypeMap);
         }
-        return new GrammarInfo(nsMap, elementTypeMap);
+        boolean noTargetNamespace = schemaElements.size() == 1 
+            && schemaElements.get(0).getNamespaceURI().isEmpty();
+        return new GrammarInfo(nsMap, elementTypeMap, noTargetNamespace);
     }
     
     private void populateElementTypeMap(Application app, Element schemaEl, 
@@ -667,7 +669,7 @@ public class SourceGenerator {
         List<Element> requestEls = getWadlElements(methodEl, "request");
         Element firstRequestEl = requestEls.size() >= 1 ? requestEls.get(0) : null;
         List<Element> allRequestReps = getWadlElements(firstRequestEl, "representation");
-        List<Element> xmlRequestReps = getXmlReps(allRequestReps);
+        List<Element> xmlRequestReps = getXmlReps(allRequestReps, info.getGrammarInfo());
         
         final String methodNameLowerCase = methodEl.getAttribute("name").toLowerCase();
         String id = methodEl.getAttribute("id");
@@ -783,12 +785,14 @@ public class SourceGenerator {
             || methodNames.size() == 1 && "*".equals(methodNames.iterator().next());
     }
 
-    private List<Element> getXmlReps(List<Element> repElements) {
+    private List<Element> getXmlReps(List<Element> repElements, GrammarInfo gInfo)
{
         Set<String> values = new HashSet<String>(repElements.size());
         List<Element> xmlReps = new ArrayList<Element>();
         for (Element el : repElements) {
             String value = el.getAttribute("element");
-            if (value.length() > 0 && value.contains(":") && !values.contains(value))
{
+            if (value.length() > 0 
+                && (value.contains(":") || gInfo.isSchemaWithoutTargetNamespace())

+                && !values.contains(value)) {
                 xmlReps.add(el);
                 values.add(value);
             }
@@ -911,7 +915,8 @@ public class SourceGenerator {
         }
         
         String elementName = responseRequired ? null : getElementRefName(
-                getActualRepElement(repElements, getXmlReps(repElements).get(0)), info, imports);
+                getActualRepElement(repElements, getXmlReps(repElements, info.getGrammarInfo()).get(0)),

+                info, imports);
         if (elementName != null) {
             sbCode.append(elementName + " ");
         } else {
@@ -1203,9 +1208,9 @@ public class SourceGenerator {
         GrammarInfo gInfo = info.getGrammarInfo();
         if (gInfo != null) {
             String namespace = gInfo.getNsMap().get(prefix);
-            if (namespace != null) {
-                                
-                String packageName = getPackageFromNamespace(namespace);
+            if (namespace != null || prefix.isEmpty() && gInfo.isSchemaWithoutTargetNamespace())
{
+                String theNs = namespace != null ? namespace : "";                
+                String packageName = getPackageFromNamespace(theNs);
                 String clsName = getSchemaClassName(packageName, gInfo, actualValue, 
                                                     info.getTypeClassNames());
                 
@@ -1241,8 +1246,11 @@ public class SourceGenerator {
         
         if (elementRef.length() > 0) {
             String[] pair = elementRef.split(":");
-            if (pair.length == 2) {
-                return convertRefToClassName(pair[0], pair[1], null, info, imports);
+            if (pair.length == 2 
+                || pair.length == 1 && info.getGrammarInfo().isSchemaWithoutTargetNamespace())
{
+                String ns = pair.length == 1 ? "" : pair[0]; 
+                String name = pair.length == 1 ? pair[0] : pair[1];
+                return convertRefToClassName(ns, name, null, info, imports);
             }
         } else {
             // try mediaTypesMap first
@@ -1628,14 +1636,17 @@ public class SourceGenerator {
     private static class GrammarInfo {
         private Map<String, String> nsMap = new HashMap<String, String>();
         private Map<String, String> elementTypeMap = new HashMap<String, String>();
-        
+        private boolean noTargetNamespace;
         public GrammarInfo() {
             
         }
         
-        public GrammarInfo(Map<String, String> nsMap, Map<String, String> elementTypeMap)
{
+        public GrammarInfo(Map<String, String> nsMap, 
+                           Map<String, String> elementTypeMap,
+                           boolean noTargetNamespace) {
             this.nsMap = nsMap;
-            this.elementTypeMap = elementTypeMap; 
+            this.elementTypeMap = elementTypeMap;
+            this.noTargetNamespace = noTargetNamespace;
         }
 
         public Map<String, String> getNsMap() {
@@ -1645,6 +1656,10 @@ public class SourceGenerator {
         public Map<String, String> getElementTypeMap() {
             return elementTypeMap;
         }
+        
+        public boolean isSchemaWithoutTargetNamespace() {
+            return noTargetNamespace;
+        }
     }
 
     private static class DefaultImportsComparator implements Comparator<String> {

http://git-wip-us.apache.org/repos/asf/cxf/blob/3a132777/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java
----------------------------------------------------------------------
diff --git a/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java
b/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java
index 5d18ead..b82fca6 100644
--- a/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java
+++ b/tools/wadlto/jaxrs/src/test/java/org/apache/cxf/tools/wadlto/jaxrs/JAXRSContainerTest.java
@@ -33,6 +33,32 @@ import org.junit.Test;
 public class JAXRSContainerTest extends ProcessorTestBase {
 
     @Test    
+    public void testNoTargetNamespace() {
+        try {
+            JAXRSContainer container = new JAXRSContainer(null);
+
+            ToolContext context = new ToolContext();
+            context.put(WadlToolConstants.CFG_OUTPUTDIR, output.getCanonicalPath());
+            context.put(WadlToolConstants.CFG_WADLURL, getLocation("/wadl/resourceSchemaNoTargetNamespace.xml"));
+            context.put(WadlToolConstants.CFG_SCHEMA_PACKAGENAME, "=custom");
+            context.put(WadlToolConstants.CFG_COMPILE, "true");
+            
+            container.setContext(context);
+            container.execute();
+
+            assertNotNull(output.list());
+            List<File> files = FileUtils.getFilesRecurse(output, ".+\\.class" + "$");
+            assertEquals(3, files.size());
+            assertTrue(checkContains(files, "application" + ".Resource.class"));
+            assertTrue(checkContains(files, "custom" + ".TestCompositeObject.class"));
+            assertTrue(checkContains(files, "custom" + ".ObjectFactory.class"));
+        } catch (Exception e) {
+            e.printStackTrace();
+            fail();
+        }
+    }
+    
+    @Test    
     public void testCodeGenInterfaces() {
         try {
             JAXRSContainer container = new JAXRSContainer(null);

http://git-wip-us.apache.org/repos/asf/cxf/blob/3a132777/tools/wadlto/jaxrs/src/test/resources/wadl/resourceSchemaNoTargetNamespace.xml
----------------------------------------------------------------------
diff --git a/tools/wadlto/jaxrs/src/test/resources/wadl/resourceSchemaNoTargetNamespace.xml
b/tools/wadlto/jaxrs/src/test/resources/wadl/resourceSchemaNoTargetNamespace.xml
new file mode 100644
index 0000000..5682e2d
--- /dev/null
+++ b/tools/wadlto/jaxrs/src/test/resources/wadl/resourceSchemaNoTargetNamespace.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0"?>
+<application xmlns="http://wadl.dev.java.net/2009/02" 
+	xmlns:xs="http://www.w3.org/2001/XMLSchema">
+    <doc title="My Application"/>
+    <grammars>
+        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
+        	<xs:element name="test" type="testCompositeObject"/>
+            <xs:complexType name="testCompositeObject">
+                <xs:sequence>
+                    <xs:element name="id" type="xs:int"/>
+                    <xs:element minOccurs="0" name="name" type="xs:string"/>
+                </xs:sequence>
+            </xs:complexType>
+        </xs:schema>
+    </grammars>
+    <resources base="http://localhost:8080/baz">
+        <resource path="/">
+            <resource path="rest">
+                <method name="PUT">
+                    <request>
+                        <representation mediaType="application/xml" element="test"/>
+                    </request>
+                    <response>
+                        <representation mediaType="application/xml" element="test"/>
+                    </response>
+                </method>
+            </resource>
+        </resource>
+    </resources>
+</application>


Mime
View raw message