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.

EqualsFieldPart.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.utils.text.AdvancedMessageFormat;
  21. import org.apache.fop.utils.text.AdvancedMessageFormat.Part;
  22. import org.apache.fop.utils.text.AdvancedMessageFormat.PartFactory;
  23. /**
  24. * Defines an "equals" field part that can compare a field's string value against another string.
  25. * It returns either of two possible values attached as additional part parameters. Example:
  26. * <code>{field,equals,new,This is new!,This is old!}</code>
  27. */
  28. public class EqualsFieldPart extends IfFieldPart {
  29. private String equalsValue;
  30. /**
  31. * Creates a new "equals" field part.
  32. * @param fieldName the field name
  33. * @param values the unparsed parameter values
  34. */
  35. public EqualsFieldPart(String fieldName, String values) {
  36. super(fieldName, values);
  37. }
  38. /** {@inheritDoc} */
  39. protected void parseValues(String values) {
  40. String[] parts = AdvancedMessageFormat.COMMA_SEPARATOR_REGEX.split(values, 3);
  41. this.equalsValue = parts[0];
  42. if (parts.length == 1) {
  43. throw new IllegalArgumentException(
  44. "'equals' format must have at least 2 parameters");
  45. }
  46. if (parts.length == 3) {
  47. ifValue = AdvancedMessageFormat.unescapeComma(parts[1]);
  48. elseValue = AdvancedMessageFormat.unescapeComma(parts[2]);
  49. } else {
  50. ifValue = AdvancedMessageFormat.unescapeComma(parts[1]);
  51. }
  52. }
  53. /** {@inheritDoc} */
  54. protected boolean isTrue(Map params) {
  55. Object obj = params.get(fieldName);
  56. if (obj != null) {
  57. return String.valueOf(obj).equals(this.equalsValue);
  58. } else {
  59. return false;
  60. }
  61. }
  62. /** {@inheritDoc} */
  63. public String toString() {
  64. return "{" + this.fieldName + ", equals " + this.equalsValue + "}";
  65. }
  66. /**
  67. * Part factory for "equals".
  68. */
  69. public static class Factory implements PartFactory {
  70. /** {@inheritDoc} */
  71. public Part newPart(String fieldName, String values) {
  72. return new EqualsFieldPart(fieldName, values);
  73. }
  74. /** {@inheritDoc} */
  75. public String getFormat() {
  76. return "equals";
  77. }
  78. }
  79. }