- Revision
- 194
- Author
- mauro
- Date
- 2007-06-29 10:55:15 -0500 (Fri, 29 Jun 2007)
Log Message
Added Serialiser and XStream-based implementation. Refactored XMLView to use Serialiser.
Modified Paths
- trunk/core/src/main/java/org/codehaus/waffle/view/XMLView.java
- trunk/core/src/test/java/org/codehaus/waffle/view/XMLViewTest.java
Added Paths
- trunk/core/src/main/java/org/codehaus/waffle/serialisation/
- trunk/core/src/main/java/org/codehaus/waffle/serialisation/Serialiser.java
- trunk/core/src/main/java/org/codehaus/waffle/serialisation/XStreamSerialiser.java
Diff
Added: trunk/core/src/main/java/org/codehaus/waffle/serialisation/Serialiser.java (0 => 194)
--- trunk/core/src/main/java/org/codehaus/waffle/serialisation/Serialiser.java (rev 0) +++ trunk/core/src/main/java/org/codehaus/waffle/serialisation/Serialiser.java 2007-06-29 15:55:15 UTC (rev 194) @@ -0,0 +1,39 @@ +/***************************************************************************** + * Copyright (C) 2005,2006 Michael Ward * + * All rights reserved. * + * ------------------------------------------------------------------------- * + * The software in this package is published under the terms of the BSD * + * style license a copy of which has been included with this distribution in * + * the LICENSE.txt file. * + * * + * Original code by: Mauro Talevi * + *****************************************************************************/ +package org.codehaus.waffle.serialisation; + +import java.io.Reader; +import java.io.Writer; + +/** + * Serialiser is responsible for serialiasing objects. + * + * @author Mauro Talevi + */ +public interface Serialiser { + + /** + * Marshalls object to a writer + * + * @param object the Object to marshall + * @param writer the writer to which the object is marshalled + */ + void marshall(Object object, Writer writer); + + /** + * Unmarshalls object from reader input + * + * @param reader the input resource + * @return An unmarshalled Object + */ + Object unmarshall(Reader reader); + +}
Added: trunk/core/src/main/java/org/codehaus/waffle/serialisation/XStreamSerialiser.java (0 => 194)
--- trunk/core/src/main/java/org/codehaus/waffle/serialisation/XStreamSerialiser.java (rev 0) +++ trunk/core/src/main/java/org/codehaus/waffle/serialisation/XStreamSerialiser.java 2007-06-29 15:55:15 UTC (rev 194) @@ -0,0 +1,51 @@ +/***************************************************************************** + * Copyright (C) 2005,2006 Michael Ward * + * All rights reserved. * + * ------------------------------------------------------------------------- * + * The software in this package is published under the terms of the BSD * + * style license a copy of which has been included with this distribution in * + * the LICENSE.txt file. * + * * + * Original code by: Mauro Talevi * + *****************************************************************************/ +package org.codehaus.waffle.serialisation; + +import java.io.Reader; +import java.io.Writer; + +import com.thoughtworks.xstream.XStream; +import com.thoughtworks.xstream.io.xml.DomDriver; + +/** + * XStream-based serialiser. Delegates to XStream the marshalling. + * + * @author Mauro Talevi + */ +public class XStreamSerialiser implements Serialiser { + + private XStream xstream; + + /** + * Creates a XStreamSerialiser with default XStream instance with minimal dependencies + */ + public XStreamSerialiser(){ + this(new XStream(new DomDriver())); + } + + /** + * Creates a XStreamSerialiser with a given XStream instance + * @param xstream the XStream instance + */ + public XStreamSerialiser(XStream xstream) { + this.xstream = xstream; + } + + public void marshall(Object object, Writer writer) { + xstream.toXML(object, writer); + } + + public Object unmarshall(Reader reader) { + return xstream.fromXML(reader); + } + +}
Modified: trunk/core/src/main/java/org/codehaus/waffle/view/XMLView.java (193 => 194)
--- trunk/core/src/main/java/org/codehaus/waffle/view/XMLView.java 2007-06-29 15:01:07 UTC (rev 193) +++ trunk/core/src/main/java/org/codehaus/waffle/view/XMLView.java 2007-06-29 15:55:15 UTC (rev 194) @@ -1,70 +1,72 @@ package org.codehaus.waffle.view; +import java.io.IOException; +import java.io.StringWriter; +import java.io.Writer; +import java.util.Collection; + +import javax.servlet.ServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.codehaus.waffle.Constants; +import org.codehaus.waffle.serialisation.Serialiser; +import org.codehaus.waffle.serialisation.XStreamSerialiser; + import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.converters.collections.CollectionConverter; import com.thoughtworks.xstream.io.xml.DomDriver; import com.thoughtworks.xstream.mapper.MapperWrapper; -import org.codehaus.waffle.Constants; -import javax.servlet.ServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.util.Collection; - /** * A view that renders the controller as XML. * * @author Paulo Silveira + * @author Mauro Talevi */ public class XMLView extends ResponderView { - public static final String CONTENT_TYPE = "text/plain"; + private static final String CHARACTER_ENCODING = "ISO-8859-1"; + public static final String CONTENT_TYPE = "text/xml"; + @Override - public void respond(ServletRequest request, HttpServletResponse response) throws IOException { - XStream xStream = new WaffleXStream(); - xStream.registerConverter(new BeanPropertyConverter(), -19); - xStream.registerConverter(new CollectionConverter(xStream.getMapper()) { + public void respond(ServletRequest request, HttpServletResponse response) throws IOException { + response.setCharacterEncoding(CHARACTER_ENCODING); + response.setContentType(CONTENT_TYPE); + response.getOutputStream().print(serialise(request.getAttribute(Constants.CONTROLLER_KEY))); + } + + private String serialise(Object attribute) { + Serialiser serialiser = createSerialiser(); + Writer writer = new StringWriter(); + serialiser.marshall(attribute, writer); + return writer.toString(); + } + + //TODO: should the serialiser(s) be registered in WaffleComponentRegistry? + private Serialiser createSerialiser() { + // TODO: should we stream.setMode(XStream.NO_REFERENCES); ? + XStream xstream = new XStream(new DomDriver()) { + protected MapperWrapper wrapMapper(MapperWrapper next) { + return new MapperWrapper(next) { + @Override + public String serializedClass(Class type) { + String value = super.serializedClass(type); + if (type.getName().replace('$', '-').equals(value)) { + return type.getSimpleName(); + } + return value; + } + }; + } + }; + xstream.registerConverter(new BeanPropertyConverter(), -19); + xstream.registerConverter(new CollectionConverter(xstream.getMapper()) { @Override public boolean canConvert(Class clazz) { return Collection.class.isAssignableFrom(clazz); } }, -18); - - // TODO: should we stream.setMode(XStream.NO_REFERENCES); ? - - String data = "" - response.setContentType(CONTENT_TYPE); // should be set for xml content - - // TODO: char encoding? - response.getOutputStream().print(data); + return new XStreamSerialiser(xstream); } } - -// todo: public class? isolated unit test -/** - * A XStream class already configured to output user friendly XML based on - * getters. - * - */ -class WaffleXStream extends XStream { - - public WaffleXStream() { - super(new DomDriver()); - } - - @Override - protected MapperWrapper wrapMapper(MapperWrapper next) { - return new MapperWrapper(next) { - @Override - public String serializedClass(Class type) { - String value = super.serializedClass(type); - if (type.getName().replace('$', '-').equals(value)) { - return type.getSimpleName(); - } - return value; - } - }; - } - -} \ No newline at end of file
Modified: trunk/core/src/test/java/org/codehaus/waffle/view/XMLViewTest.java (193 => 194)
--- trunk/core/src/test/java/org/codehaus/waffle/view/XMLViewTest.java 2007-06-29 15:01:07 UTC (rev 193) +++ trunk/core/src/test/java/org/codehaus/waffle/view/XMLViewTest.java 2007-06-29 15:55:15 UTC (rev 194) @@ -1,29 +1,33 @@ package org.codehaus.waffle.view; -import com.thoughtworks.xstream.converters.javabean.BeanProperty; -import com.thoughtworks.xstream.converters.javabean.PropertyDictionary; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +import java.io.IOException; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + import org.codehaus.waffle.Constants; import org.jmock.Expectations; -import org.jmock.MockObjectTestCase; import org.jmock.Mockery; import org.jmock.integration.junit4.JMock; import org.junit.Test; import org.junit.runner.RunWith; -import javax.servlet.ServletOutputStream; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; +import com.thoughtworks.xstream.converters.javabean.BeanProperty; +import com.thoughtworks.xstream.converters.javabean.PropertyDictionary; /** * @author Paulo Silveira */ @RunWith(JMock.class) -public class XMLViewTest extends MockObjectTestCase { +public class XMLViewTest { private Mockery mockery = new Mockery(); private HttpServletRequest mockRequest = mockRequest(); private HttpServletResponse mockResponse = mockResponse(); @@ -93,6 +97,7 @@ { one(mockRequest).getAttribute(Constants.CONTROLLER_KEY); will(returnValue(object)); + one(mockResponse).setCharacterEncoding(with(any(String.class))); one(mockResponse).setContentType(with(any(String.class))); one(mockResponse).getOutputStream(); will(returnValue(mockOutput));
To unsubscribe from this list please visit:
