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.

PDFReference.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.pdf;
  19. import java.io.OutputStream;
  20. import java.lang.ref.Reference;
  21. import java.lang.ref.SoftReference;
  22. /**
  23. * Class representing a PDF object reference. The object holds a soft reference to the actual
  24. * PDF object so the garbage collector can free the object if it's not referenced elsewhere. The
  25. * important thing about the class is the reference information to the actual PDF object in the
  26. * PDF file.
  27. */
  28. public class PDFReference implements PDFWritable {
  29. private PDFObjectNumber objectNumber;
  30. private int generation;
  31. private Reference<PDFObject> objReference;
  32. /**
  33. * Creates a new PDF reference.
  34. * @param obj the object to be referenced
  35. */
  36. public PDFReference(PDFObject obj) {
  37. this.objectNumber = obj.getObjectNumber();
  38. this.generation = obj.getGeneration();
  39. this.objReference = new SoftReference<PDFObject>(obj);
  40. }
  41. /**
  42. * Creates a new PDF reference, but without a reference to the original object.
  43. * @param ref an object reference
  44. */
  45. public PDFReference(String ref) {
  46. if (ref == null) {
  47. throw new NullPointerException("ref must not be null");
  48. }
  49. String[] parts = ref.split(" ");
  50. assert parts.length == 3;
  51. this.objectNumber = new PDFObjectNumber(Integer.parseInt(parts[0]));
  52. this.generation = Integer.parseInt(parts[1]);
  53. assert "R".equals(parts[2]);
  54. }
  55. /**
  56. * Returns the PDF object
  57. * @return the PDF object, or null if it has been released
  58. */
  59. public PDFObject getObject() {
  60. if (this.objReference != null) {
  61. PDFObject obj = this.objReference.get();
  62. if (obj == null) {
  63. this.objReference = null;
  64. }
  65. return obj;
  66. } else {
  67. return null;
  68. }
  69. }
  70. /**
  71. * Returns the object number.
  72. * @return the object number
  73. */
  74. public PDFObjectNumber getObjectNumber() {
  75. return this.objectNumber;
  76. }
  77. /**
  78. * Returns the generation.
  79. * @return the generation
  80. */
  81. public int getGeneration() {
  82. return this.generation;
  83. }
  84. /** {@inheritDoc} */
  85. @Override
  86. public String toString() {
  87. StringBuilder textBuffer = new StringBuilder();
  88. outputInline(null, textBuffer);
  89. return textBuffer.toString();
  90. }
  91. /** {@inheritDoc} */
  92. public void outputInline(OutputStream out, StringBuilder textBuffer) {
  93. textBuffer.append(getObjectNumber().getNumber()).append(' ').append(getGeneration()).append(" R");
  94. }
  95. }