001: /*
002: *
003: * $Id: PDFObject.java,v 1.1.1.1 2001/10/29 19:51:08 ezb Exp $
004: *
005: * $Date: 2001/10/29 19:51:08 $
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: package gnu.jpdf;
022:
023: import java.io.*;
024: import java.util.*;
025:
026: /**
027: * This is the base class for all Objects that form the PDF document.
028: *
029: * @author Peter T Mount, http://www.retep.org.uk/pdf/
030: * @author Eric Z. Beard, ericzbeard@hotmail.com
031: * @author $Author: ezb $
032: * @version $Revision: 1.1.1.1 $, $Date: 2001/10/29 19:51:08 $
033: */
034: public abstract class PDFObject implements Serializable {
035:
036: /*
037: * NOTE: The original class is the work of Peter T. Mount, who released it
038: * in the uk.org.retep.pdf package. It was modified by Eric Z. Beard as
039: * follows:
040: * The package name was changed to gnu.pdf.
041: * The formatting was changed a little bit.
042: * It is still licensed under the LGPL.
043: */
044:
045: /**
046: * This is the object's PDF Type
047: */
048: private String type;
049:
050: /**
051: * This is the unique serial number for this object.
052: */
053: protected int objser;
054:
055: /**
056: * This allows any PDF object to refer to the document being constructed.
057: */
058: protected PDFDocument pdfDocument;
059:
060: /**
061: * This is usually called by extensors to this class, and sets the
062: * PDF Object Type
063: */
064: public PDFObject(String type) {
065: this .type = type;
066: }
067:
068: /**
069: * Returns the PDF Type of this object
070: * @return The PDF Type of this object
071: */
072: public String getType() {
073: return type;
074: }
075:
076: /**
077: * Returns the unique serial number of this object.
078: * @return Unique serial number of this object.
079: */
080: public final int getSerialID() {
081: return objser;
082: }
083:
084: /**
085: * Returns the PDF document this object belongs to.
086: * @return PDF containing this object
087: */
088: public final PDFDocument getPDFDocument() {
089: return pdfDocument;
090: }
091:
092: /**
093: * <p>Writes the object to the output stream.
094: * This method must be overidden.</p>
095: *
096: * <p><b>Note:</b> It should not write any other objects, even if they are
097: * it's Kids, as they will be written by the calling routine.</p>
098: *
099: * @param os OutputStream to send the object to
100: * @exception IOException on error
101: */
102: public abstract void write(OutputStream os) throws IOException;
103:
104: /**
105: * The write method should call this before writing anything to the
106: * OutputStream. This will send the standard header for each object.
107: *
108: * <p>Note: There are a few rare cases where this method is not called.
109: *
110: * @param os OutputStream to write to
111: * @exception IOException on error
112: */
113: public final void writeStart(OutputStream os) throws IOException {
114: os.write(Integer.toString(objser).getBytes());
115: os.write(" 0 obj\n<<\n".getBytes());
116: if (type != null) {
117: os.write("/Type ".getBytes());
118: os.write(type.getBytes());
119: os.write("\n".getBytes());
120: }
121: }
122:
123: /**
124: * The write method should call this after writing anything to the
125: * OutputStream. This will send the standard footer for each object.
126: *
127: * <p>Note: There are a few rare cases where this method is not called.
128: *
129: * @param os OutputStream to write to
130: * @exception IOException on error
131: */
132: public final void writeEnd(OutputStream os) throws IOException {
133: os.write(">>\nendobj\n".getBytes());
134: }
135:
136: /**
137: * Returns the unique serial number in PDF format
138: * @return the serial number in PDF format
139: */
140: public String toString() {
141: return "" + objser + " 0 R";
142: }
143:
144: /**
145: * This utility method returns a String containing an array definition
146: * based on a Vector containing PDFObjects
147: * @param v Vector containing PDFObjects
148: * @return String containing a PDF array
149: */
150: public static String toArray(Vector v) {
151: if (v.size() == 0)
152: return "";
153:
154: StringBuffer b = new StringBuffer();
155: String bs = "[";
156: for (Enumeration en = v.elements(); en.hasMoreElements();) {
157: b.append(bs);
158: b.append(en.nextElement().toString());
159: bs = " ";
160: }
161: b.append("]");
162: return b.toString();
163: }
164: }
|