1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.betwixt.strategy;
19
20 import org.apache.commons.betwixt.ElementDescriptor;
21 import org.apache.commons.betwixt.io.read.ArrayBindAction;
22 import org.apache.commons.betwixt.io.read.BeanBindAction;
23 import org.apache.commons.betwixt.io.read.MappingAction;
24 import org.apache.commons.betwixt.io.read.ReadContext;
25 import org.apache.commons.betwixt.io.read.SimpleTypeBindAction;
26 import org.xml.sax.Attributes;
27
28 /***
29 * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
30 * @version $Revision: 438373 $
31 */
32 public class DefaultActionMappingStrategy extends ActionMappingStrategy {
33
34 /***
35 * Gets the mapping action to map the given element.
36 * @param namespace not null
37 * @param name not null
38 * @param attributes <code>Attributes</code>, not null
39 * @param context <code>ReadContext</code>, not null
40 * @return <code>MappingAction</code>, not null
41 * @throws Exception
42 */
43 public MappingAction getMappingAction(
44 String namespace,
45 String name,
46 Attributes attributes,
47 ReadContext context)
48 throws Exception {
49 MappingAction result = MappingAction.EMPTY;
50
51 ElementDescriptor activeDescriptor = context.getCurrentDescriptor();
52 if (activeDescriptor != null) {
53 if (activeDescriptor.isHollow()) {
54 if (isArrayDescriptor(activeDescriptor)) {
55 result = ArrayBindAction.createMappingAction(activeDescriptor);
56 } else {
57 result = BeanBindAction.INSTANCE;
58 }
59 }
60 else if (activeDescriptor.isSimple())
61 {
62 result = SimpleTypeBindAction.INSTANCE;
63 }
64 else
65 {
66 ElementDescriptor[] descriptors
67 = activeDescriptor.getElementDescriptors();
68 if (descriptors.length == 1) {
69 ElementDescriptor childDescriptor = descriptors[0];
70 if (childDescriptor.isHollow()
71 && isArrayDescriptor(childDescriptor)) {
72 result = ArrayBindAction.createMappingAction(childDescriptor);
73 }
74 }
75 }
76 }
77 return result;
78 }
79
80 /***
81 * Is the give
82 * @param descriptor <code>ElementDescriptor</code>, possibly null
83 * @return true if the descriptor describes an array property, if null returns false
84 */
85 private boolean isArrayDescriptor(ElementDescriptor descriptor) {
86 boolean result = false;
87 if (descriptor != null) {
88 Class propertyType = descriptor.getPropertyType();
89 if (propertyType != null) {
90 result = propertyType.isArray();
91 }
92 }
93 return result;
94 }
95 }