This commit is contained in:
Arjen Poutsma
2007-10-22 22:43:28 +00:00
parent 91749a44c0
commit 7ee16b9a86
5 changed files with 149 additions and 9 deletions

View File

@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>spring-ws</artifactId>
<groupId>org.springframework.ws</groupId>
@@ -26,8 +27,8 @@
<configuration>
<tasks>
<ant antfile="${basedir}/build-maven2.xml" inheritRefs="true">
<property name="maven.test.skip" value="${maven.test.skip}" />
<target name="generate-test-sources" />
<property name="maven.test.skip" value="${maven.test.skip}"/>
<target name="generate-test-sources"/>
</ant>
</tasks>
<testSourceRoot>${project.build.directory}/generated-sources/test/java</testSourceRoot>
@@ -89,6 +90,7 @@
<scope>provided</scope>
</dependency>
<!-- O/X Mapping dependencies -->
<!-- JAXB -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
@@ -107,6 +109,12 @@
<version>2.1.3</version>
<scope>test</scope>
</dependency>
<!-- XStream -->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<optional>true</optional>
</dependency>
<!-- JEE dependencies -->
<dependency>
<groupId>javax.activation</groupId>

View File

@@ -0,0 +1,53 @@
/*
* 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.xstream;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.Annotations;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import org.springframework.util.Assert;
/**
* Subclass of the {@link XStreamMarshaller} that supports JDK 1.5+ annotation metadata for aliases.
*
* @author Arjen Poutsma
* @see XStreamAlias
* @since 1.0.2
*/
public class AnnotationXStreamMarshaller extends XStreamMarshaller {
/**
* Sets the classes, for which mappings will be read from class-level JDK 1.5+ annotation metadata.
*
* @see Annotations#configureAliases(XStream, Class[])
*/
public void setAnnotatedClass(Class<?> annotatedClass) {
Assert.notNull(annotatedClass, "'annotatedClass' must not be null");
Annotations.configureAliases(getXStream(), annotatedClass);
}
/**
* Sets annotated classes, for which aliases will be read from class-level JDK 1.5+ annotation metadata.
*
* @see Annotations#configureAliases(XStream, Class[])
*/
public void setAnnotatedClasses(Class<?>[] annotatedClasses) {
Assert.notEmpty(annotatedClasses, "'annotatedClasses' must not be empty");
Annotations.configureAliases(getXStream(), annotatedClasses);
}
}

View File

@@ -0,0 +1,46 @@
/*
* 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.xstream;
import java.io.StringWriter;
import javax.xml.transform.stream.StreamResult;
import org.custommonkey.xmlunit.XMLTestCase;
public class AnnotationXStreamMarshallerTest extends XMLTestCase {
private AnnotationXStreamMarshaller marshaller;
private static final String EXPECTED_STRING = "<flight><number>42</number></flight>";
private Flight flight;
protected void setUp() throws Exception {
marshaller = new AnnotationXStreamMarshaller();
marshaller.setAnnotatedClass(Flight.class);
flight = new Flight();
flight.setFlightNumber(42L);
}
public void testMarshalStreamResultWriter() throws Exception {
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
marshaller.marshal(flight, result);
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
}
}

View File

@@ -0,0 +1,34 @@
/*
* 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.xstream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("flight")
public class Flight {
@XStreamAlias("number")
private long flightNumber;
public long getFlightNumber() {
return flightNumber;
}
public void setFlightNumber(long number) {
this.flightNumber = number;
}
}

View File

@@ -81,7 +81,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
private IBindingFactory bindingFactory;
private TransformerFactory transfomerFactory;
private static TransformerFactory transformerFactory = TransformerFactory.newInstance();
private int indent = -1;
@@ -135,7 +135,6 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
catch (JiBXException ex) {
throw new JibxSystemException(ex);
}
transfomerFactory = TransformerFactory.newInstance();
}
public boolean supports(Class clazz) {
@@ -208,7 +207,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
ByteArrayOutputStream os = new ByteArrayOutputStream();
marshalOutputStream(graph, os);
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
Transformer transformer = transfomerFactory.newTransformer();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(new StreamSource(is), new DOMResult(node));
}
catch (IOException ex) {
@@ -226,7 +225,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
ByteArrayOutputStream os = new ByteArrayOutputStream();
marshalOutputStream(graph, os);
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
Transformer transformer = transfomerFactory.newTransformer();
Transformer transformer = transformerFactory.newTransformer();
SAXResult saxResult = new SAXResult(contentHandler);
saxResult.setLexicalHandler(lexicalHandler);
transformer.transform(new StreamSource(is), saxResult);
@@ -296,7 +295,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
protected Object unmarshalDomNode(Node node) throws XmlMappingException {
try {
Transformer transformer = transfomerFactory.newTransformer();
Transformer transformer = transformerFactory.newTransformer();
ByteArrayOutputStream os = new ByteArrayOutputStream();
transformer.transform(new DOMSource(node), new StreamResult(os));
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
@@ -313,7 +312,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource)
throws XmlMappingException, IOException {
try {
Transformer transformer = transfomerFactory.newTransformer();
Transformer transformer = transformerFactory.newTransformer();
ByteArrayOutputStream os = new ByteArrayOutputStream();
transformer.transform(new SAXSource(xmlReader, inputSource), new StreamResult(os));
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());