| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.apache.shindig.social.core.util.xstream; |
| 19 | |
|
| 20 | |
import com.google.common.collect.Lists; |
| 21 | |
import com.google.common.collect.Maps; |
| 22 | |
|
| 23 | |
import com.thoughtworks.xstream.mapper.FieldAliasingMapper; |
| 24 | |
import com.thoughtworks.xstream.mapper.Mapper; |
| 25 | |
import com.thoughtworks.xstream.mapper.MapperWrapper; |
| 26 | |
|
| 27 | |
import java.util.ArrayList; |
| 28 | |
import java.util.HashMap; |
| 29 | |
import java.util.List; |
| 30 | |
import java.util.Map; |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
public class InterfaceFieldAliasingMapper extends MapperWrapper { |
| 36 | |
|
| 37 | 222 | private Map<String, List<InterfaceFieldAliasMapping>> serializedMap = Maps.newHashMap(); |
| 38 | 222 | private Map<String, List<InterfaceFieldAliasMapping>> membersMap = Maps.newHashMap(); |
| 39 | |
private WriterStack writerStack; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
public InterfaceFieldAliasingMapper(Mapper wrapped, WriterStack writerStack, |
| 45 | |
List<InterfaceFieldAliasMapping> ifaList) { |
| 46 | 222 | super(wrapped); |
| 47 | 222 | this.writerStack = writerStack; |
| 48 | 222 | for (InterfaceFieldAliasMapping ifa : ifaList) { |
| 49 | |
|
| 50 | 0 | List<InterfaceFieldAliasMapping> serializedMatches = serializedMap.get(ifa.getFieldName()); |
| 51 | 0 | if (serializedMatches == null) { |
| 52 | 0 | serializedMatches = Lists.newArrayList(); |
| 53 | 0 | serializedMap.put(ifa.getFieldName(), serializedMatches); |
| 54 | |
} |
| 55 | 0 | serializedMatches.add(ifa); |
| 56 | 0 | List<InterfaceFieldAliasMapping> memberMatches = membersMap.get(ifa.getAlias()); |
| 57 | 0 | if (memberMatches == null) { |
| 58 | 0 | memberMatches = Lists.newArrayList(); |
| 59 | 0 | membersMap.put(ifa.getAlias(), memberMatches); |
| 60 | |
} |
| 61 | 0 | memberMatches.add(ifa); |
| 62 | 0 | } |
| 63 | 222 | } |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
@Override |
| 72 | |
public String realMember(Class type, String serialized) { |
| 73 | |
|
| 74 | |
|
| 75 | 14 | List<InterfaceFieldAliasMapping> serializedMatches = membersMap |
| 76 | |
.get(serialized); |
| 77 | 14 | if (serializedMatches != null) { |
| 78 | 0 | for (InterfaceFieldAliasMapping ifa : serializedMatches) { |
| 79 | 0 | if (ifa.getType().isAssignableFrom(type)) { |
| 80 | 0 | return ifa.getFieldName(); |
| 81 | |
} |
| 82 | 0 | } |
| 83 | |
} |
| 84 | 14 | return super.realMember(type, serialized); |
| 85 | |
} |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
@Override |
| 94 | |
public String serializedMember(Class type, String memberName) { |
| 95 | |
|
| 96 | |
|
| 97 | 373 | List<InterfaceFieldAliasMapping> memberMatches = serializedMap |
| 98 | |
.get(memberName); |
| 99 | 373 | if (memberMatches != null) { |
| 100 | 0 | for (InterfaceFieldAliasMapping ifa : memberMatches) { |
| 101 | 0 | if (ifa.getParent() == null) { |
| 102 | 0 | if (ifa.getType().isAssignableFrom(type)) { |
| 103 | 0 | return ifa.getAlias(); |
| 104 | |
} |
| 105 | |
} else { |
| 106 | 0 | if (ifa.getType().isAssignableFrom(type) |
| 107 | |
&& ifa.getParent().equals(writerStack.peek())) { |
| 108 | 0 | return ifa.getAlias(); |
| 109 | |
} |
| 110 | |
} |
| 111 | 0 | } |
| 112 | |
} |
| 113 | 373 | return super.serializedMember(type, memberName); |
| 114 | |
} |
| 115 | |
|
| 116 | |
} |