1 /**
2  *  ServingXML
3  *
4  *  Copyright (C) 2006  Daniel Parker
5  *    daniel.parker@servingxml.com
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  **/
20 
21 package com.servingxml.components.flatfile.options;
22 
23 import com.servingxml.util.MessageFormatter;
24 import com.servingxml.util.ServingXmlMessages;
25 import com.servingxml.util.ServingXmlException;
26 import com.servingxml.util.StringHelper;
27 import com.servingxml.components.common.YesNoEnum;
28 
29 /**
30  * The <code>QuoteEnum</code> represents yes/no
31  *
32  *
33  * @author Daniel A. Parker (daniel.parker@servingxml.com)
34  */
35 
36 public abstract class QuoteEnum {
37 
38   public static final QuoteEnum ALWAYS = new AlwaysEnumValue();
39   public static final QuoteEnum NEVER = new NeverEnumValue();
40   public static final QuoteEnum AUTO = new AutoEnumValue();
41 
42   static final String[] values = {AutoEnumValue.VALUE, AlwaysEnumValue.VALUE, NeverEnumValue.VALUE};
43 
auto()44   public abstract boolean auto();
always()45   public abstract boolean always();
46 
never()47   public boolean never() {
48     return !(auto() || always());
49   }
50 
parse(String value)51   public static QuoteEnum parse(String value) {
52     QuoteEnum indicator = null;
53 
54     if (value.length() > 0) {
55       if (value.equals(AutoEnumValue.VALUE)) {
56         indicator = AUTO;
57       } else if (value.equals(AlwaysEnumValue.VALUE)) {
58         indicator = ALWAYS;
59       } else if (value.equals(NeverEnumValue.VALUE)) {
60         indicator = NEVER;
61       } else if (value.equals(YesNoEnum.YES.toString())) {
62         indicator = AUTO;
63       } else if (value.equals(YesNoEnum.NO.toString())) {
64         indicator = NEVER;
65       }
66     }
67     if (indicator == null) {
68       String message = MessageFormatter.getInstance().getMessage(ServingXmlMessages.VALUE_UNKNOWN,
69                          value,
70                          StringHelper.toString(values,",","\""));
71       throw new ServingXmlException(message);
72     }
73 
74     return indicator;
75   }
76 
77   private static class AlwaysEnumValue extends QuoteEnum {
78     static final String VALUE = "always";
79 
toString()80     public String toString() {
81       return VALUE;
82     }
83 
auto()84     public boolean auto() {
85       return false;
86     }
always()87     public boolean always() {
88       return true;
89     }
90   }
91 
92   private static class NeverEnumValue extends QuoteEnum {
93     static final String VALUE = "never";
94 
toString()95     public String toString() {
96       return VALUE;
97     }
98 
auto()99     public boolean auto() {
100       return false;
101     }
always()102     public boolean always() {
103       return false;
104     }
105   }
106 
107   private static class AutoEnumValue extends QuoteEnum {
108     static final String VALUE = "auto";
109 
toString()110     public String toString() {
111       return VALUE;
112     }
113 
auto()114     public boolean auto() {
115       return true;
116     }
always()117     public boolean always() {
118       return false;
119     }
120   }
121 }
122 
123 
124