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 _contributingModule;
|
55
|
|
|
56
|
|
|
57
|
|
|
58
|
|
|
59
|
|
private Map _elementMap = new HashMap();
|
60
|
|
|
61
|
|
|
62
|
|
|
63
|
|
|
64
|
|
private List _elementStack = new ArrayList();
|
65
|
|
|
66
|
980
|
public SchemaProcessorImpl(ErrorLog errorLog, Schema schema)
|
67
|
|
{
|
68
|
980
|
_errorLog = errorLog;
|
69
|
980
|
_schema = schema;
|
70
|
980
|
_stack.add(this);
|
71
|
|
|
72
|
980
|
if (_schema != null)
|
73
|
|
{
|
74
|
967
|
List l = _schema.getElementModel();
|
75
|
|
|
76
|
967
|
int count = l.size();
|
77
|
967
|
for (int i = 0; i < count; i++)
|
78
|
|
{
|
79
|
979
|
ElementModel model = (ElementModel) l.get(i);
|
80
|
979
|
_elementMap.put(model.getElementName(), new SchemaElement(this, model));
|
81
|
|
}
|
82
|
|
|
83
|
967
|
_canElementsBeMapped = schema.canInstancesBeKeyed();
|
84
|
|
}
|
85
|
|
}
|
86
|
|
|
87
|
|
|
88
|
|
|
89
|
|
|
90
|
2943
|
public void addElement(Object element)
|
91
|
|
{
|
92
|
2943
|
_elements.add(element);
|
93
|
|
|
94
|
2943
|
if (_canElementsBeMapped)
|
95
|
|
{
|
96
|
578
|
Element currentElement = peekElement();
|
97
|
578
|
String keyAttribute = _activeElement.getKeyAttribute();
|
98
|
|
|
99
|
578
|
String expandedKey = getContributingModule().expandSymbols(
|
100
|
|
currentElement.getAttributeValue(keyAttribute),
|
101
|
|
currentElement.getLocation());
|
102
|
|
|
103
|
578
|
Translator t = getAttributeTranslator(keyAttribute);
|
104
|
|
|
105
|
578
|
Object finalValue = t.translate(
|
106
|
|
getContributingModule(),
|
107
|
|
Object.class,
|
108
|
|
expandedKey,
|
109
|
|
currentElement.getLocation());
|
110
|
|
|
111
|
578
|
_mappedElements.put(finalValue, element);
|
112
|
|
}
|
113
|
|
}
|
114
|
|
|
115
|
975
|
public List getElements()
|
116
|
|
{
|
117
|
975
|
return _elements;
|
118
|
|
}
|
119
|
|
|
120
|
117
|
public Map getMappedElements()
|
121
|
|
{
|
122
|
117
|
if (_canElementsBeMapped)
|
123
|
117
|
return _mappedElements;
|
124
|
|
|
125
|
0
|
return null;
|
126
|
|
}
|
127
|
|
|
128
|
6099
|
public void push(Object object)
|
129
|
|
{
|
130
|
6099
|
_stack.add(object);
|
131
|
|
}
|
132
|
|
|
133
|
6100
|
public Object pop()
|
134
|
|
{
|
135
|
6100
|
if (_stack.isEmpty())
|
136
|
1
|
throw new ArrayIndexOutOfBoundsException(ImplMessages.schemaStackViolation(this));
|
137
|
|
|
138
|
6099
|
return _stack.remove(_stack.size() - 1);
|
139
|
|
}
|
140
|
|
|
141
|
11807
|
public Object peek()
|
142
|
|
{
|
143
|
11807
|
return peek(0);
|
144
|
|
}
|
145
|
|
|
146
|
17908
|
public Object peek(int depth)
|
147
|
|
{
|
148
|
17908
|
int count = _stack.size();
|
149
|
|
|
150
|
17908
|
int position = count - 1 - depth;
|
151
|
|
|
152
|
17908
|
if (position < 0)
|
153
|
1
|
throw new ArrayIndexOutOfBoundsException(ImplMessages.schemaStackViolation(this));
|
154
|
|
|
155
|
17907
|
return _stack.get(count - 1 - depth);
|
156
|
|
}
|
157
|
|
|
158
|
22303
|
public Module getContributingModule()
|
159
|
|
{
|
160
|
22303
|
return _contributingModule;
|
161
|
|
}
|
162
|
|
|
163
|
|
|
164
|
|
|
165
|
5404
|
public Module getDefiningModule()
|
166
|
|
{
|
167
|
5404
|
return _schema.getDefiningModule();
|
168
|
|
}
|
169
|
|
|
170
|
11
|
public String getElementPath()
|
171
|
|
{
|
172
|
11
|
StringBuffer buffer = new StringBuffer();
|
173
|
11
|
int count = _elementStack.size();
|
174
|
|
|
175
|
11
|
for (int i = 0; i < count; i++)
|
176
|
|
{
|
177
|
10
|
if (i > 0)
|
178
|
1
|
buffer.append('/');
|
179
|
|
|
180
|
10
|
buffer.append(((Element) _elementStack.get(i)).getElementName());
|
181
|
|
}
|
182
|
|
|
183
|
11
|
return buffer.toString();
|
184
|
|
}
|
185
|
|
|
186
|
3396
|
private void pushElement(Element element)
|
187
|
|
{
|
188
|
3396
|
_elementStack.add(element);
|
189
|
|
}
|
190
|
|
|
191
|
578
|
private Element peekElement()
|
192
|
|
{
|
193
|
578
|
return (Element) _elementStack.get(_elementStack.size() - 1);
|
194
|
|
}
|
195
|
|
|
196
|
3393
|
private void popElement()
|
197
|
|
{
|
198
|
3393
|
_elementStack.remove(_elementStack.size() - 1);
|
199
|
|
}
|
200
|
|
|
201
|
|
|
202
|
|
|
203
|
|
|
204
|
986
|
public void process(List elements, Module contributingModule)
|
205
|
|
{
|
206
|
986
|
if (elements == null)
|
207
|
19
|
return;
|
208
|
|
|
209
|
967
|
if (_schema == null)
|
210
|
|
{
|
211
|
3
|
_elements.addAll(elements);
|
212
|
3
|
return;
|
213
|
|
}
|
214
|
|
|
215
|
964
|
_contributingModule = contributingModule;
|
216
|
|
|
217
|
964
|
int count = elements.size();
|
218
|
|
|
219
|
964
|
for (int i = 0; i < count; i++)
|
220
|
|
{
|
221
|
2947
|
Element e = (Element) elements.get(i);
|
222
|
|
|
223
|
2947
|
processRootElement(e);
|
224
|
|
}
|
225
|
|
|
226
|
961
|
_contributingModule = null;
|
227
|
|
}
|
228
|
|
|
229
|
2947
|
private void processRootElement(Element element)
|
230
|
|
{
|
231
|
2947
|
String name = element.getElementName();
|
232
|
|
|
233
|
2947
|
SchemaElement schemaElement = (SchemaElement) _elementMap.get(name);
|
234
|
|
|
235
|
2947
|
processElement(element, schemaElement);
|
236
|
|
}
|
237
|
|
|
238
|
|
private SchemaElement _activeElement;
|
239
|
|
|
240
|
3396
|
private void processElement(Element element, SchemaElement schemaElement)
|
241
|
|
{
|
242
|
3396
|
pushElement(element);
|
243
|
|
|
244
|
3396
|
if (schemaElement == null)
|
245
|
1
|
_errorLog
|
246
|
|
.error(ImplMessages.unknownElement(this, element), element.getLocation(), null);
|
247
|
|
else
|
248
|
|
{
|
249
|
3395
|
SchemaElement prior = _activeElement;
|
250
|
|
|
251
|
3395
|
schemaElement.validateAttributes(element);
|
252
|
|
|
253
|
3393
|
_activeElement = schemaElement;
|
254
|
|
|
255
|
3393
|
schemaElement.fireBegin(element);
|
256
|
|
|
257
|
3392
|
processNestedElements(element, schemaElement);
|
258
|
|
|
259
|
3392
|
schemaElement.fireEnd(element);
|
260
|
|
|
261
|
3392
|
_activeElement = prior;
|
262
|
|
}
|
263
|
|
|
264
|
3393
|
popElement();
|
265
|
|
}
|
266
|
|
|
267
|
3392
|
private void processNestedElements(Element element, SchemaElement schemaElement)
|
268
|
|
{
|
269
|
3392
|
List l = element.getElements();
|
270
|
3392
|
int count = l.size();
|
271
|
|
|
272
|
3392
|
for (int i = 0; i < count; i++)
|
273
|
|
{
|
274
|
449
|
Element nested = (Element) l.get(i);
|
275
|
449
|
String name = nested.getElementName();
|
276
|
|
|
277
|
449
|
processElement(nested, schemaElement.getNestedElement(name));
|
278
|
|
}
|
279
|
|
}
|
280
|
|
|
281
|
21
|
public Translator getContentTranslator()
|
282
|
|
{
|
283
|
21
|
return _activeElement.getContentTranslator();
|
284
|
|
}
|
285
|
|
|
286
|
6551
|
public Translator getAttributeTranslator(String attributeName)
|
287
|
|
{
|
288
|
6551
|
return _activeElement.getAttributeTranslator(attributeName);
|
289
|
|
}
|
290
|
|
|
291
|
7600
|
public Translator getTranslator(String translator)
|
292
|
|
{
|
293
|
7600
|
return getContributingModule().getTranslator(translator);
|
294
|
|
}
|
295
|
|
}
|