<commit_before>/**
 *  Copyright (C) 2010 Orbeon, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify it under the terms of the
 *  GNU Lesser General Public License as published by the Free Software Foundation; either version
 *  2.1 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 GNU Lesser General Public License for more details.
 *
 *  The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
 */
package org.orbeon.oxf.xforms.analysis

import java.io.ByteArrayOutputStream
import java.{util ⇒ ju}

import org.orbeon.dom.Element
import org.orbeon.io.CharsetNames
import org.orbeon.oxf.common.{OXFException, OrbeonLocationException}
import org.orbeon.oxf.util.XPath.CompiledExpression
import org.orbeon.oxf.util.{IndentedLogger, XPath}
import org.orbeon.oxf.xforms._
import org.orbeon.oxf.xforms.function.Instance
import org.orbeon.oxf.xforms.function.xxforms.XXFormsInstance
import org.orbeon.oxf.xforms.xbl.Scope
import org.orbeon.oxf.xml.dom4j._
import org.orbeon.oxf.xml.{NamespaceMapping, XMLReceiverHelper, XMLUtils}
import org.orbeon.saxon.Configuration
import org.orbeon.saxon.expr.PathMap.{PathMapArc, PathMapNode}
import org.orbeon.saxon.expr._
import org.orbeon.saxon.om.Axis
import org.orbeon.saxon.trace.ExpressionPresenter
import org.orbeon.xforms.XFormsId

import scala.collection.JavaConverters._
import scala.collection.mutable.{LinkedHashSet, Stack}
import scala.util.control.NonFatal
import scala.xml._

