You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EnumProperty.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.fo.properties;
  19. import org.apache.fop.fo.FObj;
  20. import org.apache.fop.fo.PropertyList;
  21. import org.apache.fop.fo.expr.PropertyException;
  22. /**
  23. * Superclass for properties that wrap an enumeration value
  24. */
  25. public final class EnumProperty extends Property {
  26. /** cache holding all canonical EnumProperty instances */
  27. private static final PropertyCache cache = new PropertyCache();
  28. /**
  29. * Inner class for creating EnumProperty instances
  30. */
  31. public static class Maker extends PropertyMaker {
  32. /**
  33. * @param propId the id of the property for which a Maker should be created
  34. */
  35. public Maker(int propId) {
  36. super(propId);
  37. }
  38. /**
  39. * Called by subclass if no match found.
  40. * @param value string containing the value to be checked
  41. * @return null (indicates that an appropriate match was not found)
  42. */
  43. public Property checkEnumValues(String value) {
  44. //log.error("Unknown enumerated value for property '"
  45. // + getPropName() + "': " + value);
  46. return super.checkEnumValues(value);
  47. }
  48. public Property convertProperty(Property p,
  49. PropertyList propertyList,
  50. FObj fo) throws PropertyException {
  51. if (p instanceof EnumProperty) {
  52. return p;
  53. } else {
  54. return super.convertProperty(p, propertyList, fo);
  55. }
  56. }
  57. }
  58. private final int value;
  59. private final String text;
  60. /**
  61. * @param explicitValue enumerated value to be set for this property
  62. * @param text the string value of the enum.
  63. */
  64. private EnumProperty(int explicitValue, String text) {
  65. this.value = explicitValue;
  66. this.text = text;
  67. }
  68. public static EnumProperty getInstance(int explicitValue, String text) {
  69. return (EnumProperty) cache.fetch(
  70. new EnumProperty(explicitValue, text));
  71. }
  72. /**
  73. * @return this.value
  74. */
  75. public int getEnum() {
  76. return this.value;
  77. }
  78. /**
  79. * @return this.value cast as an Object
  80. */
  81. public Object getObject() {
  82. return text;
  83. }
  84. /**
  85. * {@inheritDoc}
  86. */
  87. public boolean equals(Object obj) {
  88. if (obj instanceof EnumProperty) {
  89. EnumProperty ep = (EnumProperty)obj;
  90. return (ep.value == this.value)
  91. && ((ep.text == this.text)
  92. || (ep.text != null
  93. && ep.text.equals(this.text)));
  94. } else {
  95. return false;
  96. }
  97. }
  98. /**
  99. * {@inheritDoc}
  100. */
  101. public int hashCode() {
  102. return value + text.hashCode();
  103. }
  104. }