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.

HexFieldPart.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. * Function formatting a number or character to a hex value.
  24. */
  25. public class HexFieldPart implements Part {
  26. private String fieldName;
  27. /**
  28. * Creates a new hex field part
  29. * @param fieldName the field name
  30. */
  31. public HexFieldPart(String fieldName) {
  32. this.fieldName = fieldName;
  33. }
  34. /** {@inheritDoc} */
  35. public boolean isGenerated(Map params) {
  36. Object obj = params.get(fieldName);
  37. return obj != null;
  38. }
  39. /** {@inheritDoc} */
  40. public void write(StringBuffer sb, Map params) {
  41. if (!params.containsKey(fieldName)) {
  42. throw new IllegalArgumentException(
  43. "Message pattern contains unsupported field name: " + fieldName);
  44. }
  45. Object obj = params.get(fieldName);
  46. if (obj instanceof Character) {
  47. sb.append(Integer.toHexString((Character) obj));
  48. } else if (obj instanceof Number) {
  49. sb.append(Integer.toHexString(((Number)obj).intValue()));
  50. } else {
  51. throw new IllegalArgumentException("Incompatible value for hex field part: "
  52. + obj.getClass().getName());
  53. }
  54. }
  55. /** {@inheritDoc} */
  56. public String toString() {
  57. return "{" + this.fieldName + ",hex}";
  58. }
  59. /** Factory for {@link HexFieldPart}. */
  60. public static class Factory implements PartFactory {
  61. /** {@inheritDoc} */
  62. public Part newPart(String fieldName, String values) {
  63. return new HexFieldPart(fieldName);
  64. }
  65. /** {@inheritDoc} */
  66. public String getFormat() {
  67. return "hex";
  68. }
  69. }
  70. }