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

http://github.com/scalate/scalate · Scala · 75 lines · 37 code · 6 blank · 32 comment · 4 complexity · b2625bf86dd228554df9477f50e107db 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
  19. package support
  20. import util.{ Log, ClassLoaders, Files }
  21. object DefaultTemplatePackage extends Log
  22. /**
  23. * A TemplatePackage where we try and find an object/controller/resource type based on the name of the current template and if we can
  24. * find it then we create a variable called **it** of the controller type and import its values into the template.
  25. *
  26. * This approach can be used for JAXRS controllers or for template views of objects. It avoids having to explicitly
  27. * import the controller or 'it' variable from the attribute scope
  28. */
  29. class DefaultTemplatePackage extends TemplatePackage {
  30. import DefaultTemplatePackage._
  31. def header(source: TemplateSource, bindings: List[Binding]) = {
  32. bindings.find(_.name == "it") match {
  33. case Some(b) =>
  34. // already has a binding so don't do anything
  35. ""
  36. case _ =>
  37. val cleanUri = source.uri.stripPrefix("/").stripPrefix("WEB-INF/")
  38. val extensions = cleanUri.split('.').tail
  39. var className = cleanUri.replace('/', '.')
  40. extensions.map {
  41. e =>
  42. className = Files.dropExtension(className)
  43. ClassLoaders.findClass(className)
  44. }.find(_.isDefined).flatten match {
  45. case Some(clazz) =>
  46. val it = "val " + variableName + " = attribute[_root_." + clazz.getName + "](\"" + variableName + "\")\n"
  47. if (importMethod) {
  48. it + "import it._\n"
  49. } else {
  50. it
  51. }
  52. case _ =>
  53. if (!className.split('.').last.startsWith("_")) {
  54. // lets ignore partial templates which are never bound to a resource directly
  55. debug("Could not find a class on the classpath based on the current url: %s", cleanUri)
  56. }
  57. ""
  58. }
  59. }
  60. }
  61. /**
  62. * The name of the variable
  63. */
  64. def variableName = "it"
  65. /**
  66. * Returns whether or not the methods on the variable should be imported
  67. */
  68. def importMethod = true
  69. }