001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.edl.impl;
017
018import java.util.HashMap;
019import java.util.Iterator;
020import java.util.LinkedHashMap;
021import java.util.Map;
022
023import javax.xml.xpath.XPath;
024import javax.xml.xpath.XPathConstants;
025import javax.xml.xpath.XPathExpressionException;
026import javax.xml.xpath.XPathFactory;
027
028import org.kuali.rice.kew.api.WorkflowRuntimeException;
029import org.w3c.dom.Element;
030import org.w3c.dom.Node;
031
032
033/**
034 * Store global EDL config information parsed from config file.
035 * 
036 * @author Kuali Rice Team (rice.collab@kuali.org)
037 *
038 */
039public class EDLGlobalConfig {
040
041        private Map preProcessors = new HashMap();
042        private Map postProcessors = new HashMap();
043        private Map stateComponents = new HashMap();
044    private Map configProcessors = new HashMap();
045
046    public void addPreProcessor(String preProcessorName, Element element) {
047                try {
048                        preProcessors.put(Class.forName(preProcessorName), element);    
049                } catch (ClassNotFoundException ce) {
050                        throw new WorkflowRuntimeException("Class " + preProcessorName + " not found.", ce);
051                }
052        }
053        
054        public void addPostProcessor(String postProcessorName, Element configElement) {
055                try {
056                        postProcessors.put(Class.forName(postProcessorName), configElement);
057                } catch (ClassNotFoundException ce) {
058                        throw new WorkflowRuntimeException("Class " + postProcessorName + " not found.", ce);
059                }
060        }
061        
062        public void addStateComponent(String stateComponentName, Element configElement) {
063                try {
064                        stateComponents.put(Class.forName(stateComponentName), configElement);
065                } catch (ClassNotFoundException ce) {
066                        throw new WorkflowRuntimeException("Class " + stateComponentName + " not found.", ce);
067                }
068        }
069        
070        public void addConfigProcessor(String xpathExpression, String configProcessorName) {
071                Class configProcessor;
072                try {
073                        configProcessor = Class.forName(configProcessorName);
074                } catch (ClassNotFoundException ce) {
075                        throw new WorkflowRuntimeException("Class " + configProcessorName + " not found.", ce);
076                }
077                if (configProcessors.containsKey(configProcessor)) {
078                        throw new WorkflowRuntimeException("Config processor " + configProcessorName + " attempted to register an xpath expression twice.  " +
079                                        "The expression being used is " + configProcessors.get(configProcessor));
080                } else {
081                        configProcessors.put(configProcessor, xpathExpression); 
082                }
083        }
084        
085        public Map getPreProcessors() {
086                return preProcessors;
087        }
088        
089        public Map getPostProcessors() {
090                return postProcessors;
091        }
092        
093        public Map getStateComponents() {
094                return stateComponents;
095        }
096
097        public Class getConfigProcessor(Node configElement, EDLContext edlContext) {
098                if (configElement instanceof Element) {
099            XPath xpath = null;
100            if (edlContext != null) {
101                xpath = edlContext.getXpath();
102            } else {
103                xpath = XPathFactory.newInstance().newXPath();
104            }
105                        String xpathExpression = "";
106                        try {
107                                for (Iterator iter = configProcessors.entrySet().iterator(); iter.hasNext();) {
108                                        Map.Entry configProcessor = (Map.Entry) iter.next();
109                                        xpathExpression = (String) configProcessor.getKey();
110                                        Boolean match = (Boolean) xpath.evaluate(xpathExpression, configElement, XPathConstants.BOOLEAN);
111                                        if (match.booleanValue()) {
112                                                return (Class) configProcessor.getValue();
113                                        }
114                                }
115                                return null;
116                        } catch (XPathExpressionException e) {
117                                throw new WorkflowRuntimeException("Unable to evaluate xpath expression " + xpathExpression, e);
118                        } catch (Exception ie) {
119                                throw new WorkflowRuntimeException(ie);
120                        }
121                }
122                return null;
123        }
124
125        public Map getConfigProcessors() {
126                return configProcessors;
127        }
128
129        public void setConfigProcessors(Map configProcessors) {
130                this.configProcessors = configProcessors;
131        }
132
133        public void setPostProcessors(Map postProcessors) {
134                this.postProcessors = postProcessors;
135        }
136
137        public void setPreProcessors(Map preProcessors) {
138                this.preProcessors = preProcessors;
139        }
140
141        public void setStateComponents(Map stateComponents) {
142                this.stateComponents = stateComponents;
143        }
144        
145        
146}