1. /*
  2. * Copyright 2000-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. */
  17. package org.apache.tools.ant.types;
  18. import org.apache.tools.ant.BuildException;
  19. /**
  20. * Helper class for attributes that can only take one of a fixed list
  21. * of values.
  22. *
  23. * <p>See {@link org.apache.tools.ant.taskdefs.FixCRLF FixCRLF} for an
  24. * example.
  25. *
  26. */
  27. public abstract class EnumeratedAttribute {
  28. /**
  29. * The selected value in this enumeration.
  30. */
  31. protected String value;
  32. /**
  33. * the index of the selected value in the array.
  34. */
  35. private int index = -1;
  36. /**
  37. * This is the only method a subclass needs to implement.
  38. *
  39. * @return an array holding all possible values of the enumeration.
  40. * The order of elements must be fixed so that <tt>indexOfValue(String)</tt>
  41. * always return the same index for the same value.
  42. */
  43. public abstract String[] getValues();
  44. /** bean constructor */
  45. protected EnumeratedAttribute() {
  46. }
  47. /**
  48. * Invoked by {@link org.apache.tools.ant.IntrospectionHelper IntrospectionHelper}.
  49. */
  50. public final void setValue(String value) throws BuildException {
  51. int index = indexOfValue(value);
  52. if (index == -1) {
  53. throw new BuildException(value + " is not a legal value for this attribute");
  54. }
  55. this.index = index;
  56. this.value = value;
  57. }
  58. /**
  59. * Is this value included in the enumeration?
  60. */
  61. public final boolean containsValue(String value) {
  62. return (indexOfValue(value) != -1);
  63. }
  64. /**
  65. * get the index of a value in this enumeration.
  66. * @param value the string value to look for.
  67. * @return the index of the value in the array of strings
  68. * or -1 if it cannot be found.
  69. * @see #getValues()
  70. */
  71. public final int indexOfValue(String value) {
  72. String[] values = getValues();
  73. if (values == null || value == null) {
  74. return -1;
  75. }
  76. for (int i = 0; i < values.length; i++) {
  77. if (value.equals(values[i])) {
  78. return i;
  79. }
  80. }
  81. return -1;
  82. }
  83. /**
  84. * @return the selected value.
  85. */
  86. public final String getValue() {
  87. return value;
  88. }
  89. /**
  90. * @return the index of the selected value in the array.
  91. * @see #getValues()
  92. */
  93. public final int getIndex() {
  94. return index;
  95. }
  96. /**
  97. * Convert the value to its string form.
  98. *
  99. * @return the string form of the value.
  100. */
  101. public String toString() {
  102. return getValue();
  103. }
  104. }