📄 csvprocessor.java
字号:
final StartState startState;
try
{
startState = new StartState(getReport());
}
catch (CloneNotSupportedException e)
{
throw new ReportProcessingException("Unable to clone the start state.", e);
}
catch (FunctionInitializeException e)
{
throw new ReportProcessingException("Unable to initialize the expressions/functions.", e);
}
ReportState state = startState;
ReportState retval = null;
// the report processing can be splitted into 2 separate processes.
// The first is the ReportPreparation; all function values are resolved and
// a dummy run is done to calculate the final layout. This dummy run is
// also necessary to resolve functions which use or depend on the PageCount.
// the second process is the printing of the report, this is done in the
// processReport() method.
// during a prepare run the REPORT_PREPARERUN_PROPERTY is set to true.
state.setProperty(JFreeReport.REPORT_PREPARERUN_PROPERTY, Boolean.TRUE);
// the pageformat is added to the report properties, PageFormat is not serializable,
// so a repaginated report is no longer serializable.
//
// The pageformat will cause trouble in later versions, when printing over
// multiple pages gets implemented. This property will be replaced by a more
// suitable alternative.
final PageFormat p = getReport().getDefaultPageFormat();
state.setProperty(JFreeReport.REPORT_PAGEFORMAT_PROPERTY, p.clone());
// now change the writer function to be a dummy writer. We don't want any
// output in the prepare runs.
final CSVWriter w = (CSVWriter) state.getDataRow().get(CSV_WRITER);
w.setWriter(new OutputStreamWriter(new NullOutputStream()));
// now process all function levels.
// there is at least one level defined, as we added the CSVWriter
// to the report.
final Iterator it = startState.getLevels();
if (it.hasNext() == false)
{
throw new IllegalStateException("No functions defined, invalid implementation.");
}
boolean hasNext;
int level = ((Integer) it.next()).intValue();
// outer loop: process all function levels
ReportStateProgress progress = null;
do
{
// if the current level is the output-level, then save the report state.
// The state is used later to restart the report processing.
if (level == -1)
{
retval = (ReportState) state.clone();
}
// inner loop: process the complete report, calculate the function values
// for the current level. Higher level functions are not available in the
// dataRow.
final boolean failOnError
= (level == -1) && getReport().getReportConfiguration().isStrictErrorHandling();
while (!state.isFinish())
{
checkInterrupted();
progress = state.createStateProgress(progress);
state = state.advance();
if (failOnError)
{
if (state.isErrorOccured() == true)
{
throw new ReportEventException("Failed to dispatch an event.", state.getErrors());
}
}
if (!state.isFinish())
{
// if the report processing is stalled, throw an exception; an infinite
// loop would be caused.
if (!state.isProceeding(progress))
{
throw new ReportProcessingException("State did not proceed, bailing out!");
}
}
}
// if there is an other level to process, then use the finish state to
// create a new start state, which will continue the report processing on
// the next higher level.
hasNext = it.hasNext();
if (hasNext)
{
level = ((Integer) it.next()).intValue();
if (state instanceof FinishState)
{
state = new StartState((FinishState) state, level);
}
else
{
throw new IllegalStateException("The processing did not produce an finish state");
}
}
}
while (hasNext == true);
// root of evilness here ... pagecount should not be handled specially ...
// The pagecount should not be added as report property, there are functions to
// do this.
/*
state.setProperty(JFreeReportConstants.REPORT_PAGECOUNT_PROPERTY,
new Integer(state.getCurrentPage() - 1));
*/
state.setProperty(JFreeReport.REPORT_PREPARERUN_PROPERTY, Boolean.FALSE);
// finally prepeare the returned start state.
final StartState sretval = (StartState) retval;
if (sretval == null)
{
throw new IllegalStateException("There was no valid pagination done.");
}
// reset the state, so that the datarow points to the first row of the tablemodel.
sretval.resetState();
return sretval;
}
/**
* Processes the report. The generated output is written using the defined
* writer, the report is repaginated before the final writing.
*
* @throws ReportProcessingException if the report processing failed.
* @throws IllegalStateException if there is no writer defined.
*/
public void processReport() throws ReportProcessingException
{
if (writer == null)
{
throw new IllegalStateException("No writer defined");
}
try
{
ReportState state = repaginate();
final CSVWriter w = (CSVWriter) state.getDataRow().get(CSV_WRITER);
w.setWriter(getWriter());
final boolean failOnError =
getReport().getReportConfiguration().isStrictErrorHandling();
ReportStateProgress progress = null;
while (!state.isFinish())
{
checkInterrupted();
progress = state.createStateProgress(progress);
state = state.advance();
if (failOnError && state.isErrorOccured() == true)
{
throw new ReportEventException("Failed to dispatch an event.", state.getErrors());
}
if (!state.isFinish())
{
if (!state.isProceeding(progress))
{
throw new ReportProcessingException("State did not proceed, bailing out!");
}
}
}
}
catch (CloneNotSupportedException cne)
{
throw new ReportProcessingException("StateCopy was not supported");
}
}
/**
* Returns whether the processor should check the threads interrupted state.
* If this is set to true and the thread was interrupted, then the report processing
* is aborted.
*
* @return true, if the processor should check the current thread state, false otherwise.
*/
public boolean isHandleInterruptedState()
{
return handleInterruptedState;
}
/**
* Defines, whether the processor should check the threads interrupted state.
* If this is set to true and the thread was interrupted, then the report processing
* is aborted.
*
* @param handleInterruptedState true, if the processor should check the current thread state,
* false otherwise.
*/
public void setHandleInterruptedState(final boolean handleInterruptedState)
{
this.handleInterruptedState = handleInterruptedState;
}
/**
* Checks, whether the current thread is interrupted.
*
* @throws org.jfree.report.ReportInterruptedException if the thread is interrupted to
* abort the report processing.
*/
protected void checkInterrupted () throws ReportInterruptedException
{
if (isHandleInterruptedState() && Thread.interrupted())
{
throw new ReportInterruptedException("Current thread is interrupted. Returning.");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -