1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.betwixt.io.read;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.apache.commons.betwixt.ElementDescriptor;
24 import org.apache.commons.betwixt.expression.Context;
25 import org.apache.commons.betwixt.expression.Updater;
26 import org.xml.sax.Attributes;
27
28
29
30
31
32
33
34
35
36
37
38
39
40 public class ArrayBindAction extends MappingAction.Base {
41
42
43
44
45
46
47
48
49 public static final MappingAction createMappingAction(ElementDescriptor elementDescriptor) {
50 MappingAction result = new ArrayBindAction();
51 if (elementDescriptor.getSingularPropertyType() != null &&
52 !elementDescriptor.getSingularPropertyType().isArray()) {
53 result = BeanBindAction.INSTANCE;
54 }
55 return result;
56 }
57
58 private BeanMapping beanMapping = new BeanMapping();
59 private Updater originalUpdater;
60
61
62
63
64
65
66
67 public MappingAction begin(
68 String namespace,
69 String name,
70 Attributes attributes,
71 ReadContext context)
72 throws Exception {
73
74 context.pushBean(new ArrayList());
75 return this;
76 }
77
78
79
80
81
82
83 public void end(ReadContext context) throws Exception {
84 if (originalUpdater != null) {
85
86 List values = (List) context.popBean();
87 originalUpdater.update(context, values);
88 }
89 }
90
91
92 public MappingAction next(
93 String namespace,
94 String name,
95 Attributes attributes,
96 ReadContext context)
97 throws Exception {
98 originalUpdater = context.getCurrentUpdater();
99 MappingAction nextBindAction = BeanBindAction.INSTANCE;
100 beanMapping.setDelegate(nextBindAction);
101 return beanMapping;
102 }
103
104
105
106
107 private static class ListUpdater implements Updater {
108
109 private static final ListUpdater INSTANCE = new ListUpdater();
110
111
112 public void update(Context context, Object newValue) {
113 List values = (List) context.getBean();
114 values.add(newValue);
115 }
116
117 }
118
119 private static class BeanMapping extends MappingAction.Base {
120 private MappingAction delegate;
121
122 BeanMapping() {}
123
124
125
126
127
128
129 MappingAction getDelegate() {
130 return delegate;
131 }
132
133
134
135
136
137 void setDelegate(MappingAction action) {
138 delegate = action;
139 }
140
141
142 public MappingAction begin(
143 String namespace,
144 String name,
145 Attributes attributes,
146 ReadContext context)
147 throws Exception {
148 context.pushUpdater(ListUpdater.INSTANCE);
149 delegate = delegate.begin(namespace, name, attributes, context);
150 return this;
151 }
152
153
154 public void body(String text, ReadContext context) throws Exception {
155 delegate.body(text, context);
156 }
157
158
159 public void end(ReadContext context) throws Exception {
160 delegate.end(context);
161 Updater updater = context.popUpdater();
162 }
163
164
165 public MappingAction next(
166 String namespace,
167 String name,
168 Attributes attributes,
169 ReadContext context)
170 throws Exception {
171 return delegate.next(namespace, name, attributes, context);
172 }
173
174
175 }
176 }