1 17 18 package org.apache.commons.digester.plugins; 19 20 import java.util.Properties ; 21 22 import org.apache.commons.digester.Rule; 23 import org.apache.commons.digester.Digester; 24 25 import org.apache.commons.logging.Log; 26 27 37 38 public class PluginDeclarationRule extends Rule { 39 40 42 43 public PluginDeclarationRule() { 44 super(); 45 } 46 47 49 64 65 public void begin(String namespace, String name, 66 org.xml.sax.Attributes attributes) 67 throws java.lang.Exception { 68 69 int nAttrs = attributes.getLength(); 70 Properties props = new Properties (); 71 for(int i=0; i<nAttrs; ++i) { 72 String key = attributes.getLocalName(i); 73 if ((key == null) || (key.length() == 0)) { 74 key = attributes.getQName(i); 75 } 76 String value = attributes.getValue(i); 77 props.setProperty(key, value); 78 } 79 80 try { 81 declarePlugin(digester, props); 82 } catch(PluginInvalidInputException ex) { 83 throw new PluginInvalidInputException( 84 "Error on element [" + digester.getMatch() + 85 "]: " + ex.getMessage()); 86 } 87 } 88 89 public static void declarePlugin(Digester digester, Properties props) 90 throws PluginException { 91 92 Log log = digester.getLogger(); 93 boolean debug = log.isDebugEnabled(); 94 95 String id = props.getProperty("id"); 96 String pluginClassName = props.getProperty("class"); 97 98 if (id == null) { 99 throw new PluginInvalidInputException( 100 "mandatory attribute id not present on plugin declaration"); 101 } 102 103 if (pluginClassName == null) { 104 throw new PluginInvalidInputException( 105 "mandatory attribute class not present on plugin declaration"); 106 } 107 108 Declaration newDecl = new Declaration(pluginClassName); 109 newDecl.setId(id); 110 newDecl.setProperties(props); 111 112 PluginRules rc = (PluginRules) digester.getRules(); 113 PluginManager pm = rc.getPluginManager(); 114 115 newDecl.init(digester, pm); 116 pm.addDeclaration(newDecl); 117 118 } 123 } 124 125 | Popular Tags |