Clover Coverage Report - Kuali Student 1.2-M1-SNAPSHOT (Aggregated)
Coverage timestamp: Fri Mar 4 2011 04:03:38 EST
../../../../../../../../img/srcFileCovDistChart9.png 28% of files have more coverage
127   330   71   14.11
94   248   0.56   9
9     7.89  
1    
 
  DictionaryBeanDefinitionParser       Line # 59 127 0% 71 24 89.6% 0.8956522
 
  (7)
 
1    /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10    * software distributed under the License is distributed on an "AS IS"
11    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12    * or implied. See the License for the specific language governing
13    * permissions and limitations under the License.
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 Daniel Epstein
56    *
57    */
58    @Deprecated
 
59    public class DictionaryBeanDefinitionParser extends AbstractSingleBeanDefinitionParser{
60   
61    final Logger logger = Logger.getLogger(DictionaryBeanDefinitionParser.class);
62   
63    //Resolves the tag name to an actual class
 
64  107912 toggle @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   
 
126  107912 toggle @Override
127    protected void doParse(Element element, ParserContext pc, BeanDefinitionBuilder builder) {
128   
129    //Copy Attributes
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    //Parse the children
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    //We are only interested in child elements that have not been visited
147  440787 if(Node.ELEMENT_NODE == node.getNodeType()){
148   
149    //Get the local name minus the "Ref"
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    //Check if the child element belongs in a list
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    //Check if this is an attribute map
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    //Check if the child element is a Ref
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    //Add in the nested object too
173  1351 builder.addPropertyReference("objectStructure", ((Element)node).getAttribute("bean"));
174    }else{
175  22006 builder.addPropertyReference(localName, ((Element)node).getAttribute("bean"));
176    }
177    }else{
178    //Get the child of the child to see if we need to parse the nested node, or just set the text value
179  65227 Element childElement = getFirstChildElement(node);
180  65227 if(childElement!=null ||"search".equals(node.getLocalName())){
181    //Parse the nested Node
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    //Set the text value
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    * Parses attribute map from
211    * &lt;attributes&gt;
212    * &lt;attribute key="attr1" value="attr2"/&gt;
213    * &lt;/attributes&gt;
214    * @param element
215    * @return map of attributes
216    */
 
217  2 toggle 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    //This builds up a list of the child nodes so that the spring parseListElement can be used
231    //it also translates <fooRef> elements into straight spring <ref> elements
 
232  26524 toggle private Element getChildList(Element element, String localName) {
233  26524 try{
234    //Create a new document to contain our list of elements
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    //Copy the node from the spring config to our list
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    //Create a new spring ref element and copy the bean attribute
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    //This is called to resolve tag names to field names based on the element and parent element local names
 
266  91751 toggle 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    //Gets the first child element
 
290  65227 toggle 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    //Returns true if the element should be part of a list
 
301  115110 toggle 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    //This makes use of the spring parent="" functionality
 
316  107912 toggle @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    //This means any bean without an id attribute gets one auto generated for it
 
325  12496 toggle @Override
326    protected boolean shouldGenerateIdAsFallback() {
327  12496 return true;
328    }
329   
330    }