<commit_before>/*
 * This program is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
 * Foundation.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 * or from the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 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 GNU Lesser General Public License for more details.
 *
 * Copyright (c) 2001 - 2009 Object Refinery Ltd, Pentaho Corporation and Contributors..  All rights reserved.
 */

package org.pentaho.reporting.engine.classic.core.layout.output;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.pentaho.reporting.engine.classic.core.ClassicEngineCoreModule;
import org.pentaho.reporting.engine.classic.core.EmptyReportException;
import org.pentaho.reporting.engine.classic.core.MasterReport;
import org.pentaho.reporting.engine.classic.core.ReportEventException;
import org.pentaho.reporting.engine.classic.core.ReportInterruptedException;
import org.pentaho.reporting.engine.classic.core.ReportProcessingException;
import org.pentaho.reporting.engine.classic.core.event.ReportProgressEvent;
import org.pentaho.reporting.engine.classic.core.event.ReportProgressListener;
import org.pentaho.reporting.engine.classic.core.function.OutputFunction;
import org.pentaho.reporting.engine.classic.core.layout.AbstractRenderer;
import org.pentaho.reporting.engine.classic.core.layout.Renderer;
import org.pentaho.reporting.engine.classic.core.states.CollectingReportErrorHandler;
import org.pentaho.reporting.engine.classic.core.states.IgnoreEverythingReportErrorHandler;
import org.pentaho.reporting.engine.classic.core.states.InitialLayoutProcess;
import org.pentaho.reporting.engine.classic.core.states.LayoutProcess;
import org.pentaho.reporting.engine.classic.core.states.ProcessStateHandle;
import org.pentaho.reporting.engine.classic.core.states.ReportProcessingErrorHandler;
import org.pentaho.reporting.engine.classic.core.states.ReportStateKey;
import org.pentaho.reporting.engine.classic.core.states.process.PendingPagesHandler;
import org.pentaho.reporting.engine.classic.core.states.process.ProcessState;
import org.pentaho.reporting.engine.classic.core.states.process.RestartOnNewPageHandler;
import org.pentaho.reporting.engine.classic.core.util.IntList;
import org.pentaho.reporting.libraries.base.config.Configuration;
import org.pentaho.reporting.libraries.base.util.DebugLog;
import org.pentaho.reporting.libraries.base.util.MemoryUsageMessage;
import org.pentaho.reporting.libraries.docbundle.DocumentBundle;
import org.pentaho.reporting.libraries.docbundle.DocumentMetaData;
import org.pentaho.reporting.libraries.docbundle.MemoryDocumentMetaData;
import org.pentaho.reporting.libraries.xmlns.common.ParserUtil;

/**
 * @noinspection HardCodedStringLiteral
 */
public abstract class AbstractReportProcessor implements ReportProcessor
{
  private static final Log logger = LogFactory.getLog(AbstractReportProcessor.class);
  private static final boolean SHOW_ROLLBACKS = logger.isTraceEnabled();

  protected static final int MAX_EVENTS_PER_RUN = 200;
  protected static final int MIN_ROWS_PER_EVENT = 100;
  protected static final int COMMIT_RATE = 10;

  /**
   * A flag defining whether to check for Thread-Interrupts.
   */
  private boolean handleInterruptedState;

  /**
   * Storage for listener references.
   */
  private ArrayList<ReportProgressListener> listeners;

  /**
   * The listeners as object array for faster access.
   */
  private transient Object[] listenersCache;

  private MasterReport report;

  private OutputProcessor outputProcessor;

  private PageStateList stateList;
  private transient ProcessStateHandle activeDataFactory;

  private IntList physicalMapping;
  private IntList logicalMapping;
  private boolean paranoidChecks;
  /**
   * A flag controlling whether we save page-states to make browsing pages easier and faster.
   */
  private boolean fullStreamingProcessor;
  /**
   * An internal flag that is only valid after the pagination has started.
   */
  private boolean pagebreaksSupported;

