1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
package org.kuali.student.common.dictionary.service.impl.old; |
17 |
|
|
18 |
|
import java.text.SimpleDateFormat; |
19 |
|
import java.util.HashMap; |
20 |
|
import java.util.HashSet; |
21 |
|
import java.util.List; |
22 |
|
import java.util.Map; |
23 |
|
|
24 |
|
import javax.xml.parsers.DocumentBuilder; |
25 |
|
import javax.xml.parsers.DocumentBuilderFactory; |
26 |
|
|
27 |
|
import org.apache.log4j.Logger; |
28 |
|
import org.kuali.student.common.dictionary.old.dto.CaseConstraint; |
29 |
|
import org.kuali.student.common.dictionary.old.dto.ConstraintDescriptor; |
30 |
|
import org.kuali.student.common.dictionary.old.dto.ConstraintSelector; |
31 |
|
import org.kuali.student.common.dictionary.old.dto.Context; |
32 |
|
import org.kuali.student.common.dictionary.old.dto.Field; |
33 |
|
import org.kuali.student.common.dictionary.old.dto.FieldDescriptor; |
34 |
|
import org.kuali.student.common.dictionary.old.dto.LookupConstraint; |
35 |
|
import org.kuali.student.common.dictionary.old.dto.LookupKeyConstraint; |
36 |
|
import org.kuali.student.common.dictionary.old.dto.ObjectStructure; |
37 |
|
import org.kuali.student.common.dictionary.old.dto.OccursConstraint; |
38 |
|
import org.kuali.student.common.dictionary.old.dto.RequireConstraint; |
39 |
|
import org.kuali.student.common.dictionary.old.dto.SearchSelector; |
40 |
|
import org.kuali.student.common.dictionary.old.dto.State; |
41 |
|
import org.kuali.student.common.dictionary.old.dto.Type; |
42 |
|
import org.kuali.student.common.dictionary.old.dto.TypeStateCaseConstraint; |
43 |
|
import org.kuali.student.common.dictionary.old.dto.TypeStateWhenConstraint; |
44 |
|
import org.kuali.student.common.dictionary.old.dto.ValidCharsConstraint; |
45 |
|
import org.kuali.student.common.dictionary.old.dto.WhenConstraint; |
46 |
|
import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
47 |
|
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; |
48 |
|
import org.springframework.beans.factory.xml.ParserContext; |
49 |
|
import org.w3c.dom.Attr; |
50 |
|
import org.w3c.dom.Document; |
51 |
|
import org.w3c.dom.Element; |
52 |
|
import org.w3c.dom.Node; |
53 |
|
|
54 |
|
|
55 |
|
@author |
56 |
|
|
57 |
|
|
58 |
|
@Deprecated |
|
|
| 89.6% |
Uncovered Elements: 24 (230) |
Complexity: 71 |
Complexity Density: 0.56 |
|
59 |
|
public class DictionaryBeanDefinitionParser extends AbstractSingleBeanDefinitionParser{ |
60 |
|
|
61 |
|
final Logger logger = Logger.getLogger(DictionaryBeanDefinitionParser.class); |
62 |
|
|
63 |
|
|
|
|
| 83.6% |
Uncovered Elements: 12 (73) |
Complexity: 19 |
Complexity Density: 0.51 |
|
64 |
107912
|
@Override... |
65 |
|
protected Class<?> getBeanClass(Element element) { |
66 |
|
|
67 |
107912
|
if (element.getLocalName().equals("case")){ |
68 |
0
|
return CaseConstraint.class; |
69 |
|
} |
70 |
107912
|
if (element.getLocalName().equals("constraintDescriptor")) { |
71 |
22783
|
return ConstraintDescriptor.class; |
72 |
|
} |
73 |
85129
|
if (element.getLocalName().equals("constraint")) { |
74 |
8981
|
return ConstraintSelector.class; |
75 |
|
} |
76 |
76148
|
if (element.getLocalName().equals("context")) { |
77 |
0
|
return Context.class; |
78 |
|
} |
79 |
76148
|
if (element.getLocalName().equals("field")) { |
80 |
22783
|
return Field.class; |
81 |
|
} |
82 |
53365
|
if (element.getLocalName().equals("fieldDescriptor")) { |
83 |
23284
|
return FieldDescriptor.class; |
84 |
|
} |
85 |
30081
|
if (element.getLocalName().equals("lookup")) { |
86 |
2375
|
return LookupConstraint.class; |
87 |
|
} |
88 |
27706
|
if (element.getLocalName().equals("lookupKey")) { |
89 |
2375
|
return LookupKeyConstraint.class; |
90 |
|
} |
91 |
25331
|
if (element.getLocalName().equals("objectStructure")) { |
92 |
6238
|
return ObjectStructure.class; |
93 |
|
} |
94 |
19093
|
if (element.getLocalName().equals("occurs")) { |
95 |
0
|
return OccursConstraint.class; |
96 |
|
} |
97 |
19093
|
if (element.getLocalName().equals("require")) { |
98 |
0
|
return RequireConstraint.class; |
99 |
|
} |
100 |
19093
|
if (element.getLocalName().equals("search")) { |
101 |
6
|
return SearchSelector.class; |
102 |
|
} |
103 |
19087
|
if (element.getLocalName().equals("state")) { |
104 |
9013
|
return State.class; |
105 |
|
} |
106 |
10074
|
if (element.getLocalName().equals("type")) { |
107 |
6426
|
return Type.class; |
108 |
|
} |
109 |
3648
|
if (element.getLocalName().equals("typeStateCase")) { |
110 |
4
|
return TypeStateCaseConstraint.class; |
111 |
|
} |
112 |
3644
|
if (element.getLocalName().equals("typeStateWhen")) { |
113 |
6
|
return TypeStateWhenConstraint.class; |
114 |
|
} |
115 |
3638
|
if (element.getLocalName().equals("when")) { |
116 |
0
|
return WhenConstraint.class; |
117 |
|
} |
118 |
3638
|
if (element.getLocalName().equals("validChars")) { |
119 |
3638
|
return ValidCharsConstraint.class; |
120 |
|
} |
121 |
|
|
122 |
|
|
123 |
0
|
return super.getBeanClass(element); |
124 |
|
} |
125 |
|
|
|
|
| 97.2% |
Uncovered Elements: 2 (72) |
Complexity: 21 |
Complexity Density: 0.5 |
|
126 |
107912
|
@Override... |
127 |
|
protected void doParse(Element element, ParserContext pc, BeanDefinitionBuilder builder) { |
128 |
|
|
129 |
|
|
130 |
107912
|
if(element.hasAttributes()){ |
131 |
368701
|
for(int i = 0;i<element.getAttributes().getLength();i++){ |
132 |
266140
|
Attr attr = (Attr) element.getAttributes().item(i); |
133 |
266140
|
if("abstract".equals(attr.getName())){ |
134 |
43761
|
builder.setAbstract(true); |
135 |
222379
|
}else if(!"id".equals(attr.getName())&&!"parent".equals(attr.getName())){ |
136 |
82058
|
builder.addPropertyValue(attr.getName(), attr.getValue()); |
137 |
|
} |
138 |
|
} |
139 |
|
} |
140 |
|
|
141 |
|
|
142 |
107912
|
HashSet<String> visitedNodes = new HashSet<String>(); |
143 |
548699
|
for(int i = 0;i<element.getChildNodes().getLength();i++){ |
144 |
440787
|
Node node = element.getChildNodes().item(i); |
145 |
|
|
146 |
|
|
147 |
440787
|
if(Node.ELEMENT_NODE == node.getNodeType()){ |
148 |
|
|
149 |
|
|
150 |
176027
|
String localName=node.getLocalName(); |
151 |
176027
|
if(localName.endsWith("Ref")){ |
152 |
103377
|
localName=localName.substring(0, localName.length()-"Ref".length()); |
153 |
|
} |
154 |
176027
|
if(!visitedNodes.contains(localName)){ |
155 |
|
|
156 |
115110
|
if(isList(localName)){ |
157 |
26524
|
Element childList=getChildList(element,localName); |
158 |
26524
|
visitedNodes.add(localName); |
159 |
26524
|
List<?> refList = pc.getDelegate().parseListElement(childList, pc.getContainingBeanDefinition()); |
160 |
26524
|
if(refList!=null&&!refList.isEmpty()){ |
161 |
26524
|
String fieldName=resolveFieldName(element.getLocalName(),localName); |
162 |
26524
|
builder.addPropertyValue(fieldName,refList); |
163 |
|
} |
164 |
|
|
165 |
88586
|
}else if("attributes".equals(node.getLocalName())){ |
166 |
2
|
Map<String,String> attributes = getAttributeMap((Element)node); |
167 |
2
|
builder.addPropertyValue(node.getLocalName(), attributes); |
168 |
|
|
169 |
88584
|
}else if(node.getLocalName().endsWith("Ref")){ |
170 |
23357
|
if("objectStructureRef".equals(node.getLocalName())){ |
171 |
1351
|
builder.addPropertyValue("objectStructureRef", ((Element)node).getAttribute("bean")); |
172 |
|
|
173 |
1351
|
builder.addPropertyReference("objectStructure", ((Element)node).getAttribute("bean")); |
174 |
|
}else{ |
175 |
22006
|
builder.addPropertyReference(localName, ((Element)node).getAttribute("bean")); |
176 |
|
} |
177 |
|
}else{ |
178 |
|
|
179 |
65227
|
Element childElement = getFirstChildElement(node); |
180 |
65227
|
if(childElement!=null ||"search".equals(node.getLocalName())){ |
181 |
|
|
182 |
5467
|
Object childBean = pc.getDelegate().parsePropertySubElement((Element)node, pc.getContainingBeanDefinition()); |
183 |
5467
|
String fieldName=resolveFieldName(element.getLocalName(),node.getLocalName()); |
184 |
5467
|
builder.addPropertyValue(fieldName, childBean); |
185 |
|
}else{ |
186 |
|
|
187 |
|
|
188 |
|
|
189 |
59760
|
String fieldName=resolveFieldName(element.getLocalName(),node.getLocalName()); |
190 |
|
|
191 |
59760
|
if(Node.ELEMENT_NODE == node.getNodeType()&&"date".equals(((Element)node).getSchemaTypeInfo().getTypeName())){ |
192 |
2945
|
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); |
193 |
2945
|
try { |
194 |
2945
|
builder.addPropertyValue(fieldName, df.parse(node.getTextContent())); |
195 |
|
} catch (Exception e) { |
196 |
0
|
logger.error("Cannot convert date, must be in format 'YYYY-MM-DD' :"+node.getTextContent(),e); |
197 |
|
} |
198 |
|
}else{ |
199 |
56815
|
builder.addPropertyValue(fieldName, node.getTextContent()); |
200 |
|
} |
201 |
|
} |
202 |
|
} |
203 |
|
} |
204 |
|
} |
205 |
|
} |
206 |
|
} |
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
@param |
215 |
|
@return |
216 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
217 |
2
|
private Map<String, String> getAttributeMap(Element element) {... |
218 |
2
|
Map<String, String> attributes = new HashMap<String, String>(); |
219 |
12
|
for(int i = 0;i<element.getChildNodes().getLength();i++){ |
220 |
10
|
Node node = element.getChildNodes().item(i); |
221 |
10
|
if(Node.ELEMENT_NODE == node.getNodeType() && "attribute".equals(node.getLocalName())){ |
222 |
4
|
String key = ((Element)node).getAttribute("key"); |
223 |
4
|
String value = ((Element)node).getAttribute("value"); |
224 |
4
|
attributes.put(key, value); |
225 |
|
} |
226 |
|
} |
227 |
2
|
return attributes; |
228 |
|
} |
229 |
|
|
230 |
|
|
231 |
|
|
|
|
| 91.3% |
Uncovered Elements: 2 (23) |
Complexity: 7 |
Complexity Density: 0.41 |
|
232 |
26524
|
private Element getChildList(Element element, String localName) {... |
233 |
26524
|
try{ |
234 |
|
|
235 |
26524
|
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
236 |
26524
|
DocumentBuilder builder = dbf.newDocumentBuilder(); |
237 |
26524
|
Document doc = builder.newDocument(); |
238 |
|
|
239 |
26524
|
Element root = doc.createElement("listRoot"); |
240 |
|
|
241 |
261224
|
for(int i = 0;i<element.getChildNodes().getLength();i++){ |
242 |
234700
|
Node node = element.getChildNodes().item(i); |
243 |
234700
|
if(Node.ELEMENT_NODE == node.getNodeType() && localName.equals(node.getLocalName())){ |
244 |
|
|
245 |
|
|
246 |
7421
|
Node copied = doc.importNode(node, true); |
247 |
7421
|
root.appendChild(copied); |
248 |
|
} |
249 |
234700
|
if(Node.ELEMENT_NODE == node.getNodeType() && (localName+"Ref").equals(node.getLocalName())){ |
250 |
|
|
251 |
|
|
252 |
80020
|
Element ref = doc.createElement("ref"); |
253 |
80020
|
ref.setAttribute("bean", ((Element)node).getAttribute("bean")); |
254 |
80020
|
root.appendChild(ref); |
255 |
|
} |
256 |
|
} |
257 |
|
|
258 |
26524
|
return root; |
259 |
|
}catch(Exception e){ |
260 |
0
|
logger.error("Exception occured: ", e); |
261 |
|
} |
262 |
0
|
return null; |
263 |
|
} |
264 |
|
|
265 |
|
|
|
|
| 68% |
Uncovered Elements: 8 (25) |
Complexity: 13 |
Complexity Density: 1 |
|
266 |
91751
|
private String resolveFieldName(String parentName, String nodeName) {... |
267 |
91751
|
if("constraint".equals(parentName)&&"case".equals(nodeName)){ |
268 |
0
|
return "caseConstraint"; |
269 |
|
} |
270 |
91751
|
if("constraint".equals(parentName)&&"typeStateCase".equals(nodeName)){ |
271 |
4
|
return "typeStateCaseConstraint"; |
272 |
|
} |
273 |
91747
|
if("constraint".equals(parentName)&&"lookup".equals(nodeName)){ |
274 |
2375
|
return "lookupConstraint"; |
275 |
|
} |
276 |
89372
|
if("constraint".equals(parentName)&&"occurs".equals(nodeName)){ |
277 |
0
|
return "occursConstraint"; |
278 |
|
} |
279 |
89372
|
if("constraint".equals(parentName)&&"require".equals(nodeName)){ |
280 |
0
|
return "requireConstraint"; |
281 |
|
} |
282 |
89372
|
if("case".equals(parentName)&&"when".equals(nodeName)){ |
283 |
0
|
return "whenConstraint"; |
284 |
|
} |
285 |
|
|
286 |
89372
|
return nodeName; |
287 |
|
} |
288 |
|
|
289 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
290 |
65227
|
private Element getFirstChildElement(Node node) {... |
291 |
130980
|
for(int i = 0;i<node.getChildNodes().getLength();i++){ |
292 |
71214
|
Node childNode = node.getChildNodes().item(i); |
293 |
71214
|
if(Node.ELEMENT_NODE == childNode.getNodeType()){ |
294 |
5461
|
return (Element) childNode; |
295 |
|
} |
296 |
|
} |
297 |
59766
|
return null; |
298 |
|
} |
299 |
|
|
300 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
301 |
115110
|
private boolean isList(String localName) {... |
302 |
|
|
303 |
115110
|
return "field".equals(localName)|| |
304 |
|
"case".equals(localName)|| |
305 |
|
"when".equals(localName)|| |
306 |
|
"lookup".equals(localName)|| |
307 |
|
"lookupKey".equals(localName)|| |
308 |
|
"occurs".equals(localName)|| |
309 |
|
"constraint".equals(localName)|| |
310 |
|
"type".equals(localName)|| |
311 |
|
"state".equals(localName)|| |
312 |
|
"require".equals(localName); |
313 |
|
} |
314 |
|
|
315 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
316 |
107912
|
@Override... |
317 |
|
protected String getParentName(Element element) { |
318 |
107912
|
if(element.hasAttribute("parent")){ |
319 |
44905
|
return element.getAttribute("parent"); |
320 |
|
} |
321 |
63007
|
return super.getParentName(element); |
322 |
|
} |
323 |
|
|
324 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
325 |
12496
|
@Override... |
326 |
|
protected boolean shouldGenerateIdAsFallback() { |
327 |
12496
|
return true; |
328 |
|
} |
329 |
|
|
330 |
|
} |