Skip to content

Commit

Permalink
Merge pull request #635 from veraPDF/struct_tree_loop
Browse files Browse the repository at this point in the history
Add warnings for struct tree loop
  • Loading branch information
MaximPlusov authored Jan 24, 2024
2 parents aa9cd8a + 05b8408 commit cdf0bf9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.verapdf.cos.COSKey;
import org.verapdf.cos.COSObjType;
import org.verapdf.cos.COSObject;
import org.verapdf.exceptions.LoopedException;
import org.verapdf.gf.model.impl.pd.util.PDResourcesHandler;
import org.verapdf.model.baselayer.Object;
import org.verapdf.model.operator.Op_BDC;
Expand All @@ -37,12 +36,16 @@
import java.util.List;
import java.util.Set;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* @author Timur Kamalov
*/
public class GFOp_BDC extends GFOpMarkedContent implements Op_BDC {

private static final Logger LOGGER = Logger.getLogger(GFOp_BDC.class.getCanonicalName());

/** Type name for {@code GFOp_BDC} */
public static final String OP_BDC_TYPE = "Op_BDC";
public final StructureElementAccessObject structureElementAccessObject;
Expand Down Expand Up @@ -101,7 +104,8 @@ public String getStructParentLang() {
while (baseLang == null && parent != null) {
COSKey key = parent.getObjectKey();
if (keys.contains(key)) {
throw new LoopedException("Struct tree loop found");
LOGGER.log(Level.WARNING, "Struct tree loop found");
break;
}
if (key != null) {
keys.add(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.verapdf.cos.COSString;
import org.verapdf.cos.COSKey;
import org.verapdf.cos.COSArray;
import org.verapdf.exceptions.LoopedException;
import org.verapdf.gf.model.impl.containers.StaticContainers;
import org.verapdf.gf.model.impl.cos.GFCosActualText;
import org.verapdf.gf.model.impl.cos.GFCosAlt;
Expand All @@ -46,11 +45,16 @@
import org.verapdf.tools.TaggedPDFHelper;

import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* @author Maksim Bezrukov
*/
public class GFPDStructElem extends GFPDStructTreeNode implements PDStructElem {

private static final Logger LOGGER = Logger.getLogger(GFPDStructElem.class.getCanonicalName());

/**
* Type name for {@code GFPDStructElem}
*/
Expand Down Expand Up @@ -264,7 +268,8 @@ public String getparentLang() {
while (baseLang == null && parent != null) {
key = parent.getObject().getObjectKey();
if (keys.contains(key)) {
throw new LoopedException("Struct tree loop found");
LOGGER.log(Level.WARNING, "Struct tree loop found");
break;
}
if (key != null) {
keys.add(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package org.verapdf.gf.model.impl.pd.gfse.contents;

import org.verapdf.as.ASAtom;
import org.verapdf.cos.COSKey;
import org.verapdf.cos.COSObject;
import org.verapdf.model.operator.Operator;
import org.verapdf.model.selayer.SEGroupedContent;
Expand All @@ -29,10 +30,16 @@
import org.verapdf.tools.TaggedPDFConstants;
import org.verapdf.tools.TaggedPDFRoleMapHelper;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

public abstract class GFSEGroupedContent extends GFSEContentItem implements SEGroupedContent {

private static final Logger LOGGER = Logger.getLogger(GFSEGroupedContent.class.getCanonicalName());

protected final List<Operator> operators;
protected final COSObject parentStructElem;
protected final String parentsTags;
Expand Down Expand Up @@ -87,12 +94,21 @@ public Boolean getisArtifact() {

protected Boolean hasParentWithStandardType(String standardType) {
TaggedPDFRoleMapHelper taggedPDFRoleMapHelper = StaticResources.getRoleMapHelper();
Set<COSKey> keys = new HashSet<>();
if (parentStructElem != null && taggedPDFRoleMapHelper != null) {
PDStructElem structElem = new PDStructElem(parentStructElem, taggedPDFRoleMapHelper.getRoleMap());
while (structElem != null) {
if (standardType.equals(PDStructElem.getStructureElementStandardType(structElem))) {
return true;
}
COSKey key = structElem.getObject().getObjectKey();
if (keys.contains(key)) {
LOGGER.log(Level.WARNING, "Struct tree loop found");
break;
}
if (key != null) {
keys.add(key);
}
structElem = structElem.getParent();
}
}
Expand Down Expand Up @@ -127,11 +143,20 @@ public Boolean getisTaggedContent() {
}

private boolean isTaggedContent() {
Set<COSKey> keys = new HashSet<>();
PDStructElem structElem = new PDStructElem(parentStructElem, null);
while (structElem != null) {
if (structElem.getType() == ASAtom.STRUCT_TREE_ROOT) {
return true;
}
COSKey key = structElem.getObject().getObjectKey();
if (keys.contains(key)) {
LOGGER.log(Level.WARNING, "Struct tree loop found");
break;
}
if (key != null) {
keys.add(key);
}
structElem = structElem.getParent();
}
return false;
Expand Down

0 comments on commit cdf0bf9

Please sign in to comment.