View Javadoc

1   /**
2    * Copyright 2005-2013 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.kew.plugin;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.jdom.Document;
20  import org.jdom.Element;
21  import org.jdom.JDOMException;
22  import org.jdom.input.SAXBuilder;
23  import org.kuali.rice.core.api.config.property.Config;
24  import org.kuali.rice.core.api.util.xml.XmlException;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.net.URL;
29  import java.util.HashMap;
30  import java.util.Iterator;
31  import java.util.List;
32  import java.util.Map;
33  
34  
35  /**
36   * Parses a {@link PluginConfig} configuration from an XML file.
37   *
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   */
40  public class PluginConfigParser {
41  
42      private static final String PARAMETER_TAG = "parameter";
43      private static final String LISTENER_TAG = "listener";
44      private static final String LISTENER_CLASS_TAG = "listener-class";
45      private static final String RESOURCE_LOADER_TAG = "resourceLoader";
46  
47      private static final String NAME_ATTRIBUTE = "name";
48      private static final String VALUE_ATTRIBUTE = "value";
49      private static final String CLASS_ATTRIBUTE = "class";
50  
51      public PluginConfig parse(File configFile, Config parentConfig) throws IOException, XmlException {
52      	return parse(configFile.toURI().toURL(), parentConfig);
53      }
54  
55  	public PluginConfig parse(URL url, Config parentConfig) throws IOException, XmlException {
56  		SAXBuilder builder = new SAXBuilder(false);
57  		try {
58              // NOTE: need to be wary of whether streams are closed properly
59              // by builder
60  			Document doc = builder.build(url);
61  			Element root = doc.getRootElement();
62  			PluginConfig pluginConfig = new PluginConfig(url, parentConfig);
63  			parseResourceLoader(root, pluginConfig);
64  			parseListeners(root, pluginConfig);
65  			return pluginConfig;
66  		} catch (JDOMException e) {
67  		    throw new PluginException("Error when parsing the plugin config file.", e);
68  		}
69  	}
70  
71  	public void parseResourceLoader(Element element, PluginConfig pluginConfig) throws XmlException {
72  		List loaderElements = element.getChildren(RESOURCE_LOADER_TAG);
73  		if (loaderElements.size() > 1) {
74  			throw new XmlException("Only one <resourceLoader> tag may be defined.");
75  		} else if (!loaderElements.isEmpty()) {
76  			Element loaderElement = (Element)loaderElements.get(0);
77  			String attributeClass = loaderElement.getAttributeValue(CLASS_ATTRIBUTE);
78  			if (StringUtils.isEmpty(attributeClass)) {
79  				throw new XmlException("<resourceLoader> element must define a 'class' attribute.");
80  			}
81  			pluginConfig.setResourceLoaderClassname(attributeClass);
82  		}
83  	}
84  
85  	public void parseListeners(Element element, PluginConfig pluginConfig) throws XmlException {
86  		List listeners = element.getChildren(LISTENER_TAG);
87  		for (Iterator iterator = listeners.iterator(); iterator.hasNext();) {
88  		    pluginConfig.addListener(parseListenerProperties((Element)iterator.next()));
89  		}
90  	}
91  
92  	private String parseListenerProperties(Element element) throws XmlException {
93  		String listenerClass = element.getChildText(LISTENER_CLASS_TAG);
94  		if (org.apache.commons.lang.StringUtils.isEmpty(listenerClass)) {
95  			throw new XmlException("Listener Class tag must have a class property defined");
96  		}
97  		return listenerClass;
98  	}
99  
100 	public Map parseParameters(Element element) throws XmlException {
101         Map parsedParms = new HashMap();
102 	    List parameters = element.getChildren(PARAMETER_TAG);
103 		for (Iterator iterator = parameters.iterator(); iterator.hasNext();) {
104 		    String [] parm = parseParameter((Element)iterator.next());
105 		    parsedParms.put(parm[0], parm[1]);
106 		}
107 		return parsedParms;
108 	}
109 
110 	private String [] parseParameter(Element element) throws XmlException {
111 		String name = element.getAttributeValue(NAME_ATTRIBUTE);
112 		String value = element.getAttributeValue(VALUE_ATTRIBUTE);
113 		if (org.apache.commons.lang.StringUtils.isEmpty(name)) {
114 			throw new XmlException("Parameter tag must have a name attribute defined");
115 		}
116 		if (org.apache.commons.lang.StringUtils.isEmpty(value)) {
117 			throw new XmlException("Parameter tag must have a value attribute defined");
118 		}
119 		return new String [] {name, value};
120 	}
121 
122 
123 }