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 13. September 2007 by Joerg Schaible
10: */
11: package com.thoughtworks.xstream.benchmark.xmlfriendly.product;
12:
13: import com.thoughtworks.xstream.XStream;
14: import com.thoughtworks.xstream.io.xml.XppDriver;
15: import com.thoughtworks.xstream.tools.benchmark.Product;
16:
17: import java.io.InputStream;
18: import java.io.OutputStream;
19:
20: /**
21: * Uses a combined lookup and replaces characters.
22: *
23: * @author Jörg Schaible
24: */
25: public class IterativeReplacer implements Product {
26:
27: private final XStream xstream;
28: private final int bufferIncrement;
29:
30: public IterativeReplacer(int bufferIncrement) {
31: this .bufferIncrement = bufferIncrement;
32: this .xstream = new XStream(new XppDriver(
33: new XmlFriendlyReplacer(bufferIncrement)));
34: }
35:
36: public void serialize(Object object, OutputStream output)
37: throws Exception {
38: xstream.toXML(object, output);
39: }
40:
41: public Object deserialize(InputStream input) throws Exception {
42: return xstream.fromXML(input);
43: }
44:
45: public String toString() {
46: return "Iterative Replacer"
47: + (bufferIncrement == 0 ? ""
48: : (" (" + bufferIncrement + ")"));
49: }
50:
51: public static class XmlFriendlyReplacer extends
52: AbstractXmlFriendlyReplacer {
53:
54: public XmlFriendlyReplacer(int bufferIncrement) {
55: super ("_-", "__", bufferIncrement);
56: }
57:
58: public XmlFriendlyReplacer(String dollarReplacement,
59: String underscoreReplacement, int bufferIncrement) {
60: super (dollarReplacement, underscoreReplacement,
61: bufferIncrement);
62: }
63:
64: public String escapeName(String name) {
65: return super .escapeIterativelyReplacing(name);
66: }
67:
68: public String unescapeName(String name) {
69: return super.unescapeIterativelyReplacing(name);
70: }
71: }
72: }
|