|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
SchemaProcessorImpl.java | 100% | 100% | 100% | 100% |
|
1 |
// Copyright 2004 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
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.commons.logging.Log;
|
|
23 |
import org.apache.hivemind.Element;
|
|
24 |
import org.apache.hivemind.ErrorHandler;
|
|
25 |
import org.apache.hivemind.internal.Module;
|
|
26 |
import org.apache.hivemind.schema.ElementModel;
|
|
27 |
import org.apache.hivemind.schema.Schema;
|
|
28 |
import org.apache.hivemind.schema.SchemaProcessor;
|
|
29 |
import org.apache.hivemind.schema.Translator;
|
|
30 |
|
|
31 |
/**
|
|
32 |
* Used to assemble all the {@link org.apache.hivemind.Contribution}s
|
|
33 |
* contributed to an {@link org.apache.hivemind.ConfigurationPoint} while
|
|
34 |
* converting the XML (represented as {@link org.apache.hivemind.Element}s
|
|
35 |
* into Java objects.
|
|
36 |
*
|
|
37 |
* @author Howard Lewis Ship
|
|
38 |
*/
|
|
39 |
public final class SchemaProcessorImpl implements SchemaProcessor |
|
40 |
{ |
|
41 |
private ErrorHandler _errorHandler;
|
|
42 |
private Log _log;
|
|
43 |
private Schema _schema;
|
|
44 |
|
|
45 |
/**
|
|
46 |
* The assembled elements that will be contributed into
|
|
47 |
* the ConfigurationPoint.
|
|
48 |
*/
|
|
49 |
private List _elements = new ArrayList(); |
|
50 |
private List _stack = new ArrayList(); |
|
51 |
private Module _module;
|
|
52 |
|
|
53 |
/**
|
|
54 |
* Map on element name to {@link SchemaElement}.
|
|
55 |
*/
|
|
56 |
private Map _elementMap = new HashMap(); |
|
57 |
|
|
58 |
/**
|
|
59 |
* Used to track the nesting of elements.
|
|
60 |
*/
|
|
61 |
private List _elementStack = new ArrayList(); |
|
62 |
|
|
63 | 811 |
public SchemaProcessorImpl(ErrorHandler errorHandler, Log log, Schema schema)
|
64 |
{ |
|
65 | 811 |
_errorHandler = errorHandler; |
66 | 811 |
_log = log; |
67 | 811 |
_schema = schema; |
68 | 811 |
_stack.add(this);
|
69 |
|
|
70 | 811 |
if (_schema != null) |
71 |
{ |
|
72 |
|
|
73 | 803 |
List l = _schema.getElementModel(); |
74 | 803 |
int count = l.size();
|
75 | 803 |
for (int i = 0; i < count; i++) |
76 |
{ |
|
77 | 815 |
ElementModel model = (ElementModel) l.get(i); |
78 | 815 |
_elementMap.put(model.getElementName(), new SchemaElement(this, model)); |
79 |
} |
|
80 |
} |
|
81 |
} |
|
82 |
|
|
83 | 2360 |
public void addElement(Object element) |
84 |
{ |
|
85 | 2360 |
_elements.add(element); |
86 |
} |
|
87 |
|
|
88 | 807 |
public List getElements()
|
89 |
{ |
|
90 | 807 |
return _elements;
|
91 |
} |
|
92 |
|
|
93 | 4551 |
public void push(Object object) |
94 |
{ |
|
95 | 4551 |
_stack.add(object); |
96 |
} |
|
97 |
|
|
98 | 4552 |
public Object pop()
|
99 |
{ |
|
100 | 4552 |
if (_stack.isEmpty())
|
101 | 1 |
throw new ArrayIndexOutOfBoundsException(ImplMessages.schemaStackViolation(this)); |
102 |
|
|
103 | 4551 |
return _stack.remove(_stack.size() - 1);
|
104 |
} |
|
105 |
|
|
106 | 9841 |
public Object peek()
|
107 |
{ |
|
108 | 9841 |
return peek(0);
|
109 |
} |
|
110 |
|
|
111 | 14394 |
public Object peek(int depth) |
112 |
{ |
|
113 | 14394 |
int count = _stack.size();
|
114 |
|
|
115 | 14394 |
int position = count - 1 - depth;
|
116 |
|
|
117 | 14394 |
if (position < 0)
|
118 | 1 |
throw new ArrayIndexOutOfBoundsException(ImplMessages.schemaStackViolation(this)); |
119 |
|
|
120 | 14393 |
return _stack.get(count - 1 - depth);
|
121 |
} |
|
122 |
|
|
123 | 20140 |
public Module getContributingModule()
|
124 |
{ |
|
125 | 20140 |
return _module;
|
126 |
} |
|
127 |
|
|
128 | 12 |
public String getElementPath()
|
129 |
{ |
|
130 | 12 |
StringBuffer buffer = new StringBuffer();
|
131 | 12 |
int count = _elementStack.size();
|
132 |
|
|
133 | 12 |
for (int i = 0; i < count; i++) |
134 |
{ |
|
135 | 12 |
if (i > 0)
|
136 | 2 |
buffer.append('/'); |
137 |
|
|
138 | 12 |
buffer.append(_elementStack.get(i).toString()); |
139 |
} |
|
140 |
|
|
141 | 12 |
return buffer.toString();
|
142 |
} |
|
143 |
|
|
144 | 2730 |
private void pushElement(String elementName) |
145 |
{ |
|
146 | 2730 |
_elementStack.add(elementName); |
147 |
} |
|
148 |
|
|
149 | 2727 |
private void popElement() |
150 |
{ |
|
151 | 2727 |
_elementStack.remove(_elementStack.size() - 1); |
152 |
} |
|
153 |
|
|
154 |
/**
|
|
155 |
* Processes a single extension.
|
|
156 |
*/
|
|
157 | 816 |
public void process(List elements, Module contributingModule) |
158 |
{ |
|
159 | 816 |
if (elements == null) |
160 | 14 |
return;
|
161 |
|
|
162 | 802 |
if (_schema == null) |
163 |
{ |
|
164 | 3 |
_elements.addAll(elements); |
165 | 3 |
return;
|
166 |
} |
|
167 |
|
|
168 | 799 |
_module = contributingModule; |
169 |
|
|
170 | 799 |
int count = elements.size();
|
171 |
|
|
172 | 799 |
for (int i = 0; i < count; i++) |
173 |
{ |
|
174 | 2364 |
Element e = (Element) elements.get(i); |
175 |
|
|
176 | 2364 |
processRootElement(e); |
177 |
} |
|
178 |
|
|
179 | 796 |
_module = null;
|
180 |
} |
|
181 |
|
|
182 | 2364 |
private void processRootElement(Element element) |
183 |
{ |
|
184 | 2364 |
String name = element.getElementName(); |
185 |
|
|
186 | 2364 |
SchemaElement schemaElement = (SchemaElement) _elementMap.get(name); |
187 |
|
|
188 | 2364 |
processElement(element, schemaElement); |
189 |
} |
|
190 |
|
|
191 |
private SchemaElement _activeElement;
|
|
192 |
|
|
193 | 2730 |
private void processElement(Element element, SchemaElement schemaElement) |
194 |
{ |
|
195 | 2730 |
String name = element.getElementName(); |
196 |
|
|
197 | 2730 |
pushElement(name); |
198 |
|
|
199 | 2730 |
if (schemaElement == null) |
200 | 1 |
_errorHandler.error( |
201 |
_log, |
|
202 |
ImplMessages.unknownElement(this, element),
|
|
203 |
element.getLocation(), |
|
204 |
null);
|
|
205 |
else
|
|
206 |
{ |
|
207 | 2729 |
SchemaElement prior = _activeElement; |
208 |
|
|
209 | 2729 |
schemaElement.validateAttributes(element); |
210 |
|
|
211 | 2727 |
_activeElement = schemaElement; |
212 |
|
|
213 | 2727 |
schemaElement.fireBegin(element); |
214 |
|
|
215 | 2726 |
processNestedElements(element, schemaElement); |
216 |
|
|
217 | 2726 |
schemaElement.fireEnd(element); |
218 |
|
|
219 | 2726 |
_activeElement = prior; |
220 |
} |
|
221 |
|
|
222 | 2727 |
popElement(); |
223 |
} |
|
224 |
|
|
225 | 2726 |
private void processNestedElements(Element element, SchemaElement schemaElement) |
226 |
{ |
|
227 | 2726 |
List l = element.getElements(); |
228 | 2726 |
int count = l.size();
|
229 |
|
|
230 | 2726 |
for (int i = 0; i < count; i++) |
231 |
{ |
|
232 | 366 |
Element nested = (Element) l.get(i); |
233 | 366 |
String name = nested.getElementName(); |
234 |
|
|
235 | 366 |
processElement(nested, schemaElement.getNestedElement(name)); |
236 |
} |
|
237 |
} |
|
238 |
|
|
239 | 15 |
public Translator getContentTranslator()
|
240 |
{ |
|
241 | 15 |
return _activeElement.getContentTranslator();
|
242 |
} |
|
243 |
|
|
244 | 5367 |
public Translator getAttributeTranslator(String attributeName)
|
245 |
{ |
|
246 | 5367 |
return _activeElement.getAttributeTranslator(attributeName);
|
247 |
} |
|
248 |
|
|
249 | 5373 |
public Translator getTranslator(String translator)
|
250 |
{ |
|
251 | 5373 |
return getContributingModule().getTranslator(translator);
|
252 |
} |
|
253 |
} |
|
254 |
|
|