1 10 package org.mmbase.module; 11 12 import java.util.*; 13 import javax.servlet.http.*; 14 import org.mmbase.module.core.*; 15 import org.mmbase.bridge.Cloud; 16 import org.mmbase.util.*; 17 import org.mmbase.util.functions.*; 18 import org.mmbase.util.logging.*; 19 20 21 28 public class ProcessorModule extends Module implements ProcessorInterface { 29 private static final Logger log = Logging.getLoggerInstance(ProcessorModule.class); 30 33 public MMObjectBuilder getListBuilder(String command, Map params) { 34 return new VirtualBuilder(null); 35 } 36 37 protected static final Parameter[] PARAMS_PAGEINFO = new Parameter[] {Parameter.REQUEST, Parameter.RESPONSE, Parameter.CLOUD}; 38 protected static final Parameter.Wrapper PARAM_PAGEINFO = new Parameter.Wrapper(PARAMS_PAGEINFO); 39 40 41 45 private static PageInfo getPageInfo(Parameters arguments) { 46 PageInfo pageInfo = null; 47 if (arguments.indexOfParameter(Parameter.REQUEST)> -1) { 48 HttpServletRequest req = (HttpServletRequest) arguments.get(Parameter.REQUEST); 49 HttpServletResponse res = (HttpServletResponse) arguments.get(Parameter.RESPONSE); 50 Cloud cloud = (Cloud) arguments.get(Parameter.CLOUD); 51 pageInfo = new PageInfo(req, res, cloud); 52 } 53 return pageInfo; 54 } 55 59 private static String getCommand(String functionName, Parameters arguments) { 60 StringBuffer buf = new StringBuffer (functionName); 61 Iterator i = arguments.iterator(); 62 while (i.hasNext()) { 63 Object argument = i.next(); 64 if (argument instanceof String && ! "".equals(argument)) { 65 buf.append('-').append(argument); 66 } 67 } 68 return buf.toString(); 69 } 70 74 protected class GetNodeListFunction extends AbstractFunction { 75 public GetNodeListFunction(String name, Parameter[] params) { 76 super(name, params, ReturnType.NODELIST); 77 } 78 public Object getFunctionValue(Parameters arguments) { 79 return getNodeList(getPageInfo(arguments), getCommand(getName(), arguments), arguments.toMap()); 80 } 81 } 82 86 protected class ReplaceFunction extends AbstractFunction { 87 public ReplaceFunction(String name, Parameter[] params) { 88 super(name, params, ReturnType.STRING); 89 } 90 public Object getFunctionValue(Parameters arguments) { 91 return replace(getPageInfo(arguments), getCommand(getName(), arguments)); 92 } 93 } 94 102 protected class ProcessFunction extends AbstractFunction { 103 public ProcessFunction(String name, Parameter[] params) { 104 super(name, params, ReturnType.MAP); 105 } 106 107 public Object getFunctionValue(Parameters arguments) { 108 Hashtable cmds = new Hashtable(); 109 Hashtable vars = new Hashtable(); 110 Parameter[] def = arguments.getDefinition(); 111 for (int i = 0; i < def.length; i++) { 112 Parameter param = def[i]; 113 Object value = arguments.get(param); 114 if (String .class.isAssignableFrom(param.getTypeAsClass()) && cmds.size() == 0) { 115 cmds.put(getName(), value); 116 } 117 vars.put(param.getName(), value); 118 } 119 if (cmds.size() == 0) cmds.put(getName(), ""); 120 boolean ok = process(getPageInfo(arguments), cmds, vars); 121 return vars; 122 } 123 } 124 125 131 public Vector getNodeList(Object context, String command, Map params) { 132 StringTagger tagger=null; 133 if (params instanceof StringTagger) { 134 tagger = (StringTagger)params; 135 } else { 136 tagger = new StringTagger(""); 137 if (params != null) { 138 for (Iterator entries = params.entrySet().iterator(); entries.hasNext(); ) { 139 Map.Entry entry = (Map.Entry) entries.next(); 140 String key=(String ) entry.getKey(); 141 Object o = entry.getValue(); 142 if (o instanceof Vector) { 143 tagger.setValues(key, (Vector)o); 144 } else { 145 tagger.setValue(key, "" + o); 146 } 147 } 148 } 149 } 150 PageInfo sp = null; 151 if (context instanceof PageInfo) { 152 sp = (PageInfo)context; 153 } 154 Vector v = getList(sp, tagger, command); 155 int items = 1; 156 try { items = Integer.parseInt(tagger.Value("ITEMS")); } catch (NumberFormatException e) {} 157 Vector fieldlist = tagger.Values("FIELDS"); 158 Vector res = new Vector(v.size() / items); 159 MMObjectBuilder bul = getListBuilder(command, params); 160 for(int i= 0; i < v.size(); i+=items) { 161 VirtualNode node = new VirtualNode(bul); 162 for(int j= 0; (j<items) && (j<v.size()); j++) { 163 if ((fieldlist!=null) && (j<fieldlist.size())) { 164 node.setValue((String )fieldlist.get(j),v.get(i+j)); 165 } else { 166 node.setValue("item"+(j+1),v.get(i+j)); 167 } 168 } 169 res.add(node); 170 } 171 return res; 172 } 173 174 177 public Vector getList(PageInfo sp,StringTagger params, String command) { 178 throw new UnsupportedOperationException ("Module " + this.getClass().getName() + " does not implement LIST"); 179 } 180 181 184 public boolean process(PageInfo sp, Hashtable cmds, Hashtable vars) { 185 return false; 186 } 187 188 191 public String replace (PageInfo sp, String command) { 192 return "This module doesn't implement this processor call"; 193 } 194 195 199 public String replace (PageInfo sp, StringTagger command) { 200 return "This module doesn't implement this processor call"; 201 } 202 203 206 public boolean cacheCheck(PageInfo sp, String cmd) { 207 return false; 208 } 209 210 211 216 217 public void reload() { 218 } 219 220 221 226 public void unload() { 227 } 228 229 233 public void init() { 234 } 235 236 240 public void onload() { 241 } 242 243 244 } 245
| Popular Tags
|