public PDPageTreeBranch findTerminal(int index) { if (isTerminal) { return this; } for (PDPageTreeNode branch : this.children) { if (index >= branch.getLeafCount()) { index -= branch.getLeafCount(); } else { return branch.findTerminal(index); } } int lastIndex = this.children.size() - 1; return this.children.get(lastIndex).findTerminal(index); }
public PDPage findTerminalPDPage(int index) { if (isTerminal) { index = Math.min(index, leafCount); return (PDPage) this.getChild(index); } for (PDPageTreeNode branch : this.children) { if (index >= branch.getLeafCount()) { index -= branch.getLeafCount(); } else { return branch.findTerminalPDPage(index); } } int lastIndex = this.children.size() - 1; return this.children.get(lastIndex).findTerminalPDPage(index); }
public PDPageTreeBranch(final COSObject obj) { this.isTerminal = true; this.leafCount = 0; this.children = new ArrayList<>(); super.setObject(obj); }
private boolean insertNode(final PDPageTreeNode node, int insertAt) { node.setParent(this); this.children.add(insertAt, node); getObject().getKey(ASAtom.KIDS).insert(insertAt, node.getObject()); if (getChildCount() > PD_TREE_MAX_CHILD) { PDPageTreeBranch rightNeighbour = new PDPageTreeBranch(this.getObject()); if (getParent() != null) { // Determining the position of current branch in its parent's childlist int insPos = getParent().getIndex(this) + 1; return getParent().insertNode(rightNeighbour, insPos); } else { new PDPageTreeBranch(this, rightNeighbour); return true; } } return false; }
private Set<COSKey> parentKeysForChildren() { Set<COSKey> res = new HashSet<>(); PDPageTreeNode curr = this; while (curr != null) { COSObject object = curr.getObject(); COSKey objectKey = object.getObjectKey(); if (res.contains(objectKey)) { throw new LoopedException("Page tree loop found"); } res.add(objectKey); curr = this.getParent(); } return Collections.unmodifiableSet(res); }
protected void updateFromObject() { clear(); COSObject kids = getObject().getKey(ASAtom.KIDS); if (kids != null && !kids.empty()) { Set<COSKey> keys = parentKeysForChildren(); for (int i = 0; i < kids.size(); i++) { COSObject obj = kids.at(i); if (keys.contains(obj.getObjectKey())) { throw new LoopedException("Page tree loop found"); } PDPageTreeNode kid_i; if (obj.getNameKey(ASAtom.TYPE) == ASAtom.PAGE) { kid_i = new PDPage(obj); } else if (obj.getNameKey(ASAtom.TYPE) == ASAtom.PAGES) { kid_i = new PDPageTreeBranch(obj); isTerminal = false; } else { //TODO : ASException throw new RuntimeException("PDPageTreeBranch::UpdateFromObject()" + StringExceptions.UNKNOWN_TYPE_PAGE_TREE_NODE); } kid_i.setParent(this); this.children.add(kid_i); } } Long leafCount = getObject().getIntegerKey(ASAtom.COUNT); if (leafCount != null) { this.leafCount = leafCount.intValue(); } }
public PDDocument getPDDocument() { final COSDocument cosDocument = super.getObject().getDocument(); if (cosDocument != null) { return cosDocument.getPDDocument(); } else { return null; } }
protected COSObject getInheritableResources() { COSObject value = getObject().getKey(ASAtom.RESOURCES); if (value != null && !value.empty()) { return value; } if (parent != null) { return parent.getInheritableResources(); } return null; }
public PDPageTreeBranch() { this.isTerminal = true; this.leafCount = 0; this.children = new ArrayList<>(); super.setObject(new COSObject()); }
protected void updateToObject() { COSObject branch = getObject(); COSObject kids = COSArray.construct(); for (PDPageTreeNode node : this.children) { kids.add(node.getObject()); } branch.setKey(ASAtom.KIDS, kids); COSObject count = COSInteger.construct(this.leafCount); branch.setKey(ASAtom.COUNT, count); PDPageTreeBranch parentNode = getParent(); if (parentNode != null) { branch.setKey(ASAtom.PARENT, parentNode.getObject()); } }
public PDPage(final COSObject obj) { super(); this.pageNumber = 0; this.pagesTotal = 0; super.setObject(obj); initializeContents(obj); }