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.

VersionController.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 org.apache.fop.pdf.StandardStructureAttributes.Table.Scope;
  20. /**
  21. * An abstraction that controls the mutability of the PDF version for a document.
  22. */
  23. public abstract class VersionController {
  24. private Version version;
  25. private VersionController(Version version) {
  26. this.version = version;
  27. }
  28. /**
  29. * Returns the PDF version of the document.
  30. *
  31. * @return the PDF version
  32. */
  33. public Version getPDFVersion() {
  34. return version;
  35. }
  36. /**
  37. * Sets the PDF version of the document.
  38. *
  39. * @param version the PDF version
  40. * @throws IllegalStateException if the PDF version is not allowed to change.
  41. */
  42. public abstract void setPDFVersion(Version version);
  43. abstract void addTableHeaderScopeAttribute(PDFStructElem th, Scope scope);
  44. @Override
  45. public String toString() {
  46. return version.toString();
  47. }
  48. /**
  49. * A class representing the version of a PDF document. This class doesn't allow the version to
  50. * change once it has been set, it is immutable. Any attempt to set the version will result in
  51. * an exception being thrown.
  52. */
  53. private static final class FixedVersion extends VersionController {
  54. private FixedVersion(Version version) {
  55. super(version);
  56. }
  57. @Override
  58. public void setPDFVersion(Version version) {
  59. if (super.version.compareTo(version) != 0) {
  60. throw new IllegalStateException("Cannot change the version of this PDF document.");
  61. }
  62. }
  63. @Override
  64. void addTableHeaderScopeAttribute(PDFStructElem th, Scope scope) {
  65. if (super.version.compareTo(Version.V1_4) > 0) {
  66. Scope.addScopeAttribute(th, scope);
  67. }
  68. }
  69. }
  70. /**
  71. * A class representing the version of a PDF document. This class allows the version to be
  72. * changed once it has been set (it is mutable) ONLY if the new version is greater. If the PDF
  73. * version is changed after it has been instantiated, the version will be set in the document
  74. * catalog.
  75. */
  76. private static final class DynamicVersion extends VersionController {
  77. private PDFDocument doc;
  78. private DynamicVersion(Version version, PDFDocument doc) {
  79. super(version);
  80. this.doc = doc;
  81. }
  82. @Override
  83. public void setPDFVersion(Version version) {
  84. if (super.version.compareTo(version) < 0) {
  85. super.version = version;
  86. doc.getRoot().setVersion(version);
  87. }
  88. }
  89. @Override
  90. void addTableHeaderScopeAttribute(PDFStructElem th, Scope scope) {
  91. setPDFVersion(Version.V1_5);
  92. Scope.addScopeAttribute(th, scope);
  93. }
  94. }
  95. /**
  96. * Returns a controller that disallows subsequent change to the document's version. The minimum
  97. * allowed version is v1.4.
  98. *
  99. * @param version the PDF version (must be &gt;= v1.4)
  100. * @return the fixed PDF version controller
  101. */
  102. public static VersionController getFixedVersionController(Version version) {
  103. if (version.compareTo(Version.V1_4) < 0) {
  104. throw new IllegalArgumentException("The PDF version cannot be set below version 1.4");
  105. }
  106. return new FixedVersion(version);
  107. }
  108. /**
  109. * Returns a controller that allows subsequent changes to the document's version.
  110. *
  111. * @param initialVersion the initial PDF version
  112. * @param doc the document whose version is being set
  113. * @return the dynamic PDF version controller
  114. */
  115. public static VersionController getDynamicVersionController(Version initialVersion,
  116. PDFDocument doc) {
  117. return new DynamicVersion(initialVersion, doc);
  118. }
  119. }