All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.thoughtworks.xstream.mapper.AbstractXmlFriendlyMapper Maven / Gradle / Ivy

There is a newer version: 1.4.20_1
Show newest version
/*
 * Copyright (C) 2006 Joe Walnes.
 * Copyright (C) 2006, 2007, 2011 XStream Committers.
 * All rights reserved.
 *
 * The software in this package is published under the terms of the BSD
 * style license a copy of which has been included with this distribution in
 * the LICENSE.txt file.
 * 
 * Created on 03. May 2006 by Mauro Talevi
 */
package com.thoughtworks.xstream.mapper;


/**
 * Mapper that ensures that all names in the serialization stream are XML friendly.
 * The replacement chars and strings are:
 * 
    *
  • $ (dollar) chars appearing in class names are replaced with _ (underscore) chars.
  • *
  • $ (dollar) chars appearing in field names are replaced with _DOLLAR_ string.
  • *
  • _ (underscore) chars appearing in field names are replaced with __ (double underscore) string.
  • *
  • default as the prefix for class names with no package.
  • *
* * Note, this class is no longer in regular use for current XStream versions. It exists to provide backward compatibility * to existing XML data written with older XStream versions. * * @author Joe Walnes * @author Mauro Talevi * @deprecated As of 1.4 use {@link com.thoughtworks.xstream.io.xml.XmlFriendlyReader} */ public class AbstractXmlFriendlyMapper extends MapperWrapper { private char dollarReplacementInClass = '-'; private String dollarReplacementInField = "_DOLLAR_"; private String underscoreReplacementInField = "__"; private String noPackagePrefix = "default"; protected AbstractXmlFriendlyMapper(Mapper wrapped) { super(wrapped); } protected String escapeClassName(String className) { // the $ used in inner class names is illegal as an xml element getNodeName className = className.replace('$', dollarReplacementInClass); // special case for classes named $Blah with no package; <-Blah> is illegal XML if (className.charAt(0) == dollarReplacementInClass) { className = noPackagePrefix + className; } return className; } protected String unescapeClassName(String className) { // special case for classes named $Blah with no package; <-Blah> is illegal XML if (className.startsWith(noPackagePrefix+dollarReplacementInClass)) { className = className.substring(noPackagePrefix.length()); } // the $ used in inner class names is illegal as an xml element getNodeName className = className.replace(dollarReplacementInClass, '$'); return className; } protected String escapeFieldName(String fieldName) { StringBuffer result = new StringBuffer(); int length = fieldName.length(); for(int i = 0; i < length; i++) { char c = fieldName.charAt(i); if (c == '$' ) { result.append(dollarReplacementInField); } else if (c == '_') { result.append(underscoreReplacementInField); } else { result.append(c); } } return result.toString(); } protected String unescapeFieldName(String xmlName) { StringBuffer result = new StringBuffer(); int length = xmlName.length(); for(int i = 0; i < length; i++) { char c = xmlName.charAt(i); if ( stringFoundAt(xmlName, i,underscoreReplacementInField)) { i +=underscoreReplacementInField.length() - 1; result.append('_'); } else if ( stringFoundAt(xmlName, i,dollarReplacementInField)) { i +=dollarReplacementInField.length() - 1; result.append('$'); } else { result.append(c); } } return result.toString(); } private boolean stringFoundAt(String name, int i, String replacement) { if ( name.length() >= i + replacement.length() && name.substring(i, i + replacement.length()).equals(replacement) ){ return true; } return false; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy