|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  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.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 |     | 
     | 
  |  38 |     | 
     | 
  |  39 |     | 
     | 
  |  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 |     | 
                 | 
  |  60 |     | 
                 | 
  |  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 |     | 
   }  |