Coverage Report - org.kuali.rice.edl.impl.EDLGlobalConfigFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
EDLGlobalConfigFactory
0%
0/25
0%
0/4
1.667
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.edl.impl;
 17  
 
 18  
 import org.kuali.rice.core.api.util.RiceUtilities;
 19  
 import org.w3c.dom.Document;
 20  
 import org.w3c.dom.Element;
 21  
 import org.w3c.dom.NodeList;
 22  
 
 23  
 import javax.xml.parsers.DocumentBuilderFactory;
 24  
 import javax.xml.xpath.XPath;
 25  
 import javax.xml.xpath.XPathConstants;
 26  
 import javax.xml.xpath.XPathFactory;
 27  
 import java.io.InputStream;
 28  
 import java.util.LinkedHashMap;
 29  
 import java.util.Map;
 30  
 
 31  
 /**
 32  
  * Builds a EDLGlobalConfig.
 33  
  *
 34  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 35  
  */
 36  0
 public class EDLGlobalConfigFactory {
 37  
 
 38  
         private static final String CONFIG_PROCESSOR_XPATH_XPRSN = "xpathExp";
 39  
         private static final String CONFIG_PROCESSOR_CLASS_NAME = "className";
 40  
 
 41  
 
 42  
         public static EDLGlobalConfig createEDLGlobalConfig(String edlConfigLocation) throws Exception {
 43  0
                 EDLGlobalConfig edlConfig = new EDLGlobalConfig();
 44  0
                 InputStream configStream = RiceUtilities.getResourceAsStream(edlConfigLocation);
 45  0
                 Document configXml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(configStream);
 46  
 
 47  0
                 edlConfig.setPreProcessors(createProcessorMap("//preProcessors/preProcessor", configXml));
 48  0
                 edlConfig.setPostProcessors(createProcessorMap("//postProcessors/postProcessor", configXml));
 49  0
                 edlConfig.setStateComponents(createProcessorMap("//stateComponents/stateComponent", configXml));
 50  0
                 edlConfig.setConfigProcessors(createConfigProcessorMap("//configProcessors/configProcessor", configXml));
 51  
 
 52  0
                 return edlConfig;
 53  
         }
 54  
 
 55  
         private static Map createProcessorMap(String xpathExpression, Document doc) throws Exception {
 56  0
                 Map processors = new LinkedHashMap();//preserve parsing order
 57  0
                 XPath xpath = XPathFactory.newInstance().newXPath();
 58  0
                 NodeList globalProcessorDeclarations = (NodeList)xpath.evaluate(xpathExpression, doc, XPathConstants.NODESET);
 59  0
                 for (int i = 0; i < globalProcessorDeclarations.getLength(); i++) {
 60  0
                         Element globalProcessorDeclaraion = (Element)globalProcessorDeclarations.item(i);
 61  0
                         processors.put(globalProcessorDeclaraion, Class.forName(globalProcessorDeclaraion.getFirstChild().getNodeValue()));
 62  
                 }
 63  0
                 return processors;
 64  
         }
 65  
 
 66  
         private static Map createConfigProcessorMap(String xpathExpression, Document doc) throws Exception {
 67  0
                 Map configProcessors = new LinkedHashMap();//preserve parsing order
 68  0
                 XPath xpath = XPathFactory.newInstance().newXPath();
 69  0
                 NodeList globalConfigProcessorDeclarations = (NodeList)xpath.evaluate(xpathExpression, doc, XPathConstants.NODESET);
 70  0
                 for (int i = 0; i < globalConfigProcessorDeclarations.getLength(); i++) {
 71  0
                         Element globalProcessorDeclaraion = (Element)globalConfigProcessorDeclarations.item(i);
 72  0
                         String xpathEx = (String)xpath.evaluate(CONFIG_PROCESSOR_XPATH_XPRSN, globalProcessorDeclaraion, XPathConstants.STRING);
 73  0
                         String className = (String)xpath.evaluate(CONFIG_PROCESSOR_CLASS_NAME, globalProcessorDeclaraion, XPathConstants.STRING);
 74  0
                         configProcessors.put(xpathEx, Class.forName(className));
 75  
                 }
 76  0
                 return configProcessors;
 77  
         }
 78  
 }