From 1200c45f8fa6876447a32e43e217d514ec8531d8 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Thu, 30 Nov 2006 22:07:15 +0000 Subject: [PATCH] Added WSDL4J support for Wsdl definitions --- core/pom.xml | 8 +- .../wsdl/wsdl11/SimpleWsdl11Definition.java | 16 +++- .../ws/wsdl/wsdl11/Wsdl4jDefinition.java | 81 +++++++++++++++++++ .../ws/wsdl/wsdl11/Wsdl4jDefinitionTest.java | 67 +++++++++++++++ 4 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 core/src/main/java/org/springframework/ws/wsdl/wsdl11/Wsdl4jDefinition.java create mode 100644 core/src/test/java/org/springframework/ws/wsdl/wsdl11/Wsdl4jDefinitionTest.java diff --git a/core/pom.xml b/core/pom.xml index 6cb27c92..31705fd7 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -1,5 +1,6 @@ - + spring-ws org.springframework.ws @@ -83,6 +84,11 @@ com.sun.xml.messaging.saaj saaj-impl + + + wsdl4j + wsdl4j + javax.xml.soap diff --git a/core/src/main/java/org/springframework/ws/wsdl/wsdl11/SimpleWsdl11Definition.java b/core/src/main/java/org/springframework/ws/wsdl/wsdl11/SimpleWsdl11Definition.java index d58d37ca..ef6ea872 100644 --- a/core/src/main/java/org/springframework/ws/wsdl/wsdl11/SimpleWsdl11Definition.java +++ b/core/src/main/java/org/springframework/ws/wsdl/wsdl11/SimpleWsdl11Definition.java @@ -37,7 +37,17 @@ public class SimpleWsdl11Definition implements Wsdl11Definition, InitializingBea private Resource wsdlResource; - public void setWsdl(Resource wsdlResource) { + /** + * Constructs a new SimpleWsdl11Definition. Calling setWsdl is required. + * + * @see #setWsdl(org.springframework.core.io.Resource) + */ + public SimpleWsdl11Definition() { + } + + /** Constructs a new SimpleWsdl11Definition with the given resource. */ + public SimpleWsdl11Definition(Resource wsdlResource) { + Assert.notNull(wsdlResource, "wsdlResource must not be null"); this.wsdlResource = wsdlResource; } @@ -54,4 +64,8 @@ public class SimpleWsdl11Definition implements Wsdl11Definition, InitializingBea throw new WsdlDefinitionException("Could not create source from " + wsdlResource, ex); } } + + public void setWsdl(Resource wsdlResource) { + this.wsdlResource = wsdlResource; + } } \ No newline at end of file diff --git a/core/src/main/java/org/springframework/ws/wsdl/wsdl11/Wsdl4jDefinition.java b/core/src/main/java/org/springframework/ws/wsdl/wsdl11/Wsdl4jDefinition.java new file mode 100644 index 00000000..7b869014 --- /dev/null +++ b/core/src/main/java/org/springframework/ws/wsdl/wsdl11/Wsdl4jDefinition.java @@ -0,0 +1,81 @@ +/* + * Copyright 2006 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ws.wsdl.wsdl11; + +import javax.wsdl.Definition; +import javax.wsdl.WSDLException; +import javax.wsdl.factory.WSDLFactory; +import javax.wsdl.xml.WSDLWriter; +import javax.xml.transform.Source; +import javax.xml.transform.dom.DOMSource; + +import org.springframework.util.Assert; +import org.springframework.ws.wsdl.WsdlDefinitionException; +import org.w3c.dom.Document; + +/** + * Implementation of the Wsdl11Definition based on WSDL4J. A {@link javax.wsdl.Definition} can be given as + * as constructor argument, or set using a property. + * + * @author Arjen Poutsma + * @see #Wsdl4jDefinition(javax.wsdl.Definition) + * @see #setDefinition(javax.wsdl.Definition) + */ +public class Wsdl4jDefinition implements Wsdl11Definition { + + private Definition definition; + + /** + * Constructs a new, empty Wsdl4jDefinition. + * + * @see #setDefinition(javax.wsdl.Definition) + */ + public Wsdl4jDefinition() { + } + + /** + * Constructs a new Wsdl4jDefinition based on the given Definition. + * + * @param definition the WSDL4J definition + */ + public Wsdl4jDefinition(Definition definition) { + this.definition = definition; + } + + /** Returns the WSDL4J Definition. */ + public Definition getDefinition() { + return definition; + } + + /** Set the WSDL4J Definition. */ + public void setDefinition(Definition definition) { + this.definition = definition; + } + + public Source getSource() { + Assert.notNull(definition, "definition must not be null"); + try { + WSDLFactory wsdlFactory = WSDLFactory.newInstance(); + WSDLWriter wsdlWriter = wsdlFactory.newWSDLWriter(); + Document document = wsdlWriter.getDocument(definition); + return new DOMSource(document); + } + catch (WSDLException ex) { + throw new WsdlDefinitionException(ex.getMessage(), ex); + } + } +} diff --git a/core/src/test/java/org/springframework/ws/wsdl/wsdl11/Wsdl4jDefinitionTest.java b/core/src/test/java/org/springframework/ws/wsdl/wsdl11/Wsdl4jDefinitionTest.java new file mode 100644 index 00000000..560ff913 --- /dev/null +++ b/core/src/test/java/org/springframework/ws/wsdl/wsdl11/Wsdl4jDefinitionTest.java @@ -0,0 +1,67 @@ +/* + * Copyright 2006 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ws.wsdl.wsdl11; + +import java.io.InputStream; +import javax.wsdl.Definition; +import javax.wsdl.factory.WSDLFactory; +import javax.wsdl.xml.WSDLReader; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMResult; + +import org.custommonkey.xmlunit.XMLTestCase; +import org.custommonkey.xmlunit.XMLUnit; +import org.w3c.dom.Document; +import org.xml.sax.InputSource; + +public class Wsdl4jDefinitionTest extends XMLTestCase { + + private Wsdl4jDefinition definition; + + private Transformer transformer; + + protected void setUp() throws Exception { + XMLUnit.setIgnoreWhitespace(true); + WSDLFactory factory = WSDLFactory.newInstance(); + WSDLReader reader = factory.newWSDLReader(); + InputStream is = getClass().getResourceAsStream("complete.wsdl"); + try { + Definition wsdl4jDefinition = reader.readWSDL(null, new InputSource(is)); + definition = new Wsdl4jDefinition(wsdl4jDefinition); + } + finally { + is.close(); + } + transformer = TransformerFactory.newInstance().newTransformer(); + } + + public void testGetSource() throws Exception { + Source source = definition.getSource(); + assertNotNull("Source is null", source); + DOMResult result = new DOMResult(); + transformer.transform(source, result); + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + documentBuilderFactory.setNamespaceAware(true); + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); + Document expected = documentBuilder.parse(getClass().getResourceAsStream("complete.wsdl")); + assertXMLEqual(expected, (Document) result.getNode()); + } +} \ No newline at end of file