|
|||||||||||||||||||
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.commons.logging.LogFactory;
|
|
24 |
import org.apache.hivemind.Element;
|
|
25 |
import org.apache.hivemind.ErrorHandler;
|
|
26 |
import org.apache.hivemind.internal.Module;
|
|
27 |
import org.apache.hivemind.schema.ElementModel;
|
|
28 |
import org.apache.hivemind.schema.Schema;
|
|
29 |
import org.apache.hivemind.schema.SchemaProcessor;
|
|
30 |
import org.apache.hivemind.schema.Translator;
|
|
31 |
|
|
32 |
/**
|
|
33 |
* Used to assemble all the {@link org.apache.hivemind.Contribution}s
|
|
34 |
* contributed to an {@link org.apache.hivemind.ConfigurationPoint} while
|
|
35 |
* converting the XML (represented as {@link org.apache.hivemind.Element}s
|
|
36 |
* into Java objects.
|
|
37 |
*
|
|
38 |
* @author Howard Lewis Ship
|
|
39 |
*/
|
|
40 |
public final class SchemaProcessorImpl implements SchemaProcessor |
|
41 |
{ |
|
42 |
private static final Log LOG = LogFactory.getLog(SchemaProcessorImpl.class); |
|
43 |
|
|
44 |
private ErrorHandler _errorHandler;
|
|
45 |
private Schema _schema;
|
|
46 |
|
|
47 |
/**
|
|
48 |
* The assembled elements that will be contributed into
|
|
49 |
* the ConfigurationPoint.
|
|
50 |
*/
|
|
51 |
private List _elements = new ArrayList(); |
|
52 |
private List _stack = new ArrayList(); |
|
53 |
private Module _module;
|
|
54 |
|
|
55 |
/**
|
|
56 |
* Map on element name to {@link SchemaElement}.
|
|
57 |
*/
|
|
58 |
private Map _elementMap = new HashMap(); |
|
59 |
|
|
60 |
/**
|
|
61 |
* Used to track the nesting of elements.
|
|
62 |
*/
|
|
63 |
private List _elementStack = new ArrayList(); |
|
64 |
|
|
65 | 499 |
public SchemaProcessorImpl(ErrorHandler errorHandler, Schema schema)
|
66 |
{ |
|
67 | 499 |
_errorHandler = errorHandler; |
68 | 499 |
_schema = schema; |
69 | 499 |
_stack.add(this);
|
70 |
|
|
71 | 499 |
if (_schema != null) |
72 |
{ |
|
73 |
|
|
74 | 493 |
List l = _schema.getElementModel(); |
75 | 493 |
int count = l.size();
|
76 | 493 |
for (int i = 0; i < count; i++) |
77 |
{ |
|
78 | 579 |
ElementModel model = (ElementModel) l.get(i); |
79 | 579 |
_elementMap.put(model.getElementName(), new SchemaElement(this, model)); |
80 |
} |
|
81 |
} |
|
82 |
} |
|
83 |
|
|
84 | 1606 |
public void addElement(Object element) |
85 |
{ |
|
86 | 1606 |
_elements.add(element); |
87 |
} |
|
88 |
|
|
89 | 497 |
public List getElements()
|
90 |
{ |
|
91 | 497 |
return _elements;
|
92 |
} |
|
93 |
|
|
94 | 2878 |
public void push(Object object) |
95 |
{ |
|
96 | 2878 |
_stack.add(object); |
97 |
} |
|
98 |
|
|
99 | 2877 |
public Object pop()
|
100 |
{ |
|
101 | 2877 |
return _stack.remove(_stack.size() - 1);
|
102 |
} |
|
103 |
|
|
104 | 6117 |
public Object peek()
|
105 |
{ |
|
106 | 6117 |
return peek(0);
|
107 |
} |
|
108 |
|
|
109 | 8997 |
public Object peek(int depth) |
110 |
{ |
|
111 | 8997 |
int count = _stack.size();
|
112 |
|
|
113 | 8997 |
return _stack.get(count - 1 - depth);
|
114 |
} |
|
115 |
|
|
116 | 12665 |
public Module getContributingModule()
|
117 |
{ |
|
118 | 12665 |
return _module;
|
119 |
} |
|
120 |
|
|
121 | 9 |
public String getElementPath()
|
122 |
{ |
|
123 | 9 |
StringBuffer buffer = new StringBuffer();
|
124 | 9 |
int count = _elementStack.size();
|
125 |
|
|
126 | 9 |
for (int i = 0; i < count; i++) |
127 |
{ |
|
128 | 11 |
if (i > 0)
|
129 | 2 |
buffer.append('/'); |
130 |
|
|
131 | 11 |
buffer.append(_elementStack.get(i).toString()); |
132 |
} |
|
133 |
|
|
134 | 9 |
return buffer.toString();
|
135 |
} |
|
136 |
|
|
137 | 1826 |
private void pushElement(String elementName) |
138 |
{ |
|
139 | 1826 |
_elementStack.add(elementName); |
140 |
} |
|
141 |
|
|
142 | 1824 |
private void popElement() |
143 |
{ |
|
144 | 1824 |
_elementStack.remove(_elementStack.size() - 1); |
145 |
} |
|
146 |
|
|
147 |
/**
|
|
148 |
* Processes a single extension.
|
|
149 |
*/
|
|
150 | 502 |
public void process(List elements, Module contributingModule) |
151 |
{ |
|
152 | 502 |
if (elements == null) |
153 | 10 |
return;
|
154 |
|
|
155 | 492 |
if (_schema == null) |
156 |
{ |
|
157 | 2 |
_elements.addAll(elements); |
158 | 2 |
return;
|
159 |
} |
|
160 |
|
|
161 | 490 |
_module = contributingModule; |
162 |
|
|
163 | 490 |
int count = elements.size();
|
164 |
|
|
165 | 490 |
for (int i = 0; i < count; i++) |
166 |
{ |
|
167 | 1609 |
Element e = (Element) elements.get(i); |
168 |
|
|
169 | 1609 |
processRootElement(e); |
170 |
} |
|
171 |
|
|
172 | 488 |
_module = null;
|
173 |
} |
|
174 |
|
|
175 | 1609 |
private void processRootElement(Element element) |
176 |
{ |
|
177 | 1609 |
String name = element.getElementName(); |
178 |
|
|
179 | 1609 |
SchemaElement schemaElement = (SchemaElement) _elementMap.get(name); |
180 |
|
|
181 | 1609 |
processElement(element, schemaElement); |
182 |
} |
|
183 |
|
|
184 |
private SchemaElement _activeElement;
|
|
185 |
|
|
186 | 1826 |
private void processElement(Element element, SchemaElement schemaElement) |
187 |
{ |
|
188 | 1826 |
String name = element.getElementName(); |
189 |
|
|
190 | 1826 |
pushElement(name); |
191 |
|
|
192 | 1826 |
if (schemaElement == null) |
193 | 1 |
_errorHandler.error( |
194 |
LOG, |
|
195 |
ImplMessages.unknownElement(this, element),
|
|
196 |
element.getLocation(), |
|
197 |
null);
|
|
198 |
else
|
|
199 |
{ |
|
200 | 1825 |
SchemaElement prior = _activeElement; |
201 |
|
|
202 | 1825 |
schemaElement.validateAttributes(element); |
203 |
|
|
204 | 1824 |
_activeElement = schemaElement; |
205 |
|
|
206 | 1824 |
schemaElement.fireBegin(element); |
207 |
|
|
208 | 1823 |
processNestedElements(element, schemaElement); |
209 |
|
|
210 | 1823 |
schemaElement.fireEnd(element); |
211 |
|
|
212 | 1823 |
_activeElement = prior; |
213 |
} |
|
214 |
|
|
215 | 1824 |
popElement(); |
216 |
} |
|
217 |
|
|
218 | 1823 |
private void processNestedElements(Element element, SchemaElement schemaElement) |
219 |
{ |
|
220 | 1823 |
List l = element.getElements(); |
221 | 1823 |
int count = l.size();
|
222 |
|
|
223 | 1823 |
for (int i = 0; i < count; i++) |
224 |
{ |
|
225 | 217 |
Element nested = (Element) l.get(i); |
226 | 217 |
String name = nested.getElementName(); |
227 |
|
|
228 | 217 |
processElement(nested, schemaElement.getNestedElement(name)); |
229 |
} |
|
230 |
} |
|
231 |
|
|
232 | 15 |
public Translator getContentTranslator()
|
233 |
{ |
|
234 | 15 |
return _activeElement.getContentTranslator();
|
235 |
} |
|
236 |
|
|
237 | 3299 |
public Translator getAttributeTranslator(String attributeName)
|
238 |
{ |
|
239 | 3299 |
return _activeElement.getAttributeTranslator(attributeName);
|
240 |
} |
|
241 |
|
|
242 | 3305 |
public Translator getTranslator(String translator)
|
243 |
{ |
|
244 | 3305 |
return getContributingModule().getTranslator(translator);
|
245 |
} |
|
246 |
} |
|
247 |
|
|