diff --git a/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/GFPDStructElem.java b/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/GFPDStructElem.java index ea16bf641..e2aff85f7 100644 --- a/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/GFPDStructElem.java +++ b/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/GFPDStructElem.java @@ -49,22 +49,16 @@ import java.util.List; import java.util.HashSet; import java.util.Set; -import java.util.Objects; -import java.util.stream.Collectors; /** * @author Maksim Bezrukov */ -public class GFPDStructElem extends GFPDObject implements PDStructElem { +public class GFPDStructElem extends GFPDStructTreeNode implements PDStructElem { /** * Type name for {@code GFPDStructElem} */ public static final String STRUCTURE_ELEMENT_TYPE = "PDStructElem"; - /** - * Link name for {@code K} key - */ - public static final String CHILDREN = "K"; /** * Link name for {@code S} key */ @@ -80,8 +74,6 @@ public class GFPDStructElem extends GFPDObject implements PDStructElem { private final String standardType; - private List children; - protected GFPDStructElem(org.verapdf.pd.structure.PDStructElem structElemDictionary, String standardType, String type) { super(structElemDictionary, type); ASAtom subtype = this.simplePDObject.getNameKey(ASAtom.S); @@ -109,21 +101,6 @@ public String getType() { return type == null ? null : type.getValue(); } - @Override - public String getkidsStandardTypes() { - if (StaticContainers.getFlavour() != null && - StaticContainers.getFlavour().getPart() == PDFAFlavour.Specification.WCAG_2_1) { - return this.getChildrenStandardTypes() - .stream() - .filter(type -> type != null && !TaggedPDFConstants.ARTIFACT.equals(type)) - .collect(Collectors.joining("&")); - } - return this.getChildrenStandardTypes() - .stream() - .filter(Objects::nonNull) - .collect(Collectors.joining("&")); - } - @Override public String getparentStandardType() { org.verapdf.pd.structure.PDStructElem parent = ((org.verapdf.pd.structure.PDStructElem) simplePDObject).getParent(); @@ -141,25 +118,6 @@ public String getparentStandardType() { return null; } - @Override - public Boolean gethasContentItems() { - COSObject children = this.simplePDObject.getKey(ASAtom.K); - if (children == null) { - return false; - } - if (TaggedPDFHelper.isContentItem(children)) { - return true; - } - if (children.getType() == COSObjType.COS_ARRAY) { - for (COSObject elem : (COSArray)children.getDirectBase()) { - if (TaggedPDFHelper.isContentItem(elem)) { - return true; - } - } - } - return false; - } - @Override public String getvalueS() { COSName type = ((org.verapdf.pd.structure.PDStructElem) this.simplePDObject).getCOSStructureType(); @@ -228,8 +186,6 @@ public Boolean getcircularMappingExist() { @Override public List getLinkedObjects(String link) { switch (link) { - case CHILDREN: - return this.getChildren(); case STRUCTURE_TYPE: return this.getStructureType(); case LANG: @@ -241,39 +197,6 @@ public List getLinkedObjects(String link) { } } - public List getChildrenStandardTypes() { - return getChildrenStandardTypes(this); - } - - private static List getChildrenStandardTypes(GFPDStructElem element) { - List res = new ArrayList<>(); - for (GFPDStructElem child : element.getChildren()) { - String elementStandardType = child.getstandardType(); - if (TaggedPDFConstants.NON_STRUCT.equals(elementStandardType)) { - res.addAll(getChildrenStandardTypes(child)); - } else { - res.add(elementStandardType); - } - } - return Collections.unmodifiableList(res); - } - - public List getChildren() { - if (children == null) { - List elements = ((org.verapdf.pd.structure.PDStructElem) simplePDObject).getStructChildren(); - if (!elements.isEmpty()) { - List res = new ArrayList<>(elements.size()); - for (org.verapdf.pd.structure.PDStructElem element : elements) { - res.add(GFSEFactory.createTypedStructElem(element)); - } - children = Collections.unmodifiableList(res); - } else { - children = Collections.emptyList(); - } - } - return children; - } - private List getStructureType() { COSName type = ((org.verapdf.pd.structure.PDStructElem) this.simplePDObject).getCOSStructureType(); if (type != null) { diff --git a/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/GFPDStructTreeNode.java b/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/GFPDStructTreeNode.java new file mode 100644 index 000000000..43b88edea --- /dev/null +++ b/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/GFPDStructTreeNode.java @@ -0,0 +1,129 @@ +/** + * This file is part of veraPDF Validation, a module of the veraPDF project. + * Copyright (c) 2015, veraPDF Consortium + * All rights reserved. + * + * veraPDF Validation is free software: you can redistribute it and/or modify + * it under the terms of either: + * + * The GNU General public license GPLv3+. + * You should have received a copy of the GNU General Public License + * along with veraPDF Validation as the LICENSE.GPL file in the root of the source + * tree. If not, see http://www.gnu.org/licenses/ or + * https://www.gnu.org/licenses/gpl-3.0.en.html. + * + * The Mozilla Public License MPLv2+. + * You should have received a copy of the Mozilla Public License along with + * veraPDF Validation as the LICENSE.MPL file in the root of the source tree. + * If a copy of the MPL was not distributed with this file, you can obtain one at + * http://mozilla.org/MPL/2.0/. + */ +package org.verapdf.gf.model.impl.pd; + +import org.verapdf.as.ASAtom; +import org.verapdf.cos.*; +import org.verapdf.gf.model.impl.containers.StaticContainers; +import org.verapdf.gf.model.impl.pd.gfse.GFSEFactory; +import org.verapdf.model.baselayer.Object; +import org.verapdf.model.pdlayer.PDStructTreeNode; +import org.verapdf.pdfa.flavours.PDFAFlavour; +import org.verapdf.tools.TaggedPDFConstants; +import org.verapdf.tools.TaggedPDFHelper; + +import java.util.*; +import java.util.stream.Collectors; + +/** + * @author Maxim Plushchov + */ +public abstract class GFPDStructTreeNode extends GFPDObject implements PDStructTreeNode { + + /** + * Link name for {@code K} key + */ + public static final String CHILDREN = "K"; + + private List children; + + protected GFPDStructTreeNode(org.verapdf.pd.structure.PDStructTreeNode structTreeNodeDictionary, String type) { + super(structTreeNodeDictionary, type); + } + + + @Override + public String getkidsStandardTypes() { + if (StaticContainers.getFlavour() != null && + StaticContainers.getFlavour().getPart() == PDFAFlavour.Specification.WCAG_2_1) { + return this.getChildrenStandardTypes() + .stream() + .filter(type -> type != null && !TaggedPDFConstants.ARTIFACT.equals(type)) + .collect(Collectors.joining("&")); + } + return this.getChildrenStandardTypes() + .stream() + .filter(Objects::nonNull) + .collect(Collectors.joining("&")); + } + + @Override + public Boolean gethasContentItems() { + COSObject children = this.simplePDObject.getKey(ASAtom.K); + if (children == null) { + return false; + } + if (TaggedPDFHelper.isContentItem(children)) { + return true; + } + if (children.getType() == COSObjType.COS_ARRAY) { + for (COSObject elem : (COSArray)children.getDirectBase()) { + if (TaggedPDFHelper.isContentItem(elem)) { + return true; + } + } + } + return false; + } + + @Override + public List getLinkedObjects(String link) { + switch (link) { + case CHILDREN: + return this.getChildren(); + default: + return super.getLinkedObjects(link); + } + } + + public List getChildrenStandardTypes() { + return getChildrenStandardTypes(this); + } + + private static List getChildrenStandardTypes(GFPDStructTreeNode element) { + List res = new ArrayList<>(); + for (GFPDStructElem child : element.getChildren()) { + String elementStandardType = child.getstandardType(); + if (TaggedPDFConstants.NON_STRUCT.equals(elementStandardType) || TaggedPDFConstants.DIV.equals(elementStandardType)) { + res.addAll(getChildrenStandardTypes(child)); + } else { + res.add(elementStandardType); + } + } + return Collections.unmodifiableList(res); + } + + public List getChildren() { + if (children == null) { + List elements = ((org.verapdf.pd.structure.PDStructElem) simplePDObject).getStructChildren(); + if (!elements.isEmpty()) { + List res = new ArrayList<>(elements.size()); + for (org.verapdf.pd.structure.PDStructElem element : elements) { + res.add(GFSEFactory.createTypedStructElem(element)); + } + children = Collections.unmodifiableList(res); + } else { + children = Collections.emptyList(); + } + } + return children; + } +} diff --git a/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/GFPDStructTreeRoot.java b/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/GFPDStructTreeRoot.java index 6bf502f65..62768decc 100644 --- a/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/GFPDStructTreeRoot.java +++ b/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/GFPDStructTreeRoot.java @@ -22,35 +22,24 @@ import org.verapdf.as.ASAtom; import org.verapdf.cos.COSName; -import org.verapdf.cos.COSObjType; -import org.verapdf.cos.COSObject; import org.verapdf.gf.model.impl.cos.GFCosUnicodeName; -import org.verapdf.gf.model.impl.pd.gfse.GFSEFactory; import org.verapdf.model.baselayer.Object; import org.verapdf.model.coslayer.CosUnicodeName; -import org.verapdf.model.pdlayer.PDStructElem; import org.verapdf.model.pdlayer.PDStructTreeRoot; -import org.verapdf.tools.TaggedPDFHelper; import java.util.*; -import java.util.stream.Collectors; /** * @author Maksim Bezrukov */ -public class GFPDStructTreeRoot extends GFPDObject implements PDStructTreeRoot { +public class GFPDStructTreeRoot extends GFPDStructTreeNode implements PDStructTreeRoot { /** Type name for {@code GFPDStructTreeRoot} */ public static final String STRUCT_TREE_ROOT_TYPE = "PDStructTreeRoot"; - /** Link name for {@code K} key */ - public static final String CHILDREN = "K"; - /** Link name for {@code roleMapNames} key */ public static final String ROLE_MAP_NAMES = "roleMapNames"; - private List children = null; - /** * Default constructor * @@ -60,40 +49,9 @@ public GFPDStructTreeRoot(org.verapdf.pd.structure.PDStructTreeRoot treeRoot) { super(treeRoot, STRUCT_TREE_ROOT_TYPE); } - @Override - public String getkidsStandardTypes() { - return this.getChildren() - .stream() - .map(PDStructElem::getstandardType) - .filter(Objects::nonNull) - .collect(Collectors.joining("&")); - } - - @Override - public Boolean gethasContentItems() { - COSObject children = this.simplePDObject.getKey(ASAtom.K); - if (children == null) { - return false; - } - if (TaggedPDFHelper.isContentItem(children)) { - return true; - } - if (children.getType() == COSObjType.COS_ARRAY && children.size().intValue() > 0) { - for (int i = 0; i < children.size().intValue(); ++i) { - COSObject elem = children.at(i); - if (TaggedPDFHelper.isContentItem(elem)) { - return true; - } - } - } - return false; - } - @Override public List getLinkedObjects(String link) { switch (link) { - case CHILDREN: - return this.getChildren(); case ROLE_MAP_NAMES: return getRoleMapNames(); default: @@ -101,26 +59,6 @@ public List getLinkedObjects(String link) { } } - private List getChildren() { - if (this.children == null) { - this.children = parseChildren(); - } - return this.children; - } - - private List parseChildren() { - List elements = - ((org.verapdf.pd.structure.PDStructTreeRoot) simplePDObject).getStructChildren(); - if (!elements.isEmpty()) { - List res = new ArrayList<>(elements.size()); - for (org.verapdf.pd.structure.PDStructElem element : elements) { - res.add(GFSEFactory.createTypedStructElem(element)); - } - return Collections.unmodifiableList(res); - } - return Collections.emptyList(); - } - private List getRoleMapNames() { if (this.simplePDObject != null) { Map roleMap = ((org.verapdf.pd.structure.PDStructTreeRoot) simplePDObject).getRoleMap(); @@ -138,12 +76,8 @@ private List getRoleMapNames() { @Override public String gettopLevelFirstElementStandardType() { - if (this.children == null) { - this.children = parseChildren(); - } - - if (!this.children.isEmpty()) { - return this.children.get(0).getstandardType(); + if (!getChildren().isEmpty()) { + return getChildren().get(0).getstandardType(); } return null; }