Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed metadata extension logic #947

Merged
merged 1 commit into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class AXLExtensionSchemaDefinition extends AXLExtensionSchemaObject imple

public static final String EXTENSION_SCHEMA_PROPERTIES = "ExtensionSchemaProperties";
public static final String EXTENSION_SCHEMA_VALUE_TYPES = "ExtensionSchemaValueTypes";
private static final String NAMESPACE_URI = "namespaceURI";
public static final String NAMESPACE_URI = "namespaceURI";
private static final String PREFIX = "prefix";
private static final String PROPERTY = "property";
private static final String SCHEMA = "schema";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@
*/
package org.verapdf.model.impl.axl;

import com.adobe.xmp.XMPConst;
import com.adobe.xmp.impl.VeraPDFXMPNode;
import com.adobe.xmp.options.PropertyOptions;
import org.verapdf.model.baselayer.Object;
import org.verapdf.model.tools.xmp.SchemasDefinition;
import org.verapdf.model.tools.xmp.ValidatorsContainer;
import org.verapdf.model.tools.xmp.ValidatorsContainerCreator;
import org.verapdf.model.xmplayer.ExtensionSchemasContainer;
import org.verapdf.pdfa.flavours.PDFAFlavour;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
* @author Maksim Bezrukov
Expand All @@ -41,11 +45,11 @@ public class AXLExtensionSchemasContainer extends AXLXMPObject implements Extens
public static final String EXTENSION_SCHEMA_DEFINITIONS = "ExtensionSchemaDefinitions";

protected VeraPDFXMPNode xmpNode;
protected final ValidatorsContainer containerForPDFA_1;
protected final ValidatorsContainer containerForPDFA_2_3;
protected final Map<String, SchemasDefinition> containerForPDFA_1;
protected final Map<String, SchemasDefinition> containerForPDFA_2_3;
protected final PDFAFlavour flavour;

