PageRenderTime 21ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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