1 package org.sapia.magnet.domain; 2 3 import java.util.ArrayList ; 6 import java.util.Collection ; 7 import java.util.Collections ; 8 import java.util.HashMap ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 import java.util.Map ; 12 13 import org.sapia.util.xml.confix.ObjectHandlerIF; 16 17 import org.sapia.magnet.render.AbstractRenderable; 20 import org.sapia.magnet.render.MagnetContext; 21 import org.sapia.magnet.render.RenderableIF; 22 import org.sapia.magnet.render.RenderingException; 23 24 25 36 public abstract class AbstractObjectHandler extends AbstractRenderable implements ObjectHandlerIF { 37 38 42 43 private static final List EMPTY_LIST = Collections.unmodifiableList(new ArrayList (0)); 44 45 49 50 private List _theScriptHandlerDefs; 51 52 53 private List _theProtocolHandlerDefs; 54 55 56 private List _theLaunchHandlerDefs; 57 58 59 private Map _theObjects; 60 61 65 68 protected AbstractObjectHandler() { 69 _theLaunchHandlerDefs = new ArrayList (); 70 _theProtocolHandlerDefs = new ArrayList (); 71 _theScriptHandlerDefs = new ArrayList (); 72 } 73 74 78 83 public Collection getLaunchHandlerDefs() { 84 return _theLaunchHandlerDefs; 85 } 86 87 92 public Collection getProtocolHandlerDefs() { 93 return _theProtocolHandlerDefs; 94 } 95 96 101 public Collection getScriptHandlerDefs() { 102 return _theScriptHandlerDefs; 103 } 104 105 112 public Collection getObjectsFor(String anElementName) { 113 List someObjects = EMPTY_LIST; 114 115 if (_theObjects != null) { 116 someObjects = (List ) _theObjects.get(anElementName.toLowerCase()); 117 118 if (someObjects == null) { 119 someObjects = EMPTY_LIST; 120 } 121 } 122 123 return someObjects; 124 } 125 126 132 public Collection getElementNames() { 133 Collection someNames = EMPTY_LIST; 134 135 if (_theObjects != null) { 136 someNames = _theObjects.keySet(); 137 } 138 139 return someNames; 140 } 141 142 146 151 public void addLaunchHandlerDef(LaunchHandlerDef aHandlerDef) { 152 _theLaunchHandlerDefs.add(aHandlerDef); 153 } 154 155 160 public void addProtocolHandlerDef(ProtocolHandlerDef aHandlerDef) { 161 _theProtocolHandlerDefs.add(aHandlerDef); 162 } 163 164 169 public void addScriptHandlerDef(ScriptHandlerDef aHandlerDef) { 170 _theScriptHandlerDefs.add(aHandlerDef); 171 } 172 173 177 183 public void renderHandlerDefs(MagnetContext aContext) throws RenderingException { 184 for (Iterator it = _theLaunchHandlerDefs.iterator(); it.hasNext(); ) { 186 LaunchHandlerDef aHandlerDef = (LaunchHandlerDef) it.next(); 187 aHandlerDef.render(aContext); 188 } 189 190 for (Iterator it = _theProtocolHandlerDefs.iterator(); it.hasNext(); ) { 192 ProtocolHandlerDef aHandlerDef = (ProtocolHandlerDef) it.next(); 193 aHandlerDef.render(aContext); 194 } 195 196 for (Iterator it = _theScriptHandlerDefs.iterator(); it.hasNext(); ) { 198 ScriptHandlerDef aHandlerDef = (ScriptHandlerDef) it.next(); 199 aHandlerDef.render(aContext); 200 } 201 } 202 203 209 public void render(MagnetContext aContext) throws RenderingException { 210 for (Iterator it = getElementNames().iterator(); it.hasNext(); ) { 211 String anElementName = (String ) it.next(); 212 213 for (Iterator someObjects = getObjectsFor(anElementName).iterator(); someObjects.hasNext(); ) { 214 Object anObject = someObjects.next(); 215 216 if (anObject instanceof RenderableIF) { 217 ((RenderableIF) anObject).render(aContext); 218 } 219 } 220 } 221 } 222 223 229 public void handleObject(String anElementName, Object anObject) { 230 if (_theObjects == null) { 232 _theObjects = new HashMap (); 233 } 234 235 List someObjects = (List ) _theObjects.get(anElementName.toLowerCase()); 237 if (someObjects == null) { 238 someObjects = new ArrayList (); 239 _theObjects.put(anElementName.toLowerCase(), someObjects); 240 } 241 242 someObjects.add(anObject); 244 } 245 246 250 255 public String toString() { 256 StringBuffer aBuffer = new StringBuffer (super.toString()); 257 aBuffer.append("[launchHandlerDefs=").append(_theLaunchHandlerDefs). 258 append(" protocolHandlerDefs=").append(_theProtocolHandlerDefs). 259 append(" scriptHandlerDefs=").append(_theScriptHandlerDefs). 260 append(" objects=").append(_theObjects). 261 append("]"); 262 263 return aBuffer.toString(); 264 } 265 } 266 | Popular Tags |