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