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.

FileIDGenerator.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.security.MessageDigest;
  20. import java.security.NoSuchAlgorithmException;
  21. import java.text.DateFormat;
  22. import java.text.SimpleDateFormat;
  23. import java.util.Date;
  24. import java.util.Random;
  25. /**
  26. * A class to generate the File Identifier of a PDF document (the ID entry of the file
  27. * trailer dictionary).
  28. */
  29. abstract class FileIDGenerator {
  30. abstract byte[] getOriginalFileID();
  31. abstract byte[] getUpdatedFileID();
  32. private static final class RandomFileIDGenerator extends FileIDGenerator {
  33. private byte[] fileID;
  34. private RandomFileIDGenerator() {
  35. Random random = new Random();
  36. fileID = new byte[16];
  37. random.nextBytes(fileID);
  38. }
  39. @Override
  40. byte[] getOriginalFileID() {
  41. return fileID;
  42. }
  43. @Override
  44. byte[] getUpdatedFileID() {
  45. return fileID;
  46. }
  47. }
  48. private static final class DigestFileIDGenerator extends FileIDGenerator {
  49. private byte[] fileID;
  50. private final PDFDocument document;
  51. private final MessageDigest digest;
  52. DigestFileIDGenerator(PDFDocument document) throws NoSuchAlgorithmException {
  53. this.document = document;
  54. this.digest = MessageDigest.getInstance("MD5");
  55. }
  56. @Override
  57. byte[] getOriginalFileID() {
  58. if (fileID == null) {
  59. generateFileID();
  60. }
  61. return fileID;
  62. }
  63. @Override
  64. byte[] getUpdatedFileID() {
  65. return getOriginalFileID();
  66. }
  67. private void generateFileID() {
  68. DateFormat df = new SimpleDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS");
  69. digest.update(PDFDocument.encode(df.format(new Date())));
  70. // Ignoring the filename here for simplicity even though it's recommended
  71. // by the PDF spec
  72. digest.update(PDFDocument.encode(String.valueOf(document.getCurrentFileSize())));
  73. digest.update(document.getInfo().toPDF());
  74. fileID = digest.digest();
  75. }
  76. }
  77. /**
  78. * Use this method when the file ID is needed before the document is finalized. The
  79. * digest method recommended by the PDF Reference is based, among other things, on the
  80. * file size.
  81. *
  82. * @return an instance that generates a random sequence of bytes for the File
  83. * Identifier
  84. */
  85. static FileIDGenerator getRandomFileIDGenerator() {
  86. return new RandomFileIDGenerator();
  87. }
  88. /**
  89. * Returns an instance that generates a file ID using the digest method recommended by
  90. * the PDF Reference. To properly follow the Reference, the size of the document must
  91. * no longer change after this method is called.
  92. *
  93. * @param document the document whose File Identifier must be generated
  94. * @return the generator
  95. * @throws NoSuchAlgorithmException if the MD5 Digest algorithm is not available
  96. */
  97. static FileIDGenerator getDigestFileIDGenerator(PDFDocument document)
  98. throws NoSuchAlgorithmException {
  99. return new DigestFileIDGenerator(document);
  100. }
  101. }