alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lift Framework example source code file (Templates.scala)

This example Lift Framework source code file (Templates.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lift Framework tags/keywords

box, box, constructortype, elem, empty, empty, full, full, io, list, list, nodeseq, none, some, string, util

The Lift Framework Templates.scala source code

/*
 * Copyright 2007-2011 WorldWide Conferencing, LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.liftweb 
package http 

import common._
import java.util.Locale
import scala.xml._
import util._
import Helpers._
import java.io.InputStream

/**
 * Contains functions for obtaining templates
 */
object Templates {
  private val suffixes = LiftRules.templateSuffixes

  private def checkForLiftView(part: List[String], last: String, what: LiftRules.ViewDispatchPF): Box[NodeSeq] = {
    if (what.isDefinedAt(part)) {
      what(part) match {
        case Right(lv) => if (lv.dispatch.isDefinedAt(last)) lv.dispatch(last)() else Empty
        case _ => Empty
      }
    } else Empty
  }

  private def checkForFunc(whole: List[String], what: LiftRules.ViewDispatchPF): Box[NodeSeq] =
    if (what.isDefinedAt(whole)) what(whole) match {
      case Left(func) => func()
      case _ => Empty
    }
    else Empty

  private def findInViews(whole: List[String], part: List[String],
                          last: String,
                          what: List[LiftRules.ViewDispatchPF]): Box[NodeSeq] =
    what match {
      case Nil => Empty
      case x :: xs =>
        (checkForLiftView(part, last, x) or checkForFunc(whole, x)) match {
          case Full(ret) => Full(ret)
          case _ => findInViews(whole, part, last, xs)
        }
    }

  /**
   * Given a list of paths (e.g. List("foo", "index")),
   * find the template.  This method runs checkForContentId
   * on any found templates.  To get the raw template,
   * use findRawTemplate
   * @param places - the path to look in
   *
   * @return the template if it can be found
   */
  def apply(places: List[String]): Box[NodeSeq] = 
    apply(places, S.locale)


  /**
   * Given a list of paths (e.g. List("foo", "index")),
   * find the template.  This method runs checkForContentId
   * on any found templates.  To get the raw template,
   * use findRawTemplate
   * @param places - the path to look in
   * @param locale the locale of the template
   *
   * @return the template if it can be found
   */
  def apply(places: List[String], locale: Locale): Box[NodeSeq] = 
    findRawTemplate(places, locale).map(checkForContentId)

  /**
   * Given a list of paths (e.g. List("foo", "index")),
   * find the template.
   * @param places - the path to look in
   *
   * @return the template if it can be found
   */
  @deprecated("use apply")
  def findAnyTemplate(places: List[String]): Box[NodeSeq] =
    findRawTemplate(places, S.locale)

  /**
   * Given a list of paths (e.g. List("foo", "index")),
   * find the template.
   * @param places - the path to look in
   * @param locale - the locale of the template to search for
   *
   * @return the template if it can be found
   */
  @deprecated("use apply")
  def findAnyTemplate(places: List[String], locale: Locale): Box[NodeSeq] = findRawTemplate(places, locale)


  /**
   * Check to see if the template is marked designer friendly
   * and lop off the stuff before the first surround
   */
  def checkForContentId(in: NodeSeq): NodeSeq = {
    def df(in: MetaData): Option[PrefixedAttribute] = in match {
      case Null => None
      case p: PrefixedAttribute 
      if (p.pre == "l" || p.pre == "lift") && 
      (p.key == "content_id") => Some(p)
      case n => df(n.next)
    }
    
    
    in.flatMap {
      case e: Elem if e.label == "html" => df(e.attributes)
      case _ => None
    }.flatMap {
      md => Helpers.findId(in, md.value.text)
    }.headOption orElse 
    in.flatMap {
      case e: Elem if e.label == "html" =>
        e.child.flatMap {
          case e: Elem if e.label == "body" => {
            e.attribute("class").flatMap {
              ns => {
                val clz = ns.text.charSplit(' ')
                clz.flatMap {
                  case s if s.startsWith("lift:content_id=") =>
                    Some(urlDecode(s.substring("lift:content_id=".length)))
                  case _ => None
                }.headOption
                
              }
            }
          }

          case _ => None
        }
      case _ => None
    }.flatMap {
      id => Helpers.findId(in, id)
    }.headOption getOrElse in
  }


  /**
   * Given a list of paths (e.g. List("foo", "index")),
   * find the template.
   * @param places - the path to look in
   * @param locale - the locale of the template to search for
   *
   * @return the template if it can be found
   */
  def findRawTemplate(places: List[String], locale: Locale): Box[NodeSeq] = {
    /*
     From a Scala coding standpoint, this method is ugly.  It's also a performance
     hotspot that needed some tuning.  I've made the code very imperative and
     tried to make sure there are no anonymous functions created in this method.
     The few extra lines of code and the marginal reduction in readibility should
     yield better performance.  Please don't change this method without chatting with
     me first.  Thanks!  DPP
     */
    val lrCache = LiftRules.templateCache
    val cache = if (lrCache.isDefined) lrCache.open_! else NoCache

    val parserFunction: InputStream => Box[NodeSeq] = 
      S.htmlProperties.htmlParser

    val key = (locale, places)
    val tr = cache.get(key)

    if (tr.isDefined) tr
    else
      {
        val part = places.dropRight(1)
        val last = places.last

        findInViews(places, part, last, LiftRules.viewDispatch.toList) match {
          case Full(lv) =>
            Full(lv)

          case _ =>
            val pls = places.mkString("/", "/", "")

            val se = suffixes.iterator
            val sl = List("_" + locale.toString, "_" + locale.getLanguage, "")

            var found = false
            var ret: NodeSeq = null

            while (!found && se.hasNext) {
              val s = se.next
              val le = sl.iterator
              while (!found && le.hasNext) {
                val p = le.next
                val name = pls + p + (if (s.length > 0) "." + s else "")
                import scala.xml.dtd.ValidationException
                val xmlb = try {
                  LiftRules.doWithResource(name) { parserFunction } match {
                    case Full(seq) => seq
                    case _ => Empty
                  }
                } catch {
                  case e: ValidationException if Props.devMode | Props.testMode =>
                    return Helpers.errorDiv(<div>Error locating template: {name}
Message: <b>{e.getMessage}
{ <pre>{e.toString}{e.getStackTrace.map(_.toString).mkString("\n")}
} </div>) case e: ValidationException => Empty } if (xmlb.isDefined) { found = true ret = (cache(key) = xmlb.open_!) } else if (xmlb.isInstanceOf[Failure] && (Props.devMode | Props.testMode)) { val msg = xmlb.asInstanceOf[Failure].msg val e = xmlb.asInstanceOf[Failure].exception return Helpers.errorDiv(<div>Error locating template: {name}
Message: {msg}
{ { e match { case Full(e) => <pre>{e.toString}{e.getStackTrace.map(_.toString).mkString("\n")} case _ => NodeSeq.Empty } }} </div>) } } } if (found) Full(ret) else lookForClasses(places) } } } private def lookForClasses(places: List[String]): Box[NodeSeq] = { val (controller, action) = places match { case ctl :: act :: _ => (ctl, act) case ctl :: _ => (ctl, "index") case Nil => ("default_template", "index") } val trans = List[String => String](n => n, n => camelify(n)) val toTry = trans.flatMap(f => (LiftRules.buildPackage("view") ::: ("lift.app.view" :: Nil)).map(_ + "." + f(controller))) first(toTry) { clsName => try { tryo(List(classOf[ClassNotFoundException]), Empty)(Class.forName(clsName).asInstanceOf[Class[AnyRef]]).flatMap { c => (c.newInstance match { case inst: InsecureLiftView => c.getMethod(action).invoke(inst) case inst: LiftView if inst.dispatch.isDefinedAt(action) => inst.dispatch(action)() case _ => Empty }) match { case null | Empty | None => Empty case n: Group => Full(n) case n: Elem => Full(n) case s: NodeSeq => Full(s) case Some(n: Group) => Full(n) case Some(n: Elem) => Full(n) case Some(n: NodeSeq) => Full(n) case Some(SafeNodeSeq(n)) => Full(n) case Full(n: Group) => Full(n) case Full(n: Elem) => Full(n) case Full(n: NodeSeq) => Full(n) case Full(SafeNodeSeq(n)) => Full(n) case _ => Empty } } } catch { case ite: java.lang.reflect.InvocationTargetException => throw ite.getCause case e: NoClassDefFoundError => Empty } } } } /** * A case class that contains the information necessary to set up a CometActor */ final case class CometCreationInfo(contType: String, name: Box[String], defaultXml: NodeSeq, attributes: Map[String, String], session: LiftSession) /** * Throw this exception if there's a catostrophic failure executing * a snippet */ class SnippetExecutionException(msg: String) extends SnippetFailureException(msg) { def snippetFailure = LiftRules.SnippetFailures.ExecutionFailure } /** * An abstract exception that may be thrown during page rendering. * The exception is caught and the appropriate report of a SnippetError * is generated */ abstract class SnippetFailureException(msg: String) extends LiftFlowOfControlException(msg) { def snippetFailure: LiftRules.SnippetFailures.Value def buildStackTrace: NodeSeq = getStackTrace.toList.dropWhile { e => { val cn = e.getClassName cn.startsWith("net.liftweb.http") || cn.startsWith("net.liftweb.common") || cn.startsWith("net.liftweb.util") } }.filter { e => { val cn = e.getClassName !cn.startsWith("java.lang") && !cn.startsWith("sun.") } }.take(10).toList.map{ e => <code>
{e.toString}
} } class StateInStatelessException(msg: String) extends SnippetFailureException(msg) { def snippetFailure: LiftRules.SnippetFailures.Value = LiftRules.SnippetFailures.StateInStateless } /** * Holds a pair of parameters */ private case class ParamPair(v: Any, clz: Class[_]) /** * a trait that defines some ways of constructing an instance */ private sealed trait ConstructorType /** * A unit constructor... just pass in null */ private final case class UnitConstructor(c: java.lang.reflect.Constructor[_]) extends ConstructorType { def makeOne[T]: T = c.newInstance().asInstanceOf[T] } /** * A parameter and session constructor */ private final case class PAndSessionConstructor(c: java.lang.reflect.Constructor[_]) extends ConstructorType { def makeOne[T](p: Any, s: LiftSession): T = c.newInstance(p.asInstanceOf[Object], s).asInstanceOf[T] } /** * A parameter constructor */ private final case class PConstructor(c: java.lang.reflect.Constructor[_]) extends ConstructorType { def makeOne[T](p: Any): T = c.newInstance(p.asInstanceOf[Object]).asInstanceOf[T] }

Other Lift Framework examples (source code examples)

Here is a short list of links related to this Lift Framework Templates.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.