1 64 65 package com.jcorporate.expresso.core.controller; 66 67 import com.jcorporate.expresso.core.misc.StringUtil; 68 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 69 import org.w3c.dom.NamedNodeMap ; 70 import org.w3c.dom.Node ; 71 import org.w3c.dom.NodeList ; 72 73 import java.io.Serializable ; 74 import java.util.Enumeration ; 75 import java.util.HashMap ; 76 import java.util.Map ; 77 import java.util.Vector ; 78 79 96 public class Block 97 extends ControllerElement 98 implements Cloneable , 99 Serializable { 100 private static final String thisClass = Block.class.getName() + "."; 101 private Transition formTransition = null; 102 103 private Map mNestedBlocks = null; 104 private Map mNestedTransitions = null; 105 private Map mNestedInputs = null; 106 private Map mNestedOutputs = null; 107 108 111 public Block() { 112 } 113 114 120 public Block(String newName) { 121 setName(newName); 122 } 123 124 129 public void add(ControllerElement element) { 130 addNested(element); 131 if (element instanceof Output) { 134 if (mNestedOutputs == null) { 135 mNestedOutputs = new HashMap (); 136 } 137 mNestedOutputs.put(element.getName(), element); 138 } else if (element instanceof Input) { 139 if (mNestedInputs == null) { 140 mNestedInputs = new HashMap (); 141 } 142 mNestedInputs.put(element.getName(), element); 143 } else if (element instanceof Transition) { 144 if (mNestedTransitions == null) { 145 mNestedTransitions = new HashMap (); 146 } 147 mNestedTransitions.put(element.getName(), element); 148 } else if (element instanceof Block) { 149 if (mNestedBlocks == null) { 150 mNestedBlocks = new HashMap (); 151 } 152 mNestedBlocks.put(element.getName(), element); 153 } 154 } 155 156 157 163 public Map getNamedTransitions() { 164 return mNestedTransitions; 165 } 166 167 173 public Map getNamedOutputs() { 174 return mNestedOutputs; 175 } 176 177 183 public Map getNamedInputs() { 184 return mNestedInputs; 185 } 186 187 193 public Map getNamedBlocks() { 194 return mNestedBlocks; 195 } 196 197 204 public void removeNested(ControllerElement elementToRemove) 205 throws ControllerException { 206 super.removeNested(elementToRemove); 207 208 if (elementToRemove instanceof Input) { 209 mNestedInputs.remove(elementToRemove.getName()); 210 } else if (elementToRemove instanceof Output) { 211 mNestedOutputs.remove(elementToRemove.getName()); 212 } else if (elementToRemove instanceof Transition) { 213 mNestedTransitions.remove(elementToRemove.getName()); 214 } else if (elementToRemove instanceof Block) { 215 mNestedBlocks.remove(elementToRemove.getName()); 216 } 217 } 218 219 224 public Object clone() 225 throws CloneNotSupportedException { 226 Block b; 227 228 synchronized (this) { 229 b = (Block) super.clone(); 230 b.formTransition = this.formTransition; 231 b.mNestedBlocks = this.mNestedBlocks; 232 b.mNestedTransitions = this.mNestedTransitions; 233 b.mNestedInputs = this.mNestedInputs; 234 b.mNestedOutputs = this.mNestedOutputs; 235 } 236 237 return b; 238 } 239 240 247 public static ControllerElement fromXML(Node n) 248 throws ControllerException { 249 250 if (n.getNodeName().equals("#document")) { 252 return fromXML(n.getChildNodes().item(0)); 253 } 254 if (!n.getNodeName().equals("block")) { 255 return null; 256 } 257 258 Block b = new Block(); 259 260 NamedNodeMap transitionAttributes = n.getAttributes(); 262 Node attributeNode = transitionAttributes.getNamedItem("name"); 263 264 if (attributeNode != null) { 265 String value = attributeNode.getNodeValue(); 266 267 if (value != null) { 268 b.setName(value); 269 } 270 } 271 272 NodeList nl = n.getChildNodes(); 273 274 for (int i = 0; i < nl.getLength(); i++) { 275 Node oneChild = nl.item(i); 276 String nodeName = oneChild.getNodeName(); 277 278 if (nodeName.equals("controller-element")) { 279 b = (Block) ControllerElement.fromXML(oneChild, b); 280 break; 281 } 282 } 283 284 return b; 285 } 286 287 292 public Vector getBlocks() { 293 Vector elements = new Vector (16); 294 ControllerElement t = null; 295 296 for (Enumeration e = getNested().elements(); e.hasMoreElements();) { 297 t = (ControllerElement) e.nextElement(); 298 299 if (t instanceof Block) { 300 elements.add(t); 301 } 302 } 303 304 return elements; 305 } 306 307 315 public Block getBlock(String blockName) { 316 return (Block) this.getNestedMap().get(blockName); 317 } 318 319 327 public Input getInput(String inputName) { 328 return (Input) this.getNestedMap().get(inputName); 329 } 330 331 339 public Output getOutput(String outputName) { 340 return (Output) this.getNestedMap().get(outputName); 341 } 342 343 351 public Transition getTransition(String transitionName) { 352 return (Transition) this.getNestedMap().get(transitionName); 353 } 354 355 360 public String getFormEncoding() { 361 return getAttribute("formEncoding"); 362 } 363 364 369 public String getFormMethod() { 370 String formMethod = StringUtil.notNull(getAttribute("formMethod")); 371 372 if (!formMethod.equalsIgnoreCase("get")) { 373 formMethod = "post"; 374 } 375 376 return formMethod; 377 } 378 379 384 public Transition getFormTransition() { 385 return formTransition; 386 } 387 388 389 394 public Vector getInputs() { 395 Vector elements = new Vector (16); 396 ControllerElement t = null; 397 398 for (Enumeration e = getNested().elements(); e.hasMoreElements();) { 399 t = (ControllerElement) e.nextElement(); 400 401 if (t instanceof Input) { 402 elements.add(t); 403 } 404 } 405 406 return elements; 407 } 408 409 414 public int getNumContents() { 415 return getNested().size(); 416 } 417 418 419 424 public Vector getOutputs() { 425 Vector elements = new Vector (16); 426 ControllerElement t = null; 427 428 for (Enumeration e = getNested().elements(); e.hasMoreElements();) { 429 t = (ControllerElement) e.nextElement(); 430 431 if (t instanceof Output) { 432 elements.add(t); 433 } 434 } 435 436 return elements; 437 } 438 439 444 public Vector getTransitions() { 445 Vector elements = new Vector (16); 446 ControllerElement t = null; 447 448 for (Enumeration e = getNested().elements(); e.hasMoreElements();) { 449 t = (ControllerElement) e.nextElement(); 450 451 if (t instanceof Transition) { 452 elements.add(t); 453 } 454 } 455 456 return elements; 457 } 458 459 464 public boolean isForm() { 465 if (StringUtil.notNull(getAttribute("form")).equals("true")) { 466 return true; 467 } else { 468 return false; 469 } 470 } 471 472 478 public void setForm(String isForm) 479 throws ControllerException { 480 String thisMethod = thisClass + "setForm"; 481 482 if (!(isForm.equals("true") || isForm.equals("false"))) { 483 throw new ControllerException(thisMethod + 484 ":parameter passed in must be \"true\" or \"false\""); 485 } 486 487 setAttribute("form", isForm); 488 } 489 490 491 497 public void setFormEncoding(String encoding) 498 throws ControllerException { 499 setAttribute("formEncoding", encoding); 500 } 501 502 503 509 public void setFormMethod(String method) 510 throws ControllerException { 511 String thisMethod = thisClass + "setFormMethod"; 512 513 if (!(method.equals("get") || method.equals("post"))) { 514 throw new ControllerException(thisMethod + 515 ": parameter passed in must be \"get\" or \"post\""); 516 } 517 518 setAttribute("formMethod", method); 519 } 520 521 522 529 public void setFormTransition(Transition a) 530 throws ControllerException { 531 String thisMethod = thisClass + "setFormTransition"; 532 533 if (a == null) { 534 throw new ControllerException(thisMethod + 535 ":Transition cannot be null"); 536 } 537 538 formTransition = a; 539 } 540 541 542 549 public FastStringBuffer toXML(FastStringBuffer stream) { 550 stream.append("<block name=\"" + StringUtil.xmlEscape(this.getName()) + 551 "\">\n"); 552 stream = super.toXML(stream); 553 554 stream.append("</block>"); 559 560 return stream; 561 } 562 563 protected FastStringBuffer toNestedXML(FastStringBuffer stream, 564 String blockLabel, Vector elements) { 565 stream.append("<"); 566 stream.append(blockLabel); 567 stream.append(">"); 568 569 for (Enumeration eb = elements.elements(); eb.hasMoreElements();) { 570 ControllerElement oneControllerElement = (ControllerElement) eb.nextElement(); 571 stream = oneControllerElement.toXML(stream); 572 } 573 574 575 stream.append("</"); 576 stream.append(blockLabel); 577 stream.append(">"); 578 579 return stream; 580 } 581 } 582 583 584 | Popular Tags |