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