PageRenderTime 30ms CodeModel.GetById 21ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

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