PageRenderTime 296ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/scalaxx/util/Marker.scala

http://scalaxx.googlecode.com/
Scala | 53 lines | 31 code | 7 blank | 15 comment | 0 complexity | 618b86dd8aba3e27eb092675d7596c57 MD5 | raw file
  1. // scalaxx - Scala XML eXtensions
  2. // (c) 2009-2010, Normen M?ller, normen.mueller@gmail.com
  3. // $Id: Marker.scala 84 2010-09-26 16:02:32Z normen.mueller $
  4. package scalaxx.util
  5. import scala.xml._
  6. import scala.xml.NodeSeq.Empty
  7. import scala.xml.Utility.{trimProper => trim}
  8. import scalaxx.path._
  9. /**
  10. * @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
  11. * @version 0.0.5
  12. */
  13. case class Mark(before: Option[Node], self: Option[(Option[MetaData], Option[Node])], after: Option[Node])
  14. /**
  15. * @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
  16. * @version 0.0.5
  17. */
  18. object Marker {
  19. def mark(doc: Node, markers: Map[LocationPath, Mark]) = new Marker(markers).mark(trim(doc), Root.childAt(1))
  20. }
  21. /**
  22. * @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
  23. * @version 0.0.5
  24. */
  25. private [util] class Marker(markers: Map[LocationPath, Mark]) {
  26. def mark(doc: NodeSeq, path: LocationPath): NodeSeq = doc.toList match {
  27. case hd :: tl => (hd match {
  28. case Elem(p,l,as,s,cs@ _*) => markers get path match {
  29. case Some(Mark(before, self, after)) => before.getOrElse(Empty) ++ (self match {
  30. case Some((a, e)) => e match {
  31. case Some(m) => Elem(p, l, as append a.getOrElse(Null), s, (m :: marks(cs, path childAt 1).toList):_*)
  32. case None => Elem(p, l, as append a.getOrElse(Null), s, marks(cs, path childAt 1):_*)
  33. }
  34. case None => Elem(p, l, as, s, marks(cs, path childAt 1):_*)
  35. }) ++ after.getOrElse(Empty)
  36. case None => Elem(p, l, as, s, marks(cs, path childAt 1):_*)
  37. }
  38. case z@_ => z ++ marks(z.child, path childAt 1)
  39. }) ++ mark(tl, path++)
  40. case Nil => NodeSeq.Empty
  41. }
  42. def marks(ns: NodeSeq, path: LocationPath): NodeSeq = ns.toList match {
  43. case hd :: tl => mark(hd, path) ++ marks(tl, path++)
  44. case Nil => NodeSeq.Empty
  45. }
  46. }