View Javadoc

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