1
|
|
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
package org.apache.hivemind.impl;
|
16
|
|
|
17
|
|
import java.util.ArrayList;
|
18
|
|
import java.util.HashMap;
|
19
|
|
import java.util.List;
|
20
|
|
import java.util.Map;
|
21
|
|
|
22
|
|
import org.apache.hivemind.Element;
|
23
|
|
import org.apache.hivemind.ErrorLog;
|
24
|
|
import org.apache.hivemind.internal.Module;
|
25
|
|
import org.apache.hivemind.schema.ElementModel;
|
26
|
|
import org.apache.hivemind.schema.Schema;
|
27
|
|
import org.apache.hivemind.schema.SchemaProcessor;
|
28
|
|
import org.apache.hivemind.schema.Translator;
|
29
|
|
|
30
|
|
|
31
|
|
|
32
|
|
|
33
|
|
|
34
|
|
|
35
|
|
|
36
|
|
|
37
|
|
public final class SchemaProcessorImpl implements SchemaProcessor
|
38
|
|
{
|
39
|
|
private ErrorLog _errorLog;
|
40
|
|
|
41
|
|
private Schema _schema;
|
42
|
|
|
43
|
|
|
44
|
|
|
45
|
|
|
46
|
|
private List _elements = new ArrayList();
|
47
|
|
|
48
|
|
private boolean _canElementsBeMapped;
|
49
|
|
|
50
|
|
private Map _mappedElements = new HashMap();
|
51
|
|
|
52
|
|
private List _stack = new ArrayList();
|
53
|
|
|
54
|
|
private Module _module;
|
55
|
|
|
56
|
|
|
57
|
|
|
58
|
|
|
59
|
|
private Map _elementMap = new HashMap();
|
60
|
|
|
61
|
|
|
62
|
|
|
63
|
|
|
64
|
|
private List _elementStack = new ArrayList();
|
65
|
|
|
66
|
859
|
public SchemaProcessorImpl(ErrorLog errorLog, Schema schema)
|
67
|
|
{
|
68
|
859
|
_errorLog = errorLog;
|
69
|
859
|
_schema = schema;
|
70
|
859
|
_stack.add(this);
|
71
|
|
|
72
|
859
|
if (_schema != null)
|
73
|
|
{
|
74
|
851
|
List l = _schema.getElementModel();
|
75
|
|
|
76
|
851
|
int count = l.size();
|
77
|
851
|
for (int i = 0; i < count; i++)
|
78
|
|
{
|
79
|
863
|
ElementModel model = (ElementModel) l.get(i);
|
80
|
863
|
_elementMap.put(model.getElementName(), new SchemaElement(this, model));
|
81
|
|
}
|
82
|
|
|
83
|
851
|
_canElementsBeMapped = schema.canInstancesBeKeyed();
|
84
|
|
}
|
85
|
|
}
|
86
|
|
|
87
|
|
|
88
|
|
|
89
|
|
|
90
|
2503
|
public void addElement(Object element)
|
91
|
|
{
|
92
|
2503
|
_elements.add(element);
|
93
|
|
|
94
|
2503
|
if (_canElementsBeMapped)
|
95
|
|
{
|
96
|
411
|
String key = peekElement().getAttributeValue(_activeElement.getKeyAttribute());
|
97
|
|
|
98
|
411
|
_mappedElements.put(key, element);
|
99
|
|
}
|
100
|
|
}
|
101
|
|
|
102
|
854
|
public List getElements()
|
103
|
|
{
|
104
|
854
|
return _elements;
|
105
|
|
}
|
106
|
|
|
107
|
104
|
public Map getMappedElements()
|
108
|
|
{
|
109
|
104
|
if (_canElementsBeMapped)
|
110
|
104
|
return _mappedElements;
|
111
|
|
|
112
|
0
|
return null;
|
113
|
|
}
|
114
|
|
|
115
|
5227
|
public void push(Object object)
|
116
|
|
{
|
117
|
5227
|
_stack.add(object);
|
118
|
|
}
|
119
|
|
|
120
|
5228
|
public Object pop()
|
121
|
|
{
|
122
|
5228
|
if (_stack.isEmpty())
|
123
|
1
|
throw new ArrayIndexOutOfBoundsException(ImplMessages.schemaStackViolation(this));
|
124
|
|
|
125
|
5227
|
return _stack.remove(_stack.size() - 1);
|
126
|
|
}
|
127
|
|
|
128
|
10287
|
public Object peek()
|
129
|
|
{
|
130
|
10287
|
return peek(0);
|
131
|
|
}
|
132
|
|
|
133
|
15516
|
public Object peek(int depth)
|
134
|
|
{
|
135
|
15516
|
int count = _stack.size();
|
136
|
|
|
137
|
15516
|
int position = count - 1 - depth;
|
138
|
|
|
139
|
15516
|
if (position < 0)
|
140
|
1
|
throw new ArrayIndexOutOfBoundsException(ImplMessages.schemaStackViolation(this));
|
141
|
|
|
142
|
15515
|
return _stack.get(count - 1 - depth);
|
143
|
|
}
|
144
|
|
|
145
|
20791
|
public Module getContributingModule()
|
146
|
|
{
|
147
|
20791
|
return _module;
|
148
|
|
}
|
149
|
|
|
150
|
11
|
public String getElementPath()
|
151
|
|
{
|
152
|
11
|
StringBuffer buffer = new StringBuffer();
|
153
|
11
|
int count = _elementStack.size();
|
154
|
|
|
155
|
11
|
for (int i = 0; i < count; i++)
|
156
|
|
{
|
157
|
10
|
if (i > 0)
|
158
|
1
|
buffer.append('/');
|
159
|
|
|
160
|
10
|
buffer.append(((Element) _elementStack.get(i)).getElementName());
|
161
|
|
}
|
162
|
|
|
163
|
11
|
return buffer.toString();
|
164
|
|
}
|
165
|
|
|
166
|
2902
|
private void pushElement(Element element)
|
167
|
|
{
|
168
|
2902
|
_elementStack.add(element);
|
169
|
|
}
|
170
|
|
|
171
|
411
|
private Element peekElement()
|
172
|
|
{
|
173
|
411
|
return (Element) _elementStack.get(_elementStack.size() - 1);
|
174
|
|
}
|
175
|
|
|
176
|
2899
|
private void popElement()
|
177
|
|
{
|
178
|
2899
|
_elementStack.remove(_elementStack.size() - 1);
|
179
|
|
}
|
180
|
|
|
181
|
|
|
182
|
|
|
183
|
|
|
184
|
864
|
public void process(List elements, Module contributingModule)
|
185
|
|
{
|
186
|
864
|
if (elements == null)
|
187
|
14
|
return;
|
188
|
|
|
189
|
850
|
if (_schema == null)
|
190
|
|
{
|
191
|
3
|
_elements.addAll(elements);
|
192
|
3
|
return;
|
193
|
|
}
|
194
|
|
|
195
|
847
|
_module = contributingModule;
|
196
|
|
|
197
|
847
|
int count = elements.size();
|
198
|
|
|
199
|
847
|
for (int i = 0; i < count; i++)
|
200
|
|
{
|
201
|
2507
|
Element e = (Element) elements.get(i);
|
202
|
|
|
203
|
2507
|
processRootElement(e);
|
204
|
|
}
|
205
|
|
|
206
|
844
|
_module = null;
|
207
|
|
}
|
208
|
|
|
209
|
2507
|
private void processRootElement(Element element)
|
210
|
|
{
|
211
|
2507
|
String name = element.getElementName();
|
212
|
|
|
213
|
2507
|
SchemaElement schemaElement = (SchemaElement) _elementMap.get(name);
|
214
|
|
|
215
|
2507
|
processElement(element, schemaElement);
|
216
|
|
}
|
217
|
|
|
218
|
|
private SchemaElement _activeElement;
|
219
|
|
|
220
|
2902
|
private void processElement(Element element, SchemaElement schemaElement)
|
221
|
|
{
|
222
|
2902
|
pushElement(element);
|
223
|
|
|
224
|
2902
|
if (schemaElement == null)
|
225
|
1
|
_errorLog
|
226
|
|
.error(ImplMessages.unknownElement(this, element), element.getLocation(), null);
|
227
|
|
else
|
228
|
|
{
|
229
|
2901
|
SchemaElement prior = _activeElement;
|
230
|
|
|
231
|
2901
|
schemaElement.validateAttributes(element);
|
232
|
|
|
233
|
2899
|
_activeElement = schemaElement;
|
234
|
|
|
235
|
2899
|
schemaElement.fireBegin(element);
|
236
|
|
|
237
|
2898
|
processNestedElements(element, schemaElement);
|
238
|
|
|
239
|
2898
|
schemaElement.fireEnd(element);
|
240
|
|
|
241
|
2898
|
_activeElement = prior;
|
242
|
|
}
|
243
|
|
|
244
|
2899
|
popElement();
|
245
|
|
}
|
246
|
|
|
247
|
2898
|
private void processNestedElements(Element element, SchemaElement schemaElement)
|
248
|
|
{
|
249
|
2898
|
List l = element.getElements();
|
250
|
2898
|
int count = l.size();
|
251
|
|
|
252
|
2898
|
for (int i = 0; i < count; i++)
|
253
|
|
{
|
254
|
395
|
Element nested = (Element) l.get(i);
|
255
|
395
|
String name = nested.getElementName();
|
256
|
|
|
257
|
395
|
processElement(nested, schemaElement.getNestedElement(name));
|
258
|
|
}
|
259
|
|
}
|
260
|
|
|
261
|
21
|
public Translator getContentTranslator()
|
262
|
|
{
|
263
|
21
|
return _activeElement.getContentTranslator();
|
264
|
|
}
|
265
|
|
|
266
|
5187
|
public Translator getAttributeTranslator(String attributeName)
|
267
|
|
{
|
268
|
5187
|
return _activeElement.getAttributeTranslator(attributeName);
|
269
|
|
}
|
270
|
|
|
271
|
5190
|
public Translator getTranslator(String translator)
|
272
|
|
{
|
273
|
5190
|
return getContributingModule().getTranslator(translator);
|
274
|
|
}
|
275
|
|
}
|