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 local attribute 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#aliasAttribute(Class, String, String)
30: */
31: public class XStreamLocalAttributeAliases implements Product {
32:
33: private final XStream xstream;
34:
35: public XStreamLocalAttributeAliases() {
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.useAttributeFor(clsFields, "field" + no);
43: xstream.aliasAttribute(clsFields, "field" + no, "f"
44: + no);
45: Class cls = Class.forName(A100Parents.class.getName()
46: + "$Parent" + no);
47: xstream.useAttributeFor(cls, "field" + no);
48: xstream.aliasAttribute(cls, "field" + no, "f" + no);
49: }
50: } catch (ClassNotFoundException e) {
51: throw new RuntimeException(e);
52: }
53: }
54:
55: public void serialize(Object object, OutputStream output)
56: throws Exception {
57: xstream.toXML(object, output);
58: }
59:
60: public Object deserialize(InputStream input) throws Exception {
61: return xstream.fromXML(input);
62: }
63:
64: public String toString() {
65: return "XStream (local attribute aliases)";
66: }
67:
68: }
|