  protected AbstractReportProcessor(final MasterReport report,
                                    final OutputProcessor outputProcessor)
      throws ReportProcessingException
  {
    if (report == null)
    {
      throw new NullPointerException("Report cannot be null.");
    }

    if (outputProcessor == null)
    {
      throw new NullPointerException("OutputProcessor cannot be null");
    }

    // first cloning ... protect the page layouter function ...
    // and any changes we may do to the report instance.

    // a second cloning is done in the start state, to protect the
    // processed data.
    this.fullStreamingProcessor = true;
    this.handleInterruptedState = true;
    this.outputProcessor = outputProcessor;

    final Configuration configuration = report.getReportConfiguration();
    this.paranoidChecks = "true".equals(configuration.getConfigProperty
        ("org.pentaho.reporting.engine.classic.core.layout.ParanoidChecks"));
    final String yieldRateText = configuration.getConfigProperty("org.pentaho.reporting.engine.classic.core.YieldRate");
    final int yieldRate = ParserUtil.parseInt(yieldRateText, 0);
    if (yieldRate > 0)
    {
      addReportProgressListener(new YieldReportListener(yieldRate));
    }
    final String profile = configuration.getConfigProperty
        ("org.pentaho.reporting.engine.classic.core.ProfileReportProcessing");
    if ("true".equals(profile))
    {
      final boolean logLevelProgress = "true".equals
          (configuration.getConfigProperty("org.pentaho.reporting.engine.classic.core.performance.LogLevelProgress"));
      final boolean logPageProgress = "true".equals
          (configuration.getConfigProperty("org.pentaho.reporting.engine.classic.core.performance.LogPageProgress"));
      final boolean logRowProgress = "true".equals
          (configuration.getConfigProperty("org.pentaho.reporting.engine.classic.core.performance.LogRowProgress"));
      addReportProgressListener(new PerformanceProgressLogger(logLevelProgress, logPageProgress, logRowProgress));
    }

    final boolean designtime = outputProcessor.getMetaData().isFeatureSupported(OutputProcessorFeature.DESIGNTIME);
    this.report = (MasterReport) report.derive(designtime);
  }

  protected ProcessStateHandle getProcessStateHandle()
  {
    return activeDataFactory;
  }

  protected MasterReport getReport()
  {
    return report;
  }

  public OutputProcessor getOutputProcessor()
  {
    return outputProcessor;
  }

  protected OutputProcessorMetaData getOutputProcessorMetaData()<commit_msg>PRD-4448: Code style<commit_after>/*
 * This program is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
 * Foundation.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 * or from the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 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 GNU Lesser General Public License for more details.
 *
 * Copyright (c) 2001 - 2009 Object Refinery Ltd, Pentaho Corporation and Contributors..  All rights reserved.
 */

package org.pentaho.reporting.engine.classic.core.layout.output;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.pentaho.reporting.engine.classic.core.ClassicEngineCoreModule;
import org.pentaho.reporting.engine.classic.core.EmptyReportException;
import org.pentaho.reporting.engine.classic.core.MasterReport;
import org.pentaho.reporting.engine.classic.core.ReportEventException;
import org.pentaho.reporting.engine.classic.core.ReportInterruptedException;
import org.pentaho.reporting.engine.classic.core.ReportProcessingException;
import org.pentaho.reporting.engine.classic.core.event.ReportProgressEvent;
import org.pentaho.reporting.engine.classic.core.event.ReportProgressListener;
import org.pentaho.reporting.engine.classic.core.function.OutputFunction;
import org.pentaho.reporting.engine.classic.core.layout.AbstractRenderer;
import org.pentaho.reporting.engine.classic.core.layout.Renderer;
import org.pentaho.reporting.engine.classic.core.states.CollectingReportErrorHandler;
import org.pentaho.reporting.engine.classic.core.states.IgnoreEverythingReportErrorHandler;
import org.pentaho.reporting.engine.classic.core.states.InitialLayoutProcess;
import org.pentaho.reporting.engine.classic.core.states.LayoutProcess;
import org.pentaho.reporting.engine.classic.core.states.ProcessStateHandle;
import org.pentaho.reporting.engine.classic.core.states.ReportProcessingErrorHandler;
import org.pentaho.reporting.engine.classic.core.states.ReportStateKey;
import org.pentaho.reporting.engine.classic.core.states.process.PendingPagesHandler;
import org.pentaho.reporting.engine.classic.core.states.process.ProcessState;
import org.pentaho.reporting.engine.classic.core.states.process.RestartOnNewPageHandler;
import org.pentaho.reporting.engine.classic.core.util.IntList;
import org.pentaho.reporting.libraries.base.config.Configuration;
import org.pentaho.reporting.libraries.base.util.MemoryUsageMessage;
import org.pentaho.reporting.libraries.docbundle.DocumentBundle;
import org.pentaho.reporting.libraries.docbundle.DocumentMetaData;
import org.pentaho.reporting.libraries.docbundle.MemoryDocumentMetaData;
import org.pentaho.reporting.libraries.xmlns.common.ParserUtil;

/**
 * @noinspection HardCodedStringLiteral
 */
public abstract class AbstractReportProcessor implements ReportProcessor
{
  private static final Log logger = LogFactory.getLog(AbstractReportProcessor.class);
  private static final boolean SHOW_ROLLBACKS = logger.isTraceEnabled();

