From d2adf7793c0a5b2cc10dea0238e011a6e4ba23dc Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Sat, 10 Nov 2007 16:02:28 +0000 Subject: [PATCH] Working on SWS-121 --- .../config/Jaxb2OxmNamespaceHandlerTest.java | 41 +++++ .../config/jaxb2OxmNamespaceHandlerTest.xml | 11 ++ oxm/pom.xml | 19 ++- .../Jaxb1MarshallerBeanDefinitionParser.java | 23 +++ .../Jaxb2MarshallerBeanDefinitionParser.java | 62 ++++++++ .../JibxMarshallerBeanDefinitionParser.java | 23 +++ .../oxm/config/OxmNamespaceHandler.java | 18 +++ ...mlBeansMarshallerBeanDefinitionParser.java | 33 ++++ .../main/resources/META-INF/spring.handlers | 1 + .../main/resources/META-INF/spring.schemas | 1 + .../oxm/config/spring-oxm-1.1.xsd | 146 ++++++++++++++++++ .../oxm/config/OxmNamespaceHandlerTest.java | 52 +++++++ .../oxm/config/oxmNamespaceHandlerTest.xml | 17 ++ 13 files changed, 441 insertions(+), 6 deletions(-) create mode 100644 oxm-tiger/src/test/java/org/springframework/oxm/config/Jaxb2OxmNamespaceHandlerTest.java create mode 100644 oxm-tiger/src/test/resources/org/springframework/oxm/config/jaxb2OxmNamespaceHandlerTest.xml create mode 100644 oxm/src/main/java/org/springframework/oxm/config/Jaxb1MarshallerBeanDefinitionParser.java create mode 100644 oxm/src/main/java/org/springframework/oxm/config/Jaxb2MarshallerBeanDefinitionParser.java create mode 100644 oxm/src/main/java/org/springframework/oxm/config/JibxMarshallerBeanDefinitionParser.java create mode 100644 oxm/src/main/java/org/springframework/oxm/config/OxmNamespaceHandler.java create mode 100644 oxm/src/main/java/org/springframework/oxm/config/XmlBeansMarshallerBeanDefinitionParser.java create mode 100644 oxm/src/main/resources/META-INF/spring.handlers create mode 100644 oxm/src/main/resources/META-INF/spring.schemas create mode 100644 oxm/src/main/resources/org/springframework/oxm/config/spring-oxm-1.1.xsd create mode 100644 oxm/src/test/java/org/springframework/oxm/config/OxmNamespaceHandlerTest.java create mode 100644 oxm/src/test/resources/org/springframework/oxm/config/oxmNamespaceHandlerTest.xml diff --git a/oxm-tiger/src/test/java/org/springframework/oxm/config/Jaxb2OxmNamespaceHandlerTest.java b/oxm-tiger/src/test/java/org/springframework/oxm/config/Jaxb2OxmNamespaceHandlerTest.java new file mode 100644 index 00000000..c7a4548e --- /dev/null +++ b/oxm-tiger/src/test/java/org/springframework/oxm/config/Jaxb2OxmNamespaceHandlerTest.java @@ -0,0 +1,41 @@ +/* + * Copyright ${YEAR} 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.oxm.config; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.oxm.jaxb.Jaxb2Marshaller; + +import junit.framework.TestCase; + +public class Jaxb2OxmNamespaceHandlerTest extends TestCase { + + private ApplicationContext applicationContext; + + protected void setUp() throws Exception { + applicationContext = new ClassPathXmlApplicationContext("jaxb2OxmNamespaceHandlerTest.xml", getClass()); + } + + public void testContextPathMarshaller() throws Exception { + applicationContext.getBean("contextPathMarshaller", Jaxb2Marshaller.class); + } + + public void testClassesToBeBoundMarshaller() throws Exception { + applicationContext.getBean("classesMarshaller", Jaxb2Marshaller.class); + } + +} \ No newline at end of file diff --git a/oxm-tiger/src/test/resources/org/springframework/oxm/config/jaxb2OxmNamespaceHandlerTest.xml b/oxm-tiger/src/test/resources/org/springframework/oxm/config/jaxb2OxmNamespaceHandlerTest.xml new file mode 100644 index 00000000..2eb98f0e --- /dev/null +++ b/oxm-tiger/src/test/resources/org/springframework/oxm/config/jaxb2OxmNamespaceHandlerTest.xml @@ -0,0 +1,11 @@ + + + + + + + + diff --git a/oxm/pom.xml b/oxm/pom.xml index 5424ec1b..c34cf70a 100644 --- a/oxm/pom.xml +++ b/oxm/pom.xml @@ -1,4 +1,5 @@ - + spring-ws org.springframework.ws @@ -28,8 +29,8 @@ - - + + ${project.build.directory}/generated-sources/test/java @@ -44,8 +45,8 @@ - - + + @@ -80,6 +81,12 @@ org.springframework.ws spring-xml + + + org.springframework + spring-context + test + xml-apis @@ -243,4 +250,4 @@ test - \ No newline at end of file + diff --git a/oxm/src/main/java/org/springframework/oxm/config/Jaxb1MarshallerBeanDefinitionParser.java b/oxm/src/main/java/org/springframework/oxm/config/Jaxb1MarshallerBeanDefinitionParser.java new file mode 100644 index 00000000..603adc3c --- /dev/null +++ b/oxm/src/main/java/org/springframework/oxm/config/Jaxb1MarshallerBeanDefinitionParser.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2007, Your Corporation. All Rights Reserved. + */ + +package org.springframework.oxm.config; + +import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser; +import org.springframework.oxm.jaxb.Jaxb1Marshaller; + +import org.w3c.dom.Element; + +/** + * Parser for the <oxm:jaxb1-marshaller/> element. + * + * @author Arjen Poutsma + * @since 1.1.0 + */ +class Jaxb1MarshallerBeanDefinitionParser extends AbstractSimpleBeanDefinitionParser { + + protected Class getBeanClass(Element element) { + return Jaxb1Marshaller.class; + } +} diff --git a/oxm/src/main/java/org/springframework/oxm/config/Jaxb2MarshallerBeanDefinitionParser.java b/oxm/src/main/java/org/springframework/oxm/config/Jaxb2MarshallerBeanDefinitionParser.java new file mode 100644 index 00000000..e27cb293 --- /dev/null +++ b/oxm/src/main/java/org/springframework/oxm/config/Jaxb2MarshallerBeanDefinitionParser.java @@ -0,0 +1,62 @@ +/* + * Copyright 2007 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.oxm.config; + +import java.util.Iterator; +import java.util.List; + +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.ManagedList; +import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; +import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.util.StringUtils; +import org.springframework.util.xml.DomUtils; + +import org.w3c.dom.Element; + +/** + * Parser for the <oxm:jaxb2-marshaller/> element. + * + * @author Arjen Poutsma + * @since 1.1.0 + */ +public class Jaxb2MarshallerBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { + + public static final String JAXB2_MARSHALLER_BEAN_NAME = "org.springframework.oxm.jaxb.Jaxb2Marshaller"; + + protected String getBeanClassName(Element element) { + return JAXB2_MARSHALLER_BEAN_NAME; + } + + protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder beanDefinitionBuilder) { + String contextPath = element.getAttribute("contextPath"); + if (StringUtils.hasText(contextPath)) { + beanDefinitionBuilder.addPropertyValue("contextPath", contextPath); + } + List classes = DomUtils.getChildElementsByTagName(element, "class"); + if (!classes.isEmpty()) { + ManagedList classesToBeBound = new ManagedList(); + for (Iterator iterator = classes.iterator(); iterator.hasNext();) { + Element classToBeBound = (Element) iterator.next(); + String className = classToBeBound.getAttribute("name"); + classesToBeBound.add(className); + } + beanDefinitionBuilder.addPropertyValue("classesToBeBound", classesToBeBound); + } + } + +} diff --git a/oxm/src/main/java/org/springframework/oxm/config/JibxMarshallerBeanDefinitionParser.java b/oxm/src/main/java/org/springframework/oxm/config/JibxMarshallerBeanDefinitionParser.java new file mode 100644 index 00000000..aae66a9b --- /dev/null +++ b/oxm/src/main/java/org/springframework/oxm/config/JibxMarshallerBeanDefinitionParser.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2007, Your Corporation. All Rights Reserved. + */ + +package org.springframework.oxm.config; + +import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser; +import org.springframework.oxm.jibx.JibxMarshaller; + +import org.w3c.dom.Element; + +/** + * Parser for the <oxm:jibx-marshaller/> element. + * + * @author Arjen Poutsma + * @since 1.1.0 + */ +class JibxMarshallerBeanDefinitionParser extends AbstractSimpleBeanDefinitionParser { + + protected Class getBeanClass(Element element) { + return JibxMarshaller.class; + } +} \ No newline at end of file diff --git a/oxm/src/main/java/org/springframework/oxm/config/OxmNamespaceHandler.java b/oxm/src/main/java/org/springframework/oxm/config/OxmNamespaceHandler.java new file mode 100644 index 00000000..3cfdb3c5 --- /dev/null +++ b/oxm/src/main/java/org/springframework/oxm/config/OxmNamespaceHandler.java @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2007, Your Corporation. All Rights Reserved. + */ + +package org.springframework.oxm.config; + +import org.springframework.beans.factory.xml.NamespaceHandlerSupport; + +/** @author Arjen Poutsma */ +public class OxmNamespaceHandler extends NamespaceHandlerSupport { + + public void init() { + registerBeanDefinitionParser("jaxb1-marshaller", new Jaxb1MarshallerBeanDefinitionParser()); + registerBeanDefinitionParser("jaxb2-marshaller", new Jaxb2MarshallerBeanDefinitionParser()); + registerBeanDefinitionParser("jibx-marshaller", new JibxMarshallerBeanDefinitionParser()); + registerBeanDefinitionParser("xmlbeans-marshaller", new XmlBeansMarshallerBeanDefinitionParser()); + } +} diff --git a/oxm/src/main/java/org/springframework/oxm/config/XmlBeansMarshallerBeanDefinitionParser.java b/oxm/src/main/java/org/springframework/oxm/config/XmlBeansMarshallerBeanDefinitionParser.java new file mode 100644 index 00000000..0250e371 --- /dev/null +++ b/oxm/src/main/java/org/springframework/oxm/config/XmlBeansMarshallerBeanDefinitionParser.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2007, Your Corporation. All Rights Reserved. + */ + +package org.springframework.oxm.config; + +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; +import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.oxm.xmlbeans.XmlBeansMarshaller; +import org.springframework.util.StringUtils; + +import org.w3c.dom.Element; + +/** + * Parser for the <oxm:xmlbeans-marshaller/> element. + * + * @author Arjen Poutsma + * @since 1.1.0 + */ +class XmlBeansMarshallerBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { + + protected Class getBeanClass(Element element) { + return XmlBeansMarshaller.class; + } + + protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder beanDefinitionBuilder) { + String optionsName = element.getAttribute("options"); + if (StringUtils.hasText(optionsName)) { + beanDefinitionBuilder.addPropertyReference("xmlOptions", optionsName); + } + } +} \ No newline at end of file diff --git a/oxm/src/main/resources/META-INF/spring.handlers b/oxm/src/main/resources/META-INF/spring.handlers new file mode 100644 index 00000000..97f187b8 --- /dev/null +++ b/oxm/src/main/resources/META-INF/spring.handlers @@ -0,0 +1 @@ +http\://www.springframework.org/spring-ws/schema/oxm=org.springframework.oxm.config.OxmNamespaceHandler \ No newline at end of file diff --git a/oxm/src/main/resources/META-INF/spring.schemas b/oxm/src/main/resources/META-INF/spring.schemas new file mode 100644 index 00000000..b679a948 --- /dev/null +++ b/oxm/src/main/resources/META-INF/spring.schemas @@ -0,0 +1 @@ +http\://www.springframework.org/spring-ws/schema/oxm/spring-oxm-1.1.xsd=/org/springframework/oxm/config/spring-oxm-1.1.xsd \ No newline at end of file diff --git a/oxm/src/main/resources/org/springframework/oxm/config/spring-oxm-1.1.xsd b/oxm/src/main/resources/org/springframework/oxm/config/spring-oxm-1.1.xsd new file mode 100644 index 00000000..e71f4aac --- /dev/null +++ b/oxm/src/main/resources/org/springframework/oxm/config/spring-oxm-1.1.xsd @@ -0,0 +1,146 @@ + + + + + + + + + Defines the elements used in Spring's Object/XML Mapping integration. + + + + + + + + Defines a JAXB1 Marshaller. + + + + + + + + + + + + The JAXB Context path + + + + + Whether incoming XML should be validated. + + + + + + + + + + + + Defines a JAXB2 Marshaller. + + + + + + + + + + + + + + + + + + + The JAXB Context path + + + + + + + + + + + + Defines a JiBX Marshaller. + + + + + + + + + + + + + The binding name used by this marshaller. + + + + + + + + + + + + Defines a XMLBeans Marshaller. + + + + + + + + + + + + + The bean name of the XmlOptions that is to be used for this marshaller. Typically a + XmlOptionsFactoryBean definition. + + + + + + + + + + + + + + + + A class supported by a marshaller. + + + + + + + + + + diff --git a/oxm/src/test/java/org/springframework/oxm/config/OxmNamespaceHandlerTest.java b/oxm/src/test/java/org/springframework/oxm/config/OxmNamespaceHandlerTest.java new file mode 100644 index 00000000..aa81c6ae --- /dev/null +++ b/oxm/src/test/java/org/springframework/oxm/config/OxmNamespaceHandlerTest.java @@ -0,0 +1,52 @@ +/* + * Copyright ${YEAR} 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.oxm.config; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.oxm.jaxb.Jaxb1Marshaller; +import org.springframework.oxm.jibx.JibxMarshaller; +import org.springframework.oxm.xmlbeans.XmlBeansMarshaller; + +import junit.framework.TestCase; +import org.apache.xmlbeans.XmlOptions; + +public class OxmNamespaceHandlerTest extends TestCase { + + private ApplicationContext applicationContext; + + protected void setUp() throws Exception { + applicationContext = new ClassPathXmlApplicationContext("oxmNamespaceHandlerTest.xml", getClass()); + } + + public void testJaxb1Marshaller() throws Exception { + applicationContext.getBean("jaxb1Marshaller", Jaxb1Marshaller.class); + } + + public void testJibxMarshaller() throws Exception { + applicationContext.getBean("jibxMarshaller", JibxMarshaller.class); + } + + public void testXmlBeansMarshaller() throws Exception { + XmlBeansMarshaller marshaller = + (XmlBeansMarshaller) applicationContext.getBean("xmlBeansMarshaller", XmlBeansMarshaller.class); + XmlOptions options = marshaller.getXmlOptions(); + assertNotNull("Options not set", options); + assertTrue("option not set", options.hasOption("SAVE_PRETTY_PRINT")); + assertEquals("option not set", "true", options.get("SAVE_PRETTY_PRINT")); + } +} \ No newline at end of file diff --git a/oxm/src/test/resources/org/springframework/oxm/config/oxmNamespaceHandlerTest.xml b/oxm/src/test/resources/org/springframework/oxm/config/oxmNamespaceHandlerTest.xml new file mode 100644 index 00000000..1355bbd6 --- /dev/null +++ b/oxm/src/test/resources/org/springframework/oxm/config/oxmNamespaceHandlerTest.xml @@ -0,0 +1,17 @@ + + + + + + + + + + true + + + +