KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > jpdf > PDFObject


1 /*
2  *
3  * $Id: PDFObject.java,v 1.1.1.1 2001/10/29 19:51:08 ezb Exp $
4  *
5  * $Date: 2001/10/29 19:51:08 $
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package gnu.jpdf;
22
23 import java.io.*;
24 import java.util.*;
25
26 /**
27  * This is the base class for all Objects that form the PDF document.
28  *
29  * @author Peter T Mount, http://www.retep.org.uk/pdf/
30  * @author Eric Z. Beard, ericzbeard@hotmail.com
31  * @author $Author: ezb $
32  * @version $Revision: 1.1.1.1 $, $Date: 2001/10/29 19:51:08 $
33  */

34 public abstract class PDFObject implements Serializable
35 {
36
37   /*
38    * NOTE: The original class is the work of Peter T. Mount, who released it
39    * in the uk.org.retep.pdf package. It was modified by Eric Z. Beard as
40    * follows:
41    * The package name was changed to gnu.pdf.
42    * The formatting was changed a little bit.
43    * It is still licensed under the LGPL.
44    */

45
46   /**
47    * This is the object's PDF Type
48    */

49   private String JavaDoc type;
50     
51   /**
52    * This is the unique serial number for this object.
53    */

54   protected int objser;
55     
56   /**
57    * This allows any PDF object to refer to the document being constructed.
58    */

59   protected PDFDocument pdfDocument;
60     
61       
62   /**
63    * This is usually called by extensors to this class, and sets the
64    * PDF Object Type
65    */

66   public PDFObject(String JavaDoc type)
67   {
68     this.type = type;
69   }
70     
71   /**
72    * Returns the PDF Type of this object
73    * @return The PDF Type of this object
74    */

75   public String JavaDoc getType()
76   {
77     return type;
78   }
79     
80   /**
81    * Returns the unique serial number of this object.
82    * @return Unique serial number of this object.
83    */

84   public final int getSerialID()
85   {
86     return objser;
87   }
88     
89   /**
90    * Returns the PDF document this object belongs to.
91    * @return PDF containing this object
92    */

93   public final PDFDocument getPDFDocument()
94   {
95     return pdfDocument;
96   }
97     
98   /**
99    * <p>Writes the object to the output stream.
100    * This method must be overidden.</p>
101    *
102    * <p><b>Note:</b> It should not write any other objects, even if they are
103    * it's Kids, as they will be written by the calling routine.</p>
104    *
105    * @param os OutputStream to send the object to
106    * @exception IOException on error
107    */

108   public abstract void write(OutputStream os) throws IOException;
109     
110   /**
111    * The write method should call this before writing anything to the
112    * OutputStream. This will send the standard header for each object.
113    *
114    * <p>Note: There are a few rare cases where this method is not called.
115    *
116    * @param os OutputStream to write to
117    * @exception IOException on error
118    */

119   public final void writeStart(OutputStream os) throws IOException
120   {
121     os.write(Integer.toString(objser).getBytes());
122     os.write(" 0 obj\n<<\n".getBytes());
123     if(type!=null) {
124       os.write("/Type ".getBytes());
125       os.write(type.getBytes());
126       os.write("\n".getBytes());
127     }
128   }
129
130
131
132     
133   /**
134    * The write method should call this after writing anything to the
135    * OutputStream. This will send the standard footer for each object.
136    *
137    * <p>Note: There are a few rare cases where this method is not called.
138    *
139    * @param os OutputStream to write to
140    * @exception IOException on error
141    */

142   public final void writeEnd(OutputStream os) throws IOException
143   {
144     os.write(">>\nendobj\n".getBytes());
145   }
146     
147   /**
148    * Returns the unique serial number in PDF format
149    * @return the serial number in PDF format
150    */

151   public String JavaDoc toString()
152   {
153     return ""+objser+" 0 R";
154   }
155     
156   /**
157    * This utility method returns a String containing an array definition
158    * based on a Vector containing PDFObjects
159    * @param v Vector containing PDFObjects
160    * @return String containing a PDF array
161    */

162   public static String JavaDoc toArray(Vector v)
163   {
164     if(v.size()==0)
165       return "";
166         
167     StringBuffer JavaDoc b = new StringBuffer JavaDoc();
168     String JavaDoc bs = "[";
169     for(Enumeration en = v.elements(); en.hasMoreElements(); ) {
170       b.append(bs);
171       b.append(en.nextElement().toString());
172       bs = " ";
173     }
174     b.append("]");
175     return b.toString();
176   }
177 }
178
Popular Tags