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