<commit_before>/**
 * Copyright (C) 2007 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.function

import org.orbeon.oxf.util.CollectionUtils._
import org.orbeon.oxf.xforms.model.RuntimeBind
import org.orbeon.oxf.xml.DependsOnContextItem
import org.orbeon.saxon.expr.XPathContext
import org.orbeon.saxon.om.{EmptyIterator, ListIterator, SequenceIterator}


class Bind extends XFormsFunction with DependsOnContextItem {

  override def iterate(xpathContext: XPathContext): SequenceIterator = {

    implicit val ctx = xpathContext

    val bindId          = stringArgument(0)
    val searchAncestors = booleanArgument(1, default = false)

    val contextItemOpt =
      Option(xpathContext.getContextItem)

    val startContainer = XFormsFunction.context.container

    val startContainerIt =
      startContainer.searchContainedModelsInScope(getSourceEffectiveId, bindId, contextItemOpt).iterator

    val searchIt =
      if (searchAncestors)
        startContainerIt ++
          startContainer.ancestorsIterator.drop(1).flatMap(_.searchContainedModels(bindId, contextItemOpt))
      else
        startContainerIt

    searchIt.nextOption() match {
      case Some(bind: RuntimeBind) => new ListIterator(bind.items)
      case _                       => EmptyIterator.getInstance
    }
  }
}<commit_msg>Comment<commit_after>/**
 * Copyright (C) 2007 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.function

import org.orbeon.oxf.util.CollectionUtils._
import org.orbeon.oxf.xforms.model.RuntimeBind
import org.orbeon.oxf.xml.DependsOnContextItem
import org.orbeon.saxon.expr.XPathContext
import org.orbeon.saxon.om.{EmptyIterator, ListIterator, SequenceIterator}


class Bind extends XFormsFunction with DependsOnContextItem {

  override def iterate(xpathContext: XPathContext): SequenceIterator = {

    implicit val ctx = xpathContext

    val bindId          = stringArgument(0)
    val searchAncestors = booleanArgument(1, default = false)

    val contextItemOpt =
      Option(xpathContext.getContextItem)

    val startContainer = XFormsFunction.context.container

    val startContainerIt =
      startContainer.searchContainedModelsInScope(getSourceEffectiveId, bindId, contextItemOpt).iterator

    val searchIt =
      if (searchAncestors)
        startContainerIt ++
          startContainer.ancestorsIterator.drop(1).flatMap(_.searchContainedModels(bindId, contextItemOpt))
      else
        startContainerIt

    searchIt.nextOption() match {
      case Some(bind: RuntimeBind) => new ListIterator(bind.items)
      case _                       => EmptyIterator.getInstance
    }
  }

  // TODO: PathMap
}
{'fraction_non_alphanumeric': 0.07232375979112271, 'fraction_numerical': 0.004699738903394256, 'mean_word_length': 3.7121771217712176, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5327914', 'n_tokens_mistral': 1081, 'n_tokens_neox': 1038, 'n_words': 328}