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