01: /*
02: * Copyright (C) 2007 XStream Committers.
03: * All rights reserved.
04: *
05: * The software in this package is published under the terms of the BSD
06: * style license a copy of which has been included with this distribution in
07: * the LICENSE.txt file.
08: *
09: * Created on 26. June 2007 by Joerg Schaible
10: */
11: package com.thoughtworks.xstream.benchmark.reflection.products;
12:
13: import com.thoughtworks.xstream.XStream;
14: import com.thoughtworks.xstream.benchmark.reflection.model.A100Parents;
15: import com.thoughtworks.xstream.io.xml.XppDriver;
16: import com.thoughtworks.xstream.tools.benchmark.Product;
17:
18: import java.io.InputStream;
19: import java.io.OutputStream;
20:
21: /**
22: * Uses XStream with the XPP driver for parsing XML with aliases for field names.
23: *
24: * @author Jörg Schaible
25: * @author Joe Walnes
26: * @see com.thoughtworks.xstream.tools.benchmark.Harness
27: * @see Product
28: * @see XStream#alias(String, Class)
29: */
30: public class XStreamClassAliases implements Product {
31:
32: private final XStream xstream;
33:
34: public XStreamClassAliases() {
35: this .xstream = new XStream(new XppDriver());
36: for (int i = 0; i < 100; ++i) {
37: String no = "00" + i;
38: no = no.substring(no.length() - 3);
39: try {
40: Class cls = Class.forName(A100Parents.class.getName()
41: + "$Parent" + no);
42: xstream.alias("p" + no, cls);
43: } catch (ClassNotFoundException e) {
44: throw new RuntimeException(e);
45: }
46: }
47: }
48:
49: public void serialize(Object object, OutputStream output)
50: throws Exception {
51: xstream.toXML(object, output);
52: }
53:
54: public Object deserialize(InputStream input) throws Exception {
55: return xstream.fromXML(input);
56: }
57:
58: public String toString() {
59: return "XStream (class aliases)";
60: }
61:
62: }
|