1 55 package org.lateralnz.common.cli; 56 57 import java.io.OutputStream ; 58 import java.io.BufferedReader ; 59 import java.util.Iterator ; 60 import java.util.List ; 61 import java.util.HashMap ; 62 import java.util.Map ; 63 64 import org.w3c.dom.Node ; 65 66 import org.lateralnz.common.util.Constants; 67 import org.lateralnz.common.util.XMLUtils; 68 import org.lateralnz.common.wrapper.IntHolder; 69 70 public class EInput extends CLElement implements Constants { 71 72 private String name; 73 private String type; 74 private String text; 75 private Map messages = new HashMap (); 76 77 public EInput(String formName, String name, String type, String text, List messageNodes) throws Exception { 78 this.name = formName + DOT + name; 79 this.type = type; 80 this.text = text; 81 82 Iterator iter = messageNodes.iterator(); 83 while (iter.hasNext()) { 84 Node n = (Node )iter.next(); 85 String val = EMPTY; 86 if (n.getFirstChild() != null) { 87 val = n.getFirstChild().getNodeValue(); 88 } 89 messages.put(XMLUtils.getAttributeValue(n, "type", ""), val); 90 } 91 } 92 93 public int process(BufferedReader in, OutputStream out, Map ctx, CLIOHandler ioh, CLValidator validator, boolean prompt) throws Exception { 94 ioh.write(out, text, false); 96 if (type.equalsIgnoreCase("password")) { 97 ioh.setEcho(out, false); 98 } 99 String s = ioh.read(in); 100 ioh.setEcho(out, true); 101 if (type.equalsIgnoreCase("password")) { 102 ioh.write(out, NEWLINE, false); 103 } 104 int valid = 0; 105 if (validator != null) { 106 valid = validator.validate(ctx, name, s); 107 } 108 109 String key = Integer.toString(valid); 110 if (messages.containsKey(key)) { 111 ioh.write(out, (String )messages.get(key) + NEWLINE, prompt); 112 } 113 114 if (valid >= 100) { 115 IntHolder tmp = (IntHolder)ctx.get("##" + name + ".error_counter"); 116 if (tmp == null) { 117 tmp = new IntHolder(0); 118 ctx.put("##" + name + ".error_counter", tmp); 119 } 120 121 if (tmp.value >= 2) { 122 ioh.write(out, NEWLINE, true); 124 return -1; 125 } 126 else { 127 tmp.value++; 128 return process(in, out, ctx, ioh, validator, prompt); 129 } 130 } 131 132 ctx.remove("##" + name + ".error_counter"); 133 if (valid == 0 || valid == Integer.MIN_VALUE) { 134 ctx.put(name, s); 135 return 0; 136 } 137 else { 138 ioh.write(out, NEWLINE, true); 139 return -1; 140 } 141 } 142 } | Popular Tags |