Skip to content

Commit 94f6a23

Browse files
author
Andreas Røsdal
committed
Return of ExceptionConverter. We have permission to use this class with a LGPL/MPL license.
"Hi Andreas, you have my permission to use my code with your license. Regards Heinz -- Dr Heinz M. Kabutz (PhD CompSci)"
1 parent 1125ccf commit 94f6a23

71 files changed

Lines changed: 449 additions & 331 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

openpdf/src/main/java/com/lowagie/bouncycastle/BouncyCastleHelper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.lowagie.bouncycastle;
22

3-
import com.lowagie.text.exceptions.ExceptionUtil;
3+
import com.lowagie.text.ExceptionConverter;
44
import com.lowagie.text.pdf.PdfArray;
55
import com.lowagie.text.pdf.PdfObject;
66

@@ -24,7 +24,7 @@ public static void checkCertificateEncodingOrThrowException(Certificate certific
2424
try {
2525
X509CertificateHolder certificateHolder = new X509CertificateHolder(certificate.getEncoded());
2626
} catch (CertificateEncodingException | IOException f) {
27-
throw ExceptionUtil.wrap(f);
27+
throw new ExceptionConverter(f);
2828
}
2929
// ******************************************************************************
3030
}
@@ -51,7 +51,7 @@ public static byte[] getEnvelopedData(PdfArray recipients, List<PdfObject> strin
5151

5252
}
5353
} catch (Exception f) {
54-
throw ExceptionUtil.wrap(f);
54+
throw new ExceptionConverter(f);
5555
}
5656
}
5757
return envelopedData;

openpdf/src/main/java/com/lowagie/text/DocWriter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
import java.util.Iterator;
5656
import java.util.Properties;
5757

58-
import com.lowagie.text.exceptions.ExceptionUtil;
58+
import com.lowagie.text.ExceptionConverter;
5959
import com.lowagie.text.pdf.OutputStreamCounter;
6060

6161

@@ -288,7 +288,7 @@ public void close() {
288288
os.close();
289289
}
290290
catch(IOException ioe) {
291-
throw ExceptionUtil.wrap(ioe);
291+
throw new ExceptionConverter(ioe);
292292
}
293293
}
294294

@@ -346,7 +346,7 @@ public void flush() {
346346
os.flush();
347347
}
348348
catch(IOException ioe) {
349-
throw ExceptionUtil.wrap(ioe);
349+
throw new ExceptionConverter(ioe);
350350
}
351351
}
352352

openpdf/src/main/java/com/lowagie/text/Document.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
import java.util.List;
5656

5757
import com.lowagie.text.error_messages.MessageLocalization;
58-
import com.lowagie.text.exceptions.ExceptionUtil;
58+
import com.lowagie.text.ExceptionConverter;
5959

6060

