View Javadoc

1   /**
2    * Copyright 2005-2014 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 java.util.HashMap;
19  import java.util.Iterator;
20  import java.util.LinkedHashMap;
21  import java.util.Map;
22  
23  import javax.xml.xpath.XPath;
24  import javax.xml.xpath.XPathConstants;
25  import javax.xml.xpath.XPathExpressionException;
26  import javax.xml.xpath.XPathFactory;
27  
28  import org.kuali.rice.kew.api.WorkflowRuntimeException;
29  import org.w3c.dom.Element;
30  import org.w3c.dom.Node;
31  
32  
33  /**
34   * Store global EDL config information parsed from config file.
35   * 
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   *
38   */
39  public class EDLGlobalConfig {
40  
41  	private Map preProcessors = new HashMap();
42  	private Map postProcessors = new HashMap();
43  	private Map stateComponents = new HashMap();
44      private Map configProcessors = new HashMap();
45  
46      public void addPreProcessor(String preProcessorName, Element element) {
47  		try {
48  			preProcessors.put(Class.forName(preProcessorName), element);	
49  		} catch (ClassNotFoundException ce) {
50  			throw new WorkflowRuntimeException("Class " + preProcessorName + " not found.", ce);
51  		}
52  	}
53  	
54  	public void addPostProcessor(String postProcessorName, Element configElement) {
55  		try {
56  			postProcessors.put(Class.forName(postProcessorName), configElement);
57  		} catch (ClassNotFoundException ce) {
58  			throw new WorkflowRuntimeException("Class " + postProcessorName + " not found.", ce);
59  		}
60  	}
61  	
62  	public void addStateComponent(String stateComponentName, Element configElement) {
63  		try {
64  			stateComponents.put(Class.forName(stateComponentName), configElement);
65  		} catch (ClassNotFoundException ce) {
66  			throw new WorkflowRuntimeException("Class " + stateComponentName + " not found.", ce);
67  		}
68  	}
69  	
70  	public void addConfigProcessor(String xpathExpression, String configProcessorName) {
71  		Class configProcessor;
72  		try {
73  			configProcessor = Class.forName(configProcessorName);
74  		} catch (ClassNotFoundException ce) {
75  			throw new WorkflowRuntimeException("Class " + configProcessorName + " not found.", ce);
76  		}
77  		if (configProcessors.containsKey(configProcessor)) {
78  			throw new WorkflowRuntimeException("Config processor " + configProcessorName + " attempted to register an xpath expression twice.  " +
79  					"The expression being used is " + configProcessors.get(configProcessor));
80  		} else {
81  			configProcessors.put(configProcessor, xpathExpression);	
82  		}
83  	}
84  	
85  	public Map getPreProcessors() {
86  		return preProcessors;
87  	}
88  	
89  	public Map getPostProcessors() {
90  		return postProcessors;
91  	}
92  	
93  	public Map getStateComponents() {
94  		return stateComponents;
95  	}
96  
97  	public Class getConfigProcessor(Node configElement, EDLContext edlContext) {
98  		if (configElement instanceof Element) {
99              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 }