Subject: [PATCH 12/20] Add support for lazy object number assignment --- src/java/org/apache/fop/pdf/PDFDocument.java | 34 +++++++++++++++++++++++++- src/java/org/apache/fop/pdf/PDFObject.java | 14 ++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/java/org/apache/fop/pdf/PDFDocument.java b/src/java/org/apache/fop/pdf/PDFDocument.java index e97e649..697af11 100644 --- a/src/java/org/apache/fop/pdf/PDFDocument.java +++ b/src/java/org/apache/fop/pdf/PDFDocument.java @@ -404,7 +404,7 @@ public class PDFDocument { if (obj == null) { throw new NullPointerException("obj must not be null"); } - if (obj.hasObjectNumber()) { + if (!obj.hasLazyObjectNumber() && obj.hasObjectNumber()) { throw new IllegalStateException( "Error registering a PDFObject: " + "PDFObject already has an object number"); @@ -424,6 +424,35 @@ public class PDFDocument { } /** + * Assigns the {@link PDFObject} an object number when it is first used, + * and sets the parent of the {@link PDFObject} to this document. + * + * @param obj {@link PDFObject} to assign a number to + */ + public void lazyAssignObjectNumber(PDFObject obj) { + if (obj == null) { + throw new NullPointerException("obj must not be null"); + } + if (obj.hasObjectNumber()) { + throw new IllegalStateException( + "Error registering a PDFObject: " + + "PDFObject already has an object number"); + } + PDFDocument currentParent = obj.getDocument(); + if (currentParent != null && currentParent != this) { + throw new IllegalStateException( + "Error registering a PDFObject: " + + "PDFObject already has a parent PDFDocument"); + } + + obj.setObjectNumber(-1); + + if (currentParent == null) { + obj.setDocument(this); + } + } + + /** * Adds a {@link PDFObject} to this document. * The object MUST have an object number assigned. * @@ -1107,6 +1136,9 @@ public class PDFDocument { throw new IOException("PDF file too large. PDF cannot grow beyond approx. 9.3GB."); } + if (s.equals("0")) + throw new IOException("PDF object "+(count+1)+" with position 0"); + /* contruct xref entry for object */ loc = padding.substring(s.length()) + s; diff --git a/src/java/org/apache/fop/pdf/PDFObject.java b/src/java/org/apache/fop/pdf/PDFObject.java index 897c2f8..ef2f3e1 100644 --- a/src/java/org/apache/fop/pdf/PDFObject.java +++ b/src/java/org/apache/fop/pdf/PDFObject.java @@ -69,6 +69,9 @@ public abstract class PDFObject implements PDFWritable { if (this.objnum == 0) { throw new IllegalStateException("Object has no number assigned: " + this.toString()); } + if (this.objnum == -1) { + document.assignObjectNumber(this); + } return this.objnum; } @@ -93,7 +96,16 @@ public abstract class PDFObject implements PDFWritable { * @return True if it has an object number */ public boolean hasObjectNumber() { - return this.objnum > 0; + return this.objnum > 0 || this.objnum == -1; + } + + /** + * Indicates whether this PDFObject will be assigned an + * object number. + * @return True if it has an object number + */ + public boolean hasLazyObjectNumber() { + return this.objnum == -1; } /** -- 1.6.4.2