Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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