-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #593 from veraPDF/pdfua2
PDF/UA-2. Add GFPDTextField
- Loading branch information
1 parent
b2ca608
commit 6112085
Showing
5 changed files
with
174 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
validation-model/src/main/java/org/verapdf/gf/model/impl/pd/GFPDTextField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
validation-model/src/main/java/org/verapdf/gf/model/tools/DictionaryKeysHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |