KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > commands > options > BooleanOption


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: BooleanOption.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.commands.options;
25
26 import org.enhydra.xml.io.ErrorReporter;
27 import org.enhydra.xml.xmlc.XMLCException;
28
29 /**
30  * An option that takes a boolean value of true/false/yes/no.
31  */

32 public abstract class BooleanOption extends Option {
33     /**
34      * Construct a new object.
35      *
36      * @param name The name of the option.
37      * @param help Help string for the option.
38      */

39     public BooleanOption(String JavaDoc name,
40                          String JavaDoc help) {
41         super(name, 1, false, help);
42     }
43
44     /**
45      * Method called to set the value.
46      */

47     abstract protected void set(boolean value,
48                                 Object JavaDoc clientData) throws XMLCException;
49
50     /**
51      * Parse an instance of the option and set the field in the Options
52      * object using Java reflection.
53      *
54      * @param args The option's arguments.
55      */

56     public void parse(String JavaDoc[] args,
57                       ErrorReporter errorReporter,
58                       Object JavaDoc clientData) throws XMLCException {
59         String JavaDoc strValue = args[0];
60         try {
61             boolean value;
62             if (strValue.equalsIgnoreCase("yes")) {
63                 value = true;
64             } else if (strValue.equalsIgnoreCase("no")) {
65                 value = false;
66             } else if (strValue.equalsIgnoreCase("true")) {
67                 value = true;
68             } else if (strValue.equalsIgnoreCase("false")) {
69                 value = false;
70             } else {
71                 throw new XMLCException("Illegal value for " + name
72                                         + ", expected yes, no, true, or false");
73             }
74             set(value, clientData);
75         } catch (Exception JavaDoc except) {
76             throw new XMLCException(except);
77         }
78     }
79 }
80
Popular Tags