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