Example usage for org.apache.pdfbox.cos COSObject getObject

List of usage examples for org.apache.pdfbox.cos COSObject getObject

Introduction

In this page you can find the example usage for org.apache.pdfbox.cos COSObject getObject.

Prototype

public COSBase getObject() 

Source Link

Document

This will get the object that this object encapsulates.

Usage

From source file:net.padaf.preflight.helpers.FileSpecificationValidationHelper.java

License:Apache License

/**
 * Validate a FileSpec dictionary, a FileSpec dictionary mustn't have the EF
 * (EmbeddedFile) entry./*from w w  w  .j a va2 s .c o  m*/
 * 
 * @param handler
 *          The document handler
 * @param cObj
 *          the FileSpec Dictionary
 * @return
 */
public List<ValidationError> validateFileSpecification(DocumentHandler handler, COSObject cObj) {
    List<ValidationError> result = new ArrayList<ValidationError>(0);
    COSDictionary fileSpec = (COSDictionary) cObj.getObject();

    // ---- Check dictionary entries
    // ---- Only the EF entry is forbidden
    if (fileSpec.getItem(COSName.getPDFName(FILE_SPECIFICATION_KEY_EMBEDDED_FILE)) != null) {
        result.add(new ValidationError(ERROR_SYNTAX_EMBEDDED_FILES,
                "EmbeddedFile entry is present in a FileSpecification dictionary"));
    }

    return result;
}

From source file:net.padaf.preflight.helpers.FontValidationHelper.java

License:Apache License

@Override
public List<ValidationError> innerValidate(DocumentHandler handler) throws ValidationException {
    List<ValidationError> result = new ArrayList<ValidationError>(0);
    PDDocument pdfDoc = handler.getDocument();
    COSDocument cDoc = pdfDoc.getDocument();

    List<?> lCOSObj = cDoc.getObjects();

    List<FontValidator> lType3 = new ArrayList<FontValidator>();

    for (Object o : lCOSObj) {
        COSObject cObj = (COSObject) o;

        // If this object represents a Stream, the Dictionary must contain the
        // Length key
        COSBase cBase = cObj.getObject();
        if (cBase instanceof COSDictionary) {
            COSDictionary dic = (COSDictionary) cBase;
            String type = dic.getNameAsString(COSName.getPDFName(DICTIONARY_KEY_TYPE));
            if (type != null && FONT_DICTIONARY_VALUE_FONT.equals(type)) {
                FontValidator fontVal = fontValidationFactory.getFontValidator(cObj, handler);
                if (fontVal instanceof Type3FontValidator) {
                    lType3.add(fontVal);
                } else {
                    validateFont(handler, fontVal, result);
                }/*from   ww w  . j  a v  a  2s. co m*/
            }
        }
    }

    // ---- Type 3 can contain other font, so type 3 are validated at the end.
    for (FontValidator t3FontVal : lType3) {
        validateFont(handler, t3FontVal, result);
    }

    return result;
}

From source file:net.padaf.preflight.helpers.GraphicsValidationHelper.java

License:Apache License

@Override
public List<ValidationError> innerValidate(DocumentHandler handler) throws ValidationException {
    List<ValidationError> result = new ArrayList<ValidationError>(0);
    PDDocument pdfDoc = handler.getDocument();

    // ---- Checks all XObjects
    COSDocument cDoc = pdfDoc.getDocument();
    List<?> lCOSObj = cDoc.getObjects();
    for (Object o : lCOSObj) {
        COSObject cObj = (COSObject) o;
        COSBase cBase = cObj.getObject();
        if (cBase instanceof COSDictionary) {
            COSDictionary dic = (COSDictionary) cBase;
            String type = dic.getNameAsString(COSName.getPDFName(DICTIONARY_KEY_TYPE));
            if (type != null && DICTIONARY_KEY_XOBJECT.equals(type)) {
                result.addAll(validateXObject(handler, cObj));
            } else if (type != null && DICTIONARY_KEY_PATTERN.equals(type)) {
                result.addAll(validatePattern(handler, cObj));
            }/*from w  w w  .j ava 2s .  c  o  m*/
        }
    }
    return result;
}

From source file:net.padaf.preflight.helpers.GraphicsValidationHelper.java

