001: /*
002: * Copyright (C) 2006 Joe Walnes.
003: * Copyright (C) 2006, 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 03. May 2006 by Mauro Talevi
011: */
012: package com.thoughtworks.xstream.mapper;
013:
014: /**
015: * Mapper that ensures that all names in the serialization stream are XML friendly.
016: * The replacement chars and strings are:
017: * <ul>
018: * <li><b>$</b> (dollar) chars appearing in class names are replaced with <b>_</b> (underscore) chars.<br></li>
019: * <li><b>$</b> (dollar) chars appearing in field names are replaced with <b>_DOLLAR_</b> string.<br></li>
020: * <li><b>_</b> (underscore) chars appearing in field names are replaced with <b>__</b> (double underscore) string.<br></li>
021: * <li><b>default</b> as the prefix for class names with no package.</li>
022: * </ul>
023: *
024: * @author Joe Walnes
025: * @author Mauro Talevi
026: */
027: public class AbstractXmlFriendlyMapper extends MapperWrapper {
028:
029: private char dollarReplacementInClass = '-';
030: private String dollarReplacementInField = "_DOLLAR_";
031: private String underscoreReplacementInField = "__";
032: private String noPackagePrefix = "default";
033:
034: protected AbstractXmlFriendlyMapper(Mapper wrapped) {
035: super (wrapped);
036: }
037:
038: protected String escapeClassName(String className) {
039: // the $ used in inner class names is illegal as an xml element getNodeName
040: className = className.replace('$', dollarReplacementInClass);
041:
042: // special case for classes named $Blah with no package; <-Blah> is illegal XML
043: if (className.charAt(0) == dollarReplacementInClass) {
044: className = noPackagePrefix + className;
045: }
046:
047: return className;
048: }
049:
050: protected String unescapeClassName(String className) {
051: // special case for classes named $Blah with no package; <-Blah> is illegal XML
052: if (className.startsWith(noPackagePrefix
053: + dollarReplacementInClass)) {
054: className = className.substring(noPackagePrefix.length());
055: }
056:
057: // the $ used in inner class names is illegal as an xml element getNodeName
058: className = className.replace(dollarReplacementInClass, '$');
059:
060: return className;
061: }
062:
063: protected String escapeFieldName(String fieldName) {
064: StringBuffer result = new StringBuffer();
065: int length = fieldName.length();
066: for (int i = 0; i < length; i++) {
067: char c = fieldName.charAt(i);
068: if (c == '$') {
069: result.append(dollarReplacementInField);
070: } else if (c == '_') {
071: result.append(underscoreReplacementInField);
072: } else {
073: result.append(c);
074: }
075: }
076: return result.toString();
077: }
078:
079: protected String unescapeFieldName(String xmlName) {
080: StringBuffer result = new StringBuffer();
081: int length = xmlName.length();
082: for (int i = 0; i < length; i++) {
083: char c = xmlName.charAt(i);
084: if (stringFoundAt(xmlName, i, underscoreReplacementInField)) {
085: i += underscoreReplacementInField.length() - 1;
086: result.append('_');
087: } else if (stringFoundAt(xmlName, i,
088: dollarReplacementInField)) {
089: i += dollarReplacementInField.length() - 1;
090: result.append('$');
091: } else {
092: result.append(c);
093: }
094: }
095: return result.toString();
096: }
097:
098: private boolean stringFoundAt(String name, int i, String replacement) {
099: if (name.length() >= i + replacement.length()
100: && name.substring(i, i + replacement.length()).equals(
101: replacement)) {
102: return true;
103: }
104: return false;
105: }
106:
107: }
|