Java Code Examples for org.apache.pdfbox.cos.COSBase#equals()

The following examples show how to use org.apache.pdfbox.cos.COSBase#equals() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PDAttributeObject.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Is the value changed?
 * 
 * @param oldValue old value
 * @param newValue new value
 * @return <code>true</code> if the value is changed, <code>false</code>
 * otherwise
 */
private boolean isValueChanged(COSBase oldValue, COSBase newValue)
{
    if (oldValue == null)
    {
        return newValue != null;
    }
    return !oldValue.equals(newValue);
}
 
Example 2
Source File: PDFMergerUtility.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private void mergeRoleMap(PDStructureTreeRoot srcStructTree, PDStructureTreeRoot destStructTree)
{
    COSDictionary srcDict = srcStructTree.getCOSObject().getCOSDictionary(COSName.ROLE_MAP);
    COSDictionary destDict = destStructTree.getCOSObject().getCOSDictionary(COSName.ROLE_MAP);
    if (srcDict == null)
    {
        return;
    }
    if (destDict == null)
    {
        destStructTree.getCOSObject().setItem(COSName.ROLE_MAP, srcDict); // clone not needed
        return;
    }
    for (Map.Entry<COSName, COSBase> entry : srcDict.entrySet())
    {
        COSBase destValue = destDict.getDictionaryObject(entry.getKey());
        if (destValue != null && destValue.equals(entry.getValue()))
        {
            // already exists, but identical
            continue;
        }
        if (destDict.containsKey(entry.getKey()))
        {
            LOG.warn("key " + entry.getKey() + " already exists in destination RoleMap");
        }
        else
        {
            destDict.setItem(entry.getKey(), entry.getValue());
        }
    }
}
 
Example 3
Source File: COSParser.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
private int checkPagesDictionary(COSDictionary pagesDict, Set<COSObject> set)
{
    // check for kids
    COSBase kids = pagesDict.getDictionaryObject(COSName.KIDS);
    int numberOfPages = 0;
    if (kids instanceof COSArray)
    {
        COSArray kidsArray = (COSArray) kids;
        List<? extends COSBase> kidsList = kidsArray.toList();
        for (COSBase kid : kidsList)
        {
            if (!(kid instanceof COSObject) || set.contains((COSObject) kid))
            {
                kidsArray.remove(kid);
                continue;
            }
            COSObject kidObject = (COSObject) kid;
            COSBase kidBaseobject = kidObject.getObject();
            // object wasn't dereferenced -> remove it
            if (kidBaseobject == null || kidBaseobject.equals(COSNull.NULL))
            {
                LOG.warn("Removed null object " + kid + " from pages dictionary");
                kidsArray.remove(kid);
            }
            else if (kidBaseobject instanceof COSDictionary)
            {
                COSDictionary kidDictionary = (COSDictionary) kidBaseobject;
                COSName type = kidDictionary.getCOSName(COSName.TYPE);
                if (COSName.PAGES.equals(type))
                {
                    // process nested pages dictionaries
                    set.add(kidObject);
                    numberOfPages += checkPagesDictionary(kidDictionary, set);
                }
                else if (COSName.PAGE.equals(type))
                {
                    // count pages
                    numberOfPages++;
                }
            }
        }
    }
    // fix counter
    pagesDict.setInt(COSName.COUNT, numberOfPages);
    return numberOfPages;
}
 
Example 4
Source File: PDStructureNode.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Removes a COS base kid.
 * 
 * @param object the COS base
 * @return <code>true</code> if the kid was removed, <code>false</code> otherwise
 */
protected boolean removeKid(COSBase object)
{
    if (object == null)
    {
        return false;
    }
    COSBase k = this.getCOSObject().getDictionaryObject(COSName.K);
    if (k == null)
    {
        // no kids: objectable is not a kid
        return false;
    }
    else if (k instanceof COSArray)
    {
        // currently more than one kid: remove kid from existing array
        COSArray array = (COSArray) k;
        boolean removed = array.removeObject(object);
        // if now only one kid: set remaining kid as kids
        if (array.size() == 1)
        {
            this.getCOSObject().setItem(COSName.K, array.getObject(0));
        }
        return removed;
    }
    else
    {
        // currently one kid: if current kid equals given object, remove kids entry
        boolean onlyKid = k.equals(object);
        if (!onlyKid && (k instanceof COSObject))
        {
            COSBase kObj = ((COSObject) k).getObject();
            onlyKid = kObj.equals(object);
        }
        if (onlyKid)
        {
            this.getCOSObject().setItem(COSName.K, null);
            return true;
        }
        return false;
    }
}