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.A100Fields;
15: import com.thoughtworks.xstream.benchmark.reflection.model.A100Parents;
16: import com.thoughtworks.xstream.io.xml.XppDriver;
17: import com.thoughtworks.xstream.tools.benchmark.Product;
18:
19: import java.io.InputStream;
20: import java.io.OutputStream;
21:
22: /**
23: * Uses XStream with the XPP driver for parsing XML with aliases for fields.
24: *
25: * @author Jörg Schaible
26: * @author Joe Walnes
27: * @see com.thoughtworks.xstream.tools.benchmark.Harness
28: * @see Product
29: * @see XStream#aliasField(String, Class, String)
30: */
31: public class XStreamFieldAliases implements Product {
32:
33: private final XStream xstream;
34:
35: public XStreamFieldAliases() {
36: this .xstream = new XStream(new XppDriver());
37: try {
38: Class clsFields = Class.forName(A100Fields.class.getName());
39: for (int i = 0; i < 100; ++i) {
40: String no = "00" + i;
41: no = no.substring(no.length() - 3);
42: xstream.aliasField("f" + no, clsFields, "field" + no);
43: Class cls = Class.forName(A100Parents.class.getName()
44: + "$Parent" + no);
45: xstream.aliasField("f" + no, cls, "field" + no);
46: }
47: } catch (ClassNotFoundException e) {
48: throw new RuntimeException(e);
49: }
50: }
51:
52: public void serialize(Object object, OutputStream output)
53: throws Exception {
54: xstream.toXML(object, output);
55: }
56:
57: public Object deserialize(InputStream input) throws Exception {
58: return xstream.fromXML(input);
59: }
60:
61: public String toString() {
62: return "XStream (field aliases)";
63: }
64:
65: }
|