You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2014/07/16 16:55:05 UTC
git commit: CXF-5881 If a given wadl has a single schema with no
target namespace then optionally support it
Repository: cxf
Updated Branches:
refs/heads/master 766417839 -> 14ce0adf1
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/14ce0adf
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/14ce0adf
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/14ce0adf
Branch: refs/heads/master
Commit: 14ce0adf133d51a6080d434783669aa46cafe2f0
Parents: 7664178
Author: Sergey Beryozkin <sb...@talend.com>
Authored: Wed Jul 16 15:54:43 2014 +0100
Committer: Sergey Beryozkin <sb...@talend.com>
Committed: Wed Jul 16 15:54:43 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/14ce0adf/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 e91ef2f..a47b809 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,
@@ -677,7 +679,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");
@@ -793,12 +795,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);
}
@@ -921,7 +925,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 {
@@ -1213,9 +1218,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());
@@ -1251,8 +1256,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
@@ -1638,14 +1646,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() {
@@ -1655,6 +1666,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/14ce0adf/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/14ce0adf/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>