Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
DefaultXMLBeanInfoRegistry |
|
| 1.8;1.8 |
1 | package org.apache.commons.betwixt.registry; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2004 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | ||
19 | import java.util.Collection; |
|
20 | import java.util.HashMap; |
|
21 | import java.util.Iterator; |
|
22 | import java.util.Map; |
|
23 | ||
24 | import org.apache.commons.betwixt.ElementDescriptor; |
|
25 | import org.apache.commons.betwixt.XMLBeanInfo; |
|
26 | import org.apache.commons.betwixt.io.read.ElementMapping; |
|
27 | import org.apache.commons.betwixt.io.read.ReadContext; |
|
28 | ||
29 | /** The default caching implementation. |
|
30 | * A hashmap is used. |
|
31 | * |
|
32 | * @author <a href="mailto:rdonkin@apache.org">Robert Burrell Donkin</a> |
|
33 | * @version $Id: DefaultXMLBeanInfoRegistry.java 190515 2005-06-13 22:46:02 +0100 (Mon, 13 Jun 2005) rdonkin $ |
|
34 | */ |
|
35 | 9214 | public class DefaultXMLBeanInfoRegistry implements XMLBeanInfoRegistry, PolymorphicReferenceResolver { |
36 | ||
37 | /** Used to associated <code>XMLBeanInfo</code>'s to classes */ |
|
38 | 4607 | private Map xmlBeanInfos = new HashMap(); |
39 | ||
40 | /** |
|
41 | * Get <code>XMLBeanInfo</code> from cache. |
|
42 | * |
|
43 | * @param forThisClass the class for which to find a <code>XMLBeanInfo</code> |
|
44 | * @return cached <code>XMLBeanInfo</code> associated with given class |
|
45 | * or <code>null</code> if no <code>XMLBeanInfo</code> has been associated |
|
46 | */ |
|
47 | public XMLBeanInfo get(Class forThisClass) { |
|
48 | 25979 | return (XMLBeanInfo) xmlBeanInfos.get(forThisClass); |
49 | } |
|
50 | ||
51 | /** |
|
52 | * Put into cache |
|
53 | * |
|
54 | * @param forThisClass the class to cache the <code>XMLBeanInfo</code> for |
|
55 | * @param beanInfo the <code>XMLBeanInfo</code> to cache |
|
56 | */ |
|
57 | public void put(Class forThisClass, XMLBeanInfo beanInfo) { |
|
58 | 8524 | xmlBeanInfos.put(forThisClass, beanInfo); |
59 | 8524 | } |
60 | ||
61 | /** |
|
62 | * Flush existing cached <code>XMLBeanInfo</code>'s. |
|
63 | */ |
|
64 | public void flush() { |
|
65 | 843 | xmlBeanInfos.clear(); |
66 | 843 | } |
67 | ||
68 | /** |
|
69 | * Checks all registered <code>XMLBeanInfo</code>'s for the |
|
70 | * first suitable match. |
|
71 | * If a suitable one is found, then the class of that info is used. |
|
72 | * @see org.apache.commons.betwixt.registry.PolymorphicReferenceResolver#resolveType(org.apache.commons.betwixt.io.read.ElementMapping, org.apache.commons.betwixt.io.read.ReadContext) |
|
73 | * @since 0.7 |
|
74 | */ |
|
75 | public Class resolveType(ElementMapping mapping, ReadContext context) { |
|
76 | 958 | Class result = null; |
77 | 958 | Collection cachedClasses = getCachedClasses(); |
78 | 958 | ElementDescriptor mappedDescriptor = mapping.getDescriptor(); |
79 | 958 | Class mappedType = mappedDescriptor.getSingularPropertyType(); |
80 | 958 | if (mappedType == null) { |
81 | 0 | mappedType = mappedDescriptor.getPropertyType(); |
82 | } |
|
83 | 5949 | for (Iterator it = cachedClasses.iterator(); it.hasNext();) { |
84 | 4279 | XMLBeanInfo beanInfo = get((Class)it.next()); |
85 | 4279 | ElementDescriptor typeDescriptor = beanInfo.getElementDescriptor(); |
86 | 4279 | boolean sameName = mapping.getName().equals(typeDescriptor.getQualifiedName()); |
87 | 4279 | if (sameName) |
88 | { |
|
89 | ||
90 | 730 | boolean compatibleClass = mappedType.isAssignableFrom(beanInfo.getBeanClass()); |
91 | 730 | if (compatibleClass ) { |
92 | 246 | result = beanInfo.getBeanClass(); |
93 | 246 | break; |
94 | } |
|
95 | } |
|
96 | } |
|
97 | 958 | return result; |
98 | } |
|
99 | ||
100 | /** |
|
101 | * Gets all classes that are cached in this registry. |
|
102 | * |
|
103 | * @return The classes |
|
104 | */ |
|
105 | private Collection getCachedClasses() |
|
106 | { |
|
107 | 958 | return xmlBeanInfos.keySet(); |
108 | } |
|
109 | } |