/scalate-core/src/main/scala/org/fusesource/scalate/scuery/NestedTransformer.scala

http://github.com/scalate/scalate · Scala · 64 lines · 26 code · 10 blank · 28 comment · 0 complexity · 8ffdbab85de84dd54df5334d3a2840ea MD5 · raw file

  1. /**
  2. * Copyright (C) 2009-2011 the original author or authors.
  3. * See the notice.md file distributed with this work for additional
  4. * information regarding copyright ownership.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.fusesource.scalate.scuery
  19. import scala.xml.{ Node, NodeSeq }
  20. /**
  21. * A transformer which makes it easy to create nested transformations by switching the thread local
  22. * transformer used for the '$' methods when inside of a transform method.
  23. *
  24. * @version $Revision : 1.1 $
  25. */
  26. class NestedTransformer {
  27. private[this] val rootTransformer = new Transformer
  28. private[this] val threadLocal = new ThreadLocal[Transformer] {
  29. override def initialValue = rootTransformer
  30. }
  31. def $(cssSelector: String) = transformer.$(cssSelector)
  32. def $(selector: Selector) = transformer.$(selector)
  33. def apply(nodes: NodeSeq, ancestors: Seq[Node] = Nil) = transformer(nodes, ancestors)
  34. def transformer: Transformer = threadLocal.get
  35. /**
  36. * Transforms the given nodes passing in a block which is used to configure a new transformer
  37. * to transform the nodes. This method is typically used when performing nested transformations such
  38. * as transforming one or more nodes when inside a transformation rule itself.
  39. */
  40. def transform(nodes: NodeSeq, ancestors: Seq[Node])(rules: Transformer => Unit): NodeSeq = {
  41. val currentTransform = transformer
  42. try {
  43. val childTransform = transformer.createChild
  44. threadLocal.set(childTransform)
  45. rules(childTransform)
  46. childTransform(nodes, ancestors)
  47. } catch {
  48. case e: Throwable =>
  49. threadLocal.set(currentTransform)
  50. throw e
  51. }
  52. }
  53. def transform(nodes: NodeSeq)(rules: Transformer => Unit): NodeSeq = transform(nodes, Nil)(rules)
  54. }