01: /*
02: * Copyright 2005-2006 The Kuali Foundation.
03: *
04: *
05: * Licensed under the Educational Community License, Version 1.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.opensource.org/licenses/ecl1.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 edu.iu.uis.eden.edl;
18:
19: import java.io.InputStream;
20: import java.util.LinkedHashMap;
21: import java.util.Map;
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:
28: import org.kuali.rice.config.HierarchicalConfigParser;
29: import org.w3c.dom.Document;
30: import org.w3c.dom.Element;
31: import org.w3c.dom.NodeList;
32:
33: /**
34: * Builds a EDLGlobalConfig.
35: *
36: * @author rkirkend
37: */
38: public class EDLGlobalConfigFactory {
39:
40: private static final String CONFIG_PROCESSOR_XPATH_XPRSN = "xpathExp";
41: private static final String CONFIG_PROCESSOR_CLASS_NAME = "className";
42:
43: public static EDLGlobalConfig createEDLGlobalConfig(
44: String edlConfigLocation) throws Exception {
45: EDLGlobalConfig edlConfig = new EDLGlobalConfig();
46: InputStream configStream = HierarchicalConfigParser
47: .getConfigAsStream(edlConfigLocation);
48: Document configXml = DocumentBuilderFactory.newInstance()
49: .newDocumentBuilder().parse(configStream);
50:
51: edlConfig.setPreProcessors(createProcessorMap(
52: "//preProcessors/preProcessor", configXml));
53: edlConfig.setPostProcessors(createProcessorMap(
54: "//postProcessors/postProcessor", configXml));
55: edlConfig.setStateComponents(createProcessorMap(
56: "//stateComponents/stateComponent", configXml));
57: edlConfig.setConfigProcessors(createConfigProcessorMap(
58: "//configProcessors/configProcessor", configXml));
59:
60: return edlConfig;
61: }
62:
63: private static Map createProcessorMap(String xpathExpression,
64: Document doc) throws Exception {
65: Map processors = new LinkedHashMap();//preserve parsing order
66: XPath xpath = XPathFactory.newInstance().newXPath();
67: NodeList globalProcessorDeclarations = (NodeList) xpath
68: .evaluate(xpathExpression, doc, XPathConstants.NODESET);
69: for (int i = 0; i < globalProcessorDeclarations.getLength(); i++) {
70: Element globalProcessorDeclaraion = (Element) globalProcessorDeclarations
71: .item(i);
72: processors.put(globalProcessorDeclaraion, Class
73: .forName(globalProcessorDeclaraion.getFirstChild()
74: .getNodeValue()));
75: }
76: return processors;
77: }
78:
79: private static Map createConfigProcessorMap(String xpathExpression,
80: Document doc) throws Exception {
81: Map configProcessors = new LinkedHashMap();//preserve parsing order
82: XPath xpath = XPathFactory.newInstance().newXPath();
83: NodeList globalConfigProcessorDeclarations = (NodeList) xpath
84: .evaluate(xpathExpression, doc, XPathConstants.NODESET);
85: for (int i = 0; i < globalConfigProcessorDeclarations
86: .getLength(); i++) {
87: Element globalProcessorDeclaraion = (Element) globalConfigProcessorDeclarations
88: .item(i);
89: String xpathEx = (String) xpath.evaluate(
90: CONFIG_PROCESSOR_XPATH_XPRSN,
91: globalProcessorDeclaraion, XPathConstants.STRING);
92: String className = (String) xpath.evaluate(
93: CONFIG_PROCESSOR_CLASS_NAME,
94: globalProcessorDeclaraion, XPathConstants.STRING);
95: configProcessors.put(xpathEx, Class.forName(className));
96: }
97: return configProcessors;
98: }
99: }
|