Skip to content

Commit

Permalink
Update logs for operator parser
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximPlusov committed Oct 27, 2024
1 parent 45a8fa9 commit d73ab08
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package org.verapdf.gf.model.factory.operators;

import org.verapdf.cos.COSBase;
import org.verapdf.cos.COSKey;
import org.verapdf.cos.COSObject;
import org.verapdf.gf.model.impl.pd.util.PDResourcesHandler;
import org.verapdf.gf.model.tools.TransparencyBehaviour;
Expand Down Expand Up @@ -99,10 +100,10 @@ public boolean isLastParsedContainsTransparency() {
public List<org.verapdf.model.operator.Operator> operatorsFromTokens(List<Object> rawTokens,
PDResourcesHandler resourcesHandler, GraphicState inheritedGraphicState,
StructureElementAccessObject structureElementAccessObject, COSObject parentStructElem,
List<String> parentsTags, boolean isRealContent) {
List<String> parentsTags, boolean isRealContent, COSKey parentObjectKey) {
this.isLastParsedContainsTransparency = false;
OperatorParser parser = new OperatorParser(inheritedGraphicState, structureElementAccessObject, resourcesHandler,
parentStructElem, parentsTags, isRealContent);
parentStructElem, parentsTags, isRealContent, parentObjectKey);

List<org.verapdf.model.operator.Operator> result = new ArrayList<>();
List<COSBase> arguments = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@ class OperatorParser {
private final List<String> parentsTags;

private final boolean isRealContent;

private final COSKey parentObjectKey;

private boolean insideText = false;

OperatorParser(GraphicState inheritedGraphicState,
StructureElementAccessObject structureElementAccessObject,
PDResourcesHandler resourcesHandler, COSObject parentStructElem,
List<String> parentsTags, boolean isRealContent) {
List<String> parentsTags, boolean isRealContent, COSKey parentObjectKey) {
if (inheritedGraphicState == null) {
this.graphicState = new GraphicState(resourcesHandler);
} else {
Expand All @@ -115,6 +116,7 @@ class OperatorParser {
this.parentStructElem = parentStructElem;
this.parentsTags = parentsTags;
this.isRealContent = isRealContent;
this.parentObjectKey = parentObjectKey;
}

public TransparencyGraphicsState getTransparencyGraphicState() {
Expand Down Expand Up @@ -173,11 +175,11 @@ void parseOperator(List<org.verapdf.model.operator.Operator> processedOperators,
Long mcid = bdcOp.getMCID();
if (mcid != null) {
if (mcidSet.contains(mcid)) {
LOGGER.log(Level.WARNING, "Content stream contains duplicate MCID - " + mcid);
LOGGER.log(Level.WARNING, getErrorMessage("Duplicate MCID - " + mcid));
}
mcidSet.add(mcid);
if (getCurrentMCID() != null) {
LOGGER.log(Level.WARNING, "Content stream contains nested MCID - " + mcid);
LOGGER.log(Level.WARNING, getErrorMessage("Nested MCID - " + mcid));
}
}
processedOperators.add(bdcOp);
Expand All @@ -189,7 +191,7 @@ void parseOperator(List<org.verapdf.model.operator.Operator> processedOperators,
if (!this.markedContentStack.empty()) {
this.markedContentStack.pop();
} else {
LOGGER.log(Level.WARNING, "Operator (EMC) not inside marked content");
LOGGER.log(Level.WARNING, getErrorMessage("Operator (EMC) not inside marked content"));
}
break;
case Operators.MP:
Expand Down Expand Up @@ -499,13 +501,13 @@ void parseOperator(List<org.verapdf.model.operator.Operator> processedOperators,
// SPECIAL GS
case Operators.CM_CONCAT:
if (insideText) {
LOGGER.log(Level.WARNING, "Special graphics state operator (cm) inside Text object");
LOGGER.log(Level.WARNING, getErrorMessage("Special graphics state operator (cm) inside Text object"));
}
processedOperators.add(new GFOp_cm(arguments));
break;
case Operators.Q_GRESTORE:
if (insideText) {
LOGGER.log(Level.WARNING, "Special graphics state operator (Q) inside Text object");
LOGGER.log(Level.WARNING, getErrorMessage("Special graphics state operator (Q) inside Text object"));
}
if (!graphicStateStack.isEmpty()) {
this.graphicState.copyProperties(this.graphicStateStack.pop());
Expand All @@ -517,7 +519,7 @@ void parseOperator(List<org.verapdf.model.operator.Operator> processedOperators,
break;
case Operators.Q_GSAVE:
if (insideText) {
LOGGER.log(Level.WARNING, "Special graphics state operator (q) inside Text object");
LOGGER.log(Level.WARNING, getErrorMessage("Special graphics state operator (q) inside Text object"));
}
this.graphicStateStack.push(this.graphicState.clone());
this.transparencyGraphicStateStack.push(this.transparencyGraphicState.clone());
Expand Down Expand Up @@ -623,15 +625,15 @@ private static void processInlineImage(List<org.verapdf.model.operator.Operator>
}
}

private static RenderingMode getRenderingMode(List<COSBase> arguments) {
private RenderingMode getRenderingMode(List<COSBase> arguments) {
if (!arguments.isEmpty()) {
COSBase renderingMode = arguments.get(0);
if (renderingMode instanceof COSInteger) {
RenderingMode mode = RenderingMode.getRenderingMode(renderingMode.getInteger().intValue());
if (mode != null) {
return mode;
}
LOGGER.log(Level.WARNING, "Wrong argument of Tr operator in stream");
LOGGER.log(Level.WARNING, getErrorMessage("Wrong argument of Tr operator"));
}
}
return RenderingMode.FILL;
Expand Down Expand Up @@ -729,4 +731,11 @@ private COSObject getParentStructElem(StructureElementAccessObject structureElem
}
return null;
}

private String getErrorMessage(String errorMessage) {
if (parentObjectKey == null) {
return errorMessage;
}
return "Content stream (object " + parentObjectKey + "): " + errorMessage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,37 +69,39 @@ public class GFPDContentStream extends GFPDObject implements PDContentStream {
private final StructureElementAccessObject structureElementAccessObject;
protected COSObject parentStructElem;
protected List<String> parentsTags;
private COSKey parentObjectKey;

public GFPDContentStream(org.verapdf.pd.PDContentStream contentStream, PDResourcesHandler resourcesHandler,
GraphicState inheritedGraphicState,
StructureElementAccessObject structureElementAccessObject) {
this(contentStream, resourcesHandler, inheritedGraphicState, structureElementAccessObject, CONTENT_STREAM_TYPE);
StructureElementAccessObject structureElementAccessObject, COSKey parentObjectKey) {
this(contentStream, resourcesHandler, inheritedGraphicState, structureElementAccessObject, parentObjectKey, CONTENT_STREAM_TYPE);
}

public GFPDContentStream(org.verapdf.pd.PDContentStream contentStream, PDResourcesHandler resourcesHandler,
GraphicState inheritedGraphicState,
StructureElementAccessObject structureElementAccessObject,
COSObject parentStructElem, List<String> parentsTags) {
COSObject parentStructElem, List<String> parentsTags, COSKey parentObjectKey) {
this(contentStream, resourcesHandler, inheritedGraphicState, structureElementAccessObject,
parentStructElem, parentsTags, CONTENT_STREAM_TYPE);
parentStructElem, parentsTags, parentObjectKey, CONTENT_STREAM_TYPE);
}

public GFPDContentStream(org.verapdf.pd.PDContentStream contentStream, PDResourcesHandler resourcesHandler,
GraphicState inheritedGraphicState,
StructureElementAccessObject structureElementAccessObject, final String type) {
StructureElementAccessObject structureElementAccessObject, COSKey parentObjectKey, final String type) {
super(contentStream, type);
this.resourcesHandler = resourcesHandler;
this.inheritedGraphicState = inheritedGraphicState;
this.structureElementAccessObject = structureElementAccessObject;
this.parentsTags = Collections.emptyList();
this.parentObjectKey = parentObjectKey;
}

public GFPDContentStream(org.verapdf.pd.PDContentStream contentStream,
PDResourcesHandler resourcesHandler,
GraphicState inheritedGraphicState,
StructureElementAccessObject structureElementAccessObject,
COSObject parentStructElem, List<String> parentsTags, final String type) {
this(contentStream, resourcesHandler, inheritedGraphicState, structureElementAccessObject, type);
COSObject parentStructElem, List<String> parentsTags, COSKey parentObjectKey, final String type) {
this(contentStream, resourcesHandler, inheritedGraphicState, structureElementAccessObject, parentObjectKey, type);
this.parentStructElem = getParentStructureElem(structureElementAccessObject);
if (this.parentStructElem == null) {
this.parentStructElem = parentStructElem;
Expand Down Expand Up @@ -151,7 +153,7 @@ protected void parseOperators() {
OperatorFactory operatorFactory = new OperatorFactory();
List<Operator> result = operatorFactory.operatorsFromTokens(streamParser.getTokens(),
resourcesHandler, inheritedGraphicState, structureElementAccessObject,
parentStructElem, parentsTags, isSemantic());
parentStructElem, parentsTags, isSemantic(), parentObjectKey);
this.containsTransparency = operatorFactory.isLastParsedContainsTransparency();
this.operators = Collections.unmodifiableList(result);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@ private List<PDContentStream> parseContentStream() {
GFPDContentStream pdContentStream;
if (!PDFFlavours.isPDFUARelatedFlavour(StaticContainers.getFlavour())) {
pdContentStream = new GFPDContentStream(page.getContent(), resourcesHandler, null,
new StructureElementAccessObject(this.simpleCOSObject));
new StructureElementAccessObject(this.simpleCOSObject), page.getObject().getObjectKey());
} else {
pdContentStream = new GFPDSemanticContentStream(page.getContent(), resourcesHandler, null,
new StructureElementAccessObject(this.simpleCOSObject));
new StructureElementAccessObject(this.simpleCOSObject), page.getObject().getObjectKey());
}
this.containsTransparency |= pdContentStream.isContainsTransparency();
pdContentStreams.add(pdContentStream);
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.cos.COSKey;
import org.verapdf.cos.COSObject;
import org.verapdf.gf.model.factory.operators.GraphicState;
import org.verapdf.gf.model.impl.operator.markedcontent.GFOpMarkedContent;
Expand Down Expand Up @@ -54,23 +55,23 @@ public class GFPDSemanticContentStream extends GFPDContentStream implements PDSe

public GFPDSemanticContentStream(org.verapdf.pd.PDContentStream contentStream, PDResourcesHandler resourcesHandler,
GraphicState inheritedGraphicState,
StructureElementAccessObject structureElementAccessObject) {
super(contentStream, resourcesHandler, inheritedGraphicState, structureElementAccessObject, SEMANTIC_CONTENT_STREAM_TYPE);
StructureElementAccessObject structureElementAccessObject, COSKey parentObjectKey) {
super(contentStream, resourcesHandler, inheritedGraphicState, structureElementAccessObject, parentObjectKey, SEMANTIC_CONTENT_STREAM_TYPE);
}

public GFPDSemanticContentStream(org.verapdf.pd.PDContentStream contentStream, PDResourcesHandler resourcesHandler,
GraphicState inheritedGraphicState,
StructureElementAccessObject structureElementAccessObject,
COSObject parentStructElem, List<String> parentsTags) {
COSObject parentStructElem, List<String> parentsTags, COSKey parentObjectKey) {
super(contentStream, resourcesHandler, inheritedGraphicState, structureElementAccessObject, parentStructElem,
parentsTags, SEMANTIC_CONTENT_STREAM_TYPE);
parentsTags, parentObjectKey, SEMANTIC_CONTENT_STREAM_TYPE);
}

public GFPDSemanticContentStream(org.verapdf.pd.PDContentStream contentStream, PDResourcesHandler resourcesHandler,
GraphicState inheritedGraphicState, StructureElementAccessObject structureElementAccessObject,
COSObject parentStructElem, List<String> parentsTags, String defaultLang, boolean isSignature) {
COSObject parentStructElem, List<String> parentsTags, String defaultLang, boolean isSignature, COSKey parentObjectKey) {
this(contentStream, resourcesHandler, inheritedGraphicState, structureElementAccessObject, parentStructElem,
parentsTags);
parentsTags, parentObjectKey);
this.defaultLang = defaultLang;
this.isSignature = isSignature;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.verapdf.as.ASAtom;
import org.verapdf.cos.COSDictionary;
import org.verapdf.cos.COSKey;
import org.verapdf.cos.COSObjType;
import org.verapdf.cos.COSObject;
import org.verapdf.gf.model.factory.operators.GraphicState;
Expand Down Expand Up @@ -103,6 +104,7 @@ private void parseCharStrings() {
COSDictionary charProcDict = ((org.verapdf.pd.font.type3.PDType3Font)
this.pdFont).getCharProcDict();
if (charProcDict != null) {
COSKey objectKey = this.pdFont.getKey(ASAtom.CHAR_PROCS).getObjectKey();
Set<ASAtom> keySet = charProcDict.getKeySet();
Map<String, PDContentStream> map = new HashMap<>(keySet.size());
for (ASAtom glyphName : keySet) {
Expand All @@ -113,7 +115,7 @@ private void parseCharStrings() {
GFPDContentStream contentStream =
new GFPDContentStream(charProc, glyphResources == null ?
this.resources : glyphResources, inheritedGraphicState,
new StructureElementAccessObject(this.simpleCOSObject));
new StructureElementAccessObject(this.simpleCOSObject), objectKey);
map.put(glyphName.getValue(), contentStream);
} else {
LOGGER.log(Level.SEVERE, "Invalid entry in the char proc dictionary, dictionary is expected.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,12 @@ private void parseContentStream() {
gfContentStream = new GFPDContentStream(
(org.verapdf.pd.images.PDXForm) this.simplePDObject, resourcesHandler,
this.inheritedGraphicState, new StructureElementAccessObject(this.simpleCOSObject),
parentStructElem, parentsTags);
parentStructElem, parentsTags, simplePDObject.getObject().getObjectKey());
} else {
gfContentStream = new GFPDSemanticContentStream(
(org.verapdf.pd.images.PDXForm) this.simplePDObject, resourcesHandler,
this.inheritedGraphicState, new StructureElementAccessObject(this.simpleCOSObject),
parentStructElem, parentsTags, defaultLang, isSignature);
parentStructElem, parentsTags, defaultLang, isSignature, simplePDObject.getObject().getObjectKey());
}
this.contentStreamContainsTransparency = gfContentStream.isContainsTransparency();
List<PDContentStream> streams = new ArrayList<>(MAX_NUMBER_OF_ELEMENTS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public boolean isContainsTransparency() {
private void parseContentStream() {
org.verapdf.pd.patterns.PDTilingPattern pattern = (org.verapdf.pd.patterns.PDTilingPattern) this.simplePDObject;
GFPDContentStream contentStream = new GFPDContentStream(pattern, this.resourcesHandler, inheritedGraphicState,
new StructureElementAccessObject(this.simpleCOSObject));
new StructureElementAccessObject(this.simpleCOSObject), simplePDObject.getObject().getObjectKey());
this.containsTransparency |= contentStream.isContainsTransparency();
List<PDContentStream> contentStreams = new ArrayList<>(MAX_NUMBER_OF_ELEMENTS);
contentStreams.add(contentStream);
Expand Down

0 comments on commit d73ab08

Please sign in to comment.