Skip to content
Browse files

Extracting node fetching into a separate object.

  • Loading branch information...
1 parent 27b1ff5 commit 7fae42620c014bbec5b2cfa3cb87c2fab1fda73a @ncreep committed Dec 9, 2011
View
25 src/main/scala/com/codecommit/antixml/PathFetcher.scala
@@ -0,0 +1,25 @@
+package com.codecommit.antixml
+import scala.annotation.tailrec
+
+/**
+ * Fetches [[com.codecommit.antixml.ZipperPath]]s from within a [[com.codecommit.antixml.Group]]
+ */
+object PathFetcher { //TODO test
+
+ /**
+ * @param source The group upon which the fetching is performed.
+ * @param path The path to be fetched, assumed to be valid for the given source.
+ * @return An optional node at the end of the path. */
+ def getNode(source: Group[Node])(path: ZipperPath): Option[Node] = getNodeRec(source, path)
+
+ @tailrec
+ private def getNodeRec(currLevel: Group[Node], path: ZipperPath): Option[Node] = {
+ if (path.isEmpty) None
+ else if (path.size == 1) Some(currLevel(path.head))
+ else {
+ // the cast must succeed otherwise the path is invalid
+ val children = currLevel(path.head).asInstanceOf[Elem].children
+ getNodeRec(children, path.tail)
+ }
+ }
+}
View
26 src/main/scala/com/codecommit/antixml/PathTransformer.scala
@@ -2,6 +2,7 @@ package com.codecommit.antixml
import scala.annotation.tailrec
import PathTransformer._
+import PathFetcher._
/** Transforms [[com.codecommit.antixml.ZipperPath]]s with predefined functions.
*
@@ -17,48 +18,31 @@ private[antixml] case class PathTransformer(source: Group[Node]) {
/** Shifts the path one step upwards, if possible.
* @param path The path to be shifted
- * @return An optional path which is the parent of the original path, a new path transformer
- * with an updated cache. */
+ * @return An optional path which is the parent of the original path. */
def shiftUp(path: ZipperPath): Option[ZipperPath] = if (path.isEmpty) None else Some(path.init)
/** Shifts the path one step to the left, if possible.
* @param path The path to be shifted
- * @return An optional path which is a sibling of the original path from the left, a new path transformer
- * with an updated cache. */
+ * @return An optional path which is a sibling of the original path from the left. */
def shiftLeft(path: ZipperPath): Option[ZipperPath] = {
shiftSideways(path, -1)
}
/** Shifts the path one step to the right, if possible.
* @param path The path to be shifted
- * @return An optional path which is a sibling of the original path from the right, a new path transformer
- * with an updated cache. */
+ * @return An optional path which is a sibling of the original path from the right. */
def shiftRight(path: ZipperPath): Option[ZipperPath] = {
shiftSideways(path, +1)
}
- /** @return An optional node at the end of the path and updated cache. */
- private def getNode(path: ZipperPath): Option[Node] = getNode(source, path)
-
- @tailrec
- private def getNode(currLevel: Group[Node], path: ZipperPath): Option[Node] = {
- if (path.isEmpty) None
- else if (path.size == 1) Some(currLevel(path.head))
- else {
- // the cast must succeed otherwise the path is invalid
- val children = currLevel(path.head).asInstanceOf[Elem].children
- getNode(children, path.tail)
- }
- }
-
/** Tries to shift the path sideways by the given increment. */
private def shiftSideways(path: ZipperPath, increment: Int): Option[ZipperPath] = {
assert(!path.isEmpty, "Cannot shift an empty path.")
val currLevel =
if (path.size == 1) source
else {
- val parent = getNode(path.init) // size > 1
+ val parent = getNode(source)(path.init) // size > 1
parent.get.asInstanceOf[Elem].children // must be an elem for a valid path
}

0 comments on commit 7fae426

Please sign in to comment.
Something went wrong with that request. Please try again.