jeremias    2003/03/27 02:29:09

  Modified:    src/java/org/apache/fop/pdf PDFObject.java
  Log:
  Streamlined. Add code for encryption of string and text values. All PDF objects may 
have a reference to their parent PDFDocuments.
  
  Revision  Changes    Path
  1.2       +145 -21   xml-fop/src/java/org/apache/fop/pdf/PDFObject.java
  
  Index: PDFObject.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/pdf/PDFObject.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PDFObject.java    11 Mar 2003 13:05:09 -0000      1.1
  +++ PDFObject.java    27 Mar 2003 10:29:09 -0000      1.2
  @@ -66,21 +66,17 @@
       /**
        * the object's number
        */
  -    protected int number;
  +    private int objnum;
   
       /**
        * the object's generation (0 in new documents)
        */
  -    protected int generation = 0;
  +    private int generation = 0;
   
       /**
  -     * create an empty object
  -     *
  -     * @param number the object's number
  +     * the parent PDFDocument
        */
  -    public PDFObject(int number) {
  -        this.number = number;
  -    }
  +    private PDFDocument document;
   
       /**
        * Create a PDFObject
  @@ -90,14 +86,93 @@
       }
   
       /**
  +     * Returns the object's number.
        * @return the PDF Object number
        */
  -    public int getNumber() {
  -        return this.number;
  +    public int getObjectNumber() {
  +        if (this.objnum == 0) {
  +            throw new IllegalStateException("Object has no number assigned: " + 
this.toString());
  +            //System.out.println("Object has no number assigned: " + 
this.toString());
  +        }
  +        return this.objnum;
  +    }
  +    
  +    /**
  +     * Indicates whether this PDFObject has already been assigned an 
  +     * object number.
  +     * @return True if it has an object number
  +     */
  +    public boolean hasObjectNumber() {
  +        return this.objnum > 0;
  +    }
  +
  +    /**
  +     * Sets the object number
  +     * @param objnum the object number
  +     */
  +    public void setObjectNumber(int objnum) {
  +        this.objnum = objnum;
  +        //System.out.println("Assigning "+this+" object number "+objnum);
  +    }
  +
  +    /**
  +     * Returns the object's generation.
  +     * @return the PDF Object generation
  +     */
  +    public int getGeneration() {
  +        return this.generation;
  +    }
  +
  +    /**
  +     * Returns the parent PDFDocument if assigned.
  +     * @return the parent PDFDocument (May be null if the parent PDFDocument
  +     * has not been assigned)
  +     */
  +    public final PDFDocument getDocument() {
  +        return this.document;
  +    }
  +
  +    /**
  +     * Returns the parent PDFDocument, but unlike <code>getDocument()</code>
  +     * it throws an informative Exception if the parent document is unavailable
  +     * instead of having a NullPointerException somewhere without a message.
  +     * @return the parent PDFDocument
  +     */
  +    public final PDFDocument getDocumentSafely() {
  +        final PDFDocument doc = getDocument();
  +        if (doc == null) {
  +            throw new IllegalStateException("Parent PDFDocument is unavailable");
  +        }
  +        return doc;
  +    }
  +
  +    /**
  +     * Sets the parent PDFDocument.
  +     * @param doc the PDFDocument.
  +     */
  +    public void setDocument(PDFDocument doc) {
  +        this.document = doc;
       }
   
       /**
  -     * write the PDF represention of this object
  +     * Returns the PDF representation of the Object ID.
  +     * @return the Object ID
  +     */
  +    public String getObjectID() {
  +        return getObjectNumber() + " " + getGeneration() + " obj\n";
  +    }
  +
  +    /**
  +     * Returns the PDF representation of a reference to this object.
  +     * @return the reference string
  +     */
  +    public String referencePDF() {
  +        String ref = getObjectNumber() + " " + getGeneration() + " R";
  +        return ref;
  +    }
  +
  +    /**
  +     * Write the PDF represention of this object
        *
        * @param stream the stream to write the PDF to
        * @throws IOException if there is an error writing to the stream
  @@ -110,19 +185,68 @@
       }
   
       /**
  -     * the PDF representation of a reference to this object
  +     * Encodes the object as a byte array for output to a PDF file.
        *
  -     * @return the reference string
  +     * @return PDF string
        */
  -    public String referencePDF() {
  -        String p = this.number + " " + this.generation + " R";
  -        return p;
  +    protected byte[] toPDF() {
  +        return encode(toPDFString());
       }
  -
  +    
  +    
       /**
  -     * represent object as PDF
  -     *
  -     * @return PDF string
  +     * This method returns a String representation of the PDF object. The result
  +     * is normally converted/encoded to a byte array by toPDF(). Only use 
  +     * this method to implement the serialization if the object can be fully 
  +     * represented as text. If the PDF representation of the object contains
  +     * binary content use toPDF() or output(OutputStream) instead.
  +     * @return String the String representation
  +     */
  +    protected String toPDFString() {
  +        throw new UnsupportedOperationException("Not implemented. "
  +                    + "Use output(OutputStream) instead.");
  +    }
  +    
  +    
  +    /**
  +     * Converts text to a byte array for writing to a PDF file.
  +     * @param text text to convert/encode
  +     * @return byte[] the resulting byte array
        */
  -    abstract byte[] toPDF();
  +    public static final byte[] encode(String text) {
  +        return PDFDocument.encode(text);
  +    }
  +    
  +    /**
  +     * Encodes a Text String (3.8.1 in PDF 1.4 specs)
  +     * @param text the text to encode
  +     * @return byte[] the encoded text
  +     */
  +    protected byte[] encodeText(String text) {
  +        if (getDocumentSafely().isEncryptionActive()) {
  +            final byte[] buf = PDFText.toUTF16(text);
  +            return PDFText.escapeByteArray(
  +                getDocument().getEncryption().encrypt(buf, this));
  +        } else {
  +            return encode(PDFText.escapeText(text, true));
  +        }
  +    }
  +    
  +    /**
  +     * Encodes a String (3.2.3 in PDF 1.4 specs)
  +     * @param string the string to encode
  +     * @return byte[] the encoded string
  +     */
  +    protected byte[] encodeString(String string) {
  +        return encodeText(string);
  +        /*
  +        final byte[] buf = encode(PDFText.escapeString(string));
  +        if (getDocumentSafely().isEncryptionActive()) {
  +            return PDFText.escapeByteArray(
  +                getDocument().getEncryption().encrypt(buf, this));
  +        } else {
  +            return buf;
  +        }*/
  +    }
  +
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to