License:Apache License

public List<ValidationError> validatePattern(DocumentHandler handler, COSObject cObj)
        throws ValidationException {
    COSDictionary cosPattern = (COSDictionary) cObj.getObject();
    int ptype = cosPattern.getInt(DICTIONARY_KEY_PATTERN_TYPE);

    XObjectValidator validator = null;//from   w  w  w . j a  v  a  2s .com

    switch (ptype) {
    case DICTIONARY_PATTERN_TILING:
        validator = new TilingPattern(handler, (COSStream) cosPattern);
        break;
    case DICTIONARY_PATTERN_SHADING:
        validator = new ShadingPattern(handler, cosPattern);
        break;
    default:
        throw new ValidationException("Unkown pattern type : " + ptype);
    }

    return validator.validate();
}

From source file:net.padaf.preflight.helpers.GraphicsValidationHelper.java

License:Apache License

public List<ValidationError> validateXObject(DocumentHandler handler, COSObject cObj)
        throws ValidationException {
    XObjectValidator xObjVal = null;//w ww .  j a  v  a2 s.c  om

    // ---- According to the XObject subtype, the validation isn't processed by
    // the same Validator
    COSStream dic = (COSStream) cObj.getObject();
    String subtype = dic.getNameAsString(COSName.getPDFName(DICTIONARY_KEY_SUBTYPE));

    if (XOBJECT_DICTIONARY_VALUE_SUBTYPE_IMG.equals(subtype)) {
        xObjVal = new XObjImageValidator(handler, dic);
    } else if (XOBJECT_DICTIONARY_VALUE_SUBTYPE_FORM.equals(subtype)) {
        xObjVal = new XObjFormValidator(handler, dic);
    } else if (XOBJECT_DICTIONARY_VALUE_SUBTYPE_POSTSCRIPT.equals(subtype)) {
        xObjVal = new XObjPostscriptValidator(handler, dic);
    } else {
        throw new ValidationException("Invalid XObject subtype");
    }

    return xObjVal.validate();
}

From source file:net.padaf.preflight.helpers.StreamValidationHelper.java

License:Apache License

@Override
public List<ValidationError> innerValidate(DocumentHandler handler) throws ValidationException {
    List<ValidationError> result = new ArrayList<ValidationError>(0);
    PDDocument pdfDoc = handler.getDocument();
    COSDocument cDoc = pdfDoc.getDocument();

    List<?> lCOSObj = cDoc.getObjects();
    for (Object o : lCOSObj) {
        COSObject cObj = (COSObject) o;

        // If this object represents a Stream, the Dictionary must contain the
        // Length key
        COSBase cBase = cObj.getObject();
        if (cBase instanceof COSStream) {
            // it is a stream
            result.addAll(validateStreamObject(handler, cObj));
        }/* w  w w  .  ja va 2s. co m*/
    }
    return result;
}

From source file:net.padaf.preflight.helpers.StreamValidationHelper.java

License:Apache License

public List<ValidationError> validateStreamObject(DocumentHandler handler, COSObject cObj)
        throws ValidationException {
    List<ValidationError> result = new ArrayList<ValidationError>(0);
    COSStream streamObj = (COSStream) cObj.getObject();

    // ---- Check dictionary entries
    // ---- Only the Length entry is mandatory
    // ---- In a PDF/A file, F, FFilter and FDecodeParms are forbidden
    checkDictionaryEntries(streamObj, result);
    // ---- check stream length
    checkStreamLength(handler, cObj, result);
    // ---- Check the Filter value(s)
    checkFilters(streamObj, handler, result);

    return result;
}

From source file:net.padaf.preflight.helpers.StreamValidationHelper.java

License:Apache License

