afpdev / alpheusafpparser

Library & parser for IBM Advanced Function Presentation (AFP) document/print stream format
GNU General Public License v3.0
16 stars 11 forks source link

setEscalateParsingErrors to false - useful? #15

Closed michaelknigge closed 8 years ago

michaelknigge commented 8 years ago

I've noticed that Alpheus can be instructed to suppress Excentions by invoking setEscalateParsingErrors(false) on the AFPParserConfiguration. I ask myself is this is really useful.... I guess the side effects (when an axception occurrs and is suppressed / irgnored) are unpredictable...

Do you really want to keep it?

Bye, michael

afpdev commented 8 years ago

This mechanism addresses the fact that there are many AFP files out there that do not exactly follow the specification. It enables to deal with erroneous AFP streams. It would be frustrating if the parser just gives up after the first parsing error.

If one setEscalateParsingErrors(true) and a parsing error occur, an exception is thrown that carries the StructuredFieldErrornouslyBuilt. If set to false, the parser produces a StructuredField(SF) of type StructuredFieldErrornouslyBuilt without throwing any exception and continuous parsing the rest of the AFP stream. Note that the SF StructuredFieldErrornouslyBuilt contains the raw payload of the SF and it should be fine to write it out as it will produce the exact record that was read.

Depending on the nature of the error in the AFP stream, it is very likely that the further parsing of the file stays predictable. Unpredictable behavior may occur if the size of SF given in the SF-Introducer is wrong. But even in that case it is very likely that the parser will recover at the begin byte (0xA5) of the following SF and continues parsing.

Yes, Michael, I really wanna keep it.

michaelknigge commented 8 years ago

Rudolf, thank you for clarification. I've added a JavaDoc to make this clear to other users of Alpheus.

But... I think it does not always work as you want it to. If for example a record is truncated ("cut off"), Alpheus may run into a IndexOutOfBoundsException on parsing the AFP record. But if you look a line 276 of AFPParser, only Exceptions of type "AFPParserException" are handled.

In case of a IndexOutOfBoundsException (well everything but AFPParserException) no StructuredFieldErrornouslyBuilt will be added to the thrown AFPParserException.

bye, Michael