1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
33
34
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();
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();
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 }