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.

ChoiceFieldPart.java 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.text.ChoiceFormat;
  20. import java.util.Map;
  21. import java.util.regex.Matcher;
  22. import java.util.regex.Pattern;
  23. import org.apache.fop.utils.text.AdvancedMessageFormat;
  24. import org.apache.fop.utils.text.AdvancedMessageFormat.Part;
  25. import org.apache.fop.utils.text.AdvancedMessageFormat.PartFactory;
  26. /**
  27. * Defines a "choice" field part that works like {@link ChoiceFormat}.
  28. */
  29. public class ChoiceFieldPart implements Part {
  30. private static final Pattern VARIABLE_REGEX = Pattern.compile("\\{([^\\}]+)\\}");
  31. private String fieldName;
  32. private ChoiceFormat choiceFormat;
  33. /**
  34. * Creates a new choice part.
  35. * @param fieldName the field name to work on
  36. * @param choicesPattern the choices pattern (as used by {@link ChoiceFormat})
  37. */
  38. public ChoiceFieldPart(String fieldName, String choicesPattern) {
  39. this.fieldName = fieldName;
  40. this.choiceFormat = new ChoiceFormat(choicesPattern);
  41. }
  42. /** {@inheritDoc} */
  43. public boolean isGenerated(Map params) {
  44. Object obj = params.get(fieldName);
  45. return obj != null;
  46. }
  47. /** {@inheritDoc} */
  48. public void write(StringBuffer sb, Map params) {
  49. Object obj = params.get(fieldName);
  50. Number num = (Number)obj;
  51. String result = this.choiceFormat.format(num.doubleValue());
  52. Matcher m = VARIABLE_REGEX.matcher(result);
  53. if (m.find()) {
  54. //Resolve inner variables
  55. AdvancedMessageFormat f = new AdvancedMessageFormat(result);
  56. f.format(params, sb);
  57. } else {
  58. sb.append(result);
  59. }
  60. }
  61. /** {@inheritDoc} */
  62. public String toString() {
  63. return "{" + this.fieldName + ",choice, ....}";
  64. }
  65. /** Factory for ChoiceFieldPart. */
  66. public static class Factory implements PartFactory {
  67. /** {@inheritDoc} */
  68. public Part newPart(String fieldName, String values) {
  69. return new ChoiceFieldPart(fieldName, values);
  70. }
  71. /** {@inheritDoc} */
  72. public String getFormat() {
  73. return "choice";
  74. }
  75. }
  76. }