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 org.kuali.rice.core.api.util.RiceUtilities;
19  import org.w3c.dom.Document;
20  import org.w3c.dom.Element;
21  import org.w3c.dom.NodeList;
22  
23  import javax.xml.parsers.DocumentBuilderFactory;
24  import javax.xml.xpath.XPath;
25  import javax.xml.xpath.XPathConstants;
26  import javax.xml.xpath.XPathFactory;
27  import java.io.InputStream;
28  import java.util.LinkedHashMap;
29  import java.util.Map;
30  
31  /**
32   * Builds a EDLGlobalConfig.
33   *
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   */
36  public class EDLGlobalConfigFactory {
37  
38  	private static final String CONFIG_PROCESSOR_XPATH_XPRSN = "xpathExp";
39  	private static final String CONFIG_PROCESSOR_CLASS_NAME = "className";
40  
41  
42  	public static EDLGlobalConfig createEDLGlobalConfig(String edlConfigLocation) throws Exception {
43  		EDLGlobalConfig edlConfig = new EDLGlobalConfig();
44  		InputStream configStream = RiceUtilities.getResourceAsStream(edlConfigLocation);
45  		Document configXml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(configStream);
46  
47  		edlConfig.setPreProcessors(createProcessorMap("//preProcessors/preProcessor", configXml));
48  		edlConfig.setPostProcessors(createProcessorMap("//postProcessors/postProcessor", configXml));
49  		edlConfig.setStateComponents(createProcessorMap("//stateComponents/stateComponent", configXml));
50  		edlConfig.setConfigProcessors(createConfigProcessorMap("//configProcessors/configProcessor", configXml));
51  
52  		return edlConfig;
53  	}
54  
55  	private static Map createProcessorMap(String xpathExpression, Document doc) throws Exception {
56  		Map processors = new LinkedHashMap();//preserve parsing order
57  		XPath xpath = XPathFactory.newInstance().newXPath();
58  		NodeList globalProcessorDeclarations = (NodeList)xpath.evaluate(xpathExpression, doc, XPathConstants.NODESET);
59  		for (int i = 0; i < globalProcessorDeclarations.getLength(); i++) {
60  			Element globalProcessorDeclaraion = (Element)globalProcessorDeclarations.item(i);
61  			processors.put(globalProcessorDeclaraion, Class.forName(globalProcessorDeclaraion.getFirstChild().getNodeValue()));
62  		}
63  		return processors;
64  	}
65  
66  	private static Map createConfigProcessorMap(String xpathExpression, Document doc) throws Exception {
67  		Map configProcessors = new LinkedHashMap();//preserve parsing order
68  		XPath xpath = XPathFactory.newInstance().newXPath();
69  		NodeList globalConfigProcessorDeclarations = (NodeList)xpath.evaluate(xpathExpression, doc, XPathConstants.NODESET);
70  		for (int i = 0; i < globalConfigProcessorDeclarations.getLength(); i++) {
71  			Element globalProcessorDeclaraion = (Element)globalConfigProcessorDeclarations.item(i);
72  			String xpathEx = (String)xpath.evaluate(CONFIG_PROCESSOR_XPATH_XPRSN, globalProcessorDeclaraion, XPathConstants.STRING);
73  			String className = (String)xpath.evaluate(CONFIG_PROCESSOR_CLASS_NAME, globalProcessorDeclaraion, XPathConstants.STRING);
74  			configProcessors.put(xpathEx, Class.forName(className));
75  		}
76  		return configProcessors;
77  	}
78  }