1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.betwixt.io.read;
17
18 import org.apache.commons.betwixt.AttributeDescriptor;
19 import org.apache.commons.betwixt.ElementDescriptor;
20 import org.xml.sax.Attributes;
21
22 /***
23 * Executes mapping action for a subgraph.
24 * It is intended that most MappingAction's will not need to maintain state.
25 *
26 * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
27 * @version $Revision: 1.2 $
28 */
29 public abstract class MappingAction {
30
31
32 public abstract MappingAction next(
33 String namespace,
34 String name,
35 Attributes attributes,
36 ReadContext context)
37 throws Exception;
38
39 /***
40 * Executes mapping action on new element.
41 * @param namespace
42 * @param name
43 * @param attributes Attributes not null
44 * @param context Context not null
45 * @return the MappingAction to be used to map the sub-graph
46 * under this element
47 * @throws Exception
48 */
49 public abstract MappingAction begin(
50 String namespace,
51 String name,
52 Attributes attributes,
53 ReadContext context)
54 throws Exception;
55
56 /***
57 * Executes mapping action for element body text
58 * @param text
59 * @param context
60 * @throws Exception
61 */
62 public abstract void body(String text, ReadContext context)
63 throws Exception;
64
65 /***
66 * Executes mapping action one element ends
67 * @param context
68 * @throws Exception
69 */
70 public abstract void end(ReadContext context) throws Exception;
71
72 public static final MappingAction EMPTY = new MappingAction.Base();
73
74 public static final MappingAction IGNORE = new MappingAction.Ignore();
75
76 private static final class Ignore extends MappingAction {
77
78 public MappingAction next(String namespace, String name, Attributes attributes, ReadContext context) throws Exception {
79 return this;
80 }
81
82 public MappingAction begin(String namespace, String name, Attributes attributes, ReadContext context) throws Exception {
83 return this;
84 }
85
86 public void body(String text, ReadContext context) throws Exception {
87
88 }
89
90 public void end(ReadContext context) throws Exception {
91
92 }
93
94 }
95
96 /***
97 * Basic action.
98 *
99 * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
100 * @version $Revision: 1.2 $
101 */
102 public static class Base extends MappingAction {
103
104 public MappingAction next(
105 String namespace,
106 String name,
107 Attributes attributes,
108 ReadContext context)
109 throws Exception {
110
111 return context.getActionMappingStrategy().getMappingAction(namespace, name, attributes, context);
112 }
113
114
115
116
117 public MappingAction begin(
118 String namespace,
119 String name,
120 Attributes attributes,
121 ReadContext context)
122 throws Exception {
123
124
125
126 ElementDescriptor descriptor = context.getCurrentDescriptor();
127 if (descriptor != null) {
128
129 AttributeDescriptor[] attributeDescriptors =
130 descriptor.getAttributeDescriptors();
131 context.populateAttributes(attributeDescriptors, attributes);
132 }
133 return this;
134 }
135
136
137
138
139 public void body(String text, ReadContext context) throws Exception {
140
141 }
142
143
144
145
146 public void end(ReadContext context) throws Exception {
147
148
149
150
151 context.popElement();
152 }
153
154 }
155 }