Example usage for org.apache.pdfbox.cos COSBase accept

List of usage examples for org.apache.pdfbox.cos COSBase accept

Introduction

In this page you can find the example usage for org.apache.pdfbox.cos COSBase accept.

Prototype

public abstract Object accept(ICOSVisitor visitor) throws IOException;

Source Link

Document

visitor pattern double dispatch method.

Usage

From source file:com.aaasec.sigserv.csspsupport.pdfbox.modifications.CsCOSWriter.java

License:Apache License

/**
 * This will write a COS object./*from   ww w. j  a  v a 2  s. co m*/
 *
 * @param obj The object to write.
 *
 * @throws COSVisitorException If there is an error visiting objects.
 */
public void doWriteObject(COSBase obj) throws COSVisitorException {
    try {
        writtenObjects.add(obj);
        if (obj instanceof COSDictionary) {
            COSDictionary dict = (COSDictionary) obj;
            COSName item = (COSName) dict.getItem(COSName.TYPE);
            if (COSName.SIG.equals(item) || COSName.DOC_TIME_STAMP.equals(item)) {
                reachedSignature = true;
            }
        }

        // find the physical reference
        currentObjectKey = getObjectKey(obj);
        // add a x ref entry
        addXRefEntry(new COSWriterXRefEntry(getStandardOutput().getPos(), obj, currentObjectKey));
        // write the object
        getStandardOutput().write(String.valueOf(currentObjectKey.getNumber()).getBytes("ISO-8859-1"));
        getStandardOutput().write(SPACE);
        getStandardOutput().write(String.valueOf(currentObjectKey.getGeneration()).getBytes("ISO-8859-1"));
        getStandardOutput().write(SPACE);
        getStandardOutput().write(OBJ);
        getStandardOutput().writeEOL();
        obj.accept(this);
        getStandardOutput().writeEOL();
        getStandardOutput().write(ENDOBJ);
        getStandardOutput().writeEOL();
    } catch (IOException e) {
        throw new COSVisitorException(e);
    }
}

From source file:com.aaasec.sigserv.csspsupport.pdfbox.modifications.CsCOSWriter.java

License:Apache License

/**
 * visitFromArray method comment./*w w  w .ja va2s  .c o  m*/
 *
 * @param obj The object that is being visited.
 *
 * @throws COSVisitorException If there is an exception while visiting this
 * object.
 *
 * @return null
 */
public Object visitFromArray(COSArray obj) throws COSVisitorException {
    try {
        int count = 0;
        getStandardOutput().write(ARRAY_OPEN);
        for (Iterator<COSBase> i = obj.iterator(); i.hasNext();) {
            COSBase current = i.next();
            if (current instanceof COSDictionary) {
                addObjectToWrite(current);
                writeReference(current);
            } else if (current instanceof COSObject) {
                COSBase subValue = ((COSObject) current).getObject();
                if (subValue instanceof COSDictionary || subValue == null) {
                    addObjectToWrite(current);
                    writeReference(current);
                } else {
                    subValue.accept(this);
                }
            } else if (current == null) {
                COSNull.NULL.accept(this);
            } else if (current instanceof COSString) {
                COSString copy = new COSString(((COSString) current).getString());
                copy.accept(this);
            } else {
                current.accept(this);
            }
            count++;
            if (i.hasNext()) {
                if (count % 10 == 0) {
                    getStandardOutput().writeEOL();
                } else {
                    getStandardOutput().write(SPACE);
                }
            }
        }
        getStandardOutput().write(ARRAY_CLOSE);
        getStandardOutput().writeEOL();
        return null;
    } catch (IOException e) {
        throw new COSVisitorException(e);
    }
}

From source file:com.aaasec.sigserv.csspsupport.pdfbox.modifications.CsCOSWriter.java

License:Apache License

/**
 * visitFromDictionary method comment./*  ww w  .ja va2 s .co m*/
 *
 * @param obj The object that is being visited.
 *
 * @throws COSVisitorException If there is an exception while visiting this
 * object.
 *
 * @return null
 */
public Object visitFromDictionary(COSDictionary obj) throws COSVisitorException {
    try {
        getStandardOutput().write(DICT_OPEN);
        getStandardOutput().writeEOL();
        for (Map.Entry<COSName, COSBase> entry : obj.entrySet()) {
            COSBase value = entry.getValue();
            if (value != null) {
                entry.getKey().accept(this);
                getStandardOutput().write(SPACE);
                if (value instanceof COSDictionary) {
                    COSDictionary dict = (COSDictionary) value;

                    // write all XObjects as direct objects, this will save some size
                    COSBase item = dict.getItem(COSName.XOBJECT);
                    if (item != null) {
                        item.setDirect(true);
                    }
                    item = dict.getItem(COSName.RESOURCES);
                    if (item != null) {
                        item.setDirect(true);
                    }

                    if (dict.isDirect()) {
                        // If the object should be written direct, we need
                        // to pass the dictionary to the visitor again.
                        visitFromDictionary(dict);
                    } else {
                        addObjectToWrite(dict);
                        writeReference(dict);
                    }
                } else if (value instanceof COSObject) {
                    COSBase subValue = ((COSObject) value).getObject();
                    if (subValue instanceof COSDictionary || subValue == null) {
                        addObjectToWrite(value);
                        writeReference(value);
                    } else {
                        subValue.accept(this);
                    }
                } else {
                    // If we reach the pdf signature, we need to determinate the position of the
                    // content and byterange
                    if (reachedSignature && COSName.CONTENTS.equals(entry.getKey())) {
                        signaturePosition = new int[2];
                        signaturePosition[0] = (int) getStandardOutput().getPos();
                        value.accept(this);
                        signaturePosition[1] = (int) getStandardOutput().getPos();
                    } else if (reachedSignature && COSName.BYTERANGE.equals(entry.getKey())) {
                        byterangePosition = new int[2];
                        byterangePosition[0] = (int) getStandardOutput().getPos() + 1;
                        value.accept(this);
                        byterangePosition[1] = (int) getStandardOutput().getPos() - 1;
                        reachedSignature = false;
                    } else {
                        value.accept(this);
                    }
                }
                getStandardOutput().writeEOL();

            } else {
                //then we won't write anything, there are a couple cases
                //were the value of an entry in the COSDictionary will
                //be a dangling reference that points to nothing
                //so we will just not write out the entry if that is the case
            }
        }
        getStandardOutput().write(DICT_CLOSE);
        getStandardOutput().writeEOL();
        return null;
    } catch (IOException e) {
        throw new COSVisitorException(e);
    }
}