From 7ee16b9a864a59ca53d481df7116ba217a329b5f Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Mon, 22 Oct 2007 22:43:28 +0000 Subject: [PATCH] SWS-192 --- oxm-tiger/pom.xml | 14 +++-- .../xstream/AnnotationXStreamMarshaller.java | 53 +++++++++++++++++++ .../AnnotationXStreamMarshallerTest.java | 46 ++++++++++++++++ .../springframework/oxm/xstream/Flight.java | 34 ++++++++++++ .../oxm/jibx/JibxMarshaller.java | 11 ++-- 5 files changed, 149 insertions(+), 9 deletions(-) create mode 100644 oxm-tiger/src/main/java/org/springframework/oxm/xstream/AnnotationXStreamMarshaller.java create mode 100644 oxm-tiger/src/test/java/org/springframework/oxm/xstream/AnnotationXStreamMarshallerTest.java create mode 100644 oxm-tiger/src/test/java/org/springframework/oxm/xstream/Flight.java diff --git a/oxm-tiger/pom.xml b/oxm-tiger/pom.xml index c02670e1..b015a138 100644 --- a/oxm-tiger/pom.xml +++ b/oxm-tiger/pom.xml @@ -1,4 +1,5 @@ - + spring-ws org.springframework.ws @@ -26,8 +27,8 @@ - - + + ${project.build.directory}/generated-sources/test/java @@ -89,6 +90,7 @@ provided + javax.xml.bind jaxb-api @@ -107,6 +109,12 @@ 2.1.3 test + + + com.thoughtworks.xstream + xstream + true + javax.activation diff --git a/oxm-tiger/src/main/java/org/springframework/oxm/xstream/AnnotationXStreamMarshaller.java b/oxm-tiger/src/main/java/org/springframework/oxm/xstream/AnnotationXStreamMarshaller.java new file mode 100644 index 00000000..6fa2452b --- /dev/null +++ b/oxm-tiger/src/main/java/org/springframework/oxm/xstream/AnnotationXStreamMarshaller.java @@ -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); + } + +} diff --git a/oxm-tiger/src/test/java/org/springframework/oxm/xstream/AnnotationXStreamMarshallerTest.java b/oxm-tiger/src/test/java/org/springframework/oxm/xstream/AnnotationXStreamMarshallerTest.java new file mode 100644 index 00000000..ab9d56b6 --- /dev/null +++ b/oxm-tiger/src/test/java/org/springframework/oxm/xstream/AnnotationXStreamMarshallerTest.java @@ -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 = "42"; + + 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()); + } + +} \ No newline at end of file diff --git a/oxm-tiger/src/test/java/org/springframework/oxm/xstream/Flight.java b/oxm-tiger/src/test/java/org/springframework/oxm/xstream/Flight.java new file mode 100644 index 00000000..8152f1e2 --- /dev/null +++ b/oxm-tiger/src/test/java/org/springframework/oxm/xstream/Flight.java @@ -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; + } +} \ No newline at end of file diff --git a/oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java b/oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java index 5fb232fb..99137668 100644 --- a/oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java +++ b/oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java @@ -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());