001    /**
002     * Copyright 2005-2014 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.edl.impl;
017    
018    import org.kuali.rice.core.api.util.RiceUtilities;
019    import org.w3c.dom.Document;
020    import org.w3c.dom.Element;
021    import org.w3c.dom.NodeList;
022    
023    import javax.xml.parsers.DocumentBuilderFactory;
024    import javax.xml.xpath.XPath;
025    import javax.xml.xpath.XPathConstants;
026    import javax.xml.xpath.XPathFactory;
027    import java.io.InputStream;
028    import java.util.LinkedHashMap;
029    import java.util.Map;
030    
031    /**
032     * Builds a EDLGlobalConfig.
033     *
034     * @author Kuali Rice Team (rice.collab@kuali.org)
035     */
036    public class EDLGlobalConfigFactory {
037    
038            private static final String CONFIG_PROCESSOR_XPATH_XPRSN = "xpathExp";
039            private static final String CONFIG_PROCESSOR_CLASS_NAME = "className";
040    
041    
042            public static EDLGlobalConfig createEDLGlobalConfig(String edlConfigLocation) throws Exception {
043                    EDLGlobalConfig edlConfig = new EDLGlobalConfig();
044                    InputStream configStream = RiceUtilities.getResourceAsStream(edlConfigLocation);
045                    Document configXml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(configStream);
046    
047                    edlConfig.setPreProcessors(createProcessorMap("//preProcessors/preProcessor", configXml));
048                    edlConfig.setPostProcessors(createProcessorMap("//postProcessors/postProcessor", configXml));
049                    edlConfig.setStateComponents(createProcessorMap("//stateComponents/stateComponent", configXml));
050                    edlConfig.setConfigProcessors(createConfigProcessorMap("//configProcessors/configProcessor", configXml));
051    
052                    return edlConfig;
053            }
054    
055            private static Map createProcessorMap(String xpathExpression, Document doc) throws Exception {
056                    Map processors = new LinkedHashMap();//preserve parsing order
057                    XPath xpath = XPathFactory.newInstance().newXPath();
058                    NodeList globalProcessorDeclarations = (NodeList)xpath.evaluate(xpathExpression, doc, XPathConstants.NODESET);
059                    for (int i = 0; i < globalProcessorDeclarations.getLength(); i++) {
060                            Element globalProcessorDeclaraion = (Element)globalProcessorDeclarations.item(i);
061                            processors.put(globalProcessorDeclaraion, Class.forName(globalProcessorDeclaraion.getFirstChild().getNodeValue()));
062                    }
063                    return processors;
064            }
065    
066            private static Map createConfigProcessorMap(String xpathExpression, Document doc) throws Exception {
067                    Map configProcessors = new LinkedHashMap();//preserve parsing order
068                    XPath xpath = XPathFactory.newInstance().newXPath();
069                    NodeList globalConfigProcessorDeclarations = (NodeList)xpath.evaluate(xpathExpression, doc, XPathConstants.NODESET);
070                    for (int i = 0; i < globalConfigProcessorDeclarations.getLength(); i++) {
071                            Element globalProcessorDeclaraion = (Element)globalConfigProcessorDeclarations.item(i);
072                            String xpathEx = (String)xpath.evaluate(CONFIG_PROCESSOR_XPATH_XPRSN, globalProcessorDeclaraion, XPathConstants.STRING);
073                            String className = (String)xpath.evaluate(CONFIG_PROCESSOR_CLASS_NAME, globalProcessorDeclaraion, XPathConstants.STRING);
074                            configProcessors.put(xpathEx, Class.forName(className));
075                    }
076                    return configProcessors;
077            }
078    }