6161
/**
@@ -469,7 +469,7 @@ public boolean addHeader(String name, String content) {
469469
try {
470470
return add(new Header(name, content));
471471
} catch (DocumentException de) {
472-
throw ExceptionUtil.wrap(de);
472+
throw new ExceptionConverter(de);
473473
}
474474
}
475475

@@ -485,7 +485,7 @@ public boolean addTitle(String title) {
485485
try {
486486
return add(new Meta(Element.TITLE, title));
487487
} catch (DocumentException de) {
488-
throw ExceptionUtil.wrap(de);
488+
throw new ExceptionConverter(de);
489489
}
490490
}
491491

@@ -501,7 +501,7 @@ public boolean addSubject(String subject) {
501501
try {
502502
return add(new Meta(Element.SUBJECT, subject));
503503
} catch (DocumentException de) {
504-
throw ExceptionUtil.wrap(de);
504+
throw new ExceptionConverter(de);
505505
}
506506
}
507507

@@ -517,7 +517,7 @@ public boolean addKeywords(String keywords) {
517517
try {
518518
return add(new Meta(Element.KEYWORDS, keywords));
519519
} catch (DocumentException de) {
520-
throw ExceptionUtil.wrap(de);
520+
throw new ExceptionConverter(de);
521521
}
522522
}
523523

@@ -533,7 +533,7 @@ public boolean addAuthor(String author) {
533533
try {
534534
return add(new Meta(Element.AUTHOR, author));
535535
} catch (DocumentException de) {
536-
throw ExceptionUtil.wrap(de);
536+
throw new ExceptionConverter(de);
537537
}
538538
}
539539

@@ -549,7 +549,7 @@ public boolean addCreator(String creator) {
549549
try {
550550
return add(new Meta(Element.CREATOR, creator));
551551
} catch (DocumentException de) {
552-
throw ExceptionUtil.wrap(de);
552+
throw new ExceptionConverter(de);
553553
}
554554
}
555555

@@ -563,7 +563,7 @@ public boolean addProducer() {
563563
try {
564564
return add(new Meta(Element.PRODUCER, getVersion()));
565565
} catch (DocumentException de) {
566-
throw ExceptionUtil.wrap(de);
566+
throw new ExceptionConverter(de);
567567
}
568568
}
569569

@@ -580,7 +580,7 @@ public boolean addCreationDate() {
580580
"EEE MMM dd HH:mm:ss zzz yyyy");
581581
return add(new Meta(Element.CREATIONDATE, sdf.format(new Date())));
582582
} catch (DocumentException de) {
583-
throw ExceptionUtil.wrap(de);
583+
throw new ExceptionConverter(de);
584584
}
585585
}
586586

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/*
2+
* The contents of this file are subject to the Mozilla Public License Version 1.1
3+
* (the "License"); you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
5+
*
6+
* Software distributed under the License is distributed on an "AS IS" basis,
7+
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8+
* for the specific language governing rights and limitations under the License.
9+
*
10+
* The Original Code is 'iText, a free JAVA-PDF library'.
11+
*
12+
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
13+
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
14+
* All Rights Reserved.
15+
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
16+
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
17+
*
18+
* Contributor(s): all the names of the contributors are added in the source code
19+
* where applicable.
20+
*
21+
* Alternatively, the contents of this file may be used under the terms of the
22+
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
23+
* provisions of LGPL are applicable instead of those above. If you wish to
24+
* allow use of your version of this file only under the terms of the LGPL
25+
* License and not to allow others to use your version of this file under
26+
* the MPL, indicate your decision by deleting the provisions above and
27+
* replace them with the notice and other provisions required by the LGPL.
28+
* If you do not delete the provisions above, a recipient may use your version
29+
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
30+
*
31+
* This library is free software; you can redistribute it and/or modify it
32+
* under the terms of the MPL as stated above or under the terms of the GNU
33+
* Library General Public License as published by the Free Software Foundation;
34+
* either version 2 of the License, or any later version.
35+
*
36+
* This library is distributed in the hope that it will be useful, but WITHOUT
37+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
38+
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
39+
* details.
40+
*
41+
* If you didn't download this code from the following link, you should check if
42+
* you aren't using an obsolete version:
43+
* http://www.lowagie.com/iText/
44+
*/
45+
46+
/*
47+
* The original version of this class was published in an article by professor Heinz Kabutz.
48+
* Read http://www.javaspecialists.co.za/archive/newsletter.do?issue=033&print=yes&locale=en_US
49+
* "This material from The Java(tm) Specialists' Newsletter by Maximum Solutions (South Africa).
50+
* Please contact Maximum Solutions for more information."
51+
*
52+
* Copyright (C) 2001 Dr. Heinz M. Kabutz
53+
*/
54+
55+
/**
56+
* The OpenPDF project has permission from the author to use this class under a LGPL/MPL license on 2018-08-06:
57+
*
58+
* Hi Andreas,
59+
*
60+
* you have my permission to use my code with your license.
61+
*
62+
* Regards
63+
*
64+
*
65+
* Heinz
66+
* --
67+
* Dr Heinz M. Kabutz (PhD CompSci)
68+
*/
69+
70+
package com.lowagie.text;
71+
72+
/**
73+
* The ExceptionConverter changes a checked exception into an
74+
* unchecked exception.
75+
*/
76+
public class ExceptionConverter extends RuntimeException {
77+
private static final long serialVersionUID = 8657630363395849399L;
78+
/** we keep a handle to the wrapped exception */
79+
private Exception ex;
80+
/** prefix for the exception */
81+
private String prefix;
82+
83+
/**
84+
* Construct a RuntimeException based on another Exception
85+
* @param ex the exception that has to be turned into a RuntimeException
86+
*/
87+
public ExceptionConverter(Exception ex) {
88+
this.ex = ex;
89+
prefix = (ex instanceof RuntimeException) ? "" : "ExceptionConverter: ";
90+
}
91+
92+
/**
93+
* Convert an Exception into an unchecked exception. Return the exception if it is
94+
* already an unchecked exception or return an ExceptionConverter wrapper otherwise
95+
*
96+
* @param ex the exception to convert
97+
* @return an unchecked exception
98+
* @since 2.1.6
99+
*/
100+
public static final RuntimeException convertException(Exception ex) {
101+
if (ex instanceof RuntimeException) {
102+
return (RuntimeException) ex;
103+
}
104+
return new ExceptionConverter(ex);
105+
}
106+
107+
/**
108+
* and allow the user of ExceptionConverter to get a handle to it.
109+
* @return the original exception
110+
*/
111+
public Exception getException() {
112+
return ex;
113+
}
114+
115+
/**
116+
* We print the message of the checked exception
117+
* @return message of the original exception
118+
*/
119+
public String getMessage() {
120+
return ex.getMessage();
121+
}
122+
123+
/**
124+
* and make sure we also produce a localized version
125+
* @return localized version of the message
126+
*/
127+
public String getLocalizedMessage() {
128+
return ex.getLocalizedMessage();
129+
}
130+
131+
/**
132+
* The toString() is changed to be prefixed with ExceptionConverter
133+
* @return String version of the exception
134+
*/
135+
public String toString() {
136+
return prefix + ex;
137+
}
138+
139+
/** we have to override this as well */
140+
public void printStackTrace() {
141+
printStackTrace(System.err);
142+
}
143+
144+
/**
145+
* here we prefix, with s.print(), not s.println(), the stack
146+
* trace with "ExceptionConverter:"
147+
* @param s
148+
*/
149+
public void printStackTrace(java.io.PrintStream s) {
150+
synchronized (s) {
151+
s.print(prefix);
152+
ex.printStackTrace(s);
153+
}
154+
}
155+
156+
/**
157+
* Again, we prefix the stack trace with "ExceptionConverter:"
158+
* @param s
159+
*/
160+
public void printStackTrace(java.io.PrintWriter s) {
161+
synchronized (s) {
162+
s.print(prefix);
163+
ex.printStackTrace(s);
164+
}
165+
}
166+
167+
/**
168+
* requests to fill in the stack trace we will have to ignore.
169+
* We can't throw an exception here, because this method
170+
* is called by the constructor of Throwable
171+
* @return a Throwable
172+
*/
173+
public Throwable fillInStackTrace() {
174+
return this;
175+
}
176+
}

