BDE 4.14.0 Production release
|
Provide common error information for XML components.
This component provides an in-core value-semantic class, balxml::ErrorInfo
, that contains the following diagnostic information:
Parsing components in the baexml
package make use of balxml::ErrorInfo
as a standard way to report errors and warnings back to the caller. The information contained within a balxml::ErrorInfo
object is sufficient to report a single diagnostic in an input document.
This section illustrates intended use of this component.
In this example, we create a parser for a simple file of percentages. The file is formatted as a sequence of lines, with each line containing a decimal number in the range "0" to "100", inclusive. Leading whitespace and blank lines are ignored. When an error occurs during parsing, the error data is stored in a balxml::ErrorInfo
object. Our parser's interface is as follows:
The constructor is straight-forward:
The parseNext
function begins by testing if a previous error occurred. By testing this condition, we can call parseNext
several times, knowing that the first error will stop the parse operation.
The parser skips leading whitespace and lines containing only whitespace. It loops until a non-empty line is found:
The input stream reports that the input line is longer than MAX_LINE
by setting the fail() condition. In this case, we set the error object to a warning state, indicating the line and column where the problem occurred. Then we clear the stream condition and discard the rest of the line.
If we detect an EOF condition, we just return -1. Otherwise, we skip the leading whitespace and go on.
Now we perform two more error checks: one or superfluous characters after the integer, the other for an out-of-range integer. If the errorInfo
object is already in warning state, either of these errors will overwrite the existing warning with the new error condition.
If there were no errors, return the result. Note that the errorInfo
object may contain a warning, but warnings typically do not cause a change in the error value.
The main program uses the PercentParser
class to parse a list of values and compute the average. Typically, the data would be stored in a file, but we'll use a literal string for demonstration purposes:
We convert the string into a stream and initialize the parser. We name our input stream "Inputs" for the purpose of error handling. We also initialize our working variables:
Any error in parsing will be stored in the errorInfo
object. When first constructed, it has a severity of BAEXML_NO_ERROR
.
Normally, parsing would proceed in a loop. However, to illustrate the different error-handling situations, we have unrolled the loop below.
The first parse succeeds, and no error is reported:
The next parse also succeeds but, because the input line was very long, a warning was generated:
After resetting the errorInfo
object, the we call nextParse
again. This time it fails with an error. The line, column, and source of the error are reported in the object.
If the errorInfo
object is not reset, calling parseNext
becomes a no-op:
After calling reset
, the next call to parseNext
produces a different error message:
The last line of the file contains two problems: a long line, which would produce a warning, and a range error, which would produce an error. The warning message is overwritten by the error message because the error has a higher severity. Therefore, on return from parseNext
, only the error message is stored in errorInfo
and the warning is lost:
Writing the errorInfo
object to a log or file will produce a readable error message:
The resulting message to standard error looks as follows:
Finally, we reach the end of the input stream and can compute our average.