1 | /* | |
2 | * Copyright 2003 Paulo Soares | |
3 | * | |
4 | * The contents of this file are subject to the Mozilla Public License Version 1.1 | |
5 | * (the "License"); you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at http://www.mozilla.org/MPL/ | |
7 | * | |
8 | * Software distributed under the License is distributed on an "AS IS" basis, | |
9 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
10 | * for the specific language governing rights and limitations under the License. | |
11 | * | |
12 | * The Original Code is 'iText, a free JAVA-PDF library'. | |
13 | * | |
14 | * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by | |
15 | * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. | |
16 | * All Rights Reserved. | |
17 | * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer | |
18 | * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. | |
19 | * | |
20 | * Contributor(s): all the names of the contributors are added in the source code | |
21 | * where applicable. | |
22 | * | |
23 | * Alternatively, the contents of this file may be used under the terms of the | |
24 | * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the | |
25 | * provisions of LGPL are applicable instead of those above. If you wish to | |
26 | * allow use of your version of this file only under the terms of the LGPL | |
27 | * License and not to allow others to use your version of this file under | |
28 | * the MPL, indicate your decision by deleting the provisions above and | |
29 | * replace them with the notice and other provisions required by the LGPL. | |
30 | * If you do not delete the provisions above, a recipient may use your version | |
31 | * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. | |
32 | * | |
33 | * This library is free software; you can redistribute it and/or modify it | |
34 | * under the terms of the MPL as stated above or under the terms of the GNU | |
35 | * Library General Public License as published by the Free Software Foundation; | |
36 | * either version 2 of the License, or any later version. | |
37 | * | |
38 | * This library is distributed in the hope that it will be useful, but WITHOUT | |
39 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
40 | * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more | |
41 | * details. | |
42 | * | |
43 | * If you didn't download this code from the following link, you should check if | |
44 | * you aren't using an obsolete version: | |
45 | * http://www.lowagie.com/iText/ | |
46 | */ | |
47 | ||
48 | package com.lowagie.text.pdf; | |
49 | ||
50 | import java.io.File; | |
51 | import java.io.FileInputStream; | |
52 | import java.io.IOException; | |
53 | import java.io.InputStream; | |
54 | import java.net.URL; | |
55 | import com.lowagie.text.error_messages.MessageLocalization; | |
56 | ||
57 | import com.lowagie.text.pdf.collection.PdfCollectionItem; | |
58 | /** Specifies a file or an URL. The file can be extern or embedded. | |
59 | * | |
60 | * @author Paulo Soares (psoares@consiste.pt) | |
61 | */ | |
62 | public class PdfFileSpecification extends PdfDictionary { | |
63 | protected PdfWriter writer; | |
64 | protected PdfIndirectReference ref; | |
65 | | |
66 | /** Creates a new instance of PdfFileSpecification. The static methods are preferred. */ | |
67 | public PdfFileSpecification() { | |
68 | super(PdfName.FILESPEC); | |
69 | } | |
70 | | |
71 | /** | |
72 | * Creates a file specification of type URL. | |
73 | * @param writer the <CODE>PdfWriter</CODE> | |
74 | * @param url the URL | |
75 | * @return the file specification | |
76 | */ | |
77 | public static PdfFileSpecification url(PdfWriter writer, String url) { | |
78 | PdfFileSpecification fs = new PdfFileSpecification(); | |
79 | fs.writer = writer; | |
80 |
1
1. url : removed call to com/lowagie/text/pdf/PdfFileSpecification::put → NO_COVERAGE |
fs.put(PdfName.FS, PdfName.URL); |
81 |
1
1. url : removed call to com/lowagie/text/pdf/PdfFileSpecification::put → NO_COVERAGE |
fs.put(PdfName.F, new PdfString(url)); |
82 |
1
1. url : mutated return of Object value for com/lowagie/text/pdf/PdfFileSpecification::url to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return fs; |
83 | } | |
84 | ||
85 | /** | |
86 | * Creates a file specification with the file embedded. The file may | |
87 | * come from the file system or from a byte array. The data is flate compressed. | |
88 | * @param writer the <CODE>PdfWriter</CODE> | |
89 | * @param filePath the file path | |
90 | * @param fileDisplay the file information that is presented to the user | |
91 | * @param fileStore the byte array with the file. If it is not <CODE>null</CODE> | |
92 | * it takes precedence over <CODE>filePath</CODE> | |
93 | * @throws IOException on error | |
94 | * @return the file specification | |
95 | */ | |
96 | public static PdfFileSpecification fileEmbedded(PdfWriter writer, String filePath, String fileDisplay, byte[] fileStore) throws IOException { | |
97 |
1
1. fileEmbedded : mutated return of Object value for com/lowagie/text/pdf/PdfFileSpecification::fileEmbedded to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return fileEmbedded(writer, filePath, fileDisplay, fileStore, PdfStream.BEST_COMPRESSION); |
98 | } | |
99 | ||
100 | /** | |
101 | * Creates a file specification with the file embedded. The file may | |
102 | * come from the file system or from a byte array. The data is flate compressed. | |
103 | * @param writer the <CODE>PdfWriter</CODE> | |
104 | * @param filePath the file path | |
105 | * @param fileDisplay the file information that is presented to the user | |
106 | * @param fileStore the byte array with the file. If it is not <CODE>null</CODE> | |
107 | * it takes precedence over <CODE>filePath</CODE> | |
108 | * @param compressionLevel the compression level to be used for compressing the file | |
109 | * it takes precedence over <CODE>filePath</CODE> | |
110 | * @throws IOException on error | |
111 | * @return the file specification | |
112 | * @since 2.1.3 | |
113 | */ | |
114 | public static PdfFileSpecification fileEmbedded(PdfWriter writer, String filePath, String fileDisplay, byte[] fileStore, int compressionLevel) throws IOException { | |
115 |
1
1. fileEmbedded : mutated return of Object value for com/lowagie/text/pdf/PdfFileSpecification::fileEmbedded to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return fileEmbedded(writer, filePath, fileDisplay, fileStore, null, null, compressionLevel); |
116 | } | |
117 | | |
118 | | |
119 | /** | |
120 | * Creates a file specification with the file embedded. The file may | |
121 | * come from the file system or from a byte array. | |
122 | * @param writer the <CODE>PdfWriter</CODE> | |
123 | * @param filePath the file path | |
124 | * @param fileDisplay the file information that is presented to the user | |
125 | * @param fileStore the byte array with the file. If it is not <CODE>null</CODE> | |
126 | * it takes precedence over <CODE>filePath</CODE> | |
127 | * @param compress sets the compression on the data. Multimedia content will benefit little | |
128 | * from compression | |
129 | * @throws IOException on error | |
130 | * @return the file specification | |
131 | */ | |
132 | public static PdfFileSpecification fileEmbedded(PdfWriter writer, String filePath, String fileDisplay, byte[] fileStore, boolean compress) throws IOException { | |
133 |
2
1. fileEmbedded : negated conditional → NO_COVERAGE 2. fileEmbedded : mutated return of Object value for com/lowagie/text/pdf/PdfFileSpecification::fileEmbedded to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return fileEmbedded(writer, filePath, fileDisplay, fileStore, null, null, compress ? PdfStream.BEST_COMPRESSION : PdfStream.NO_COMPRESSION); |
134 | } | |
135 | | |
136 | /** | |
137 | * Creates a file specification with the file embedded. The file may | |
138 | * come from the file system or from a byte array. | |
139 | * @param writer the <CODE>PdfWriter</CODE> | |
140 | * @param filePath the file path | |
141 | * @param fileDisplay the file information that is presented to the user | |
142 | * @param fileStore the byte array with the file. If it is not <CODE>null</CODE> | |
143 | * it takes precedence over <CODE>filePath</CODE> | |
144 | * @param compress sets the compression on the data. Multimedia content will benefit little | |
145 | * from compression | |
146 | * @param mimeType the optional mimeType | |
147 | * @param fileParameter the optional extra file parameters such as the creation or modification date | |
148 | * @throws IOException on error | |
149 | * @return the file specification | |
150 | */ | |
151 | public static PdfFileSpecification fileEmbedded(PdfWriter writer, String filePath, String fileDisplay, byte[] fileStore, boolean compress, String mimeType, PdfDictionary fileParameter) throws IOException { | |
152 |
2
1. fileEmbedded : negated conditional → NO_COVERAGE 2. fileEmbedded : mutated return of Object value for com/lowagie/text/pdf/PdfFileSpecification::fileEmbedded to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return fileEmbedded(writer, filePath, fileDisplay, fileStore, mimeType, fileParameter, compress ? PdfStream.BEST_COMPRESSION : PdfStream.NO_COMPRESSION); |
153 | } | |
154 | | |
155 | /** | |
156 | * Creates a file specification with the file embedded. The file may | |
157 | * come from the file system or from a byte array. | |
158 | * @param writer the <CODE>PdfWriter</CODE> | |
159 | * @param filePath the file path | |
160 | * @param fileDisplay the file information that is presented to the user | |
161 | * @param fileStore the byte array with the file. If it is not <CODE>null</CODE> | |
162 | * it takes precedence over <CODE>filePath</CODE> | |
163 | * @param mimeType the optional mimeType | |
164 | * @param fileParameter the optional extra file parameters such as the creation or modification date | |
165 | * @param compressionLevel the level of compression | |
166 | * @throws IOException on error | |
167 | * @return the file specification | |
168 | * @since 2.1.3 | |
169 | */ | |
170 | public static PdfFileSpecification fileEmbedded(PdfWriter writer, String filePath, String fileDisplay, byte[] fileStore, String mimeType, PdfDictionary fileParameter, int compressionLevel) throws IOException { | |
171 | PdfFileSpecification fs = new PdfFileSpecification(); | |
172 | fs.writer = writer; | |
173 |
1
1. fileEmbedded : removed call to com/lowagie/text/pdf/PdfFileSpecification::put → NO_COVERAGE |
fs.put(PdfName.F, new PdfString(fileDisplay)); |
174 |
1
1. fileEmbedded : removed call to com/lowagie/text/pdf/PdfFileSpecification::setUnicodeFileName → NO_COVERAGE |
fs.setUnicodeFileName(fileDisplay, false); |
175 | PdfEFStream stream; | |
176 | InputStream in = null; | |
177 | PdfIndirectReference ref; | |
178 | PdfIndirectReference refFileLength = null; | |
179 | try { | |
180 |
1
1. fileEmbedded : negated conditional → NO_COVERAGE |
if (fileStore == null) { |
181 | refFileLength = writer.getPdfIndirectReference(); | |
182 | File file = new File(filePath); | |
183 |
1
1. fileEmbedded : negated conditional → NO_COVERAGE |
if (file.canRead()) { |
184 | in = new FileInputStream(filePath); | |
185 | } | |
186 | else { | |
187 |
4
1. fileEmbedded : negated conditional → NO_COVERAGE 2. fileEmbedded : negated conditional → NO_COVERAGE 3. fileEmbedded : negated conditional → NO_COVERAGE 4. fileEmbedded : negated conditional → NO_COVERAGE |
if (filePath.startsWith("file:/") || filePath.startsWith("http://") || filePath.startsWith("https://") || filePath.startsWith("jar:")) { |
188 | in = new URL(filePath).openStream(); | |
189 | } | |
190 | else { | |
191 | in = BaseFont.getResourceStream(filePath); | |
192 |
1
1. fileEmbedded : negated conditional → NO_COVERAGE |
if (in == null) |
193 | throw new IOException(MessageLocalization.getComposedMessage("1.not.found.as.file.or.resource", filePath)); | |
194 | } | |
195 | } | |
196 | stream = new PdfEFStream(in, writer); | |
197 | } | |
198 | else { | |
199 | stream = new PdfEFStream(fileStore); | |
200 | } | |
201 |
1
1. fileEmbedded : removed call to com/lowagie/text/pdf/PdfEFStream::put → NO_COVERAGE |
stream.put(PdfName.TYPE, PdfName.EMBEDDEDFILE); |
202 |
1
1. fileEmbedded : removed call to com/lowagie/text/pdf/PdfEFStream::flateCompress → NO_COVERAGE |
stream.flateCompress(compressionLevel); |
203 | PdfDictionary param = new PdfDictionary(); | |
204 |
1
1. fileEmbedded : negated conditional → NO_COVERAGE |
if (fileParameter != null) { |
205 |
1
1. fileEmbedded : removed call to com/lowagie/text/pdf/PdfDictionary::merge → NO_COVERAGE |
param.merge(fileParameter); |
206 | } | |
207 | ||
208 |
1
1. fileEmbedded : negated conditional → NO_COVERAGE |
if (fileStore != null) { |
209 |
1
1. fileEmbedded : removed call to com/lowagie/text/pdf/PdfDictionary::put → NO_COVERAGE |
param.put(PdfName.SIZE, new PdfNumber(stream.getRawLength())); |
210 |
1
1. fileEmbedded : removed call to com/lowagie/text/pdf/PdfEFStream::put → NO_COVERAGE |
stream.put(PdfName.PARAMS, param); |
211 | } | |
212 | else | |
213 |
1
1. fileEmbedded : removed call to com/lowagie/text/pdf/PdfEFStream::put → NO_COVERAGE |
stream.put(PdfName.PARAMS, refFileLength); |
214 | ||
215 |
1
1. fileEmbedded : negated conditional → NO_COVERAGE |
if (mimeType != null) |
216 |
1
1. fileEmbedded : removed call to com/lowagie/text/pdf/PdfEFStream::put → NO_COVERAGE |
stream.put(PdfName.SUBTYPE, new PdfName(mimeType)); |
217 | ||
218 | ref = writer.addToBody(stream).getIndirectReference(); | |
219 |
1
1. fileEmbedded : negated conditional → NO_COVERAGE |
if (fileStore == null) { |
220 |
1
1. fileEmbedded : removed call to com/lowagie/text/pdf/PdfEFStream::writeLength → NO_COVERAGE |
stream.writeLength(); |
221 |
1
1. fileEmbedded : removed call to com/lowagie/text/pdf/PdfDictionary::put → NO_COVERAGE |
param.put(PdfName.SIZE, new PdfNumber(stream.getRawLength())); |
222 | writer.addToBody(param, refFileLength); | |
223 | } | |
224 | } | |
225 | finally { | |
226 |
1
1. fileEmbedded : negated conditional → NO_COVERAGE |
if (in != null) |
227 |
1
1. fileEmbedded : removed call to java/io/InputStream::close → NO_COVERAGE |
try{in.close();}catch(Exception e){} |
228 | } | |
229 | PdfDictionary f = new PdfDictionary(); | |
230 |
1
1. fileEmbedded : removed call to com/lowagie/text/pdf/PdfDictionary::put → NO_COVERAGE |
f.put(PdfName.F, ref); |
231 |
1
1. fileEmbedded : removed call to com/lowagie/text/pdf/PdfDictionary::put → NO_COVERAGE |
f.put(PdfName.UF, ref); |
232 |
1
1. fileEmbedded : removed call to com/lowagie/text/pdf/PdfFileSpecification::put → NO_COVERAGE |
fs.put(PdfName.EF, f); |
233 |
1
1. fileEmbedded : mutated return of Object value for com/lowagie/text/pdf/PdfFileSpecification::fileEmbedded to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return fs; |
234 | } | |
235 | | |
236 | /** | |
237 | * Creates a file specification for an external file. | |
238 | * @param writer the <CODE>PdfWriter</CODE> | |
239 | * @param filePath the file path | |
240 | * @return the file specification | |
241 | */ | |
242 | public static PdfFileSpecification fileExtern(PdfWriter writer, String filePath) { | |
243 | PdfFileSpecification fs = new PdfFileSpecification(); | |
244 | fs.writer = writer; | |
245 |
1
1. fileExtern : removed call to com/lowagie/text/pdf/PdfFileSpecification::put → NO_COVERAGE |
fs.put(PdfName.F, new PdfString(filePath)); |
246 |
1
1. fileExtern : removed call to com/lowagie/text/pdf/PdfFileSpecification::setUnicodeFileName → NO_COVERAGE |
fs.setUnicodeFileName(filePath, false); |
247 |
1
1. fileExtern : mutated return of Object value for com/lowagie/text/pdf/PdfFileSpecification::fileExtern to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return fs; |
248 | } | |
249 | | |
250 | /** | |
251 | * Gets the indirect reference to this file specification. | |
252 | * Multiple invocations will retrieve the same value. | |
253 | * @throws IOException on error | |
254 | * @return the indirect reference | |
255 | */ | |
256 | public PdfIndirectReference getReference() throws IOException { | |
257 |
1
1. getReference : negated conditional → NO_COVERAGE |
if (ref != null) |
258 |
1
1. getReference : mutated return of Object value for com/lowagie/text/pdf/PdfFileSpecification::getReference to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return ref; |
259 | ref = writer.addToBody(this).getIndirectReference(); | |
260 |
1
1. getReference : mutated return of Object value for com/lowagie/text/pdf/PdfFileSpecification::getReference to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return ref; |
261 | } | |
262 | | |
263 | /** | |
264 | * Sets the file name (the key /F) string as an hex representation | |
265 | * to support multi byte file names. The name must have the slash and | |
266 | * backslash escaped according to the file specification rules | |
267 | * @param fileName the file name as a byte array | |
268 | */ | |
269 | public void setMultiByteFileName(byte[] fileName) { | |
270 |
1
1. setMultiByteFileName : removed call to com/lowagie/text/pdf/PdfFileSpecification::put → NO_COVERAGE |
put(PdfName.F, new PdfString(fileName).setHexWriting(true)); |
271 | } | |
272 | | |
273 | /** | |
274 | * Adds the unicode file name (the key /UF). This entry was introduced | |
275 | * in PDF 1.7. The filename must have the slash and backslash escaped | |
276 | * according to the file specification rules. | |
277 | * @param filename the filename | |
278 | * @param unicode if true, the filename is UTF-16BE encoded; otherwise PDFDocEncoding is used; | |
279 | */ | |
280 | public void setUnicodeFileName(String filename, boolean unicode) { | |
281 |
2
1. setUnicodeFileName : negated conditional → NO_COVERAGE 2. setUnicodeFileName : removed call to com/lowagie/text/pdf/PdfFileSpecification::put → NO_COVERAGE |
put(PdfName.UF, new PdfString(filename, unicode ? PdfObject.TEXT_UNICODE : PdfObject.TEXT_PDFDOCENCODING)); |
282 | } | |
283 | | |
284 | /** | |
285 | * Sets a flag that indicates whether an external file referenced by the file | |
286 | * specification is volatile. If the value is true, applications should never | |
287 | * cache a copy of the file. | |
288 | * @param volatile_file if true, the external file should not be cached | |
289 | */ | |
290 | public void setVolatile(boolean volatile_file) { | |
291 |
1
1. setVolatile : removed call to com/lowagie/text/pdf/PdfFileSpecification::put → NO_COVERAGE |
put(PdfName.V, new PdfBoolean(volatile_file)); |
292 | } | |
293 | | |
294 | /** | |
295 | * Adds a description for the file that is specified here. | |
296 | * @param description some text | |
297 | * @param unicode if true, the text is added as a unicode string | |
298 | */ | |
299 | public void addDescription(String description, boolean unicode) { | |
300 |
2
1. addDescription : negated conditional → NO_COVERAGE 2. addDescription : removed call to com/lowagie/text/pdf/PdfFileSpecification::put → NO_COVERAGE |
put(PdfName.DESC, new PdfString(description, unicode ? PdfObject.TEXT_UNICODE : PdfObject.TEXT_PDFDOCENCODING)); |
301 | } | |
302 | | |
303 | /** | |
304 | * Adds the Collection item dictionary. | |
305 | */ | |
306 | public void addCollectionItem(PdfCollectionItem ci) { | |
307 |
1
1. addCollectionItem : removed call to com/lowagie/text/pdf/PdfFileSpecification::put → NO_COVERAGE |
put(PdfName.CI, ci); |
308 | } | |
309 | } | |
Mutations | ||
80 |
1.1 |
|
81 |
1.1 |
|
82 |
1.1 |
|
97 |
1.1 |
|
115 |
1.1 |
|
133 |
1.1 2.2 |
|
152 |
1.1 2.2 |
|
173 |
1.1 |
|
174 |
1.1 |
|
180 |
1.1 |
|
183 |
1.1 |
|
187 |
1.1 2.2 3.3 4.4 |
|
192 |
1.1 |
|
201 |
1.1 |
|
202 |
1.1 |
|
204 |
1.1 |
|
205 |
1.1 |
|
208 |
1.1 |
|
209 |
1.1 |
|
210 |
1.1 |
|
213 |
1.1 |
|
215 |
1.1 |
|
216 |
1.1 |
|
219 |
1.1 |
|
220 |
1.1 |
|
221 |
1.1 |
|
226 |
1.1 |
|
227 |
1.1 |
|
230 |
1.1 |
|
231 |
1.1 |
|
232 |
1.1 |
|
233 |
1.1 |
|
245 |
1.1 |
|
246 |
1.1 |
|
247 |
1.1 |
|
257 |
1.1 |
|
258 |
1.1 |
|
260 |
1.1 |
|
270 |
1.1 |
|
281 |
1.1 2.2 |
|
291 |
1.1 |
|
300 |
1.1 2.2 |
|
307 |
1.1 |