%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.interaction.AskTag |
|
|
1 | /* |
|
2 | * Copyright 2002,2004 The Apache Software Foundation. |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
15 | */ |
|
16 | package org.apache.commons.jelly.tags.interaction; |
|
17 | ||
18 | import java.io.InputStreamReader; |
|
19 | import java.io.BufferedReader; |
|
20 | import java.io.IOException; |
|
21 | ||
22 | import org.apache.commons.jelly.TagSupport; |
|
23 | import org.apache.commons.jelly.XMLOutput; |
|
24 | ||
25 | /** |
|
26 | * Jelly Tag that asks the user a question, and puts his answer into |
|
27 | * a variable, with the attribute "answer". |
|
28 | * This variable may be reused further as any other Jelly variable. |
|
29 | * @author <a href="mailto:smor@hasgard.net">Stéphane Mor</a> |
|
30 | */ |
|
31 | 0 | public class AskTag extends TagSupport |
32 | { |
|
33 | /** The question to ask to the user */ |
|
34 | private String question; |
|
35 | ||
36 | /** |
|
37 | * The variable in which we will stock the user's input. |
|
38 | * This defaults to "interact.answer". |
|
39 | */ |
|
40 | 0 | private String answer = "interact.answer"; |
41 | ||
42 | /** The default value, if the user doesn't answer */ |
|
43 | private String defaultInput; |
|
44 | ||
45 | /** The user's input */ |
|
46 | 0 | private String input = ""; |
47 | ||
48 | /** The prompt to display before the user input */ |
|
49 | 0 | private String prompt = ">"; |
50 | ||
51 | /** |
|
52 | * Sets the question to ask to the user. If a "default" attribute |
|
53 | * is present, it will appear inside []. |
|
54 | * @param question The question to ask to the user |
|
55 | */ |
|
56 | public void setQuestion(String question) |
|
57 | { |
|
58 | 0 | this.question = question; |
59 | 0 | } |
60 | ||
61 | /** |
|
62 | * Sets the name of the variable that will hold the answer |
|
63 | * This defaults to "interact.answer". |
|
64 | * @param answer the name of the variable that will hold the answer |
|
65 | */ |
|
66 | public void setAnswer(String answer) |
|
67 | { |
|
68 | 0 | this.answer = answer; |
69 | 0 | } |
70 | ||
71 | /** |
|
72 | * Sets the default answer to the question. |
|
73 | * If it is present, it will appear inside []. |
|
74 | * @param default the default answer to the question |
|
75 | */ |
|
76 | public void setDefault(String defaultInput) |
|
77 | { |
|
78 | 0 | this.defaultInput = defaultInput; |
79 | 0 | } |
80 | ||
81 | /** |
|
82 | * Sets the prompt that will be displayed before the user's input. |
|
83 | * @param promt the prompt that will be displayed before the user's input. |
|
84 | */ |
|
85 | public void setPrompt(String prompt) |
|
86 | { |
|
87 | 0 | this.prompt = prompt; |
88 | 0 | } |
89 | ||
90 | ||
91 | /** |
|
92 | * Perform functionality provided by the tag |
|
93 | * @param output the place to write output |
|
94 | */ |
|
95 | public void doTag(XMLOutput output) |
|
96 | { |
|
97 | 0 | if (question != null) |
98 | { |
|
99 | 0 | if (defaultInput != null) |
100 | { |
|
101 | 0 | System.out.println(question + " [" + defaultInput + "]"); |
102 | } |
|
103 | else |
|
104 | { |
|
105 | 0 | System.out.println(question); |
106 | } |
|
107 | // The prompt should be just before the user's input, |
|
108 | // but it doesn't work ... |
|
109 | //System.out.print(prompt + " "); |
|
110 | } |
|
111 | ||
112 | 0 | BufferedReader br = new BufferedReader(class="keyword">new InputStreamReader(System.in)); |
113 | ||
114 | try { |
|
115 | 0 | input = br.readLine(); |
116 | 0 | if (defaultInput != null && input.trim().equals("")) |
117 | { |
|
118 | 0 | input = defaultInput; |
119 | } |
|
120 | 0 | } catch (IOException ioe) { |
121 | } |
|
122 | 0 | context.setVariable(answer, input); |
123 | 0 | } |
124 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |