KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > etymon > pjx > PdfObject


1 /*
2   Copyright (C) Etymon Systems, Inc. <http://www.etymon.com/>
3 */

4
5 package com.etymon.pjx;
6
7 import java.io.*;
8
9 /**
10    The abstract superclass of all basic PDF object types. It is a
11    requirement that any classes that extend this class be immutable.
12    @author Nassib Nassar
13 */

14 public abstract class PdfObject
15     implements Cloneable JavaDoc {
16
17     /**
18        Filter this object recursively through
19        <code>PdfObjectFilter</code>. The filtered object is
20        returned.
21        @param filter the <code>PdfObjectFilter</code> instance.
22        @return the new filtered object.
23        @throws PdfFormatException
24      */

25     public PdfObject filter(PdfObjectFilter f) throws PdfFormatException {
26         PdfObject obj = f.preFilter(this);
27         if (obj == null) {
28             return f.postFilter(null);
29         } else {
30             return obj.filterContents(f);
31         }
32     }
33
34     /**
35        Second stage filtering, called by {@link
36        #filter(PdfObjectFilter) filter(PdfObjectFilter)}. This is
37        only called if {@link PdfObjectFilter#preFilter(PdfObject)
38        PdfObjectFilter.preFilter(PdfObject)} did not return
39        <code>null</code>.
40        @param filter the <code>PdfObjectFilter</code> instance.
41        @return the new filtered object.
42        @throws PdfFormatException
43      */

44     protected PdfObject filterContents(PdfObjectFilter f) throws PdfFormatException {
45         return f.postFilter(this);
46     }
47     
48     /**
49        Returns a shallow copy of this instance.
50        @return a clone of this instance.
51      */

52     public Object JavaDoc clone() {
53         try {
54             return super.clone();
55         }
56         catch (CloneNotSupportedException JavaDoc e) {
57             return null; // this should never happen
58
}
59     }
60
61     /**
62        Compares this instance with another PDF object for equality.
63        @param obj the object to compare this instance with.
64        @return <code>true</code> if the PDF objects are equal.
65      */

66     public boolean equals(Object JavaDoc obj) {
67         return super.equals(obj);
68     }
69     
70     /**
71        Returns a hash code for this object.
72        @return the hash code.
73      */

74     public int hashCode() {
75         return super.hashCode();
76     }
77     
78     /**
79        Returns a string representation of this instance in PDF
80        format.
81        @return the string representation.
82      */

83     public String JavaDoc toString() {
84         ByteArrayOutputStream baos = new ByteArrayOutputStream();
85         try {
86             PdfWriter w = new PdfWriter(baos);
87             writePdf(w, false);
88             w.close();
89             baos.close();
90         }
91         catch (IOException e) {
92             return null;
93         }
94         return baos.toString();
95     }
96
97     /**
98        Writes this object in PDF format.
99        @param w the <code>PdfWriter</code> to write to.
100        @param spacing specifies whether to add white-space before
101        the object. A value of <code>true</code> enables the
102        addition of white-space. If the object begins with a PDF
103        delimiter, then this option is ignored and no white-space
104        is written.
105        @return the number of bytes written by this method.
106        @throws IOException
107      */

108     protected abstract int writePdf(PdfWriter w, boolean spacing) throws IOException;
109
110 }
111
Popular Tags