KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > mapper > OuterClassMapper


1 package com.thoughtworks.xstream.mapper;
2
3 import com.thoughtworks.xstream.alias.ClassMapper;
4
5 /**
6  * Mapper that uses a more meaningful alias for the field in an inner class (this$0) that refers to the outer class.
7  *
8  * @author Joe Walnes
9  */

10 public class OuterClassMapper extends MapperWrapper {
11
12     private final String JavaDoc alias;
13
14     public OuterClassMapper(ClassMapper wrapped, String JavaDoc alias) {
15         super(wrapped);
16         this.alias = alias;
17     }
18
19     public OuterClassMapper(ClassMapper wrapped) {
20         this(wrapped, "outer-class");
21     }
22
23     public String JavaDoc serializedMember(Class JavaDoc type, String JavaDoc memberName) {
24         if (memberName.equals("this$0")) {
25             return alias;
26         } else {
27             return super.serializedMember(type, memberName);
28         }
29     }
30
31     public String JavaDoc realMember(Class JavaDoc type, String JavaDoc serialized) {
32         if (serialized.equals(alias)) {
33             return "this$0";
34         } else {
35             return super.realMember(type, serialized);
36         }
37     }
38
39     // --- Maintain backwards compatability
40

41     public String JavaDoc mapNameToXML(String JavaDoc javaName) {
42         if (javaName.equals("this$0")) {
43             return alias;
44         } else {
45             return super.mapNameToXML(javaName);
46         }
47     }
48
49     public String JavaDoc mapNameFromXML(String JavaDoc xmlName) {
50         if (xmlName.equals(alias)) {
51             return "this$0";
52         } else {
53             return super.mapNameFromXML(xmlName);
54         }
55     }
56
57 }
58
Popular Tags