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

PDF/UA-2. Add GFPDTextField #593

Merged
merged 1 commit into from
Oct 19, 2023
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 @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -104,8 +105,7 @@ private List<PDOCConfig> getConfigs() {

if (!configs.empty() && configs.getType() == COSObjType.COS_ARRAY) {
List<PDOCConfig> 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();
Expand Down Expand Up @@ -136,4 +136,16 @@ private static List<String> 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* This file is part of veraPDF Validation, a module of the veraPDF project.
* Copyright (c) 2015, veraPDF Consortium <[email protected]>
* 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* This file is part of veraPDF Validation, a module of the veraPDF project.
* Copyright (c) 2015, veraPDF Consortium <[email protected]>
* 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();
}
}
Loading