KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > pdf > PDFProfile


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
18 /* $Id$ */
19
20 package org.apache.fop.pdf;
21
22 import java.text.MessageFormat JavaDoc;
23
24 /**
25  * This class allows tracks the enabled PDF profiles (PDF/A and PDF/X) and provides methods to
26  * the libarary and its users to enable the generation of PDFs conforming to the enabled PDF
27  * profiles.
28  * <p>
29  * Some profile from PDF/X and PDF/A can be active simultaneously (example: PDF/A-1 and
30  * PDF/X-3:2003).
31  */

32 public class PDFProfile {
33
34     /**
35      * Indicates the PDF/A mode currently active. Defaults to "no restrictions", i.e.
36      * PDF/A not active.
37      */

38     protected PDFAMode pdfAMode = PDFAMode.DISABLED;
39     
40     /**
41      * Indicates the PDF/X mode currently active. Defaults to "no restrictions", i.e.
42      * PDF/X not active.
43      */

44     protected PDFXMode pdfXMode = PDFXMode.DISABLED;
45     
46     private PDFDocument doc;
47     
48     /**
49      * Main constructor
50      * @param doc the PDF document
51      */

52     public PDFProfile(PDFDocument doc) {
53         this.doc = doc;
54     }
55     
56     /**
57      * Validates if the requested profile combination is compatible.
58      */

59     protected void validateProfileCombination() {
60         if (pdfAMode != PDFAMode.DISABLED) {
61             if (pdfAMode == PDFAMode.PDFA_1A) {
62                 throw new UnsupportedOperationException JavaDoc("PDF/A-1a is not implemented, yet");
63             }
64             if (pdfAMode == PDFAMode.PDFA_1B) {
65                 if (pdfXMode != PDFXMode.DISABLED && pdfXMode != PDFXMode.PDFX_3_2003) {
66                     throw new PDFConformanceException(
67                             pdfAMode + " and " + pdfXMode + " are not compatible!");
68                 }
69             }
70         }
71     }
72     
73     /** @return the PDFDocument this profile is attached to */
74     public PDFDocument getDocument() {
75         return this.doc;
76     }
77     
78     /** @return the PDF/A mode */
79     public PDFAMode getPDFAMode() {
80         return this.pdfAMode;
81     }
82     
83     /** @return true if any PDF/A mode is active */
84     public boolean isPDFAActive() {
85         return getPDFAMode() != PDFAMode.DISABLED;
86     }
87     
88     /**
89      * Sets the PDF/A mode
90      * @param mode the PDF/A mode
91      */

92     public void setPDFAMode(PDFAMode mode) {
93         if (mode == null) {
94             mode = PDFAMode.DISABLED;
95         }
96         this.pdfAMode = mode;
97         validateProfileCombination();
98     }
99     
100     /** @return the PDF/X mode */
101     public PDFXMode getPDFXMode() {
102         return this.pdfXMode;
103     }
104     
105     /** @return true if any PDF/X mode is active */
106     public boolean isPDFXActive() {
107         return getPDFXMode() != PDFXMode.DISABLED;
108     }
109     
110     /**
111      * Sets the PDF/X mode
112      * @param mode the PDF/X mode
113      */

114     public void setPDFXMode(PDFXMode mode) {
115         if (mode == null) {
116             mode = PDFXMode.DISABLED;
117         }
118         this.pdfXMode = mode;
119         validateProfileCombination();
120     }
121
122     /** @see java.lang.Object#toString() */
123     public String JavaDoc toString() {
124         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
125         if (isPDFAActive() && isPDFXActive()) {
126             sb.append("[").append(getPDFAMode()).append(",").append(getPDFXMode()).append("]");
127         } else if (isPDFAActive()) {
128             sb.append(getPDFAMode());
129         } else if (isPDFXActive()) {
130             sb.append(getPDFXMode());
131         } else {
132             sb.append(super.toString());
133         }
134         return sb.toString();
135     }
136     
137     //---------=== Info and validation methods ===---------
138

139     private String JavaDoc format(String JavaDoc pattern, Object JavaDoc arg) {
140         return MessageFormat.format(pattern, new Object JavaDoc[] {arg});
141     }
142     
143     /** Checks if encryption is allowed. */
144     public void verifyEncryptionAllowed() {
145         final String JavaDoc err = "{0} doesn't allow encrypted PDFs";
146         if (isPDFAActive()) {
147             throw new PDFConformanceException(format(err, getPDFAMode()));
148         }
149         if (isPDFXActive()) {
150             throw new PDFConformanceException(format(err, getPDFXMode()));
151         }
152     }
153
154     /** Checks if PostScript XObjects are allowed. */
155     public void verifyPSXObjectsAllowed() {
156         final String JavaDoc err = "PostScript XObjects are prohibited when {0}"
157                 + " is active. Convert EPS graphics to another format.";
158         if (isPDFAActive()) {
159             throw new PDFConformanceException(format(err, getPDFAMode()));
160         }
161         if (isPDFXActive()) {
162             throw new PDFConformanceException(format(err, getPDFXMode()));
163         }
164     }
165
166     /**
167      * Checks if the use of transparency is allowed.
168      * @param context Context information for the user to identify the problem spot
169      */

170     public void verifyTransparencyAllowed(String JavaDoc context) {
171         final String JavaDoc err = "{0} does not allow the use of transparency. ({1})";
172         if (isPDFAActive()) {
173             throw new PDFConformanceException(MessageFormat.format(err,
174                     new Object JavaDoc[] {getPDFAMode(), context}));
175         }
176         if (isPDFXActive()) {
177             throw new PDFConformanceException(MessageFormat.format(err,
178                     new Object JavaDoc[] {getPDFXMode(), context}));
179         }
180     }
181
182     /** Checks if the right PDF version is set. */
183     public void verifyPDFVersion() {
184         final String JavaDoc err = "PDF version must be 1.4 for {0}";
185         if (getPDFAMode().isPDFA1LevelB()
186                 && getDocument().getPDFVersion() != PDFDocument.PDF_VERSION_1_4) {
187             throw new PDFConformanceException(format(err, getPDFAMode()));
188         }
189         if (getPDFXMode() == PDFXMode.PDFX_3_2003
190                 && getDocument().getPDFVersion() != PDFDocument.PDF_VERSION_1_4) {
191             throw new PDFConformanceException(format(err, getPDFXMode()));
192         }
193     }
194     
195     /** @return true if the ID entry must be present in the trailer. */
196     public boolean isIDEntryRequired() {
197         return isPDFAActive() || isPDFXActive();
198     }
199
200     /** @return true if all fonts need to be embedded. */
201     public boolean isFontEmbeddingRequired() {
202         return isPDFAActive() || isPDFXActive();
203     }
204
205     /** Checks if a title may be absent. */
206     public void verifyTitleAbsent() {
207         if (isPDFXActive()) {
208             final String JavaDoc err = "{0} requires the title to be set.";
209             throw new PDFConformanceException(format(err, getPDFXMode()));
210         }
211     }
212
213     /** @return true if the ModDate Info entry must be present. */
214     public boolean isModDateRequired() {
215         return getPDFXMode() == PDFXMode.PDFX_3_2003;
216     }
217
218     /** @return true if the Trapped Info entry must be present. */
219     public boolean isTrappedEntryRequired() {
220         return getPDFXMode() == PDFXMode.PDFX_3_2003;
221     }
222
223     /** @return true if annotations are allowed */
224     public boolean isAnnotationAllowed() {
225         return !isPDFXActive();
226     }
227     
228     /** Checks if annotations are allowed. */
229     public void verifyAnnotAllowed() {
230         if (!isAnnotationAllowed()) {
231             final String JavaDoc err = "{0} does not allow annotations inside the printable area.";
232             //Note: this rule is simplified. Refer to the standard for details.
233
throw new PDFConformanceException(format(err, getPDFXMode()));
234         }
235     }
236
237     /** Checks if Actions are allowed. */
238     public void verifyActionAllowed() {
239         if (isPDFXActive()) {
240             final String JavaDoc err = "{0} does not allow Actions.";
241             throw new PDFConformanceException(format(err, getPDFXMode()));
242         }
243     }
244
245 }
246
Popular Tags