class PathMapXPathAnalysis(
  val xpathString            : String,
  var pathmap                : Option[PathMap], // this is used when used as variables and context and can be freed afterwards
  val figuredOutDependencies : Boolean,
  val valueDependentPaths    : MapSet[String, String],
  val returnablePaths        : MapSet[String, String],
  val dependentModels        : collection.Set[String],
  val dependentInstances     : collection.Set[String]
)  extends XPathAnalysis {

  // If `values` is false, the other analysis just adds to the dependencies of the current analysis, but no new
  // returnable values are added.
  def combine(other: XPathAnalysis): XPathAnalysis =
    if (! figuredOutDependencies || ! other.figuredOutDependencies)
      // Either side is negative, return a constant negative with the combined expression
      NegativeAnalysis(XPathAnalysis.combineXPathStrings(xpathString, other.xpathString))
    else
      other match {
        case _: XPathAnalysis.ConstantXPathAnalysis ⇒
          // Other is constant positive analysis, so just return this
          this
        case other: PathMapXPathAnalysis ⇒
          // Both are PathMap analysis so actually combine
          new PathMapXPathAnalysis(
            XPathAnalysis.combineXPathStrings(xpathString, other.xpathString),
            {
              val newPathmap = pathmap.get.clone
              newPathmap.addRoots(other.pathmap.get.clone.getPathMapRoots)
              Some(newPathmap)
            },
            true,
            valueDependentPaths combine other.valueDependentPaths,
            returnablePaths combine other.returnablePaths,
            dependentModels ++ other.dependentModels,
            dependentInstances ++ other.dependentInstances)
        case _ ⇒
          throw new IllegalStateException // should not happen
      }

  def makeValuesDependencies =
    new PathMapXPathAnalysis(
      xpathString,
      Some(pathmap.get.clone),
      true,
      valueDependentPaths combine returnablePaths,
      MapSet.empty[String, String],
      dependentModels,
      dependentInstances
    )

  def toXML(helper: XMLReceiverHelper): Unit = {

    helper.startElement("analysis", Array("expression", xpathString, "analyzed", figuredOutDependencies.toString))

    def toXML(traversable: Iterable[String], enclosingElementName: String, elementName: String): Unit = {
      if (traversable.nonEmpty) {
        helper.startElement(enclosingElementName)
        for (value ← traversable)
          helper.element(elementName, PathMapXPathAnalysis.getDisplayPath(value))
        helper.endElement()
      }
    }<commit_msg>For #3595: rename parameter<commit_after>/**
 *  Copyright (C) 2010 Orbeon, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify it under the terms of the
 *  GNU Lesser General Public License as published by the Free Software Foundation; either version
 *  2.1 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 GNU Lesser General Public License for more details.
 *
 *  The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
 */
package org.orbeon.oxf.xforms.analysis

import java.io.ByteArrayOutputStream
import java.{util ⇒ ju}

import org.orbeon.dom.Element
import org.orbeon.io.CharsetNames
import org.orbeon.oxf.common.{OXFException, OrbeonLocationException}
import org.orbeon.oxf.util.XPath.CompiledExpression
import org.orbeon.oxf.util.{IndentedLogger, XPath}
import org.orbeon.oxf.xforms._
import org.orbeon.oxf.xforms.function.Instance
import org.orbeon.oxf.xforms.function.xxforms.XXFormsInstance
import org.orbeon.oxf.xforms.xbl.Scope
import org.orbeon.oxf.xml.dom4j._
import org.orbeon.oxf.xml.{NamespaceMapping, XMLReceiverHelper, XMLUtils}
import org.orbeon.saxon.Configuration
import org.orbeon.saxon.expr.PathMap.{PathMapArc, PathMapNode}
import org.orbeon.saxon.expr._
import org.orbeon.saxon.om.Axis
import org.orbeon.saxon.trace.ExpressionPresenter
import org.orbeon.xforms.XFormsId

import scala.collection.JavaConverters._
import scala.collection.mutable.{LinkedHashSet, Stack}
import scala.util.control.NonFatal
import scala.xml._

class PathMapXPathAnalysis(
  val xpathString            : String,
  var pathmap                : Option[PathMap], // this is used when used as variables and context and can be freed afterwards
  val figuredOutDependencies : Boolean,
  val valueDependentPaths    : MapSet[String, String],
  val returnablePaths        : MapSet[String, String],
  val dependentModels        : collection.Set[String],
  val dependentInstances     : collection.Set[String]
)  extends XPathAnalysis {

  // If `values` is false, the other analysis just adds to the dependencies of the current analysis, but no new
  // returnable values are added.
  def combine(other: XPathAnalysis): XPathAnalysis =
    if (! figuredOutDependencies || ! other.figuredOutDependencies)
      // Either side is negative, return a constant negative with the combined expression
      NegativeAnalysis(XPathAnalysis.combineXPathStrings(xpathString, other.xpathString))
    else
      other match {
        case _: XPathAnalysis.ConstantXPathAnalysis ⇒
          // Other is constant positive analysis, so just return this
          this
        case other: PathMapXPathAnalysis ⇒
          // Both are PathMap analysis so actually combine
          new PathMapXPathAnalysis(
            XPathAnalysis.combineXPathStrings(xpathString, other.xpathString),
            {
              val newPathmap = pathmap.get.clone
              newPathmap.addRoots(other.pathmap.get.clone.getPathMapRoots)
              Some(newPathmap)
            },
            true,
            valueDependentPaths combine other.valueDependentPaths,
            returnablePaths combine other.returnablePaths,
            dependentModels ++ other.dependentModels,
            dependentInstances ++ other.dependentInstances)
        case _ ⇒
          throw new IllegalStateException // should not happen
      }

  def makeValuesDependencies =
    new PathMapXPathAnalysis(
      xpathString,
      Some(pathmap.get.clone),
      true,
      valueDependentPaths combine returnablePaths,
      MapSet.empty[String, String],
      dependentModels,
      dependentInstances
    )

  def toXML(helper: XMLReceiverHelper): Unit = {

    helper.startElement("analysis", Array("expression", xpathString, "analyzed", figuredOutDependencies.toString))

    def toXML(iterable: Iterable[String], enclosingElementName: String, elementName: String): Unit = {
      if (iterable.nonEmpty) {
        helper.startElement(enclosingElementName)
        for (value ← iterable)
          helper.element(elementName, PathMapXPathAnalysis.getDisplayPath(value))
        helper.endElement()
      }
    }
{'fraction_non_alphanumeric': 0.07803301396744777, 'fraction_numerical': 0.0020778021470622184, 'mean_word_length': 3.393509127789047, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, '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': '2657604', 'n_tokens_mistral': 2470, 'n_tokens_neox': 2425, 'n_words': 671}