Skip to content

Commit

Permalink
Merge pull request #612 from veraPDF/inheritable_attributes
Browse files Browse the repository at this point in the history
Support inheritable attributes
  • Loading branch information
MaximPlusov authored Dec 12, 2023
2 parents caf67c5 + 6131be4 commit 0148cb0
Showing 1 changed file with 36 additions and 30 deletions.
66 changes: 36 additions & 30 deletions src/main/java/org/verapdf/tools/AttributeHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,74 +12,80 @@ public class AttributeHelper {
public static final String LIST = "List";
public static final String NONE = "None";

public static String getListNumbering(org.verapdf.pd.PDObject simplePDObject) {
return getNameAttributeValue(simplePDObject, ASAtom.LIST_NUMBERING, LIST, NONE);
public static String getListNumbering(COSObject simpleCosObject) {
return getNameAttributeValue(simpleCosObject, ASAtom.LIST_NUMBERING, LIST, NONE, true);
}

public static String getNoteType(org.verapdf.pd.PDObject simplePDObject) {
return getNameAttributeValue(simplePDObject, ASAtom.NOTE_TYPE, TaggedPDFConstants.FENOTE, NONE);
public static String getNoteType(COSObject simpleCosObject) {
return getNameAttributeValue(simpleCosObject, ASAtom.NOTE_TYPE, TaggedPDFConstants.FENOTE, NONE, false);
}
public static Long getColSpan(org.verapdf.pd.PDObject simplePDObject) {
return getIntegerAttributeValue(simplePDObject, ASAtom.COL_SPAN, TaggedPDFConstants.TABLE, 1L);
public static Long getColSpan(COSObject simpleCosObject) {
return getIntegerAttributeValue(simpleCosObject, ASAtom.COL_SPAN, TaggedPDFConstants.TABLE, 1L);
}

public static Long getRowSpan(org.verapdf.pd.PDObject simplePDObject) {
return getIntegerAttributeValue(simplePDObject, ASAtom.ROW_SPAN, TaggedPDFConstants.TABLE, 1L);
public static Long getRowSpan(COSObject simpleCosObject) {
return getIntegerAttributeValue(simpleCosObject, ASAtom.ROW_SPAN, TaggedPDFConstants.TABLE, 1L);
}

public static String getScope(org.verapdf.pd.PDObject simplePDObject) {
return getNameAttributeValue(simplePDObject, ASAtom.SCOPE, TaggedPDFConstants.TABLE, null);
public static String getScope(COSObject simpleCosObject) {
return getNameAttributeValue(simpleCosObject, ASAtom.SCOPE, TaggedPDFConstants.TABLE, null, false);
}

public static String getRole(org.verapdf.pd.PDObject simplePDObject) {
return getNameAttributeValue(simplePDObject, ASAtom.ROLE, PRINT_FIELD, null);
public static String getRole(COSObject simpleCosObject) {
return getNameAttributeValue(simpleCosObject, ASAtom.ROLE, PRINT_FIELD, null, false);
}

public static COSArray getBBox(org.verapdf.pd.PDObject simplePDObject) {
return getArrayAttributeValue(simplePDObject, ASAtom.BBOX, LAYOUT, null);
public static COSArray getBBox(COSObject simpleCosObject) {
return getArrayAttributeValue(simpleCosObject, ASAtom.BBOX, LAYOUT, null);
}

public static COSArray getArrayAttributeValue(org.verapdf.pd.PDObject simplePDObject, ASAtom attributeName, String O,
public static COSArray getArrayAttributeValue(COSObject simpleCosObject, ASAtom attributeName, String O,
COSArray defaultValue) {
COSObject object = getAttributeValue(simplePDObject, attributeName, O, COSObjType.COS_ARRAY);
COSObject object = getAttributeValue(simpleCosObject, attributeName, O, COSObjType.COS_ARRAY);
if (object.getType() == COSObjType.COS_ARRAY) {
return (COSArray) object.getDirectBase();
}
return defaultValue;
}

private static String getNameAttributeValue(org.verapdf.pd.PDObject simplePDObject, ASAtom attributeName, String O,
String defaultValue) {
COSObject object = getAttributeValue(simplePDObject, attributeName, O, COSObjType.COS_NAME);
private static String getNameAttributeValue(COSObject simpleCosObject, ASAtom attributeName, String O,
String defaultValue, boolean isInheritable) {
COSObject object = getAttributeValue(simpleCosObject, attributeName, O, COSObjType.COS_NAME);
if (object.getType() == COSObjType.COS_NAME) {
return object.getString();
}
if (isInheritable) {
COSObject parent = simpleCosObject.getKey(ASAtom.P);
if (parent != null) {
return getNameAttributeValue(parent, attributeName, O, defaultValue, true);
}
}
return defaultValue;
}

private static Long getIntegerAttributeValue(org.verapdf.pd.PDObject simplePDObject, ASAtom attributeName, String O,
private static Long getIntegerAttributeValue(COSObject simpleCosObject, ASAtom attributeName, String O,
Long defaultValue) {
COSObject object = getAttributeValue(simplePDObject, attributeName, O, COSObjType.COS_INTEGER);
COSObject object = getAttributeValue(simpleCosObject, attributeName, O, COSObjType.COS_INTEGER);
if (object.getType() == COSObjType.COS_INTEGER) {
return object.getInteger();
}
return defaultValue;
}

private static COSObject getAttributeValue(org.verapdf.pd.PDObject simplePDObject, ASAtom attributeName, String O,
private static COSObject getAttributeValue(COSObject simpleCosObject, ASAtom attributeName, String O,
COSObjType type) {
COSObject attributeValue = getAttributeValue(simplePDObject.getKey(ASAtom.A), attributeName, O, type);
COSObject attributeValue = getAttributeObject(simpleCosObject.getKey(ASAtom.A), attributeName, O, type);
if (attributeValue == null) {
COSObject className = simplePDObject.getKey(ASAtom.C);
COSObject className = simpleCosObject.getKey(ASAtom.C);
COSObject classMap = StaticResources.getDocument().getStructTreeRoot().getClassMap();
if (className != null && classMap != null) {
if (className.getType() == COSObjType.COS_NAME) {
attributeValue = getAttributeValue(classMap.getKey(className.getName()),
attributeValue = getAttributeObject(classMap.getKey(className.getName()),
attributeName, O, type);
} else if (className.getType() == COSObjType.COS_ARRAY) {
for (COSObject entry : (COSArray)className.getDirectBase()) {
if (entry != null && entry.getType() == COSObjType.COS_NAME) {
attributeValue = getAttributeValue(classMap.getKey(entry.getName()),
attributeValue = getAttributeObject(classMap.getKey(entry.getName()),
attributeName, O, type);
if (attributeValue != null) {
break;
Expand All @@ -92,24 +98,24 @@ private static COSObject getAttributeValue(org.verapdf.pd.PDObject simplePDObjec
return attributeValue != null ? attributeValue : COSObject.getEmpty();
}

private static COSObject getAttributeValue(COSObject attributeObject, ASAtom attributeName, String O,
private static COSObject getAttributeObject(COSObject attributeObject, ASAtom attributeName, String O,
COSObjType type) {
if (attributeObject == null) {
return null;
}
if (attributeObject.getType() == COSObjType.COS_ARRAY) {
for (COSObject object : (COSArray) attributeObject.getDirectBase()) {
COSObject value = getAttributeValue(object, attributeName, O);
COSObject value = getAttributeObject(object, attributeName, O);
if (value.getType() == type) {
return value;
}
}
}
COSObject value = getAttributeValue(attributeObject, attributeName, O);
COSObject value = getAttributeObject(attributeObject, attributeName, O);
return value.getType() == type ? value : null;
}

private static COSObject getAttributeValue(COSObject object, ASAtom attributeName, String O) {
private static COSObject getAttributeObject(COSObject object, ASAtom attributeName, String O) {
if (object.getType() == COSObjType.COS_DICT && O.equals(object.getNameKeyStringValue(ASAtom.O))) {
return object.getKey(attributeName);
}
Expand Down

0 comments on commit 0148cb0

Please sign in to comment.