001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.edl;
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.LinkedHashMap;
022: import java.util.Map;
023:
024: import javax.xml.xpath.XPath;
025: import javax.xml.xpath.XPathConstants;
026: import javax.xml.xpath.XPathExpressionException;
027: import javax.xml.xpath.XPathFactory;
028:
029: import org.w3c.dom.Element;
030: import org.w3c.dom.Node;
031:
032: import edu.iu.uis.eden.exception.WorkflowRuntimeException;
033:
034: /**
035: * Store global EDL config information parsed from config file.
036: *
037: * @author rkirkend
038: *
039: */
040: public class EDLGlobalConfig {
041:
042: private Map preProcessors = new HashMap();
043: private Map postProcessors = new HashMap();
044: private Map stateComponents = new HashMap();
045: private Map configProcessors = new LinkedHashMap();
046:
047: public void addPreProcessor(String preProcessorName, Element element) {
048: try {
049: preProcessors.put(Class.forName(preProcessorName), element);
050: } catch (ClassNotFoundException ce) {
051: throw new WorkflowRuntimeException("Class "
052: + preProcessorName + " not found.", ce);
053: }
054: }
055:
056: public void addPostProcessor(String postProcessorName,
057: Element configElement) {
058: try {
059: postProcessors.put(Class.forName(postProcessorName),
060: configElement);
061: } catch (ClassNotFoundException ce) {
062: throw new WorkflowRuntimeException("Class "
063: + postProcessorName + " not found.", ce);
064: }
065: }
066:
067: public void addStateComponent(String stateComponentName,
068: Element configElement) {
069: try {
070: stateComponents.put(Class.forName(stateComponentName),
071: configElement);
072: } catch (ClassNotFoundException ce) {
073: throw new WorkflowRuntimeException("Class "
074: + stateComponentName + " not found.", ce);
075: }
076: }
077:
078: public void addConfigProcessor(String xpathExpression,
079: String configProcessorName) {
080: Class configProcessor;
081: try {
082: configProcessor = Class.forName(configProcessorName);
083: } catch (ClassNotFoundException ce) {
084: throw new WorkflowRuntimeException("Class "
085: + configProcessorName + " not found.", ce);
086: }
087: if (configProcessors.containsKey(configProcessor)) {
088: throw new WorkflowRuntimeException(
089: "Config processor "
090: + configProcessorName
091: + " attempted to register an xpath expression twice. "
092: + "The expression being used is "
093: + configProcessors.get(configProcessor));
094: } else {
095: configProcessors.put(configProcessor, xpathExpression);
096: }
097: }
098:
099: public Map getPreProcessors() {
100: return preProcessors;
101: }
102:
103: public Map getPostProcessors() {
104: return postProcessors;
105: }
106:
107: public Map getStateComponents() {
108: return stateComponents;
109: }
110:
111: public Class getConfigProcessor(Node configElement) {
112: if (configElement instanceof Element) {
113:
114: XPath xpath = XPathFactory.newInstance().newXPath();
115: String xpathExpression = "";
116: try {
117: for (Iterator iter = configProcessors.entrySet()
118: .iterator(); iter.hasNext();) {
119: Map.Entry configProcessor = (Map.Entry) iter.next();
120: xpathExpression = (String) configProcessor.getKey();
121: Boolean match = (Boolean) xpath.evaluate(
122: xpathExpression, configElement,
123: XPathConstants.BOOLEAN);
124: if (match.booleanValue()) {
125: return (Class) configProcessor.getValue();
126: }
127: }
128: return null;
129: } catch (XPathExpressionException e) {
130: throw new WorkflowRuntimeException(
131: "Unable to evaluate xpath expression "
132: + xpathExpression, e);
133: } catch (Exception ie) {
134: throw new WorkflowRuntimeException(ie);
135: }
136: }
137: return null;
138: }
139:
140: public Map getConfigProcessors() {
141: return configProcessors;
142: }
143:
144: public void setConfigProcessors(Map configProcessors) {
145: this .configProcessors = configProcessors;
146: }
147:
148: public void setPostProcessors(Map postProcessors) {
149: this .postProcessors = postProcessors;
150: }
151:
152: public void setPreProcessors(Map preProcessors) {
153: this .preProcessors = preProcessors;
154: }
155:
156: public void setStateComponents(Map stateComponents) {
157: this.stateComponents = stateComponents;
158: }
159:
160: }
|