1 16 17 18 package org.apache.velocity.tools.view; 19 20 21 import java.io.InputStream ; 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import org.apache.commons.digester.Digester; 29 import org.apache.commons.digester.RuleSet; 30 import org.apache.velocity.app.Velocity; 31 import org.apache.velocity.tools.view.ToolboxRuleSet; 32 import org.apache.velocity.tools.view.context.ToolboxContext; 33 34 35 78 public class XMLToolboxManager implements ToolboxManager 79 { 80 81 private List toolinfo; 82 private Map toolbox; 83 84 private static RuleSet ruleSet = new ToolboxRuleSet(); 85 86 87 90 public XMLToolboxManager() 91 { 92 toolinfo = new ArrayList (); 93 toolbox = new HashMap (); 94 } 95 96 97 99 public void addTool(ToolInfo info) 100 { 101 toolinfo.add(info); 102 Velocity.info("Added "+info.getKey()+" ("+info.getClassname()+") to the toolbox."); 103 } 104 105 106 public ToolboxContext getToolboxContext(Object initData) 107 { 108 Iterator i = toolinfo.iterator(); 109 while(i.hasNext()) 110 { 111 ToolInfo info = (ToolInfo)i.next(); 112 toolbox.put(info.getKey(), info.getInstance(initData)); 113 } 114 115 return new ToolboxContext(toolbox); 116 } 117 118 119 121 139 public void load(InputStream input) throws Exception 140 { 141 Velocity.debug("XMLToolboxManager: Loading toolbox..."); 142 143 Digester digester = new Digester(); 144 digester.setValidating(false); 145 digester.setUseContextClassLoader(true); 146 digester.push(this); 147 digester.addRuleSet(getRuleSet()); 148 digester.parse(input); 149 150 Velocity.debug("XMLToolboxManager: Toolbox loaded."); 151 } 152 153 154 159 protected RuleSet getRuleSet() 160 { 161 return ruleSet; 162 } 163 164 } 165 | Popular Tags |