KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > UserInputTask


1 /*****************************************************************************
2  * Copyright (C) The Apache Software Foundation. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * This software is published under the terms of the Apache Software License *
5  * version 1.1, a copy of which has been included with this distribution in *
6  * the LICENSE file. *
7  *****************************************************************************/

8
9 import java.io.InputStreamReader JavaDoc;
10 import java.io.BufferedReader JavaDoc;
11 import java.io.IOException JavaDoc;
12 import org.apache.tools.ant.taskdefs.Property;
13
14 /**
15  * Task to ask property values to the user. Uses current value as default.
16  *
17  * @author <a HREF="mailto:barozzi@nicolaken.com">Nicola Ken Barozzi</a>
18  * @created 14 January 2002
19  * @version CVS $Revision: 1.1 $ $Date: 2002/03/08 14:31:27 $
20  */

21 public class UserInputTask extends org.apache.tools.ant.Task {
22
23   private String JavaDoc question;
24   private String JavaDoc name;
25   private String JavaDoc value;
26
27   /**
28    * Constructor.
29    */

30   public UserInputTask() {
31     super();
32   }
33
34   /**
35    * Initializes the task.
36    */

37   public void init() {
38     super.init();
39     question = "?";
40   }
41
42   /**
43    * Run the task.
44    * @exception org.apache.tools.ant.BuildException The exception raised during task execution.
45    */

46   public void execute() throws org.apache.tools.ant.BuildException {
47     value = project.getProperty(name);
48     String JavaDoc defaultvalue = value;
49
50     //if the property exists
51
if (value != null) {
52
53       System.out.println("\n"+question + " ["+value + "] ");
54
55       BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc (System.in));
56
57       try
58       {
59         value = reader.readLine();
60       }
61       catch (IOException JavaDoc e)
62       {
63         value = defaultvalue;
64       }
65
66       if (!value.equals("")) {
67         project.setProperty(name, value);
68       } else {
69         project.setProperty(name, defaultvalue);
70       }
71     }
72   }
73
74   /**
75    * Sets the prompt text that will be presented to the user.
76    * @param prompt String
77    */

78   public void addText(String JavaDoc question) {
79     this.question=question;
80   }
81
82   public void setQuestion(String JavaDoc question) {
83     this.question = question;
84   }
85
86   public void setName(String JavaDoc name) {
87     this.name = name;
88   }
89
90 }
91
92
Popular Tags