001: /*
002: * Copyright (C) 2007 XStream Committers.
003: * All rights reserved.
004: *
005: * The software in this package is published under the terms of the BSD
006: * style license a copy of which has been included with this distribution in
007: * the LICENSE.txt file.
008: *
009: * Created on 13. September 2007 by Joerg Schaible
010: */
011: package com.thoughtworks.xstream.benchmark.xmlfriendly.product;
012:
013: import com.thoughtworks.xstream.XStream;
014: import com.thoughtworks.xstream.io.xml.XppDriver;
015: import com.thoughtworks.xstream.tools.benchmark.Product;
016:
017: import java.io.InputStream;
018: import java.io.OutputStream;
019:
020: /**
021: * Uses XmlFriendlyReplacer of XStream 1.2.2.
022: *
023: * @author Jörg Schaible
024: */
025: public class XStream122Replacer implements Product {
026:
027: private final XStream xstream;
028:
029: public XStream122Replacer() {
030: this .xstream = new XStream(new XppDriver(
031: new XmlFriendlyReplacer()));
032: }
033:
034: public void serialize(Object object, OutputStream output)
035: throws Exception {
036: xstream.toXML(object, output);
037: }
038:
039: public Object deserialize(InputStream input) throws Exception {
040: return xstream.fromXML(input);
041: }
042:
043: public String toString() {
044: return "XStream 1.2.2 Replacer";
045: }
046:
047: public static class XmlFriendlyReplacer extends
048: com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer {
049:
050: private String dollarReplacement;
051: private String underscoreReplacement;
052:
053: /**
054: * Default constructor.
055: */
056: public XmlFriendlyReplacer() {
057: this ("_-", "__", 0);
058: }
059:
060: /**
061: * Creates an XmlFriendlyReplacer with custom replacements
062: * @param dollarReplacement the replacement for '$'
063: * @param underscoreReplacement the replacement for '_'
064: */
065: public XmlFriendlyReplacer(String dollarReplacement,
066: String underscoreReplacement, int dummy) {
067: this .dollarReplacement = dollarReplacement;
068: this .underscoreReplacement = underscoreReplacement;
069: }
070:
071: /**
072: * Escapes name substituting '$' and '_' with replacement strings
073: * @param name the name of attribute or node
074: * @return The String with the escaped name
075: */
076: public String escapeName(String name) {
077: StringBuffer result = new StringBuffer();
078: int length = name.length();
079: for (int i = 0; i < length; i++) {
080: char c = name.charAt(i);
081: if (c == '$') {
082: result.append(dollarReplacement);
083: } else if (c == '_') {
084: result.append(underscoreReplacement);
085: } else {
086: result.append(c);
087: }
088: }
089: return result.toString();
090: }
091:
092: /**
093: * Unescapes name re-enstating '$' and '_' when replacement strings are found
094: * @param name the name of attribute or node
095: * @return The String with unescaped name
096: */
097: public String unescapeName(String name) {
098: StringBuffer result = new StringBuffer();
099: int length = name.length();
100: for (int i = 0; i < length; i++) {
101: char c = name.charAt(i);
102: if (stringFoundAt(name, i, dollarReplacement)) {
103: i += dollarReplacement.length() - 1;
104: result.append('$');
105: } else if (stringFoundAt(name, i, underscoreReplacement)) {
106: i += underscoreReplacement.length() - 1;
107: result.append('_');
108: } else {
109: result.append(c);
110: }
111: }
112: return result.toString();
113: }
114:
115: private boolean stringFoundAt(String name, int i,
116: String replacement) {
117: if (name.length() >= i + replacement.length()
118: && name.substring(i, i + replacement.length())
119: .equals(replacement)) {
120: return true;
121: }
122: return false;
123: }
124:
125: }
126: }
|