Browse Source

SPR-8309 - Spring OXM schema improvement for CastorMarshaller

pull/7/head
Arjen Poutsma 14 years ago
parent
commit
9765fefeac
  1. 11
      org.springframework.oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java
  2. 37
      org.springframework.oxm/src/main/java/org/springframework/oxm/config/CastorMarshallerBeanDefinitionParser.java
  3. 5
      org.springframework.oxm/src/main/java/org/springframework/oxm/config/OxmNamespaceHandler.java
  4. 40
      org.springframework.oxm/src/main/resources/org/springframework/oxm/config/spring-oxm-3.1.xsd
  5. 51
      org.springframework.oxm/src/test/java/org/springframework/oxm/config/OxmNamespaceHandlerTests.java
  6. 53
      org.springframework.oxm/src/test/resources/org/springframework/oxm/config/oxmNamespaceHandlerTest.xml

11
org.springframework.oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2011 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.
@ -176,7 +176,14 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing @@ -176,7 +176,14 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
}
/**
* Set the package names of packages with the Castor descriptor classes.
* Set the names of package with the Castor descriptor classes.
*/
public void setTargetPackage(String targetPackage) {
this.targetPackages = new String[] {targetPackage};
}
/**
* Set the names of packages with the Castor descriptor classes.
*/
public void setTargetPackages(String[] targetPackages) {
this.targetPackages = targetPackages;

37
org.springframework.oxm/src/main/java/org/springframework/oxm/config/CastorMarshallerBeanDefinitionParser.java

@ -0,0 +1,37 @@ @@ -0,0 +1,37 @@
/*
* Copyright 2002-2011 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.w3c.dom.Element;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
/**
* Parser for the <code>&lt;oxm:castor-marshaller/&gt;</code> element.
*
* @author Jakub Narloch
* @since 3.1
*/
public class CastorMarshallerBeanDefinitionParser extends AbstractSimpleBeanDefinitionParser {
private static final String CASTOR_MARSHALLER_CLASS_NAME = "org.springframework.oxm.castor.CastorMarshaller";
@Override
protected String getBeanClassName(Element element) {
return CASTOR_MARSHALLER_CLASS_NAME;
}
}

5
org.springframework.oxm/src/main/java/org/springframework/oxm/config/OxmNamespaceHandler.java

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
/*
* Copyright 2002-2009 the original author or authors.
*
* Copyright 2002-2011 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
@ -31,6 +31,7 @@ public class OxmNamespaceHandler extends NamespaceHandlerSupport { @@ -31,6 +31,7 @@ public class OxmNamespaceHandler extends NamespaceHandlerSupport {
registerBeanDefinitionParser("jaxb2-marshaller", new Jaxb2MarshallerBeanDefinitionParser());
registerBeanDefinitionParser("jibx-marshaller", new JibxMarshallerBeanDefinitionParser());
registerBeanDefinitionParser("xmlbeans-marshaller", new XmlBeansMarshallerBeanDefinitionParser());
registerBeanDefinitionParser("castor-marshaller", new CastorMarshallerBeanDefinitionParser());
}
}

40
org.springframework.oxm/src/main/resources/org/springframework/oxm/config/spring-oxm-3.1.xsd

@ -102,6 +102,46 @@ @@ -102,6 +102,46 @@
</xsd:complexType>
</xsd:element>
<xsd:element name="castor-marshaller">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation
source="java:org.springframework.oxm.castor.CastorMarshaller">
Defines a Castor Marshaller.
</xsd:documentation>
<xsd:appinfo>
<tool:annotation>
<tool:exports type="org.springframework.oxm.castor.CastorMarshaller" />
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:attribute name="encoding" type="xsd:string">
<xsd:annotation>
<xsd:documentation>The encoding to use for stream reading.</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="target-class" type="classType">
<xsd:annotation>
<xsd:documentation>The target class to be bound with Castor marshaller.</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="target-package" type="xsd:string">
<xsd:annotation>
<xsd:documentation>The target package that contains Castor descriptor classes.</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="mapping-location" type="xsd:string">
<xsd:annotation>
<xsd:documentation>The path to Castor mapping file.</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="classType">
<xsd:annotation>
<xsd:documentation source="java:java.lang.Class">A class supported by a marshaller.</xsd:documentation>

51
org.springframework.oxm/src/test/java/org/springframework/oxm/config/OxmNamespaceHandlerTest.java → org.springframework.oxm/src/test/java/org/springframework/oxm/config/OxmNamespaceHandlerTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright ${YEAR} the original author or authors.
* Copyright 2002-2011 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.
@ -17,18 +17,24 @@ @@ -17,18 +17,24 @@
package org.springframework.oxm.config;
import org.apache.xmlbeans.XmlOptions;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.oxm.castor.CastorMarshaller;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.oxm.jibx.JibxMarshaller;
import org.springframework.oxm.xmlbeans.XmlBeansMarshaller;
public class OxmNamespaceHandlerTest {
import static org.junit.Assert.*;
/**
* Tests the {@link OxmNamespaceHandler} class.
*
* @author Arjen Poustma
* @author Jakub Narloch
*/
public class OxmNamespaceHandlerTests {
private ApplicationContext applicationContext;
@ -37,12 +43,6 @@ public class OxmNamespaceHandlerTest { @@ -37,12 +43,6 @@ public class OxmNamespaceHandlerTest {
applicationContext = new ClassPathXmlApplicationContext("oxmNamespaceHandlerTest.xml", getClass());
}
@Test
@Ignore
public void jibxMarshaller() throws Exception {
applicationContext.getBean("jibxMarshaller", JibxMarshaller.class);
}
@Test
public void xmlBeansMarshaller() throws Exception {
XmlBeansMarshaller marshaller = applicationContext.getBean("xmlBeansMarshaller", XmlBeansMarshaller.class);
@ -54,12 +54,37 @@ public class OxmNamespaceHandlerTest { @@ -54,12 +54,37 @@ public class OxmNamespaceHandlerTest {
@Test
public void jaxb2ContextPathMarshaller() throws Exception {
applicationContext.getBean("contextPathMarshaller", Jaxb2Marshaller.class);
Jaxb2Marshaller jaxb2Marshaller = applicationContext.getBean("jaxb2ContextPathMarshaller", Jaxb2Marshaller.class);
assertNotNull(jaxb2Marshaller);
}
@Test
public void jaxb2ClassesToBeBoundMarshaller() throws Exception {
applicationContext.getBean("classesMarshaller", Jaxb2Marshaller.class);
Jaxb2Marshaller jaxb2Marshaller = applicationContext.getBean("jaxb2ClassesMarshaller", Jaxb2Marshaller.class);
assertNotNull(jaxb2Marshaller);
}
@Test
public void castorEncodingMarshaller() throws Exception {
CastorMarshaller castorMarshaller = applicationContext.getBean("castorEncodingMarshaller", CastorMarshaller.class);
assertNotNull(castorMarshaller);
}
@Test
public void castorTargetClassMarshaller() throws Exception {
CastorMarshaller castorMarshaller = applicationContext.getBean("castorTargetClassMarshaller", CastorMarshaller.class);
assertNotNull(castorMarshaller);
}
@Test
public void castorTargetPackageMarshaller() throws Exception {
CastorMarshaller castorMarshaller = applicationContext.getBean("castorTargetPackageMarshaller", CastorMarshaller.class);
assertNotNull(castorMarshaller);
}
@Test
public void castorMappingLocationMarshaller() throws Exception {
CastorMarshaller castorMarshaller = applicationContext.getBean("castorMappingLocationMarshaller", CastorMarshaller.class);
assertNotNull(castorMarshaller);
}
}

53
org.springframework.oxm/src/test/resources/org/springframework/oxm/config/oxmNamespaceHandlerTest.xml

@ -1,23 +1,38 @@ @@ -1,23 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">
<!--<oxm:jibx-marshaller id="jibxMarshaller" target-class="org.springframework.oxm.jibx.Flights"/>-->
<oxm:xmlbeans-marshaller id="xmlBeansMarshaller" options="xmlBeansOptions"/>
<bean id="xmlBeansOptions" class="org.springframework.oxm.xmlbeans.XmlOptionsFactoryBean">
<property name="options">
<props>
<prop key="SAVE_PRETTY_PRINT">true</prop>
</props>
</property>
</bean>
<oxm:jaxb2-marshaller id="contextPathMarshaller" contextPath="org.springframework.oxm.jaxb.test"/>
<oxm:jaxb2-marshaller id="classesMarshaller">
<oxm:class-to-be-bound name="org.springframework.oxm.jaxb.test.Flights"/>
<oxm:class-to-be-bound name="org.springframework.oxm.jaxb.test.FlightType"/>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd">
<!-- XMLBeans -->
<oxm:xmlbeans-marshaller id="xmlBeansMarshaller"
options="xmlBeansOptions" />
<bean id="xmlBeansOptions" class="org.springframework.oxm.xmlbeans.XmlOptionsFactoryBean">
<property name="options">
<props>
<prop key="SAVE_PRETTY_PRINT">true</prop>
</props>
</property>
</bean>
<!-- JAXB2 -->
<oxm:jaxb2-marshaller id="jaxb2ContextPathMarshaller"
contextPath="org.springframework.oxm.jaxb.test" />
<oxm:jaxb2-marshaller id="jaxb2ClassesMarshaller">
<oxm:class-to-be-bound name="org.springframework.oxm.jaxb.test.Flights" />
<oxm:class-to-be-bound name="org.springframework.oxm.jaxb.test.FlightType" />
</oxm:jaxb2-marshaller>
<!-- Castor -->
<oxm:castor-marshaller id="castorEncodingMarshaller" encoding="ISO-8859-1" />
<oxm:castor-marshaller id="castorTargetClassMarshaller" target-class="org.springframework.oxm.castor.Flight" />
<oxm:castor-marshaller id="castorTargetPackageMarshaller" target-package="org.springframework.oxm.castor" />
<oxm:castor-marshaller id="castorMappingLocationMarshaller"
mapping-location="classpath:org/springframework/oxm/castor/mapping.xml" />
</beans>

Loading…
Cancel
Save