protected void checkStreamLength(DocumentHandler handler, COSObject cObj, List<ValidationError> result)
        throws ValidationException {
    COSStream streamObj = (COSStream) cObj.getObject();
    int length = streamObj.getInt(COSName.getPDFName(STREAM_DICTIONARY_KEY_LENGHT));
    InputStream ra = null;//from w w w  . j a v  a  2s . c  o m
    try {
        ra = handler.getSource().getInputStream();
        Integer offset = (Integer) handler.getDocument().getDocument().getXrefTable()
                .get(new COSObjectKey(cObj));

        // ---- go to the beginning of the object
        long skipped = 0;
        while (skipped != offset) {
            long curSkip = ra.skip(offset - skipped);
            if (curSkip < 0) {
                throw new ValidationException("Unable to skip bytes in the PDFFile to check stream length");
            }
            skipped += curSkip;
        }

        // ---- go to the stream key word
        if (readUntilStream(ra)) {
            int c = ra.read();
            if (c == '\r') {
                ra.read();
            } // else c is '\n' no more character to read

            // ---- Here is the true beginning of the Stream Content.
            // ---- Read the given length of bytes and check the 10 next bytes
            // ---- to see if there are endstream.
            byte[] buffer = new byte[1024];
            int nbBytesToRead = length;

            do {
                int cr = 0;
                if (nbBytesToRead > 1024) {
                    cr = ra.read(buffer, 0, 1024);
                } else {
                    cr = ra.read(buffer, 0, nbBytesToRead);
                }
                if (cr == -1) {
                    result.add(new ValidationResult.ValidationError(
                            ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID,
                            "Stream length is invalide"));
                    return;
                } else {
                    nbBytesToRead = nbBytesToRead - cr;
                }
            } while (nbBytesToRead > 0);

            int len = "endstream".length() + 2;
            byte[] buffer2 = new byte[len];
            for (int i = 0; i < len; ++i) {
                buffer2[i] = (byte) ra.read();
            }

            // ---- check the content of 10 last characters
            String endStream = new String(buffer2);
            if (buffer2[0] == '\r' && buffer2[1] == '\n') {
                if (!endStream.contains("endstream")) {
                    result.add(new ValidationResult.ValidationError(
                            ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID,
                            "Stream length is invalide"));
                }
            } else if (buffer2[0] == '\r' && buffer2[1] == 'e') {
                if (!endStream.contains("endstream")) {
                    result.add(new ValidationResult.ValidationError(
                            ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID,
                            "Stream length is invalide"));
                }
            } else if (buffer2[0] == '\n' && buffer2[1] == 'e') {
                if (!endStream.contains("endstream")) {
                    result.add(new ValidationResult.ValidationError(
                            ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID,
                            "Stream length is invalide"));
                }
            } else {
                result.add(new ValidationResult.ValidationError(
                        ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID, "Stream length is invalide"));
            }

        } else {
            result.add(new ValidationResult.ValidationError(
                    ValidationConstants.ERROR_SYNTAX_STREAM_LENGTH_INVALID, "Stream length is invalide"));
        }
    } catch (IOException e) {
        throw new ValidationException("Unable to read a stream to validate it due to : " + e.getMessage(), e);
    } finally {
        if (ra != null) {
            IOUtils.closeQuietly(ra);
        }
    }
}

From source file:net.padaf.preflight.utils.COSUtils.java

License:Apache License

/**
 * return true if the elt is a COSDictionary or a reference to a COSDictionary
 * /*  ww w .  j  a  v  a2s.  c  o m*/
 * @param elt
 * @param doc
 * @return
 */
public static boolean isDictionary(COSBase elt, COSDocument doc) {
    if (elt instanceof COSObject) {
        try {
            COSObjectKey key = new COSObjectKey((COSObject) elt);
            COSObject obj = doc.getObjectFromPool(key);
            return (obj != null && obj.getObject() instanceof COSDictionary);
        } catch (IOException e) {
            return false;
        }
    }
    return (elt instanceof COSDictionary);
}

From source file:net.padaf.preflight.utils.COSUtils.java

License:Apache License

/**
 * return true if the elt is a COSString or a reference to a COSString
 * /*w ww  .  j  av a 2  s  .co m*/
 * @param elt
 * @param doc
 * @return
 */
public static boolean isString(COSBase elt, COSDocument doc) {
    if (elt instanceof COSObject) {
        try {
            COSObjectKey key = new COSObjectKey((COSObject) elt);
            COSObject obj = doc.getObjectFromPool(key);
            return (obj != null
                    && (obj.getObject() instanceof COSString || obj.getObject() instanceof COSName));
        } catch (IOException e) {
            return false;
        }
    }

    return (elt instanceof COSString || elt instanceof COSName);
}