1 /*
2  * $Id: PdfVersionImp.java 3811 2009-03-23 18:15:13Z blowagie $
3  *
4  * Copyright 2006 Bruno Lowagie
5  *
6  * The contents of this file are subject to the Mozilla Public License Version 1.1
7  * (the "License"); you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the License.
13  *
14  * The Original Code is 'iText, a free JAVA-PDF library'.
15  *
16  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18  * All Rights Reserved.
19  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source code
23  * where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above.  If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34  *
35  * This library is free software; you can redistribute it and/or modify it
36  * under the terms of the MPL as stated above or under the terms of the GNU
37  * Library General Public License as published by the Free Software Foundation;
38  * either version 2 of the License, or any later version.
39  *
40  * This library is distributed in the hope that it will be useful, but WITHOUT
41  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43  * details.
44  *
45  * If you didn't download this code from the following link, you should check if
46  * you aren't using an obsolete version:
47  * http://www.lowagie.com/iText/
48  */

49
50 package com.lowagie.text.pdf.internal;
51
52 import java.io.IOException;
53
54 import com.lowagie.text.DocWriter;
55 import com.lowagie.text.pdf.OutputStreamCounter;
56 import com.lowagie.text.pdf.PdfDeveloperExtension;
57 import com.lowagie.text.pdf.PdfDictionary;
58 import com.lowagie.text.pdf.PdfName;
59 import com.lowagie.text.pdf.PdfWriter;
60 import com.lowagie.text.pdf.interfaces.PdfVersion;
61
62 /**
63  * Stores the PDF version information,
64  * knows how to write a PDF Header,
65  * and how to add the version to the catalog (if necessary).
66  */

67
68 public class PdfVersionImp implements PdfVersion {
69     
70     /** Contains different strings that are part of the header. */
71     public static final byte[][] HEADER = {
72         DocWriter.getISOBytes("\n"),
73         DocWriter.getISOBytes("%PDF-"),
74         DocWriter.getISOBytes("\n%\u00e2\u00e3\u00cf\u00d3\n")
75     };
76     
77     /** Indicates if the header was already written. */
78     protected boolean headerWasWritten = false;
79     /** Indicates if we are working in append mode. */
80     protected boolean appendmode = false;
81     /** The version that was or will be written to the header. */
82     protected char header_version = PdfWriter.VERSION_1_4;
83     /** The version that will be written to the catalog. */
84     protected PdfName catalog_version = null;
85     /**
86      * The extensions dictionary.
87      * @since    2.1.6
88      */

89     protected PdfDictionary extensions = null;
90     
91     /**
92      * @see com.lowagie.text.pdf.interfaces.PdfVersion#setPdfVersion(char)
93      */

94     public void setPdfVersion(char version) {
95         if (headerWasWritten || appendmode) {
96             setPdfVersion(getVersionAsName(version));
97         }
98         else {
99             this.header_version = version;
100         }
101     }
102     
103     /**
104      * @see com.lowagie.text.pdf.interfaces.PdfVersion#setAtLeastPdfVersion(char)
105      */

106     public void setAtLeastPdfVersion(char version) {
107         if (version > header_version) {
108             setPdfVersion(version);
109         }
110     }
111     
112     /**
113      * @see com.lowagie.text.pdf.interfaces.PdfVersion#setPdfVersion(com.lowagie.text.pdf.PdfName)
114      */

115     public void setPdfVersion(PdfName version) {
116         if (catalog_version == null || catalog_version.compareTo(version) < 0) {
117             this.catalog_version = version;
118         }
119     }
120     
121     /**
122      * Sets the append mode.
123      */

124     public void setAppendmode(boolean appendmode) {
125         this.appendmode = appendmode;
126     }
127     
128     /**
129      * Writes the header to the OutputStreamCounter.
130      * @throws IOException 
131      */

132     public void writeHeader(OutputStreamCounter os) throws IOException {
133         if (appendmode) {
134             os.write(HEADER[0]);
135         }
136         else {
137             os.write(HEADER[1]);
138             os.write(getVersionAsByteArray(header_version));
139             os.write(HEADER[2]);
140             headerWasWritten = true;
141         }
142     }
143     
144     /**
145      * Returns the PDF version as a name.
146      * @param version    the version character.
147      */

148     public PdfName getVersionAsName(char version) {
149         switch(version) {
150         case PdfWriter.VERSION_1_2:
151             return PdfWriter.PDF_VERSION_1_2;
152         case PdfWriter.VERSION_1_3:
153             return PdfWriter.PDF_VERSION_1_3;
154         case PdfWriter.VERSION_1_4:
155             return PdfWriter.PDF_VERSION_1_4;
156         case PdfWriter.VERSION_1_5:
157             return PdfWriter.PDF_VERSION_1_5;
158         case PdfWriter.VERSION_1_6:
159             return PdfWriter.PDF_VERSION_1_6;
160         case PdfWriter.VERSION_1_7:
161             return PdfWriter.PDF_VERSION_1_7;
162         default:
163             return PdfWriter.PDF_VERSION_1_4;
164         }
165     }
166     
167     /**
168      * Returns the version as a byte[].
169      * @param version the version character
170      */

171     public byte[] getVersionAsByteArray(char version) {
172         return DocWriter.getISOBytes(getVersionAsName(version).toString().substring(1));
173     }
174
175     /** Adds the version to the Catalog dictionary. */
176     public void addToCatalog(PdfDictionary catalog) {
177         if(catalog_version != null) {
178             catalog.put(PdfName.VERSION, catalog_version);
179         }
180         if (extensions != null) {
181             catalog.put(PdfName.EXTENSIONS, extensions);
182         }
183     }
184
185     /**
186      * @see com.lowagie.text.pdf.interfaces.PdfVersion#addDeveloperExtension(com.lowagie.text.pdf.PdfDeveloperExtension)
187      * @since    2.1.6
188      */

189     public void addDeveloperExtension(PdfDeveloperExtension de) {
190         if (extensions == null) {
191             extensions = new PdfDictionary();
192         }
193         else {
194             PdfDictionary extension = extensions.getAsDict(de.getPrefix());
195             if (extension != null) {
196                 int diff = de.getBaseversion().compareTo(extension.getAsName(PdfName.BASEVERSION));
197                 if (diff < 0)
198                     return;
199                 diff = de.getExtensionLevel() - extension.getAsNumber(PdfName.EXTENSIONLEVEL).intValue();
200                 if (diff <= 0)
201                     return;
202             }
203         }
204         extensions.put(de.getPrefix(), de.getDeveloperExtensions());
205     }
206 }