01: /*
02: * Copyright (C) 2006 Joe Walnes.
03: * Copyright (C) 2006, 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 27. March 2006 by Joerg Schaible
11: */
12: package com.thoughtworks.xstream.mapper;
13:
14: import java.util.HashMap;
15: import java.util.Iterator;
16: import java.util.Map;
17:
18: /**
19: * Mapper that allows aliasing of attribute names.
20: *
21: * @author Jörg Schaible
22: * @author Guilherme Silveira
23: * @since 1.2
24: */
25: public class AttributeAliasingMapper extends MapperWrapper {
26:
27: private final Map aliasToName = new HashMap();
28: private transient Map nameToAlias = new HashMap();
29:
30: public AttributeAliasingMapper(Mapper wrapped) {
31: super (wrapped);
32: }
33:
34: public void addAliasFor(final String attributeName,
35: final String alias) {
36: aliasToName.put(alias, attributeName);
37: nameToAlias.put(attributeName, alias);
38: }
39:
40: public String aliasForAttribute(String attribute) {
41: String alias = (String) nameToAlias.get(attribute);
42: return alias == null ? super .aliasForAttribute(attribute)
43: : alias;
44: }
45:
46: public String attributeForAlias(String alias) {
47: String name = (String) aliasToName.get(alias);
48: return name == null ? super .attributeForAlias(alias) : name;
49: }
50:
51: private String getAliasForName(String name) {
52: String alias = (String) nameToAlias.get(name);
53: return alias == null ? name : alias;
54: }
55:
56: private Object readResolve() {
57: nameToAlias = new HashMap();
58: for (final Iterator iter = aliasToName.keySet().iterator(); iter
59: .hasNext();) {
60: final Object alias = iter.next();
61: nameToAlias.put(aliasToName.get(alias), alias);
62: }
63: return this;
64: }
65: }
|