Search in sources :

Example 1 with ValidationReportItem

use of org.exist.validation.ValidationReportItem in project exist by eXist-db.

the class Shared method writeReport.

/**
 * Create validation report.
 * @param report The validation report data.
 * @param builder Helperclass to create in memory XML.
 * @return Validation report as node.
 */
public static NodeImpl writeReport(ValidationReport report, MemTreeBuilder builder) {
    // start root element
    final int nodeNr = builder.startElement("", "report", "report", null);
    // validation status: valid or invalid
    builder.startElement("", "status", "status", null);
    if (report.isValid()) {
        builder.characters("valid");
    } else {
        builder.characters("invalid");
    }
    builder.endElement();
    // namespace when available
    if (report.getNamespaceUri() != null) {
        builder.startElement("", "namespace", "namespace", null);
        builder.characters(report.getNamespaceUri());
        builder.endElement();
    }
    // validation duration
    final AttributesImpl durationAttribs = new AttributesImpl();
    durationAttribs.addAttribute("", "unit", "unit", "CDATA", "msec");
    builder.startElement("", "duration", "duration", durationAttribs);
    builder.characters("" + report.getValidationDuration());
    builder.endElement();
    // print exceptions if any
    if (report.getThrowable() != null) {
        builder.startElement("", "exception", "exception", null);
        final String className = report.getThrowable().getClass().getName();
        if (className != null) {
            builder.startElement("", "class", "class", null);
            builder.characters(className);
            builder.endElement();
        }
        final String message = report.getThrowable().getMessage();
        if (message != null) {
            builder.startElement("", "message", "message", null);
            builder.characters(message);
            builder.endElement();
        }
        final String stacktrace = report.getStackTrace();
        if (stacktrace != null) {
            builder.startElement("", "stacktrace", "stacktrace", null);
            builder.characters(stacktrace);
            builder.endElement();
        }
        builder.endElement();
    }
    // reusable attributes
    final AttributesImpl attribs = new AttributesImpl();
    // iterate validation report items, write message
    for (final ValidationReportItem vri : report.getValidationReportItemList()) {
        // construct attributes
        attribs.addAttribute("", "level", "level", "CDATA", vri.getTypeText());
        attribs.addAttribute("", "line", "line", "CDATA", Integer.toString(vri.getLineNumber()));
        attribs.addAttribute("", "column", "column", "CDATA", Integer.toString(vri.getColumnNumber()));
        if (vri.getRepeat() > 1) {
            attribs.addAttribute("", "repeat", "repeat", "CDATA", Integer.toString(vri.getRepeat()));
        }
        // write message
        builder.startElement("", "message", "message", attribs);
        builder.characters(vri.getMessage());
        builder.endElement();
        // Reuse attributes
        attribs.clear();
    }
    // finish root element
    builder.endElement();
    // return result
    return builder.getDocument().getNode(nodeNr);
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) ValidationReportItem(org.exist.validation.ValidationReportItem)

Aggregations

ValidationReportItem (org.exist.validation.ValidationReportItem)1 AttributesImpl (org.xml.sax.helpers.AttributesImpl)1