Subject: [PATCH 11/20] Add support for clearing objects at write time --- src/java/org/apache/fop/pdf/PDFClearable.java | 34 ++++++++++++++++ src/java/org/apache/fop/pdf/PDFDocument.java | 51 ++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 1 deletions(-) create mode 100644 src/java/org/apache/fop/pdf/PDFClearable.java diff --git a/src/java/org/apache/fop/pdf/PDFClearable.java b/src/java/org/apache/fop/pdf/PDFClearable.java new file mode 100644 index 0000000..e525c65 --- /dev/null +++ b/src/java/org/apache/fop/pdf/PDFClearable.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id: PDFWritable.java 679326 2008-07-24 09:35:34Z vhennebert $ */ + +package org.apache.fop.pdf; + +/** + * This interface is implemented by classes that can be serialized to a PDF file either by + * serializing the object or by writing a indirect reference to the actual object. + * The object supports clearing the content after writing. + */ +public interface PDFClearable extends PDFWritable { + + /** + * Clear object content + */ + void clear(); + +} diff --git a/src/java/org/apache/fop/pdf/PDFDocument.java b/src/java/org/apache/fop/pdf/PDFDocument.java index 1890669..e97e649 100644 --- a/src/java/org/apache/fop/pdf/PDFDocument.java +++ b/src/java/org/apache/fop/pdf/PDFDocument.java @@ -98,6 +98,9 @@ public class PDFDocument { /** the objects themselves */ private List objects = new LinkedList(); + /** the objects themselves (to be cleared after writing) */ + private List clearobjects = new LinkedList(); + /** Indicates what PDF version is active */ private int pdfVersion = PDF_VERSION_1_4; @@ -378,6 +381,20 @@ public class PDFDocument { } /** + * Registers a {@link PDFObject} in this PDF document. + * The object is assigned a new object number. + * + * @param obj {@link PDFObject} to add + * @param clear object after writing + * @return the added {@link PDFObject} added (with its object number set) + */ + public PDFObject registerObject(PDFObject obj, boolean clear) { + assignObjectNumber(obj); + addObject(obj, clear); + return obj; + } + + /** * Assigns the {@link PDFObject} an object number, * and sets the parent of the {@link PDFObject} to this document. * @@ -413,6 +430,17 @@ public class PDFDocument { * @param obj {@link PDFObject} to add */ public void addObject(PDFObject obj) { + addObject(obj, false); + } + + /** + * Adds a {@link PDFObject} to this document. + * The object MUST have an object number assigned. + * + * @param obj {@link PDFObject} to add + * @param clear Clear object after writing (if possible) + */ + public void addObject(PDFObject obj, boolean clear) { if (obj == null) { throw new NullPointerException("obj must not be null"); } @@ -423,7 +451,10 @@ public class PDFDocument { } //Add object to list - this.objects.add(obj); + if (clear) + this.clearobjects.add(obj); + else + this.objects.add(obj); //Add object to special lists where necessary if (obj instanceof PDFFunction) { @@ -927,6 +958,24 @@ public class PDFDocument { this.position += object.output(stream); } + while (this.clearobjects.size() > 0) { + /* Retrieve first */ + PDFObject object = (PDFObject)this.clearobjects.remove(0); + /* + * add the position of this object to the list of object + * locations + */ + setLocation(object.getObjectNumber() - 1, this.position); + + /* + * output the object and increment the character position + * by the object's length + */ + this.position += object.output(stream); + if (object instanceof PDFClearable) + ((PDFClearable)object).clear(); + } + //Clear all objects written to the file //this.objects.clear(); } -- 1.6.4.2