|
|||||||||||||||||||
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 | |||||||||||||||
SchemaElement.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.HashSet;
|
|
20 |
import java.util.Iterator;
|
|
21 |
import java.util.List;
|
|
22 |
import java.util.Map;
|
|
23 |
import java.util.Set;
|
|
24 |
|
|
25 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
26 |
import org.apache.hivemind.Attribute;
|
|
27 |
import org.apache.hivemind.Element;
|
|
28 |
import org.apache.hivemind.schema.AttributeModel;
|
|
29 |
import org.apache.hivemind.schema.ElementModel;
|
|
30 |
import org.apache.hivemind.schema.Rule;
|
|
31 |
import org.apache.hivemind.schema.SchemaProcessor;
|
|
32 |
import org.apache.hivemind.schema.Translator;
|
|
33 |
import org.apache.hivemind.schema.rules.NullTranslator;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* A wrapper around {@link org.apache.hivemind.schema.ElementModel} used
|
|
37 |
* by {@link org.apache.hivemind.impl.SchemaProcessorImpl}.
|
|
38 |
*
|
|
39 |
* @author Howard Lewis Ship
|
|
40 |
*/
|
|
41 |
final class SchemaElement
|
|
42 |
{ |
|
43 |
private SchemaProcessor _processor;
|
|
44 |
private ElementModel _model;
|
|
45 |
private List _requiredAttributes;
|
|
46 |
private Set _knownAttributes;
|
|
47 |
private Map _nestedElements;
|
|
48 |
/**
|
|
49 |
* Keyed on attribute name, value is string (possibly null) used to access a translator.
|
|
50 |
*/
|
|
51 |
private Map _attributeTranslators = new HashMap(); |
|
52 |
|
|
53 | 4669 |
SchemaElement(SchemaProcessor processor, ElementModel model) |
54 |
{ |
|
55 | 4669 |
_processor = processor; |
56 | 4669 |
_model = model; |
57 |
|
|
58 | 4669 |
_requiredAttributes = new ArrayList();
|
59 | 4669 |
_knownAttributes = new HashSet();
|
60 |
|
|
61 | 4669 |
List attributeModels = model.getAttributeModels(); |
62 | 4669 |
int count = attributeModels.size();
|
63 |
|
|
64 | 4669 |
for (int i = 0; i < count; i++) |
65 |
{ |
|
66 | 5053 |
AttributeModel am = (AttributeModel) attributeModels.get(i); |
67 |
|
|
68 | 5053 |
String name = am.getName(); |
69 |
|
|
70 | 5053 |
_knownAttributes.add(name); |
71 |
|
|
72 | 5053 |
if (am.isRequired())
|
73 | 2802 |
_requiredAttributes.add(name); |
74 |
|
|
75 | 5053 |
_attributeTranslators.put(name, am.getTranslator()); |
76 |
} |
|
77 |
} |
|
78 |
|
|
79 |
/**
|
|
80 |
* Returns a {@link SchemaElement} for a nested element, or
|
|
81 |
* null if no such element exists.
|
|
82 |
*/
|
|
83 | 233 |
SchemaElement getNestedElement(String elementName) |
84 |
{ |
|
85 | 233 |
if (_nestedElements == null) |
86 | 217 |
buildNestedElements(); |
87 |
|
|
88 | 233 |
return (SchemaElement) _nestedElements.get(elementName);
|
89 |
} |
|
90 |
|
|
91 | 217 |
private void buildNestedElements() |
92 |
{ |
|
93 | 217 |
_nestedElements = new HashMap();
|
94 |
|
|
95 | 217 |
List l = _model.getElementModel(); |
96 | 217 |
int count = l.size();
|
97 |
|
|
98 | 217 |
for (int i = 0; i < count; i++) |
99 |
{ |
|
100 | 4053 |
ElementModel nested = (ElementModel) l.get(i); |
101 |
|
|
102 | 4053 |
SchemaElement nestedElement = new SchemaElement(_processor, nested);
|
103 |
|
|
104 |
// TODO: Check for duplicates here, or at parse!
|
|
105 |
|
|
106 | 4053 |
_nestedElements.put(nested.getElementName(), nestedElement); |
107 |
} |
|
108 |
|
|
109 |
} |
|
110 |
|
|
111 |
/**
|
|
112 |
* Validates the attributes of the element; checks that all
|
|
113 |
* required attributes are present and that all attributes are defined.
|
|
114 |
* Validation errors result in logged error messages.
|
|
115 |
*
|
|
116 |
*/
|
|
117 | 1863 |
void validateAttributes(Element element)
|
118 |
{ |
|
119 | 1863 |
List l = element.getAttributes(); |
120 | 1863 |
int count = l.size();
|
121 | 1863 |
Set required = new HashSet(_requiredAttributes);
|
122 | 1863 |
List errors = new ArrayList();
|
123 |
|
|
124 | 1863 |
for (int i = 0; i < count; i++) |
125 |
{ |
|
126 | 3347 |
Attribute a = (Attribute) l.get(i); |
127 | 3347 |
String name = a.getName(); |
128 |
|
|
129 | 3347 |
if (!_knownAttributes.contains(name))
|
130 | 1 |
errors.add(ImplMessages.unknownAttribute(name)); |
131 |
|
|
132 | 3347 |
required.remove(name); |
133 |
|
|
134 |
} |
|
135 |
|
|
136 | 1863 |
Iterator it = required.iterator(); |
137 |
|
|
138 | 1863 |
while (it.hasNext())
|
139 |
{ |
|
140 | 1 |
String name = (String) it.next(); |
141 | 1 |
errors.add(ImplMessages.missingAttribute(name)); |
142 |
} |
|
143 |
|
|
144 | 1863 |
count = errors.size(); |
145 |
|
|
146 | 1863 |
if (count == 0)
|
147 | 1862 |
return;
|
148 |
|
|
149 | 1 |
StringBuffer buffer = new StringBuffer();
|
150 |
|
|
151 | 1 |
buffer.append(ImplMessages.elementErrors(_processor, element)); |
152 |
|
|
153 | 1 |
for (int i = 0; i < count; i++) |
154 |
{ |
|
155 | 2 |
buffer.append(' '); |
156 | 2 |
buffer.append(errors.get(i).toString()); |
157 |
} |
|
158 |
|
|
159 | 1 |
throw new ApplicationRuntimeException(buffer.toString(), element.getLocation(), null); |
160 |
} |
|
161 |
|
|
162 | 1862 |
void fireBegin(Element element)
|
163 |
{ |
|
164 | 1862 |
List rules = _model.getRules(); |
165 | 1862 |
int count = rules.size();
|
166 |
|
|
167 | 1862 |
for (int i = 0; i < count; i++) |
168 |
{ |
|
169 | 11782 |
Rule r = (Rule) rules.get(i); |
170 |
|
|
171 | 11782 |
r.begin(_processor, element); |
172 |
|
|
173 |
} |
|
174 |
} |
|
175 |
|
|
176 | 1861 |
void fireEnd(Element element)
|
177 |
{ |
|
178 | 1861 |
List rules = _model.getRules(); |
179 | 1861 |
int count = rules.size();
|
180 |
|
|
181 | 1861 |
for (int i = count - 1; i >= 0; i--) |
182 |
{ |
|
183 | 11778 |
Rule r = (Rule) rules.get(i); |
184 |
|
|
185 | 11778 |
r.end(_processor, element); |
186 |
|
|
187 |
} |
|
188 |
} |
|
189 |
|
|
190 |
private Translator _nullTranslator = new NullTranslator(); |
|
191 |
private Translator _contentTranslator;
|
|
192 |
|
|
193 | 15 |
public Translator getContentTranslator()
|
194 |
{ |
|
195 | 15 |
if (_contentTranslator == null) |
196 | 10 |
_contentTranslator = getTranslator(_model.getContentTranslator()); |
197 |
|
|
198 | 15 |
return _contentTranslator;
|
199 |
} |
|
200 |
|
|
201 | 3584 |
private Translator getTranslator(String translator)
|
202 |
{ |
|
203 | 3584 |
if (translator == null) |
204 | 4 |
return _nullTranslator;
|
205 |
|
|
206 | 3580 |
return _processor.getTranslator(translator);
|
207 |
} |
|
208 |
|
|
209 | 3574 |
public Translator getAttributeTranslator(String attributeName)
|
210 |
{ |
|
211 | 3574 |
String translator = (String) _attributeTranslators.get(attributeName); |
212 |
|
|
213 | 3574 |
return getTranslator(translator);
|
214 |
} |
|
215 |
} |
|
216 |
|
|