com.github.fge.jsonschema.processing.ProcessingResult Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of json-schema-core Show documentation
Show all versions of json-schema-core Show documentation
Core processing architecture for json-schema-validator
/*
* Copyright (c) 2013, Francis Galiegue
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Lesser GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package com.github.fge.jsonschema.processing;
import com.github.fge.jsonschema.exceptions.ProcessingException;
import com.github.fge.jsonschema.exceptions.unchecked.ProcessingError;
import com.github.fge.jsonschema.report.ListProcessingReport;
import com.github.fge.jsonschema.report.LogLevel;
import com.github.fge.jsonschema.report.MessageProvider;
import com.github.fge.jsonschema.report.ProcessingMessage;
import com.github.fge.jsonschema.report.ProcessingReport;
import static com.github.fge.jsonschema.messages.ProcessingErrors.*;
/**
* Wrapper class over a processing result
*
* This class is useful when you write your own wrappers over processors and
* want to be able to customize the output for said classes. It offers two
* static factory methods and offers the ability to grab the processing result,
* the processing report and the success status.
*
* It also offers a wrapper which swallows {@link ProcessingException}s and
* wraps them in a report instead (which will always be a {@link
* ListProcessingReport}: in this case, the exception message will always be
* the first message in the report.
*
* @param type of the processing output
*/
public final class ProcessingResult
{
private final ProcessingReport report;
private final R result;
private ProcessingResult(final ProcessingReport report, final R result)
{
if (report == null)
throw new ProcessingError(new ProcessingMessage()
.message(NULL_REPORT));
this.report = report;
this.result = result;
}
/**
* Build a result out of a processor, a report and an input
*
* @param processor the processor
* @param report the report
* @param input the input
* @param type of the input
* @param type of the output
* @return a processing result
* @throws ProcessingException processing failed
* @throws ProcessingError the processor or report are null
*/
public static
ProcessingResult of(final Processor processor,
final ProcessingReport report, final IN input)
throws ProcessingException
{
if (processor == null)
throw new ProcessingError(new ProcessingMessage()
.message(NULL_PROCESSOR));
final OUT out = processor.process(report, input);
return new ProcessingResult(report, out);
}
/**
* Build a result out of a computation and wrap any processing exception
*
* @param processor the processor to use
* @param report the report to use
* @param input the input
* @param type of the input
* @param type of the output
* @return a processing result
* @throws ProcessingError the processor or report are null
*/
public static
ProcessingResult uncheckedResult(
final Processor processor, final ProcessingReport report,
final IN input)
{
try {
return of(processor, report, input);
} catch (ProcessingException e) {
return new ProcessingResult(buildReport(report, e), null);
}
}
/**
* Get the report out of this result
*
* @return the report
*/
public ProcessingReport getReport()
{
return report;
}
/**
* Get the result of the computation
*
* Note that in the event of a processing failure, the return value of
* this method is undefined.
*
* @return the result
*/
public R getResult()
{
return result;
}
/**
* Tell whether the result is a success
*
* @return true if the computation occurred without a problem
* @see ProcessingReport#isSuccess()
*/
public boolean isSuccess()
{
return report.isSuccess();
}
private static ProcessingReport buildReport(final ProcessingReport report,
final ProcessingException e)
{
final ListProcessingReport ret
= new ListProcessingReport(LogLevel.DEBUG, LogLevel.NONE);
ret.log(LogLevel.FATAL, e.getProcessingMessage()
.put("info", "other messages follow (if any)"));
try {
ret.mergeWith(report);
} catch (ProcessingException ignored) {
// can't happen
}
return ret;
}
}