  protected static final int MAX_EVENTS_PER_RUN = 200;
  protected static final int MIN_ROWS_PER_EVENT = 100;
  protected static final int COMMIT_RATE = 10;

  /**
   * A flag defining whether to check for Thread-Interrupts.
   */
  private boolean handleInterruptedState;

  /**
   * Storage for listener references.
   */
  private ArrayList<ReportProgressListener> listeners;

  /**
   * The listeners as object array for faster access.
   */
  private transient Object[] listenersCache;

  private MasterReport report;

  private OutputProcessor outputProcessor;

  private PageStateList stateList;
  private transient ProcessStateHandle activeDataFactory;

  private IntList physicalMapping;
  private IntList logicalMapping;
  private boolean paranoidChecks;
  /**
   * A flag controlling whether we save page-states to make browsing pages easier and faster.
   */
  private boolean fullStreamingProcessor;
  /**
   * An internal flag that is only valid after the pagination has started.
   */
  private boolean pagebreaksSupported;

  protected AbstractReportProcessor(final MasterReport report,
                                    final OutputProcessor outputProcessor)
      throws ReportProcessingException
  {
    if (report == null)
    {
      throw new NullPointerException("Report cannot be null.");
    }

    if (outputProcessor == null)
    {
      throw new NullPointerException("OutputProcessor cannot be null");
    }

    // first cloning ... protect the page layouter function ...
    // and any changes we may do to the report instance.

    // a second cloning is done in the start state, to protect the
    // processed data.
    this.fullStreamingProcessor = true;
    this.handleInterruptedState = true;
    this.outputProcessor = outputProcessor;

    final Configuration configuration = report.getReportConfiguration();
    this.paranoidChecks = "true".equals(configuration.getConfigProperty
        ("org.pentaho.reporting.engine.classic.core.layout.ParanoidChecks"));
    final String yieldRateText = configuration.getConfigProperty("org.pentaho.reporting.engine.classic.core.YieldRate");
    final int yieldRate = ParserUtil.parseInt(yieldRateText, 0);
    if (yieldRate > 0)
    {
      addReportProgressListener(new YieldReportListener(yieldRate));
    }
    final String profile = configuration.getConfigProperty
        ("org.pentaho.reporting.engine.classic.core.ProfileReportProcessing");
    if ("true".equals(profile))
    {
      final boolean logLevelProgress = "true".equals
          (configuration.getConfigProperty("org.pentaho.reporting.engine.classic.core.performance.LogLevelProgress"));
      final boolean logPageProgress = "true".equals
          (configuration.getConfigProperty("org.pentaho.reporting.engine.classic.core.performance.LogPageProgress"));
      final boolean logRowProgress = "true".equals
          (configuration.getConfigProperty("org.pentaho.reporting.engine.classic.core.performance.LogRowProgress"));
      addReportProgressListener(new PerformanceProgressLogger(logLevelProgress, logPageProgress, logRowProgress));
    }

    final boolean designtime = outputProcessor.getMetaData().isFeatureSupported(OutputProcessorFeature.DESIGNTIME);
    this.report = report.derive(designtime);
  }

  protected ProcessStateHandle getProcessStateHandle()
  {
    return activeDataFactory;
  }

  protected MasterReport getReport()
  {
    return report;
  }

  public OutputProcessor getOutputProcessor()
  {
    return outputProcessor;
  }

  protected OutputProcessorMetaData getOutputProcessorMetaData()
{'fraction_non_alphanumeric': 0.08548511925701822, 'fraction_numerical': 0.004925068599169774, 'mean_word_length': 4.674251497005988, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1225418', 'n_tokens_mistral': 3884, 'n_tokens_neox': 3706, 'n_words': 912}