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.

PDFObjectExtension.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.render.pdf.extensions;
  19. import org.apache.fop.util.XMLUtil;
  20. // CSOFF: LineLengthCheck
  21. public class PDFObjectExtension {
  22. private PDFObjectType type;
  23. private Object value;
  24. PDFObjectExtension(PDFObjectType type) {
  25. this.type = type;
  26. }
  27. public PDFObjectType getType() {
  28. return type;
  29. }
  30. public void setValue(Object value) {
  31. this.value = value;
  32. }
  33. public Object getValue() {
  34. return value;
  35. }
  36. /**
  37. * Obtain entry value as Boolean.
  38. * @return entry value
  39. */
  40. public Boolean getValueAsBoolean() {
  41. Object value = getValue();
  42. if (value instanceof Boolean) {
  43. return (Boolean) value;
  44. } else if (value instanceof String) {
  45. return Boolean.valueOf((String)value);
  46. } else {
  47. return false;
  48. }
  49. }
  50. /**
  51. * Obtain entry value as Number.
  52. * @return entry value
  53. */
  54. public Number getValueAsNumber() {
  55. Object value = getValue();
  56. if (value instanceof Number) {
  57. return (Number) value;
  58. } else if (value instanceof String) {
  59. double d = Double.parseDouble((String) value);
  60. if (Math.abs(Math.floor(d) - d) < 1E-10) {
  61. return (long) d;
  62. } else {
  63. return d;
  64. }
  65. } else {
  66. return 0;
  67. }
  68. }
  69. public String getValueAsString() {
  70. Object value = getValue();
  71. if (value == null) {
  72. return null;
  73. } else if (value instanceof String) {
  74. return (String) value;
  75. } else {
  76. return value.toString();
  77. }
  78. }
  79. public String getValueAsXMLEscapedString() {
  80. return XMLUtil.escape(getValueAsString());
  81. }
  82. public String getElementName() {
  83. return type.elementName();
  84. }
  85. }