001    /**
002     * Copyright 2005-2012 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 java.util.HashMap;
019    import java.util.Iterator;
020    import java.util.LinkedHashMap;
021    import java.util.Map;
022    
023    import javax.xml.xpath.XPath;
024    import javax.xml.xpath.XPathConstants;
025    import javax.xml.xpath.XPathExpressionException;
026    import javax.xml.xpath.XPathFactory;
027    
028    import org.kuali.rice.kew.api.WorkflowRuntimeException;
029    import org.w3c.dom.Element;
030    import 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     */
039    public 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 LinkedHashMap();
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) {
098                    if (configElement instanceof Element) {
099    
100                            XPath xpath = XPathFactory.newInstance().newXPath();
101                            String xpathExpression = "";
102                            try {
103                                    for (Iterator iter = configProcessors.entrySet().iterator(); iter.hasNext();) {
104                                            Map.Entry configProcessor = (Map.Entry) iter.next();
105                                            xpathExpression = (String) configProcessor.getKey();
106                                            Boolean match = (Boolean) xpath.evaluate(xpathExpression, configElement, XPathConstants.BOOLEAN);
107                                            if (match.booleanValue()) {
108                                                    return (Class) configProcessor.getValue();
109                                            }
110                                    }
111                                    return null;
112                            } catch (XPathExpressionException e) {
113                                    throw new WorkflowRuntimeException("Unable to evaluate xpath expression " + xpathExpression, e);
114                            } catch (Exception ie) {
115                                    throw new WorkflowRuntimeException(ie);
116                            }
117                    }
118                    return null;
119            }
120    
121            public Map getConfigProcessors() {
122                    return configProcessors;
123            }
124    
125            public void setConfigProcessors(Map configProcessors) {
126                    this.configProcessors = configProcessors;
127            }
128    
129            public void setPostProcessors(Map postProcessors) {
130                    this.postProcessors = postProcessors;
131            }
132    
133            public void setPreProcessors(Map preProcessors) {
134                    this.preProcessors = preProcessors;
135            }
136    
137            public void setStateComponents(Map stateComponents) {
138                    this.stateComponents = stateComponents;
139            }
140            
141            
142    }