/scalate-core/src/main/scala/org/fusesource/scalate/support/TemplatePackage.scala

http://github.com/scalate/scalate · Scala · 74 lines · 36 code · 10 blank · 28 comment · 5 complexity · 1c1b353bd56f79a43dbbc8d619e28660 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.support
  19. import org.fusesource.scalate.util.Strings.isEmpty
  20. import org.fusesource.scalate.util.{ ClassLoaders, Log }
  21. import org.fusesource.scalate.{ Binding, TemplateSource }
  22. /**
  23. * The base class for any **ScalatePackage** class added to the classpath to customize the templates
  24. */
  25. abstract class TemplatePackage {
  26. /**
  27. * Returns the header to add to the top of the method in the generated Scala code for the typesafe template
  28. * implementation for languages like ssp, scaml or jade
  29. */
  30. def header(source: TemplateSource, bindings: List[Binding]): String
  31. }
  32. object TemplatePackage {
  33. val log = Log(getClass); import log._
  34. val scalatePackageClassName = "ScalatePackage"
  35. /**
  36. * Finds the ScalatePackage class by walking from the templates package up the tree until it finds one
  37. */
  38. def findTemplatePackage(source: TemplateSource): Option[TemplatePackage] = {
  39. def packageWalk(packageName: String): Option[TemplatePackage] = {
  40. val className = if (isEmpty(packageName))
  41. scalatePackageClassName
  42. else
  43. packageName + "." + scalatePackageClassName
  44. debug("Trying to find Scalate Package class: " + className)
  45. ClassLoaders.findClass(className) match {
  46. case Some(clazz) =>
  47. debug("using Scalate Package class: " + clazz.getName)
  48. Some(clazz.getConstructor().newInstance().asInstanceOf[TemplatePackage])
  49. case _ =>
  50. if (isEmpty(packageName)) {
  51. debug("No ScalatePackage class found from templates package: " + source.packageName +
  52. " on the class loaders: " + ClassLoaders.defaultClassLoaders)
  53. None
  54. } else {
  55. // lets iterate up the package tree looking for a class
  56. val idx = packageName.lastIndexOf('.')
  57. val parentPackage = if (idx >= 0) packageName.substring(0, idx) else ""
  58. packageWalk(parentPackage)
  59. }
  60. }
  61. }
  62. packageWalk(source.packageName)
  63. }
  64. }