View Javadoc

1   /*
2    * Copyright 2005-2008 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.edl.impl;
18  
19  import org.kuali.rice.core.api.util.RiceUtilities;
20  import org.w3c.dom.Document;
21  import org.w3c.dom.Element;
22  import org.w3c.dom.NodeList;
23  
24  import javax.xml.parsers.DocumentBuilderFactory;
25  import javax.xml.xpath.XPath;
26  import javax.xml.xpath.XPathConstants;
27  import javax.xml.xpath.XPathFactory;
28  import java.io.InputStream;
29  import java.util.LinkedHashMap;
30  import java.util.Map;
31  
32  /**
33   * Builds a EDLGlobalConfig.
34   *
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   */
37  public class EDLGlobalConfigFactory {
38  
39  	private static final String CONFIG_PROCESSOR_XPATH_XPRSN = "xpathExp";
40  	private static final String CONFIG_PROCESSOR_CLASS_NAME = "className";
41  
42  
43  	public static EDLGlobalConfig createEDLGlobalConfig(String edlConfigLocation) throws Exception {
44  		EDLGlobalConfig edlConfig = new EDLGlobalConfig();
45  		InputStream configStream = RiceUtilities.getResourceAsStream(edlConfigLocation);
46  		Document configXml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(configStream);
47  
48  		edlConfig.setPreProcessors(createProcessorMap("//preProcessors/preProcessor", configXml));
49  		edlConfig.setPostProcessors(createProcessorMap("//postProcessors/postProcessor", configXml));
50  		edlConfig.setStateComponents(createProcessorMap("//stateComponents/stateComponent", configXml));
51  		edlConfig.setConfigProcessors(createConfigProcessorMap("//configProcessors/configProcessor", configXml));
52  
53  		return edlConfig;
54  	}
55  
56  	private static Map createProcessorMap(String xpathExpression, Document doc) throws Exception {
57  		Map processors = new LinkedHashMap();//preserve parsing order
58  		XPath xpath = XPathFactory.newInstance().newXPath();
59  		NodeList globalProcessorDeclarations = (NodeList)xpath.evaluate(xpathExpression, doc, XPathConstants.NODESET);
60  		for (int i = 0; i < globalProcessorDeclarations.getLength(); i++) {
61  			Element globalProcessorDeclaraion = (Element)globalProcessorDeclarations.item(i);
62  			processors.put(globalProcessorDeclaraion, Class.forName(globalProcessorDeclaraion.getFirstChild().getNodeValue()));
63  		}
64  		return processors;
65  	}
66  
67  	private static Map createConfigProcessorMap(String xpathExpression, Document doc) throws Exception {
68  		Map configProcessors = new LinkedHashMap();//preserve parsing order
69  		XPath xpath = XPathFactory.newInstance().newXPath();
70  		NodeList globalConfigProcessorDeclarations = (NodeList)xpath.evaluate(xpathExpression, doc, XPathConstants.NODESET);
71  		for (int i = 0; i < globalConfigProcessorDeclarations.getLength(); i++) {
72  			Element globalProcessorDeclaraion = (Element)globalConfigProcessorDeclarations.item(i);
73  			String xpathEx = (String)xpath.evaluate(CONFIG_PROCESSOR_XPATH_XPRSN, globalProcessorDeclaraion, XPathConstants.STRING);
74  			String className = (String)xpath.evaluate(CONFIG_PROCESSOR_CLASS_NAME, globalProcessorDeclaraion, XPathConstants.STRING);
75  			configProcessors.put(xpathEx, Class.forName(className));
76  		}
77  		return configProcessors;
78  	}
79  }