KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > common > cli > EInput


1 /* ====================================================================
2  * The LateralNZ Software License, Version 1.0
3  *
4  * Copyright (c) 2003 LateralNZ. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * LateralNZ (http://www.lateralnz.org/) and other third parties."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "LateralNZ" must not be used to endorse or promote
26  * products derived from this software without prior written
27  * permission. For written permission, please
28  * contact oss@lateralnz.org.
29  *
30  * 5. Products derived from this software may not be called "Panther",
31  * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
32  * "LATERALNZ" appear in their name, without prior written
33  * permission of LateralNZ.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of LateralNZ. For more
51  * information on Lateral, please see http://www.lateralnz.com/ or
52  * http://www.lateralnz.org
53  *
54  */

55 package org.lateralnz.common.cli;
56
57 import java.io.OutputStream JavaDoc;
58 import java.io.BufferedReader JavaDoc;
59 import java.util.Iterator JavaDoc;
60 import java.util.List JavaDoc;
61 import java.util.HashMap JavaDoc;
62 import java.util.Map JavaDoc;
63
64 import org.w3c.dom.Node JavaDoc;
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 JavaDoc name;
73   private String JavaDoc type;
74   private String JavaDoc text;
75   private Map JavaDoc messages = new HashMap JavaDoc();
76   
77   public EInput(String JavaDoc formName, String JavaDoc name, String JavaDoc type, String JavaDoc text, List JavaDoc messageNodes) throws Exception JavaDoc {
78     this.name = formName + DOT + name;
79     this.type = type;
80     this.text = text;
81     
82     Iterator JavaDoc iter = messageNodes.iterator();
83     while (iter.hasNext()) {
84       Node JavaDoc n = (Node JavaDoc)iter.next();
85       String JavaDoc 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 JavaDoc in, OutputStream JavaDoc out, Map JavaDoc ctx, CLIOHandler ioh, CLValidator validator, boolean prompt) throws Exception JavaDoc {
94 // ioh.write(out, NEWLINE + text, false);
95
ioh.write(out, text, false);
96     if (type.equalsIgnoreCase("password")) {
97       ioh.setEcho(out, false);
98     }
99     String JavaDoc 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 JavaDoc key = Integer.toString(valid);
110     if (messages.containsKey(key)) {
111       ioh.write(out, (String JavaDoc)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);
123
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