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 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  0
 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  0
             return parse(configFile.toURL(), parentConfig);
 55  
     }
 56  
 
 57  
         public PluginConfig parse(URL url, Config parentConfig) throws IOException, InvalidXmlException {
 58  0
                 SAXBuilder builder = new SAXBuilder(false);
 59  
                 try {
 60  
             // NOTE: need to be wary of whether streams are closed properly
 61  
             // by builder
 62  0
                         Document doc = builder.build(url);
 63  0
                         Element root = doc.getRootElement();
 64  0
                         PluginConfig pluginConfig = new PluginConfig(url, parentConfig);
 65  0
                         parseResourceLoader(root, pluginConfig);
 66  0
                         parseListeners(root, pluginConfig);
 67  0
                         return pluginConfig;
 68  0
                 } catch (JDOMException e) {
 69  0
                     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  0
                 List loaderElements = element.getChildren(RESOURCE_LOADER_TAG);
 75  0
                 if (loaderElements.size() > 1) {
 76  0
                         throw new InvalidXmlException("Only one <resourceLoader> tag may be defined.");
 77  0
                 } else if (!loaderElements.isEmpty()) {
 78  0
                         Element loaderElement = (Element)loaderElements.get(0);
 79  0
                         String attributeClass = loaderElement.getAttributeValue(CLASS_ATTRIBUTE);
 80  0
                         if (StringUtils.isEmpty(attributeClass)) {
 81  0
                                 throw new InvalidXmlException("<resourceLoader> element must define a 'class' attribute.");
 82  
                         }
 83  0
                         pluginConfig.setResourceLoaderClassname(attributeClass);
 84  
                 }
 85  0
         }
 86  
 
 87  
         public void parseListeners(Element element, PluginConfig pluginConfig) throws InvalidXmlException {
 88  0
                 List listeners = element.getChildren(LISTENER_TAG);
 89  0
                 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) {
 90  0
                     pluginConfig.addListener(parseListenerProperties((Element)iterator.next()));
 91  
                 }
 92  0
         }
 93  
 
 94  
         private String parseListenerProperties(Element element) throws InvalidXmlException {
 95  0
                 String listenerClass = element.getChildText(LISTENER_CLASS_TAG);
 96  0
                 if (Utilities.isEmpty(listenerClass)) {
 97  0
                         throw new InvalidXmlException("Listener Class tag must have a class property defined");
 98  
                 }
 99  0
                 return listenerClass;
 100  
         }
 101  
 
 102  
         public Map parseParameters(Element element) throws InvalidXmlException {
 103  0
         Map parsedParms = new HashMap();
 104  0
             List parameters = element.getChildren(PARAMETER_TAG);
 105  0
                 for (Iterator iterator = parameters.iterator(); iterator.hasNext();) {
 106  0
                     String [] parm = parseParameter((Element)iterator.next());
 107  0
                     parsedParms.put(parm[0], parm[1]);
 108  0
                 }
 109  0
                 return parsedParms;
 110  
         }
 111  
 
 112  
         private String [] parseParameter(Element element) throws InvalidXmlException {
 113  0
                 String name = element.getAttributeValue(NAME_ATTRIBUTE);
 114  0
                 String value = element.getAttributeValue(VALUE_ATTRIBUTE);
 115  0
                 if (Utilities.isEmpty(name)) {
 116  0
                         throw new InvalidXmlException("Parameter tag must have a name attribute defined");
 117  
                 }
 118  0
                 if (Utilities.isEmpty(value)) {
 119  0
                         throw new InvalidXmlException("Parameter tag must have a value attribute defined");
 120  
                 }
 121  0
                 return new String [] {name, value};
 122  
         }
 123  
 
 124  
 
 125  
 }