KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedReader JavaDoc;
58 import java.io.OutputStream JavaDoc;
59 import java.util.Iterator JavaDoc;
60 import java.util.Map JavaDoc;
61 import java.util.List JavaDoc;
62 import java.util.LinkedList JavaDoc;
63
64 import org.w3c.dom.Document JavaDoc;
65 import org.w3c.dom.Node JavaDoc;
66 import org.w3c.dom.NodeList JavaDoc;
67
68 import org.lateralnz.common.util.XMLUtils;
69
70 public class CLTemplate {
71   private String JavaDoc name;
72   private LinkedList JavaDoc elements = new LinkedList JavaDoc();
73   private CLIOHandler ioh;
74   
75   public CLTemplate(Document JavaDoc doc, CLIOHandler ioh) throws Exception JavaDoc {
76     this.ioh = ioh;
77     NodeList JavaDoc nl;
78     NodeList JavaDoc nl1 = doc.getElementsByTagName("textform");
79     if (nl1.getLength() < 1) {
80       throw new Exception JavaDoc("expecting textform");
81     }
82     else {
83       name = XMLUtils.getAttributeValue(nl1.item(0), "name", "");
84       nl = nl1.item(0).getChildNodes();
85     }
86     
87     for (int i = 0; i < nl.getLength(); i++) {
88       Node JavaDoc n = nl.item(i);
89       String JavaDoc nodename = n.getNodeName();
90       if (nodename.equals("input")) {
91         String JavaDoc type = XMLUtils.getAttributeValue(n, "type", "text");
92         String JavaDoc iname = XMLUtils.getAttributeValue(n, "name", "");
93         Node JavaDoc tn = XMLUtils.getNamedNode(n, "text");
94         List JavaDoc l = XMLUtils.getNodesByName(n, "message");
95         
96         elements.add(new EInput(name, iname, type, tn.getFirstChild().getNodeValue(), l));
97       }
98       else if (nodename.equals("p")) {
99         if (n.getFirstChild() != null) {
100           elements.add(new EParagraph(n.getFirstChild().getNodeValue()));
101         }
102         else {
103           elements.add(new EParagraph(""));
104         }
105       }
106       else if (nodename.equals("output")) {
107         String JavaDoc oname = XMLUtils.getAttributeValue(n, "name", "");
108         String JavaDoc text = "";
109         if (n.getFirstChild() != null) {
110           text = n.getFirstChild().getNodeValue();
111         }
112         elements.add(new EOutput(oname, text));
113       }
114     }
115   }
116   
117   public boolean process(BufferedReader JavaDoc in, OutputStream JavaDoc out, Map JavaDoc ctx, CLValidator validator, boolean forceNoPrompt) throws Exception JavaDoc {
118     Iterator JavaDoc iter = elements.iterator();
119     boolean doPrompt = false;
120     int rtn = 0;
121     while (iter.hasNext()) {
122       CLElement elem = (CLElement)iter.next();
123       if (!iter.hasNext() && !forceNoPrompt) {
124         doPrompt = true;
125       }
126       rtn = elem.process(in, out, ctx, ioh, validator, doPrompt);
127       if (rtn != 0) {
128         return false;
129       }
130     }
131
132     int valid = Integer.MIN_VALUE;
133     if (validator != null) {
134       valid = validator.validate(ctx, name, null);
135     }
136     
137     if (valid != 0 && valid != Integer.MIN_VALUE) {
138       return false;
139     }
140     else {
141       return true;
142     }
143   }
144     
145
146 }
Popular Tags