Skip to content

Commit

Permalink
(cherry picked from commit f8e86b1)
Browse files Browse the repository at this point in the history
  • Loading branch information
rjregenold authored and chris-twiner committed Jan 8, 2024
1 parent 3b286c3 commit 05204b2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import scales.xml.{Elem, EndElem, PullType, QName, ScalesXml, XmlBuilder, XmlIte
import scales.xml.parser.strategies.{MemoryOptimisationStrategy, OptimisationToken}
import collection.FlatMapIterator
import scalaz.Id.Id
import scalaz.Monad
import scalaz.Scalaz.ToEqualOps
import scalaz.{Equal, Monad}
import scalaz.iteratee.Input.{Empty, Eof}
import scalaz.iteratee.Iteratee.{iteratee, iterateeT}
import scalaz.iteratee.StepT
Expand All @@ -30,7 +31,7 @@ trait PullIteratees {
* would return an iteratee that returned every <ofInterest> content </ofInterest>
* as a path (each parent node containing only one child node).
*/
def onQNames[F[_]: Monad](qnames: List[QName]): ResumableIter[PullType, F, QNamesMatch] = {
def onQNames[F[_]: Monad](qnames: List[QName])(implicit qe: Equal[QName]): ResumableIter[PullType, F, QNamesMatch] = {

/*
* The pairs allow the depth of each element to be followed. In particular this stops both descent and ascent problems in the
Expand All @@ -44,11 +45,11 @@ trait PullIteratees {
iterateeT( Monad[F].point(
s(el = {
case Left(elem@Elem(q, a, n)) => {
val nfocus = if (q == focus._1) (focus._1, focus._2 + 1)
val nfocus = if (q === focus._1) (focus._1, focus._2 + 1)
else focus
val npath = addAndFocus(path, elem)

val shouldCollect = collecting || (toGo.isEmpty && q == focus._1)
val shouldCollect = collecting || (toGo.isEmpty && q === focus._1)

Cont(
// is it our head?
Expand All @@ -68,7 +69,7 @@ trait PullIteratees {

case Right(EndElem(q, n)) =>

if (q == focus._1) {
if (q === focus._1) {
val ncfocus = (focus._1, focus._2 - 1)

if (toGo.isEmpty && ncfocus._2 == 0) // we are popping to the selected level
Expand Down Expand Up @@ -179,22 +180,22 @@ trait PullIteratees {
/**
* Wraps XmlPull
*/
def iterate(path: List[QName], xml: XmlPull): FlatMapIterator[XmlPath] = iterate(path, xml.it)
def iterate(path: List[QName], xml: XmlPull)(implicit qe: Equal[QName]): FlatMapIterator[XmlPath] = iterate(path, xml.it)

/**
* A wrapping around withIter(onDone(List(onQNames(path))))(enumXml(xml, _))
* it unwraps the data providing an Iterator[XPath]
*/
def iterate(path: List[QName], xml: Iterator[PullType]): FlatMapIterator[XmlPath] =
def iterate(path: List[QName], xml: Iterator[PullType])(implicit qe: Equal[QName]): FlatMapIterator[XmlPath] =
new Iterate(path, xml)

}

/**
* Iterates over a path of QNames producing XPaths for a given Iterator[PullType]
*/
class Iterate(path: List[QName], xml: Iterator[PullType]) extends FlatMapIterator[XmlPath] {
import ScalesXml._
class Iterate(path: List[QName], xml: Iterator[PullType])(implicit qe: Equal[QName]) extends FlatMapIterator[XmlPath] {
import ScalesXml.{qnameEqual => _, _}
import ScalesUtils._
import ximpl.TreeProxies
val qnames = path
Expand All @@ -211,7 +212,7 @@ class Iterate(path: List[QName], xml: Iterator[PullType]) extends FlatMapIterato

def reset {
set(Nil, (qnames.head, 0), qnames.tail.map((_,0)),
proxies.reuse, false)
proxies.reuse, false)
}

reset
Expand Down Expand Up @@ -242,16 +243,16 @@ class Iterate(path: List[QName], xml: Iterator[PullType]) extends FlatMapIterato

case Left(elem@Elem(q, a, n)) => {
val nfocus =
if (q == focus._1) (focus._1, focus._2 + 1)
if (q === focus._1) (focus._1, focus._2 + 1)
else focus

proxies.beginSub(elem, XmlBuilder())
//val npath = addAndFocus(path, elem)

val shouldCollect = collecting || (toGo.isEmpty && q == focus._1)
val shouldCollect = collecting || (toGo.isEmpty && q === focus._1)

// is it our head?
if ((!toGo.isEmpty) && q == focus._1)
if ((!toGo.isEmpty) && q === focus._1)
// move down
set(before :+ focus, toGo.head, toGo.tail, proxies, false)
else
Expand All @@ -270,7 +271,7 @@ class Iterate(path: List[QName], xml: Iterator[PullType]) extends FlatMapIterato

case Right(EndElem(q, n)) =>

if (q == focus._1) {
if (q === focus._1) {
val ncfocus = (focus._1, focus._2 - 1)

if (toGo.isEmpty && ncfocus._2 == 0) { // we are popping to the selected level
Expand Down
27 changes: 23 additions & 4 deletions scales-xml/src/test/scala/scales/xml/PullIterateTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import scalaz.EphemeralStream
/**
* Prior to 0.4.4 iterate was based on onQNames, the PullTest qnames test covered this, and although the implementation is pretty much identical, these test from the iterate function directly.
*/
/*class PullIterateTest extends junit.framework.TestCase {
class PullIterateTest extends junit.framework.TestCase {

import junit.framework.Assert._
import java.io._
Expand Down Expand Up @@ -71,7 +71,7 @@ import scalaz.EphemeralStream
}

}
/*
def testOnQNamesRepeatedQNames = {
val ourMax = maxIterations / 10 // full takes too long but does work in constant space
Expand All @@ -89,7 +89,26 @@ import scalaz.EphemeralStream
assertEquals(1, x.zipUp.children.size)
}
}*/

def testOnQNameEqualImplicit : Unit = {

import scalaz.Equal._

val ourMax = maxIterations / 10 // full takes too long but does work in constant space

val iter = events(ourMax).iterator

implicit val qnameEqual = equal { (a: QName, b: QName) =>
a.local.equalsIgnoreCase(b.local)
}

val QNames = List("root"l, "CHILD"l, "interESTING"l)

val itr = iterate(QNames, iter)

assertFalse("failed to match nodes", itr.isEmpty)

}

}
*/
}

0 comments on commit 05204b2

Please sign in to comment.