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.

IfFieldPart.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.util.text;
  19. import java.util.Map;
  20. import org.apache.fop.util.text.AdvancedMessageFormat.Part;
  21. import org.apache.fop.util.text.AdvancedMessageFormat.PartFactory;
  22. /**
  23. * Defines an "if" field part that checks if field's value is true or false.
  24. * It returns either of two possible values attached as additional part parameters. Example:
  25. * <code>{field,if,Yes,No}</code>
  26. */
  27. public class IfFieldPart implements Part {
  28. /** the field name for the part */
  29. protected String fieldName;
  30. /** the value being returned if the field is true */
  31. protected String ifValue;
  32. /** the value being returned if the field is false */
  33. protected String elseValue;
  34. /**
  35. * Creates a new "if" field part.
  36. * @param fieldName the field name
  37. * @param values the unparsed parameter values
  38. */
  39. public IfFieldPart(String fieldName, String values) {
  40. this.fieldName = fieldName;
  41. parseValues(values);
  42. }
  43. /**
  44. * Parses the parameter values
  45. * @param values the unparsed parameter values
  46. */
  47. protected void parseValues(String values) {
  48. String[] parts = AdvancedMessageFormat.COMMA_SEPARATOR_REGEX.split(values, 2);
  49. if (parts.length == 2) {
  50. ifValue = AdvancedMessageFormat.unescapeComma(parts[0]);
  51. elseValue = AdvancedMessageFormat.unescapeComma(parts[1]);
  52. } else {
  53. ifValue = AdvancedMessageFormat.unescapeComma(values);
  54. }
  55. }
  56. /** {@inheritDoc} */
  57. public void write(StringBuffer sb, Map params) {
  58. boolean isTrue = isTrue(params);
  59. if (isTrue) {
  60. sb.append(ifValue);
  61. } else if (elseValue != null) {
  62. sb.append(elseValue);
  63. }
  64. }
  65. /**
  66. * Indicates whether the field's value is true. If the field is not a boolen, it is true
  67. * if the field is not null.
  68. * @param params the message parameters
  69. * @return true the field's value as boolean
  70. */
  71. protected boolean isTrue(Map params) {
  72. Object obj = params.get(fieldName);
  73. if (obj instanceof Boolean) {
  74. return (Boolean) obj;
  75. } else {
  76. return (obj != null);
  77. }
  78. }
  79. /** {@inheritDoc} */
  80. public boolean isGenerated(Map params) {
  81. return isTrue(params) || (elseValue != null);
  82. }
  83. /** {@inheritDoc} */
  84. public String toString() {
  85. return "{" + this.fieldName + ", if...}";
  86. }
  87. /**
  88. * Part factory for "if".
  89. */
  90. public static class Factory implements PartFactory {
  91. /** {@inheritDoc} */
  92. public Part newPart(String fieldName, String values) {
  93. return new IfFieldPart(fieldName, values);
  94. }
  95. /** {@inheritDoc} */
  96. public String getFormat() {
  97. return "if";
  98. }
  99. }
  100. }