Skip to content

Commit

Permalink
Fix getRoleMapToSameNamespaceTag method in TaggedPDFHelper.
Browse files Browse the repository at this point in the history
Add isCircularMappingExist to TaggedPDFHelper
  • Loading branch information
MaximPlusov committed Jun 5, 2024
1 parent a75d5e6 commit 623cf08
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions src/main/java/org/verapdf/tools/TaggedPDFHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,19 +184,57 @@ public static String getRoleMapToSameNamespaceTag(StructureType type) {
addVisited(type);
StructureType prev = type;
StructureType curr = getEquivalent(prev, Collections.emptyMap());
Map<String, ASAtom> processedTypes = new HashMap<>();
while (curr != null) {
if (curr.getNameSpaceURI() != null && curr.getNameSpaceURI().equals(prev.getNameSpaceURI())) {
return prev.getNameSpaceURI() + ":" + (prev.getType() != null ? prev.getType().getValue() : null);
if (curr.getNameSpaceURI() != null && processedTypes.containsKey(curr.getNameSpaceURI())) {
ASAtom processedType = processedTypes.get(curr.getNameSpaceURI());
if (curr.getType() != null && !Objects.equals(curr.getType(), processedType)) {
return curr.getNameSpaceURI() + ":" + (processedType != null ? processedType.getValue() : null);
}
return null;
}
if (isVisited(curr)) {
return null;
}
if (curr.getNameSpaceURI() != null) {
processedTypes.put(curr.getNameSpaceURI(), curr.getType());
}
addVisited(curr);
prev = curr;
curr = getEquivalent(prev, Collections.emptyMap());
}
return null;
}

public static Boolean isCircularMappingExist(StructureType type) {
if (type == null) {
return null;
}
visitedWithNS.clear();
visitedWithoutNS.clear();
addVisited(type);
StructureType prev = type;
StructureType curr = getEquivalent(prev, Collections.emptyMap());
Map<String, ASAtom> processedTypes = new HashMap<>();
while (curr != null) {
if (curr.getNameSpaceURI() != null && processedTypes.containsKey(curr.getNameSpaceURI())) {
ASAtom processedType = processedTypes.get(curr.getNameSpaceURI());
if (curr.getType() != null && Objects.equals(curr.getType(), processedType)) {
return true;
}
}
if (isVisited(curr)) {
return false;
}
if (curr.getNameSpaceURI() != null) {
processedTypes.put(curr.getNameSpaceURI(), curr.getType());
}
addVisited(curr);
prev = curr;
curr = getEquivalent(prev, Collections.emptyMap());
}
return false;
}

private static StructureType getEquivalent(StructureType type, Map<ASAtom, ASAtom> roleMap) {
PDStructureNameSpace nameSpace = type.getNameSpace();
Expand Down

0 comments on commit 623cf08

Please sign in to comment.