From 61120853ff0120a8ca400d7add802efc865867fc Mon Sep 17 00:00:00 2001 From: MaximPlusov Date: Thu, 19 Oct 2023 16:24:45 +0300 Subject: [PATCH] Merge pull request #593 from veraPDF/pdfua2 PDF/UA-2. Add GFPDTextField --- .../gf/model/impl/pd/GFPDFormField.java | 4 +- .../gf/model/impl/pd/GFPDOCProperties.java | 16 +++- .../gf/model/impl/pd/GFPDTextField.java | 52 ++++++++++ .../impl/pd/annotations/GFPDMarkupAnnot.java | 11 +++ .../gf/model/tools/DictionaryKeysHelper.java | 94 +++++++++++++++++++ 5 files changed, 174 insertions(+), 3 deletions(-) create mode 100644 validation-model/src/main/java/org/verapdf/gf/model/impl/pd/GFPDTextField.java create mode 100644 validation-model/src/main/java/org/verapdf/gf/model/tools/DictionaryKeysHelper.java diff --git a/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/GFPDFormField.java b/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/GFPDFormField.java index 87ac3f8fc..a47060f98 100644 --- a/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/GFPDFormField.java +++ b/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/GFPDFormField.java @@ -62,8 +62,10 @@ protected GFPDFormField(org.verapdf.pd.form.PDFormField field, final String type public static GFPDFormField createTypedFormField(org.verapdf.pd.form.PDFormField field) { if (field.getFT() == ASAtom.SIG) { return new GFPDSignatureField((PDSignatureField) field); + } else if (field.getFT() == ASAtom.TX) { + return new GFPDTextField(field); } - return new GFPDFormField(field); + return new GFPDFormField(field); } @Override diff --git a/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/GFPDOCProperties.java b/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/GFPDOCProperties.java index c435e51f3..1f442cd32 100644 --- a/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/GFPDOCProperties.java +++ b/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/GFPDOCProperties.java @@ -21,6 +21,7 @@ package org.verapdf.gf.model.impl.pd; import org.verapdf.as.ASAtom; +import org.verapdf.cos.COSArray; import org.verapdf.cos.COSDictionary; import org.verapdf.cos.COSObjType; import org.verapdf.cos.COSObject; @@ -104,8 +105,7 @@ private List getConfigs() { if (!configs.empty() && configs.getType() == COSObjType.COS_ARRAY) { List result = new ArrayList<>(); - for (int i = 0; i < configs.size().intValue(); i++) { - COSObject config = configs.at(i); + for (COSObject config : (COSArray)configs.getDirectBase()) { if (!config.empty() && config.getType() == COSObjType.COS_DICT) { PDOCConfig pdConfig = new GFPDOCConfig(new PDObject(config), groupNamesList, names.contains(config.getStringKey(ASAtom.NAME))); String name = pdConfig.getName(); @@ -136,4 +136,16 @@ private static List getDName(final COSDictionary contentProperties) { return result; } + @Override + public Boolean getcontainsConfigs() { + COSObject configs = this.simplePDObject.getKey(ASAtom.CONFIGS); + if (!configs.empty() && configs.getType() == COSObjType.COS_ARRAY) { + for (COSObject config : (COSArray)configs.getDirectBase()) { + if (!config.empty() && config.getType() == COSObjType.COS_DICT) { + return true; + } + } + } + return false; + } } diff --git a/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/GFPDTextField.java b/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/GFPDTextField.java new file mode 100644 index 000000000..e3817b74f --- /dev/null +++ b/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/GFPDTextField.java @@ -0,0 +1,52 @@ +/** + * 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.gf.model.tools.DictionaryKeysHelper; +import org.verapdf.model.pdlayer.PDTextField; + +/** + * @author Sergey Shemyakov + */ +public class GFPDTextField extends GFPDFormField implements PDTextField { + + public static final String TEXT_FIELD_TYPE = "PDTextField"; + + public GFPDTextField(org.verapdf.pd.form.PDFormField pdFormField) { + super(pdFormField, TEXT_FIELD_TYPE); + } + + @Override + public String getV() { + return DictionaryKeysHelper.getStringOrStreamEntryStringRepresentation(simpleCOSObject, ASAtom.V); + } + + @Override + public String getRV() { + return DictionaryKeysHelper.getRichTextStringOrStreamEntryStringRepresentation(simpleCOSObject, ASAtom.RV); + } + + @Override + public Boolean getcontainsRV() { + return simpleCOSObject.knownKey(ASAtom.RV); + } +} diff --git a/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/annotations/GFPDMarkupAnnot.java b/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/annotations/GFPDMarkupAnnot.java index 72547692c..26f3adf26 100644 --- a/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/annotations/GFPDMarkupAnnot.java +++ b/validation-model/src/main/java/org/verapdf/gf/model/impl/pd/annotations/GFPDMarkupAnnot.java @@ -20,8 +20,10 @@ */ package org.verapdf.gf.model.impl.pd.annotations; +import org.verapdf.as.ASAtom; import org.verapdf.gf.model.impl.pd.GFPDAnnot; import org.verapdf.gf.model.impl.pd.util.PDResourcesHandler; +import org.verapdf.gf.model.tools.DictionaryKeysHelper; import org.verapdf.model.pdlayer.PDMarkupAnnot; import org.verapdf.pd.PDAnnotation; import org.verapdf.pd.PDPage; @@ -41,4 +43,13 @@ public GFPDMarkupAnnot(PDAnnotation annot, PDResourcesHandler pageResources, PDP super(annot, pageResources, page, type); } + @Override + public String getRC() { + return DictionaryKeysHelper.getRichTextStringOrStreamEntryStringRepresentation(simpleCOSObject, ASAtom.RC); + } + + @Override + public Boolean getcontainsRC() { + return simpleCOSObject.knownKey(ASAtom.RC); + } } diff --git a/validation-model/src/main/java/org/verapdf/gf/model/tools/DictionaryKeysHelper.java b/validation-model/src/main/java/org/verapdf/gf/model/tools/DictionaryKeysHelper.java new file mode 100644 index 000000000..23187b818 --- /dev/null +++ b/validation-model/src/main/java/org/verapdf/gf/model/tools/DictionaryKeysHelper.java @@ -0,0 +1,94 @@ +/** + * 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.tools; + +import org.xml.sax.InputSource; +import org.verapdf.as.ASAtom; +import org.verapdf.cos.*; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.verapdf.xmp.impl.ByteBuffer; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.InputStream; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class DictionaryKeysHelper { + + private static final Logger LOGGER = Logger.getLogger(DictionaryKeysHelper.class.getCanonicalName()); + + public static String getStringOrStreamEntryStringRepresentation(COSObject dictionary, ASAtom key) { + COSObject object = dictionary.getKey(key); + if (object == null || object.empty()) { + return null; + } + COSBase base = object.getDirectBase(); + if (base.getType() == COSObjType.COS_STREAM) { + base = ((COSStream)base).getStringFromTextStream(); + } + if (base.getType() == COSObjType.COS_STRING) { + return base.getString(); + } + return null; + } + + public static String getRichTextStringOrStreamEntryStringRepresentation(COSObject dictionary, ASAtom key) { + try { + COSObject object = dictionary.getKey(key); + if (object == null || object.empty()) { + return null; + } + COSBase base = object.getDirectBase(); + InputStream inputStream; + if (object.getType() == COSObjType.COS_STREAM) { + inputStream = base.getData(); + } else if (object.getType() == COSObjType.COS_STRING) { + inputStream = new ByteBuffer(object.getDirectBase().getString().getBytes()).getByteStream(); + } else { + return null; + } + DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + builder.setErrorHandler(null); + Document doc = builder.parse(new InputSource(inputStream)); + return getAllNodeText(doc); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Error while parsing rich text entry " + key + " in the object " + dictionary.getKey()); + } + return null; + } + + private static String getAllNodeText(Node node) { + StringBuilder stringBuilder = new StringBuilder(); + NodeList childNodes = node.getChildNodes(); + for (int i = 0; i < childNodes.getLength(); i++) { + Node child = childNodes.item(i); + if (child.getNodeValue() != null) { + stringBuilder.append(child.getNodeValue()); + } else { + stringBuilder.append(getAllNodeText(child)); + } + } + return stringBuilder.toString(); + } +}