public AXLExtensionSchemasContainer(VeraPDFXMPNode xmpNode, ValidatorsContainer containerForPDFA_1, ValidatorsContainer containerForPDFA_2_3, PDFAFlavour flavour) {
public AXLExtensionSchemasContainer(VeraPDFXMPNode xmpNode, Map<String, SchemasDefinition> containerForPDFA_1, Map<String, SchemasDefinition> containerForPDFA_2_3, PDFAFlavour flavour) {
super(EXTENSION_SCHEMAS_CONTAINER);
this.xmpNode = xmpNode;
this.containerForPDFA_1 = containerForPDFA_1;
Expand All @@ -71,7 +75,22 @@ private List<AXLExtensionSchemaDefinition> getExtensionSchemaDefinitions() {
if (this.xmpNode != null && this.xmpNode.getOptions().isArray()) {
List<AXLExtensionSchemaDefinition> res = new ArrayList<>();
for (VeraPDFXMPNode node : this.xmpNode.getChildren()) {
res.add(new AXLExtensionSchemaDefinition(node, this.containerForPDFA_1, this.containerForPDFA_2_3, this.flavour));
ValidatorsContainer containerForPDFA_1 = ValidatorsContainerCreator.EMPTY_VALIDATORS_CONTAINER;
ValidatorsContainer containerForPDFA_2_3 = ValidatorsContainerCreator.EMPTY_VALIDATORS_CONTAINER;
for (VeraPDFXMPNode child : node.getChildren()) {
if (XMPConst.NS_PDFA_SCHEMA.equals(child.getNamespaceURI()) && AXLExtensionSchemaDefinition.NAMESPACE_URI.equals(child.getName())) {
String namespace = child.getValue();
SchemasDefinition schemasDefinitionForPDFA_1 = this.containerForPDFA_1.get(namespace);
if (schemasDefinitionForPDFA_1 != null) {
containerForPDFA_1 = schemasDefinitionForPDFA_1.getValidatorsContainer();
}
SchemasDefinition schemasDefinitionForPDFA_2_3 = this.containerForPDFA_2_3.get(namespace);
if (schemasDefinitionForPDFA_2_3 != null) {
containerForPDFA_2_3 = schemasDefinitionForPDFA_2_3.getValidatorsContainer();
}
}
}
res.add(new AXLExtensionSchemaDefinition(node, containerForPDFA_1, containerForPDFA_2_3, this.flavour));
}
return Collections.unmodifiableList(res);
}
Expand Down
63 changes: 42 additions & 21 deletions core/src/main/java/org/verapdf/model/impl/axl/AXLXMPPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -56,9 +57,9 @@ public class AXLXMPPackage extends AXLXMPObject implements XMPPackage {
private final boolean isMainMetadata;
private final PDFAFlavour flavour;
private final boolean isClosedChoiceCheck;
private SchemasDefinition mainPackageSchemasDefinition;
private SchemasDefinition currentSchemasDefinitionPDFA_1;
private SchemasDefinition currentSchemasDefinitionPDFA_2_3;
private Map<String, SchemasDefinition> mainPackageSchemasDefinition;
private Map<String, SchemasDefinition> currentSchemasDefinitionPDFA_1;
private Map<String, SchemasDefinition> currentSchemasDefinitionPDFA_2_3;

public AXLXMPPackage(VeraPDFMeta xmpMetadata, boolean isSerializationValid,
boolean isClosedChoiceCheck,
Expand Down Expand Up @@ -111,9 +112,8 @@ private List<AXLExtensionSchemasContainer> getExtensionSchemasContainers() {
&& this.xmpMetadata.getExtensionSchemasNode() != null) {
List<AXLExtensionSchemasContainer> res = new ArrayList<>(1);
res.add(new AXLExtensionSchemasContainer(this.getXmpMetadata()
.getExtensionSchemasNode(), getCurrentSchemasDefinitionPDFA_1()
.getValidatorsContainer(),
getCurrentSchemasDefinitionPDFA_2_3().getValidatorsContainer(),
.getExtensionSchemasNode(), getCurrentSchemasDefinitionPDFA_1(),
getCurrentSchemasDefinitionPDFA_2_3(),
this.flavour));
return Collections.unmodifiableList(res);
}
Expand All @@ -135,20 +135,21 @@ protected List<AXLXMPProperty> getXMPProperties() {
}

private AXLXMPProperty createProperty(VeraPDFXMPNode node) {
if (XMPConst.NS_XMP_MM.equals(node.getNamespaceURI())
String namespaceURI = node.getNamespaceURI();
if (XMPConst.NS_XMP_MM.equals(namespaceURI)
&& "History".equals(node.getName())) {
return new AXLXMPMMHistoryProperty(node, this.isMainMetadata,
this.isClosedChoiceCheck, this
.getMainPackageSchemasDefinition(), this
.getCurrentSchemasDefinitionPDFA_1(), this
.getCurrentSchemasDefinitionPDFA_2_3(),
this.isClosedChoiceCheck,
this.getMainPackageSchemasDefinitionForNS(namespaceURI),
this.getCurrentSchemasDefinitionPDFA_1ForNS(namespaceURI),
this.getCurrentSchemasDefinitionPDFA_2_3ForNS(namespaceURI),
this.flavour);
}
return new AXLXMPProperty(node, this.isMainMetadata,
this.isClosedChoiceCheck, this
.getMainPackageSchemasDefinition(), this
.getCurrentSchemasDefinitionPDFA_1(), this
.getCurrentSchemasDefinitionPDFA_2_3(),
this.isClosedChoiceCheck,
this.getMainPackageSchemasDefinitionForNS(namespaceURI),
this.getCurrentSchemasDefinitionPDFA_1ForNS(namespaceURI),
this.getCurrentSchemasDefinitionPDFA_2_3ForNS(namespaceURI),
this.flavour);
}

Expand Down Expand Up @@ -193,14 +194,14 @@ private String getAttributeByRegexp(String regexp) {
return null;
}

protected SchemasDefinition getMainPackageSchemasDefinition() {
protected Map<String, SchemasDefinition> getMainPackageSchemasDefinition() {
if (this.mainPackageSchemasDefinition == null) {
this.mainPackageSchemasDefinition = SchemasDefinitionCreator.EMPTY_SCHEMAS_DEFINITION;
}
return this.mainPackageSchemasDefinition;
}

protected SchemasDefinition getCurrentSchemasDefinitionPDFA_1() {
protected Map<String, SchemasDefinition> getCurrentSchemasDefinitionPDFA_1() {
if (this.currentSchemasDefinitionPDFA_1 == null) {
if (this.xmpMetadata != null
&& this.xmpMetadata.getExtensionSchemasNode() != null) {
Expand All @@ -215,18 +216,38 @@ protected SchemasDefinition getCurrentSchemasDefinitionPDFA_1() {
return this.currentSchemasDefinitionPDFA_1;
}

protected SchemasDefinition getCurrentSchemasDefinitionPDFA_2_3() {
protected Map<String, SchemasDefinition> getCurrentSchemasDefinitionPDFA_2_3() {
if (this.currentSchemasDefinitionPDFA_2_3 == null) {
if (this.xmpMetadata != null
&& this.xmpMetadata.getExtensionSchemasNode() != null) {
this.currentSchemasDefinitionPDFA_2_3 = SchemasDefinitionCreator
.createExtendedSchemasDefinitionForPDFA_2_3(
this.currentSchemasDefinitionPDFA_2_3 = isMainMetadata ?
SchemasDefinitionCreator.createExtendedSchemasDefinitionForPDFA_2_3(
this.xmpMetadata.getExtensionSchemasNode(),
this.isClosedChoiceCheck);
this.isClosedChoiceCheck) :
SchemasDefinitionCreator.extendSchemasDefinitionForPDFA_2_3(
getMainPackageSchemasDefinition(),
this.xmpMetadata.getExtensionSchemasNode(),
this.isClosedChoiceCheck
);
} else {
this.currentSchemasDefinitionPDFA_2_3 = SchemasDefinitionCreator.EMPTY_SCHEMAS_DEFINITION;
}
}
return this.currentSchemasDefinitionPDFA_2_3;
}

protected SchemasDefinition getMainPackageSchemasDefinitionForNS(String nameSpace) {
SchemasDefinition schemasDefinition = getMainPackageSchemasDefinition().get(nameSpace);
return schemasDefinition == null ? SchemasDefinitionCreator.EMPTY_SCHEMA_DEFINITION : schemasDefinition;
}

protected SchemasDefinition getCurrentSchemasDefinitionPDFA_1ForNS(String nameSpace) {
SchemasDefinition schemasDefinition = getCurrentSchemasDefinitionPDFA_1().get(nameSpace);
return schemasDefinition == null ? SchemasDefinitionCreator.EMPTY_SCHEMA_DEFINITION : schemasDefinition;
}

protected SchemasDefinition getCurrentSchemasDefinitionPDFA_2_3ForNS(String nameSpace) {
SchemasDefinition schemasDefinition = getCurrentSchemasDefinitionPDFA_2_3().get(nameSpace);
return schemasDefinition == null ? SchemasDefinitionCreator.EMPTY_SCHEMA_DEFINITION : schemasDefinition;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@
import com.adobe.xmp.XMPConst;
import com.adobe.xmp.impl.VeraPDFXMPNode;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
* @author Maksim Bezrukov
*/
public class SchemasDefinitionCreator {

public static final SchemasDefinition EMPTY_SCHEMAS_DEFINITION = new SchemasDefinition();
public static final Map<String, SchemasDefinition> EMPTY_SCHEMAS_DEFINITION = Collections.emptyMap();
public static final SchemasDefinition EMPTY_SCHEMA_DEFINITION = new SchemasDefinition();
private static SchemasDefinition PREDEFINED_SCHEMA_DEFINITION_WITHOUT_CLOSED_CHOICE_FOR_PDFA_1 = null;
private static SchemasDefinition PREDEFINED_SCHEMA_DEFINITION_WITHOUT_CLOSED_CHOICE_FOR_PDFA_2_3 = null;
private static SchemasDefinition PREDEFINED_SCHEMA_DEFINITION_WITH_CLOSED_CHOICE_FOR_PDFA_1 = null;
Expand Down Expand Up @@ -79,8 +83,9 @@ public static SchemasDefinition getPredefinedSchemaDefinitionForPDFA_2_3(boolean
* @param schemas extension schemas container node
* @return created Schemas Definition object
*/
public static SchemasDefinition createExtendedSchemasDefinitionForPDFA_1(VeraPDFXMPNode schemas, boolean isClosedFieldsCheck) {
return createExtendedSchemasDefinition(schemas, true, isClosedFieldsCheck);
public static Map<String, SchemasDefinition> createExtendedSchemasDefinitionForPDFA_1(VeraPDFXMPNode schemas,
boolean isClosedFieldsCheck) {
return createExtendedSchemasDefinition(Collections.emptyMap(), schemas, true, isClosedFieldsCheck);
}

/**
Expand All @@ -89,30 +94,58 @@ public static SchemasDefinition createExtendedSchemasDefinitionForPDFA_1(VeraPDF
* @param schemas extension schemas container node
* @return created Schemas Definition object
*/
public static SchemasDefinition createExtendedSchemasDefinitionForPDFA_2_3(VeraPDFXMPNode schemas, boolean isClosedFieldsCheck) {
return createExtendedSchemasDefinition(schemas, false, isClosedFieldsCheck);
public static Map<String, SchemasDefinition> createExtendedSchemasDefinitionForPDFA_2_3(VeraPDFXMPNode schemas,
boolean isClosedFieldsCheck) {
return extendSchemasDefinitionForPDFA_2_3(Collections.emptyMap(), schemas, isClosedFieldsCheck);
}

private static SchemasDefinition createExtendedSchemasDefinition(VeraPDFXMPNode schemas, boolean isPDFA_1, boolean isClosedFieldsCheck) {
/**
* Extends already created extended schemas definitions object valid for PDF/A-2 or for PDF/A-3 without properties
* This method doesn't require PDF/A-1 analog because extended schemas extensions aren't allowed in PDF/A-1
*
* @param extendedSchemas extended schemas for extension
* @param schemas extension schemas container node
* @return created Schemas Definition object
*/
public static Map<String, SchemasDefinition> extendSchemasDefinitionForPDFA_2_3(Map<String, SchemasDefinition> extendedSchemas,
VeraPDFXMPNode schemas,
boolean isClosedFieldsCheck) {
if (extendedSchemas == null) {
throw new IllegalArgumentException("Nothing to extend");
}
return createExtendedSchemasDefinition(extendedSchemas, schemas, false, isClosedFieldsCheck);
}

private static Map<String, SchemasDefinition> createExtendedSchemasDefinition(Map<String, SchemasDefinition> extendedSchema,
VeraPDFXMPNode schemas,
boolean isPDFA_1,
boolean isClosedFieldsCheck) {
if (schemas == null) {
return EMPTY_SCHEMAS_DEFINITION;
}
if (!(XMPConst.NS_PDFA_EXTENSION.equals(schemas.getNamespaceURI()) && "schemas".equals(schemas.getName()) && schemas.getOptions().isArray())) {
return EMPTY_SCHEMAS_DEFINITION;
}
ValidatorsContainer typeContainer = isPDFA_1 ? ValidatorsContainerCreator.createExtendedValidatorsContainerForPDFA_1(schemas, isClosedFieldsCheck) :
ValidatorsContainerCreator.createExtendedValidatorsContainerForPDFA_2_3(schemas, isClosedFieldsCheck);
SchemasDefinition res = new SchemasDefinition(typeContainer);
Map<String, ValidatorsContainer> valueType = new HashMap<>(extendedSchema.size());
for (Map.Entry<String, SchemasDefinition> entry : extendedSchema.entrySet()) {
valueType.put(entry.getKey(), entry.getValue().getValidatorsContainer());
}
Map<String, SchemasDefinition> res = new HashMap<>();
List<VeraPDFXMPNode> schemasNodes = schemas.getChildren();
for (VeraPDFXMPNode node : schemasNodes) {
registerAllPropertiesFromExtensionSchemaNode(node, res);
registerAllPropertiesFromExtensionSchemaNode(node, res, valueType, isPDFA_1, isClosedFieldsCheck);
}
return res;
}

private static void registerAllPropertiesFromExtensionSchemaNode(VeraPDFXMPNode schema, SchemasDefinition schemasDefinition) {
private static void registerAllPropertiesFromExtensionSchemaNode(VeraPDFXMPNode schema,
Map<String, SchemasDefinition> schemasMap,
Map<String, ValidatorsContainer> oldValidators,
boolean isPDFA_1,
boolean isClosedFieldsCheck) {
List<VeraPDFXMPNode> schemaChildren = schema.getChildren();
VeraPDFXMPNode propertyNode = null;
VeraPDFXMPNode valueTypeNode = null;
String namespaceURI = null;
for (VeraPDFXMPNode child : schemaChildren) {
if (XMPConst.NS_PDFA_SCHEMA.equals(child.getNamespaceURI())) {
Expand All @@ -122,6 +155,11 @@ private static void registerAllPropertiesFromExtensionSchemaNode(VeraPDFXMPNode
propertyNode = child;
}
break;
case "valueType":
if (child.getOptions().isArray()) {
valueTypeNode = child;
}
break;
case "namespaceURI":
namespaceURI = child.getValue();
break;
Expand All @@ -131,7 +169,18 @@ private static void registerAllPropertiesFromExtensionSchemaNode(VeraPDFXMPNode
}
}
if (namespaceURI != null && propertyNode != null) {
ValidatorsContainer validatorsContainer = oldValidators.get(namespaceURI);
ValidatorsContainer currentContainer;
if (validatorsContainer == null) {
currentContainer = isPDFA_1 ? ValidatorsContainerCreator.createValidatorsContainerPredefinedForPDFA_1(isClosedFieldsCheck) :
ValidatorsContainerCreator.createValidatorsContainerPredefinedForPDFA_2_3(isClosedFieldsCheck);
} else {
currentContainer = new ValidatorsContainer(validatorsContainer);
}
ValidatorsContainerCreator.extendValidatorsContainer(currentContainer, valueTypeNode);
SchemasDefinition schemasDefinition = new SchemasDefinition(currentContainer);
registerAllPropertiesFromPropertyArrayNode(namespaceURI, propertyNode, schemasDefinition);
schemasMap.put(namespaceURI, schemasDefinition);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ public class ValidatorsContainer {
}
}

/**
* Creates new copy of validators container
* @param validatorsContainer to copy
*/
ValidatorsContainer(ValidatorsContainer validatorsContainer) {
this.validators = new HashMap<>(validatorsContainer.validators);
this.arrayValidators = new HashMap<>(validatorsContainer.arrayValidators);
}

boolean registerSimpleValidator(String typeName, Pattern pattern) {
if (pattern == null) {
throw new IllegalArgumentException("Argument pattern can not be null");
Expand Down
Loading