001/* 002 * Copyright (C) 2016 Hobrasoft s.r.o. 003 * 004 * This program is free software: you can redistribute it and/or modify 005 * it under the terms of the GNU Affero General Public License as published by 006 * the Free Software Foundation, either version 3 of the License, or 007 * (at your option) any later version. 008 * 009 * This program is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 * GNU Affero General Public License for more details. 013 * 014 * You should have received a copy of the GNU Affero General Public License 015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 016 */ 017package cz.hobrasoft.pdfmu.operation.version; 018 019import java.util.regex.Matcher; 020import java.util.regex.Pattern; 021 022/** 023 * Represents a PDF version 024 * 025 * <p> 026 * Versions 1.2 to 1.7 are supported. This range corresponds to the versions 027 * supported by iText, as seen in {@link com.itextpdf.text.pdf.PdfWriter} static 028 * fields {@link com.itextpdf.text.pdf.PdfWriter#VERSION_1_2} etc. 029 * 030 * @author <a href="mailto:filip.bartek@hobrasoft.cz">Filip Bartek</a> 031 */ 032public class PdfVersion implements Comparable<PdfVersion> { 033 034 private static final Pattern p = Pattern.compile("1\\.(?<charValue>[2-7])"); 035 036 private char charValue; 037 038 /** 039 * Creates a PDF version from its string representation 040 * 041 * <p> 042 * Versions 1.2 to 1.7 are supported. Valid string representations include: 043 * <ul> 044 * <li>{@code 1.3}</li> 045 * <li>{@code 1.6}</li> 046 * </ul> 047 * 048 * @param stringValue string representation of the version 049 * @throws IllegalArgumentException if stringValue does not represent a 050 * valid version 051 */ 052 public PdfVersion(String stringValue) throws IllegalArgumentException { 053 Matcher m = p.matcher(stringValue); 054 if (!m.matches()) { 055 throw new IllegalArgumentException("Invalid or unsupported PDF version; use 1.2 to 1.7"); 056 } 057 String charValueString = m.group("charValue"); 058 assert charValueString.length() == 1; 059 charValue = charValueString.charAt(0); 060 } 061 062 /** 063 * Creates a PDF version from the last character of its string 064 * representation 065 * 066 * <p> 067 * Since versions 1.2 to 1.7 are supported, a valid PDF version is uniquely 068 * specified by its last character (the digit 2 to 7). 069 * 070 * <p> 071 * Version in this format is returned by 072 * {@link com.itextpdf.text.pdf.PdfReader#getPdfVersion()}. 073 * 074 * @param charValue last character of the PDF version 075 * @throws IllegalArgumentException if charValue does not represent a valid 076 * version 077 */ 078 public PdfVersion(char charValue) throws IllegalArgumentException { 079 if (charValue < '2' || charValue > '7') { 080 throw new IllegalArgumentException("Invalid or unsupported PDF version; use 2 to 7"); 081 } 082 this.charValue = charValue; 083 } 084 085 @Override 086 public String toString() { 087 return String.format("1.%c", charValue); 088 } 089 090 /** 091 * Returns the last character of the PDF version 092 * 093 * <p> 094 * This format of PDF version is accepted for example by 095 * {@link com.itextpdf.text.pdf.PdfStamper#PdfStamper(PdfReader, OutputStream, char)} 096 * 097 * @return last character of the PDF version 098 */ 099 public char toChar() { 100 return charValue; 101 } 102 103 @Override 104 public int compareTo(PdfVersion o) { 105 return charValue - o.charValue; 106 } 107 108}