1 package org.codehaus.xfire.aegis.type;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import javax.xml.namespace.QName;
7
8
9
10 /***
11 * Contains type mappings for java/qname pairs and
12 * Serializer/Deserializer factories.
13 *
14 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
15 * @since Feb 21, 2004
16 */
17 public class CustomTypeMapping
18 implements TypeMapping
19 {
20 private Map class2Type;
21
22 private Map xml2Type;
23
24 private Map class2xml;
25
26 private TypeMapping defaultTM;
27
28 private String encodingStyleURI;
29
30 private TypeCreator typeCreator;
31
32 public CustomTypeMapping( TypeMapping defaultTM )
33 {
34 this();
35
36 this.defaultTM = defaultTM;
37 }
38
39 public CustomTypeMapping()
40 {
41 class2Type = new HashMap();
42 class2xml = new HashMap();
43 xml2Type = new HashMap();
44 }
45
46 public boolean isRegistered(Class javaType)
47 {
48 boolean registered = class2Type.containsKey(javaType);
49
50 if ( !registered && defaultTM != null )
51 registered = defaultTM.isRegistered(javaType);
52
53 return registered;
54 }
55
56 public boolean isRegistered(QName xmlType)
57 {
58 boolean registered = xml2Type.containsKey(xmlType);
59
60 if ( !registered && defaultTM != null )
61 registered = defaultTM.isRegistered(xmlType);
62
63 return registered;
64 }
65
66 public void register(Class javaType, QName xmlType, Type type)
67 {
68 type.setSchemaType(xmlType);
69 type.setTypeClass(javaType);
70
71 register(type);
72 }
73
74 public void register(Type type)
75 {
76 if (type.getTypeClass() == null)
77 throw new NullPointerException("Type class cannot be null.");
78
79 if (type.getSchemaType() == null)
80 throw new NullPointerException("Schema type cannot be null.");
81
82 type.setTypeMapping(this);
83
84 class2Type.put( type.getTypeClass(), type );
85 xml2Type.put( type.getSchemaType(), type );
86 class2xml.put( type.getTypeClass(), type.getSchemaType() );
87 }
88
89 public void removeType(Type type)
90 {
91 if (!xml2Type.containsKey(type.getSchemaType()))
92 {
93 defaultTM.removeType(type);
94 }
95 else
96 {
97 xml2Type.remove(type.getSchemaType());
98 class2Type.remove(type.getTypeClass());
99 class2xml.remove(type.getTypeClass());
100 }
101 }
102
103 /***
104 * @see org.codehaus.xfire.aegis.type.TypeMapping#getType(java.lang.Class)
105 */
106 public Type getType(Class javaType)
107 {
108 Type type = (Type) class2Type.get( javaType );
109
110 if ( type == null && defaultTM != null )
111 {
112 type = defaultTM.getType( javaType );
113 }
114
115 return type;
116 }
117
118 /***
119 * @see org.codehaus.xfire.aegis.type.TypeMapping#getType(javax.xml.namespace.QName)
120 */
121 public Type getType(QName xmlType)
122 {
123 Type type = (Type) xml2Type.get( xmlType );
124
125 if ( type == null && defaultTM != null )
126 type = defaultTM.getType( xmlType );
127
128 return type;
129 }
130
131 /***
132 * @see org.codehaus.xfire.aegis.type.TypeMapping#getTypeQName(java.lang.Class)
133 */
134 public QName getTypeQName(Class clazz)
135 {
136 QName qname = (QName) class2xml.get( clazz );
137
138 if ( qname == null && defaultTM != null )
139 qname = defaultTM.getTypeQName( clazz );
140
141 return qname;
142 }
143
144 public String getEncodingStyleURI()
145 {
146 return encodingStyleURI;
147 }
148
149 public void setEncodingStyleURI( String encodingStyleURI )
150 {
151 this.encodingStyleURI = encodingStyleURI;
152 }
153
154 public TypeCreator getTypeCreator()
155 {
156 return typeCreator;
157 }
158
159 public void setTypeCreator(TypeCreator typeCreator)
160 {
161 this.typeCreator = typeCreator;
162
163 typeCreator.setTypeMapping(this);
164 }
165 }