Skip to content

Commit cfd6418

Browse files
committed
Make exception messages more verbose and user-friendly. Minor fixes in PdfException constants.
DEVSIX-622
1 parent 227e30f commit cfd6418

File tree

15 files changed

+42
-40
lines changed

15 files changed

+42
-40
lines changed

io/src/main/java/com/itextpdf/io/codec/Jbig2SegmentReader.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ void readSegment(Jbig2Segment s) throws java.io.IOException {
247247
ra.seek(last);
248248
Jbig2Page p = pages.get(s.page);
249249
if (p == null) {
250-
throw new com.itextpdf.io.IOException("referring.to.widht.height.of.page.we.havent.seen.yet.1").setMessageParams(s.page);
250+
throw new com.itextpdf.io.IOException("Referring to widht or height of a page we haven't seen yet: {0}").setMessageParams(s.page);
251251
}
252252

253253
p.pageBitmapWidth = page_bitmap_width;
@@ -300,7 +300,7 @@ Jbig2Segment readHeader() throws java.io.IOException {
300300
}
301301

302302
} else if (count_of_referred_to_segments == 5 || count_of_referred_to_segments == 6) {
303-
throw new com.itextpdf.io.IOException("count.of.referred.to.segments.had.bad.value.in.header.for.segment.1.starting.at.2")
303+
throw new com.itextpdf.io.IOException("Count of referred-to segments has forbidden value in the header for segment {0} starting at {1}")
304304
.setMessageParams(segment_number, ptr);
305305

306306
}
@@ -329,7 +329,7 @@ Jbig2Segment readHeader() throws java.io.IOException {
329329
segment_page_association = ra.read();
330330
}
331331
if (segment_page_association < 0) {
332-
throw new com.itextpdf.io.IOException("page.1.invalid.for.segment.2.starting.at.3")
332+
throw new com.itextpdf.io.IOException("Page {0} is invalid for segment {1} starting at {2}")
333333
.setMessageParams(segment_page_association, segment_number, ptr);
334334
}
335335
s.page = segment_page_association;
@@ -369,7 +369,7 @@ void readFileHeader() throws java.io.IOException {
369369

370370
for (int i = 0; i < idstring.length; i++) {
371371
if (idstring[i] != refidstring[i]) {
372-
throw new com.itextpdf.io.IOException("file.header.idstring.not.good.at.byte.1").setMessageParams(i);
372+
throw new com.itextpdf.io.IOException("File header idstring is not good at byte {0}").setMessageParams(i);
373373
}
374374
}
375375

@@ -379,7 +379,7 @@ void readFileHeader() throws java.io.IOException {
379379
this.number_of_pages_known = (fileheaderflags & 0x2) == 0x0;
380380

381381
if ((fileheaderflags & 0xfc) != 0x0) {
382-
throw new com.itextpdf.io.IOException("file.header.flags.bits.2.7.not.0");
382+
throw new com.itextpdf.io.IOException("File header flags bits from 2 to 7 should be 0, some not");
383383
}
384384

385385
if (this.number_of_pages_known) {

io/src/main/java/com/itextpdf/io/font/CidFont.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class CidFont extends FontProgram {
6565
initializeCidFontNameAndStyle(fontName);
6666
Map<String, Object> fontDesc = CidFontProperties.getAllFonts().get(fontNames.getFontName());
6767
if (fontDesc == null) {
68-
throw new IOException("no.such.predefined.font.1").setMessageParams(fontName);
68+
throw new IOException("There is no such predefined font: {0}").setMessageParams(fontName);
6969
}
7070
initializeCidFontProperties(fontDesc);
7171
}

io/src/main/java/com/itextpdf/io/font/OpenTypeParser.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -393,27 +393,27 @@ private void initializeSfntTables() throws java.io.IOException {
393393
int dirIdx = ttcIndex;
394394
if (dirIdx < 0) {
395395
if (fileName != null) {
396-
throw new IOException("the.font.index.for.1.must.be.positive").setMessageParams(fileName);
396+
throw new IOException("The font index for {0} must be positive.").setMessageParams(fileName);
397397
} else {
398-
throw new IOException("the.font.index.must.be.positive");
398+
throw new IOException("The font index must be positive.");
399399
}
400400
}
401401
String mainTag = readStandardString(4);
402402
if (!mainTag.equals("ttcf")) {
403403
if (fileName != null) {
404-
throw new IOException("1.is.not.a.valid.ttc.file").setMessageParams(fileName);
404+
throw new IOException("{0} is not a valid ttc file.").setMessageParams(fileName);
405405
} else {
406-
throw new IOException("not.a.valid.ttc.file");
406+
throw new IOException("Not a valid ttc file.");
407407
}
408408
}
409409
raf.skipBytes(4);
410410
int dirCount = raf.readInt();
411411
if (dirIdx >= dirCount) {
412412
if (fileName != null) {
413-
throw new IOException("the.font.index.for.1.must.be.between.0.and.2.it.was.3")
413+
throw new IOException("The font index for {0} must be between 0 and {1}. It is {2}.")
414414
.setMessageParams(fileName, dirCount - 1, dirIdx);
415415
} else {
416-
throw new IOException("the.font.index.must.be.between.0.and.1.it.was.2")
416+
throw new IOException("The font index must be between 0 and {0}. It is {1}.")
417417
.setMessageParams(dirCount - 1, dirIdx);
418418
}
419419
}
@@ -424,9 +424,9 @@ private void initializeSfntTables() throws java.io.IOException {
424424
int ttId = raf.readInt();
425425
if (ttId != 0x00010000 && ttId != 0x4F54544F) {
426426
if (fileName != null) {
427-
throw new IOException("1.is.not.a.valid.ttf.or.otf.file").setMessageParams(fileName);
427+
throw new IOException("{0} is not a valid ttf or otf file.").setMessageParams(fileName);
428428
} else {
429-
throw new IOException("not.a.valid.ttf.or.otf.file");
429+
throw new IOException("Not a valid ttf or otf file.");
430430
}
431431
}
432432
int num_tables = raf.readUnsignedShort();

io/src/main/java/com/itextpdf/io/font/Type1Font.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected static Type1Font createStandardFont(String name) throws java.io.IOExce
8282
if (StandardFonts.isStandardFont(name)) {
8383
return new Type1Font(name, null, null, null);
8484
} else {
85-
throw new IOException("1.is.not.a.standard.type1.font").setMessageParams(name);
85+
throw new IOException("{0} is not a standard type1 font.").setMessageParams(name);
8686
}
8787
}
8888

@@ -313,9 +313,9 @@ protected void process() throws java.io.IOException {
313313
if (!startKernPairs) {
314314
String metricsPath = fontParser.getAfmPath();
315315
if (metricsPath != null) {
316-
throw new IOException("missing.startcharmetrics.in.1").setMessageParams(metricsPath);
316+
throw new IOException("startcharmetrics is missing in {0}.").setMessageParams(metricsPath);
317317
} else {
318-
throw new IOException("missing.startcharmetrics.in.the.metrics.file");
318+
throw new IOException("startcharmetrics is missing in the metrics file.");
319319
}
320320
}
321321
avgWidth = 0;
@@ -378,9 +378,9 @@ protected void process() throws java.io.IOException {
378378
if (startKernPairs) {
379379
String metricsPath = fontParser.getAfmPath();
380380
if (metricsPath != null) {
381-
throw new IOException("missing.endcharmetrics.in.1").setMessageParams(metricsPath);
381+
throw new IOException("endcharmetrics is missing in {0}.").setMessageParams(metricsPath);
382382
} else {
383-
throw new IOException("missing.endcharmetrics.in.the.metrics.file");
383+
throw new IOException("endcharmetrics is missing in the metrics file.");
384384
}
385385
}
386386

@@ -435,18 +435,18 @@ long record = ((long)firstUni << 32) + secondUni;
435435
} else if (!endOfMetrics) {
436436
String metricsPath = fontParser.getAfmPath();
437437
if (metricsPath != null) {
438-
throw new IOException("missing.endfontmetrics.in.1").setMessageParams(metricsPath);
438+
throw new IOException("endfontmetrics is missing in {0}.").setMessageParams(metricsPath);
439439
} else {
440-
throw new IOException("missing.endfontmetrics.in.the.metrics.file");
440+
throw new IOException("endfontmetrics is missing in the metrics file.");
441441
}
442442
}
443443

444444
if (startKernPairs) {
445445
String metricsPath = fontParser.getAfmPath();
446446
if (metricsPath != null) {
447-
throw new IOException("missing.endkernpairs.in.1").setMessageParams(metricsPath);
447+
throw new IOException("endkernpairs is missing in {0}.").setMessageParams(metricsPath);
448448
} else {
449-
throw new IOException("missing.endkernpairs.in.the.metrics.file");
449+
throw new IOException("endkernpairs is missing in the metrics file.");
450450
}
451451
}
452452
raf.close();

io/src/main/java/com/itextpdf/io/font/Type1Parser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public RandomAccessFileOrArray getMetricsFile() throws java.io.IOException {
9494
String resourcePath = FontResources.AFMS + afmPath + ".afm";
9595
resource = ResourceUtil.getResourceStream(resourcePath);
9696
if (resource == null) {
97-
throw new IOException("1.not.found.as.resource").setMessageParams(resourcePath);
97+
throw new IOException("{0} was not found as resource.").setMessageParams(resourcePath);
9898
}
9999
ByteArrayOutputStream stream = new ByteArrayOutputStream();
100100
int read;
@@ -131,14 +131,14 @@ public RandomAccessFileOrArray getMetricsFile() throws java.io.IOException {
131131
try {
132132
Pfm2afm.convert(rf, ba);
133133
} catch (Exception ignored) {
134-
throw new IOException("invalid.afm.or.pfm.font.file");
134+
throw new IOException("Invalid afm or pfm font file.");
135135
} finally {
136136
rf.close();
137137
}
138138
return new RandomAccessFileOrArray(sourceFactory.createSource(ba.toByteArray()));
139139
}
140140
} else {
141-
throw new IOException("invalid.afm.or.pfm.font.file");
141+
throw new IOException("Invalid afm or pfm font file.");
142142
}
143143
}
144144

io/src/main/java/com/itextpdf/io/font/cmap/CMapByteCid.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private void encodeSequence(byte[] seq, char cid) {
120120
int one = seq[idx] & 0xff;
121121
char c = plane[one];
122122
if (c != 0 && (c & 0x8000) == 0)
123-
throw new IOException("inconsistent.mapping");
123+
throw new IOException("Inconsistent mapping.");
124124
if (c == 0) {
125125
planes.add(new char[256]);
126126
c = (char)(planes.size() - 1 | 0x8000);
@@ -132,7 +132,7 @@ private void encodeSequence(byte[] seq, char cid) {
132132
int one = seq[size] & 0xff;
133133
char c = plane[one];
134134
if ((c & 0x8000) != 0)
135-
throw new IOException("inconsistent.mapping");
135+
throw new IOException("Inconsistent mapping.");
136136
plane[one] = cid;
137137
}
138138
}

io/src/main/java/com/itextpdf/io/font/cmap/CMapContentParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ public CMapObject readDictionary() throws java.io.IOException {
101101
Map<String, CMapObject> dic = new HashMap<>();
102102
while (true) {
103103
if (!nextValidToken())
104-
throw new IOException("unexpected.end.of.file");
104+
throw new IOException("Unexpected end of file.");
105105
if (tokeniser.getTokenType() == TokenType.EndDic)
106106
break;
107107
if (tokeniser.getTokenType() == TokenType.Other && "def".equals(tokeniser.getStringValue()))
108108
continue;
109109
if (tokeniser.getTokenType() != TokenType.Name)
110-
throw new IOException("dictionary.key.1.is.not.a.name").setMessageParams(tokeniser.getStringValue());
110+
throw new IOException("Dictionary key {0} is not a name.").setMessageParams(tokeniser.getStringValue());
111111
String name = tokeniser.getStringValue();
112112
CMapObject obj = readObject();
113113
if (obj.isToken()) {

kernel/src/main/java/com/itextpdf/kernel/PdfException.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ public class PdfException extends RuntimeException {
113113
public static final String CannotRemoveDocumentRootTag = "Cannot remove document root tag.";
114114
public static final String CannotRemoveMarkedContentReferenceBecauseItsPageWasAlreadyFlushed = "Cannot remove marked content reference, because its page has been already flushed.";
115115
public static final String CannotRemoveTagBecauseItsParentIsFlushed = "Cannot remove tag, because its parent is flushed.";
116+
@Deprecated
116117
public static final String CannotSetDataToPdfstreamWhichWasCreatedByInputStream = "Cannot set data to PdfStream which was created by InputStream.";
118+
public static final String CannotSetDataToPdfStreamWhichWasCreatedByInputStream = "Cannot set data to PdfStream which was created by InputStream.";
117119
public static final String CannotSetEncryptedPayloadToDocumentOpenedInReadingMode = "Cannot set encrypted payload to a document opened in read only mode.";
118120
public static final String CannotSetEncryptedPayloadToEncryptedDocument = "Cannot set encrypted payload to an encrypted document.";
119121
public static final String CannotSplitDocumentThatIsBeingWritten = "Cannot split document that is being written.";

kernel/src/main/java/com/itextpdf/kernel/colors/Color.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public static Color makeColor(PdfColorSpace colorSpace, float[] colorValue) {
148148
unknownColorSpace = true;
149149
}
150150
if (unknownColorSpace) {
151-
throw new PdfException("unknown.color.space");
151+
throw new PdfException("Unknown color space.");
152152
}
153153
return c;
154154
}

kernel/src/main/java/com/itextpdf/kernel/font/PdfTrueTypeFont.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public class PdfTrueTypeFont extends PdfSimpleFont<TrueTypeFont> {
7171
this.embedded = embedded;
7272
FontNames fontNames = ttf.getFontNames();
7373
if (embedded && !fontNames.allowEmbedding()) {
74-
throw new PdfException("1.cannot.be.embedded.due.to.licensing.restrictions")
74+
throw new PdfException("{0} cannot be embedded due to licensing restrictions.")
7575
.setMessageParams(fontNames.getFontName());
7676
}
7777
if ((encoding == null || encoding.length() == 0) && ttf.isFontSpecific()) {

0 commit comments

Comments
 (0)