選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

VersionController.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. throw new IllegalStateException("Cannot change the version of this PDF document.");
  60. }
  61. @Override
  62. void addTableHeaderScopeAttribute(PDFStructElem th, Scope scope) {
  63. if (super.version.compareTo(Version.V1_4) > 0) {
  64. Scope.addScopeAttribute(th, scope);
  65. }
  66. }
  67. }
  68. /**
  69. * A class representing the version of a PDF document. This class allows the version to be
  70. * changed once it has been set (it is mutable) ONLY if the new version is greater. If the PDF
  71. * version is changed after it has been instantiated, the version will be set in the document
  72. * catalog.
  73. */
  74. private static final class DynamicVersion extends VersionController {
  75. private PDFDocument doc;
  76. private DynamicVersion(Version version, PDFDocument doc) {
  77. super(version);
  78. this.doc = doc;
  79. }
  80. @Override
  81. public void setPDFVersion(Version version) {
  82. if (super.version.compareTo(version) < 0) {
  83. super.version = version;
  84. doc.getRoot().setVersion(version);
  85. }
  86. }
  87. @Override
  88. void addTableHeaderScopeAttribute(PDFStructElem th, Scope scope) {
  89. setPDFVersion(Version.V1_5);
  90. Scope.addScopeAttribute(th, scope);
  91. }
  92. }
  93. /**
  94. * Returns a controller that disallows subsequent change to the document's version. The minimum
  95. * allowed version is v1.4.
  96. *
  97. * @param version the PDF version (must be &gt;= v1.4)
  98. * @return the fixed PDF version controller
  99. */
  100. public static VersionController getFixedVersionController(Version version) {
  101. if (version.compareTo(Version.V1_4) < 0) {
  102. throw new IllegalArgumentException("The PDF version cannot be set below version 1.4");
  103. }
  104. return new FixedVersion(version);
  105. }
  106. /**
  107. * Returns a controller that allows subsequent changes to the document's version.
  108. *
  109. * @param initialVersion the initial PDF version
  110. * @param doc the document whose version is being set
  111. * @return the dynamic PDF version controller
  112. */
  113. public static VersionController getDynamicVersionController(Version initialVersion,
  114. PDFDocument doc) {
  115. return new DynamicVersion(initialVersion, doc);
  116. }
  117. }