Example usage for org.apache.pdfbox.cos COSArray indexOf

List of usage examples for org.apache.pdfbox.cos COSArray indexOf

Introduction

In this page you can find the example usage for org.apache.pdfbox.cos COSArray indexOf.

Prototype

public int indexOf(COSBase object) 

Source Link

Document

This will return the index of the entry or -1 if it is not found.

Usage

From source file:org.apache.pdflens.views.treeview.PDFTreeModel.java

License:Apache License

/** Returns the index of child in parent.  If <code>parent</code>
 * is <code>null</code> or <code>child</code> is <code>null</code>,
 * returns -1.//from ww w  .  j  a va 2  s . c om
 *
 * @param parent a note in the tree, obtained from this data source
 * @param child the node we are interested in
 * @return the index of the child in the parent, or -1 if either
 *    <code>child</code> or <code>parent</code> are <code>null</code>
 *
 */
public int getIndexOfChild(Object parent, Object child) {
    int retval = -1;
    if (parent != null && child != null) {
        if (parent instanceof COSArray) {
            COSArray array = (COSArray) parent;
            if (child instanceof ArrayEntry) {
                ArrayEntry arrayEntry = (ArrayEntry) child;
                retval = arrayEntry.getIndex();
            } else {
                retval = array.indexOf((COSBase) child);
            }
        } else if (parent instanceof COSDictionary) {
            MapEntry entry = (MapEntry) child;
            COSDictionary dict = (COSDictionary) parent;
            List<COSName> keys = new ArrayList<COSName>(dict.keySet());
            Collections.sort(keys);
            for (int i = 0; retval == -1 && i < keys.size(); i++) {
                if (keys.get(i).equals(entry.getKey())) {
                    retval = i;
                }
            }
        } else if (parent instanceof MapEntry) {
            retval = getIndexOfChild(((MapEntry) parent).getValue(), child);
        } else if (parent instanceof ArrayEntry) {
            retval = getIndexOfChild(((ArrayEntry) parent).getValue(), child);
        } else if (parent instanceof COSDocument) {
            retval = ((COSDocument) parent).getObjects().indexOf(child);
        } else if (parent instanceof COSObject) {
            retval = 0;
        } else {
            throw new RuntimeException("Unknown COS type " + parent.getClass().getName());
        }
    }
    return retval;
}