1 /*
2 * Copyright (C) 2016 Hobrasoft s.r.o.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 package cz.hobrasoft.pdfmu.operation.version;
18
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21
22 /**
23 * Represents a PDF version
24 *
25 * <p>
26 * Versions 1.2 to 1.7 are supported. This range corresponds to the versions
27 * supported by iText, as seen in {@link com.itextpdf.text.pdf.PdfWriter} static
28 * fields {@link com.itextpdf.text.pdf.PdfWriter#VERSION_1_2} etc.
29 *
30 * @author <a href="mailto:filip.bartek@hobrasoft.cz">Filip Bartek</a>
31 */
32 public class PdfVersion implements Comparable<PdfVersion> {
33
34 private static final Pattern p = Pattern.compile("1\\.(?<charValue>[2-7])");
35
36 private char charValue;
37
38 /**
39 * Creates a PDF version from its string representation
40 *
41 * <p>
42 * Versions 1.2 to 1.7 are supported. Valid string representations include:
43 * <ul>
44 * <li>{@code 1.3}</li>
45 * <li>{@code 1.6}</li>
46 * </ul>
47 *
48 * @param stringValue string representation of the version
49 * @throws IllegalArgumentException if stringValue does not represent a
50 * valid version
51 */
52 public PdfVersion(String stringValue) throws IllegalArgumentException {
53 Matcher m = p.matcher(stringValue);
54 if (!m.matches()) {
55 throw new IllegalArgumentException("Invalid or unsupported PDF version; use 1.2 to 1.7");
56 }
57 String charValueString = m.group("charValue");
58 assert charValueString.length() == 1;
59 charValue = charValueString.charAt(0);
60 }
61
62 /**
63 * Creates a PDF version from the last character of its string
64 * representation
65 *
66 * <p>
67 * Since versions 1.2 to 1.7 are supported, a valid PDF version is uniquely
68 * specified by its last character (the digit 2 to 7).
69 *
70 * <p>
71 * Version in this format is returned by
72 * {@link com.itextpdf.text.pdf.PdfReader#getPdfVersion()}.
73 *
74 * @param charValue last character of the PDF version
75 * @throws IllegalArgumentException if charValue does not represent a valid
76 * version
77 */
78 public PdfVersion(char charValue) throws IllegalArgumentException {
79 if (charValue < '2' || charValue > '7') {
80 throw new IllegalArgumentException("Invalid or unsupported PDF version; use 2 to 7");
81 }
82 this.charValue = charValue;
83 }
84
85 @Override
86 public String toString() {
87 return String.format("1.%c", charValue);
88 }
89
90 /**
91 * Returns the last character of the PDF version
92 *
93 * <p>
94 * This format of PDF version is accepted for example by
95 * {@link com.itextpdf.text.pdf.PdfStamper#PdfStamper(PdfReader, OutputStream, char)}
96 *
97 * @return last character of the PDF version
98 */
99 public char toChar() {
100 return charValue;
101 }
102
103 @Override
104 public int compareTo(PdfVersion o) {
105 return charValue - o.charValue;
106 }
107
108 }