openpdf/src/main/java/com/lowagie/text/Font.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
import java.awt.Color;
5353

54-
import com.lowagie.text.exceptions.ExceptionUtil;
54+
import com.lowagie.text.ExceptionConverter;
5555
import com.lowagie.text.html.Markup;
5656
import com.lowagie.text.pdf.BaseFont;
5757

@@ -720,7 +720,7 @@ public BaseFont getCalculatedBaseFont(boolean specialEncoding) {
720720
try {
721721
cfont = BaseFont.createFont(fontName, encoding, false);
722722
} catch (Exception ee) {
723-
throw ExceptionUtil.wrap(ee);
723+
throw new ExceptionConverter(ee);
724724
}
725725
return cfont;
726726
}

openpdf/src/main/java/com/lowagie/text/FontFactoryImp.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
import java.util.Properties;
6161
import java.util.Set;
6262

63-
import com.lowagie.text.exceptions.ExceptionUtil;
63+
import com.lowagie.text.ExceptionConverter;
6464
import com.lowagie.text.html.Markup;
6565
import com.lowagie.text.pdf.BaseFont;
6666

@@ -213,7 +213,7 @@ public Font getFont(String fontname, String encoding, boolean embedded, float si
213213
}
214214
catch(DocumentException de) {
215215
// this shouldn't happen
216-
throw ExceptionUtil.wrap(de);
216+
throw new ExceptionConverter(de);
217217
}
218218
catch(IOException ioe) {
219219
// the font is registered as a true type font, but the path was wrong
@@ -589,10 +589,10 @@ else if (path.toLowerCase().endsWith(".afm") || path.toLowerCase().endsWith(".pf
589589
}
590590
catch(DocumentException de) {
591591
// this shouldn't happen
592-
throw ExceptionUtil.wrap(de);
592+
throw new ExceptionConverter(de);
593593
}
594594
catch(IOException ioe) {
595-
throw ExceptionUtil.wrap(ioe);
595+
throw new ExceptionConverter(ioe);
596596
}
597597
}
598598

openpdf/src/main/java/com/lowagie/text/Image.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
import java.net.URL;
6060
import com.lowagie.text.error_messages.MessageLocalization;
6161

62-
import com.lowagie.text.exceptions.ExceptionUtil;
62+
import com.lowagie.text.ExceptionConverter;
6363
import com.lowagie.text.pdf.PRIndirectReference;
6464
import com.lowagie.text.pdf.PdfArray;
6565
import com.lowagie.text.pdf.PdfContentByte;
@@ -701,7 +701,7 @@ public static Image getInstance(java.awt.Image image, java.awt.Color color,
701701
sm.makeMask();
702702
img.setImageMask(sm);
703703
} catch (DocumentException de) {
704-
throw ExceptionUtil.wrap(de);
704+
throw new ExceptionConverter(de);
705705
}
706706
}
707707
return img;
@@ -906,7 +906,7 @@ public static Image getInstance(Image image) {
906906
.getDeclaredConstructor(Image.class);
907907
return (Image) constructor.newInstance(image);
908908
} catch (Exception e) {
909-
throw ExceptionUtil.wrap(e);
909+
throw new ExceptionConverter(e);
910910
}
911911
}
912912

0 commit comments

Comments
 (0)