001    /*
002     * Copyright 2006-2011 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.kuali.rice.kew.plugin;
018    
019    import org.apache.commons.lang.StringUtils;
020    import org.jdom.Document;
021    import org.jdom.Element;
022    import org.jdom.JDOMException;
023    import org.jdom.input.SAXBuilder;
024    import org.kuali.rice.core.api.config.property.Config;
025    import org.kuali.rice.core.util.xml.XmlException;
026    
027    import java.io.File;
028    import java.io.IOException;
029    import java.net.URL;
030    import java.util.HashMap;
031    import java.util.Iterator;
032    import java.util.List;
033    import java.util.Map;
034    
035    
036    /**
037     * Parses a {@link PluginConfig} configuration from an XML file.
038     *
039     * @author Kuali Rice Team (rice.collab@kuali.org)
040     */
041    public class PluginConfigParser {
042    
043        private static final String PARAMETER_TAG = "parameter";
044        private static final String LISTENER_TAG = "listener";
045        private static final String LISTENER_CLASS_TAG = "listener-class";
046        private static final String RESOURCE_LOADER_TAG = "resourceLoader";
047    
048        private static final String NAME_ATTRIBUTE = "name";
049        private static final String VALUE_ATTRIBUTE = "value";
050        private static final String CLASS_ATTRIBUTE = "class";
051    
052        public PluginConfig parse(File configFile, Config parentConfig) throws IOException, XmlException {
053            return parse(configFile.toURI().toURL(), parentConfig);
054        }
055    
056            public PluginConfig parse(URL url, Config parentConfig) throws IOException, XmlException {
057                    SAXBuilder builder = new SAXBuilder(false);
058                    try {
059                // NOTE: need to be wary of whether streams are closed properly
060                // by builder
061                            Document doc = builder.build(url);
062                            Element root = doc.getRootElement();
063                            PluginConfig pluginConfig = new PluginConfig(url, parentConfig);
064                            parseResourceLoader(root, pluginConfig);
065                            parseListeners(root, pluginConfig);
066                            return pluginConfig;
067                    } catch (JDOMException e) {
068                        throw new PluginException("Error when parsing the plugin config file.", e);
069                    }
070            }
071    
072            public void parseResourceLoader(Element element, PluginConfig pluginConfig) throws XmlException {
073                    List loaderElements = element.getChildren(RESOURCE_LOADER_TAG);
074                    if (loaderElements.size() > 1) {
075                            throw new XmlException("Only one <resourceLoader> tag may be defined.");
076                    } else if (!loaderElements.isEmpty()) {
077                            Element loaderElement = (Element)loaderElements.get(0);
078                            String attributeClass = loaderElement.getAttributeValue(CLASS_ATTRIBUTE);
079                            if (StringUtils.isEmpty(attributeClass)) {
080                                    throw new XmlException("<resourceLoader> element must define a 'class' attribute.");
081                            }
082                            pluginConfig.setResourceLoaderClassname(attributeClass);
083                    }
084            }
085    
086            public void parseListeners(Element element, PluginConfig pluginConfig) throws XmlException {
087                    List listeners = element.getChildren(LISTENER_TAG);
088                    for (Iterator iterator = listeners.iterator(); iterator.hasNext();) {
089                        pluginConfig.addListener(parseListenerProperties((Element)iterator.next()));
090                    }
091            }
092    
093            private String parseListenerProperties(Element element) throws XmlException {
094                    String listenerClass = element.getChildText(LISTENER_CLASS_TAG);
095                    if (org.apache.commons.lang.StringUtils.isEmpty(listenerClass)) {
096                            throw new XmlException("Listener Class tag must have a class property defined");
097                    }
098                    return listenerClass;
099            }
100    
101            public Map parseParameters(Element element) throws XmlException {
102            Map parsedParms = new HashMap();
103                List parameters = element.getChildren(PARAMETER_TAG);
104                    for (Iterator iterator = parameters.iterator(); iterator.hasNext();) {
105                        String [] parm = parseParameter((Element)iterator.next());
106                        parsedParms.put(parm[0], parm[1]);
107                    }
108                    return parsedParms;
109            }
110    
111            private String [] parseParameter(Element element) throws XmlException {
112                    String name = element.getAttributeValue(NAME_ATTRIBUTE);
113                    String value = element.getAttributeValue(VALUE_ATTRIBUTE);
114                    if (org.apache.commons.lang.StringUtils.isEmpty(name)) {
115                            throw new XmlException("Parameter tag must have a name attribute defined");
116                    }
117                    if (org.apache.commons.lang.StringUtils.isEmpty(value)) {
118                            throw new XmlException("Parameter tag must have a value attribute defined");
119                    }
120                    return new String [] {name, value};
121            }
122    
123    
124    }