<commit_before>package org.pentaho.reporting.platform.plugin.drilldown;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;

import org.pentaho.reporting.engine.classic.core.ReportEnvironment;
import org.pentaho.reporting.engine.classic.core.function.ReportFormulaContext;
import org.pentaho.reporting.engine.classic.core.function.formula.QuoteTextFunction;
import org.pentaho.reporting.libraries.base.util.CSVTokenizer;
import org.pentaho.reporting.libraries.base.util.StringUtils;
import org.pentaho.reporting.libraries.formula.EvaluationException;
import org.pentaho.reporting.libraries.formula.FormulaContext;
import org.pentaho.reporting.libraries.formula.LibFormulaErrorValue;
import org.pentaho.reporting.libraries.formula.function.Function;
import org.pentaho.reporting.libraries.formula.function.ParameterCallback;
import org.pentaho.reporting.libraries.formula.lvalues.TypeValuePair;
import org.pentaho.reporting.libraries.formula.typing.ArrayCallback;
import org.pentaho.reporting.libraries.formula.typing.Sequence;
import org.pentaho.reporting.libraries.formula.typing.Type;
import org.pentaho.reporting.libraries.formula.typing.coretypes.TextType;
import org.pentaho.reporting.libraries.formula.typing.sequence.RecursiveSequence;

/**
 * Todo: Document me!
 * <p/>
 * Date: 14.01.11
 * Time: 16:46
 *
 * @author <NAME>.
 */
public class ContentLinkFunction implements Function
{
  public ContentLinkFunction()
  {
  }

  public String getCanonicalName()
  {
    return "CONTENTLINK";
  }

  public TypeValuePair evaluate(final FormulaContext context,
                                final ParameterCallback parameters) throws EvaluationException
  {
    final String[] contentLink = getContentLink(context);
    if (contentLink.length == 0)
    {
      throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_NA_VALUE);
    }

    final Object o = parameters.getValue(0);
    final Type type = parameters.getType(0);
    final HashMap<String, Object[]> values = collectParameterValues(o, type, context);

    final StringBuilder builder = new StringBuilder();
    builder.append("javascript:");
    //window.parent.Dashboards.fireChange(PARAM, VALUE);"
    for (int i = 0; i < contentLink.length; i++)
    {
      final String variable = contentLink[i];
      builder.append("var wnd=window.parent;var slf;while(!wnd.Dashboards && wnd.parent){slf=wnd;wnd=wnd.parent};wnd.Dashboards.fireOutputParam(slf,");
      builder.append('\'');
      builder.append(QuoteTextFunction.saveConvert(variable));
      builder.append('\'');
      builder.append(",");

      final Object[] objects = values.get(variable);
      if (objects == null || objects.length == 0)
      {
        builder.append("null");
      }
      else if (objects.length == 1)
      {
        builder.append('\'');
        builder.append(QuoteTextFunction.saveConvert(String.valueOf(objects[0])));
        builder.append('\'');
      }<commit_msg>[PDB-902] Fix for content-link JS potentially looping indefinitely.<commit_after>package org.pentaho.reporting.platform.plugin.drilldown;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;

import org.pentaho.reporting.engine.classic.core.ReportEnvironment;
import org.pentaho.reporting.engine.classic.core.function.ReportFormulaContext;
import org.pentaho.reporting.engine.classic.core.function.formula.QuoteTextFunction;
import org.pentaho.reporting.libraries.base.util.CSVTokenizer;
import org.pentaho.reporting.libraries.base.util.StringUtils;
import org.pentaho.reporting.libraries.formula.EvaluationException;
import org.pentaho.reporting.libraries.formula.FormulaContext;
import org.pentaho.reporting.libraries.formula.LibFormulaErrorValue;
import org.pentaho.reporting.libraries.formula.function.Function;
import org.pentaho.reporting.libraries.formula.function.ParameterCallback;
import org.pentaho.reporting.libraries.formula.lvalues.TypeValuePair;
import org.pentaho.reporting.libraries.formula.typing.ArrayCallback;
import org.pentaho.reporting.libraries.formula.typing.Sequence;
import org.pentaho.reporting.libraries.formula.typing.Type;
import org.pentaho.reporting.libraries.formula.typing.coretypes.TextType;
import org.pentaho.reporting.libraries.formula.typing.sequence.RecursiveSequence;

/**
 * Todo: Document me!
 * <p/>
 * Date: 14.01.11
 * Time: 16:46
 *
 * @author <NAME>.
 */
public class ContentLinkFunction implements Function
{
  public ContentLinkFunction()
  {
  }

  public String getCanonicalName()
  {
    return "CONTENTLINK";
  }

  public TypeValuePair evaluate(final FormulaContext context,
                                final ParameterCallback parameters) throws EvaluationException
  {
    final String[] contentLink = getContentLink(context);
    if (contentLink.length == 0)
    {
      throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_NA_VALUE);
    }

    final Object o = parameters.getValue(0);
    final Type type = parameters.getType(0);
    final HashMap<String, Object[]> values = collectParameterValues(o, type, context);

    final StringBuilder builder = new StringBuilder();
    builder.append("javascript:");
    //window.parent.Dashboards.fireChange(PARAM, VALUE);"
    for (int i = 0; i < contentLink.length; i++)
    {
      final String variable = contentLink[i];
      builder.append("var wnd=window.parent;var slf;while(!wnd.Dashboards && wnd.parent && wnd.parent !== wnd){slf=wnd;wnd=wnd.parent};wnd.Dashboards.fireOutputParam(slf,");
      builder.append('\'');
      builder.append(QuoteTextFunction.saveConvert(variable));
      builder.append('\'');
      builder.append(",");

      final Object[] objects = values.get(variable);
      if (objects == null || objects.length == 0)
      {
        builder.append("null");
      }
      else if (objects.length == 1)
      {
        builder.append('\'');
        builder.append(QuoteTextFunction.saveConvert(String.valueOf(objects[0])));
        builder.append('\'');
      }
{'fraction_non_alphanumeric': 0.12296173044925125, 'fraction_numerical': 0.006156405990016639, 'mean_word_length': 4.310070671378092, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5109695', 'n_tokens_mistral': 1842, 'n_tokens_neox': 1737, 'n_words': 289}