<commit_before>package org.bowlerframework.view.scalate

import org.bowlerframework.view.ViewRenderer
import java.io.{StringWriter, PrintWriter}
import org.bowlerframework.exception.{ValidationException, HttpException}
import collection.mutable.MutableList
import org.bowlerframework.model.ViewModelBuilder
import org.bowlerframework.{GET, HTTP, Response, Request}
import org.bowlerframework.view.squery.ViewComponentRegistry

/**
 * A ViewRenderer that uses Scalate templates to render views
 */
class ScalateViewRenderer extends BrowserViewRenderer {

  protected def render(request: Request, response: Response, model: Map[String, Any]) = {
    val viewValue: String = {
      val squeryView = ViewComponentRegistry(request, model)
      squeryView match{
        case None => {
          val view = TemplateRegistry.templateResolver.resolveViewTemplate(request)
          val engine = RenderEngine.getEngine
          val writer = new StringWriter
          val pw = new PrintWriter(writer)
          val context = new BowlerRenderContext(view.uri, engine, pw)
          context.render(view.uri, model)
          writer.toString
        }
        case Some(component) => component.render.toString
      }
    }

    val layout = Layout.activeLayout(request)
    if (layout != None)
      renderLayout(layout.get, request, response, model, viewValue)
    else
      response.getWriter.write(viewValue)

    if (request.getMethod == GET) {
      request.getSession.setLastGetPath(HTTP.relativeUrl(request.getPath))
    }
  }

  private def renderLayout(layout: Layout, request: Request, response: Response, viewModel: Map[String, Any], view: String) {
    val engine = RenderEngine.getEngine
    var layoutModel = layout.layoutModel.model(request, viewModel, view)

    val parent = TemplateRegistry.templateResolver.resolveLayout(request, layout)
    val stringWriter = new StringWriter
    var writer: PrintWriter = null
    if (layout.parentLayout == None)
      writer = response.getWriter
    else {
      writer = new PrintWriter(stringWriter)
    }

    val responseContext = new BowlerRenderContext(TemplateRegistry.templateResolver.resolveLayout(request, layout).uri, engine, writer)
    responseContext.render(parent.uri, layoutModel.toMap)
    if (layout.parentLayout != None)
      renderLayout(layout.parentLayout.get, request, response, viewModel, stringWriter.toString)
  }

}<commit_msg>minor cleanup of non-Scalaesque code<commit_after>package org.bowlerframework.view.scalate

import java.io.{StringWriter, PrintWriter}
import org.bowlerframework.{GET, HTTP, Response, Request}
import org.bowlerframework.view.squery.ViewComponentRegistry

/**
 * A ViewRenderer that uses Scalate templates to render views
 */
class ScalateViewRenderer extends BrowserViewRenderer {

  protected def render(request: Request, response: Response, model: Map[String, Any]) = {
    val viewValue: String = {
      val squeryView = ViewComponentRegistry(request, model)
      squeryView match{
        case None => {
          val view = TemplateRegistry.templateResolver.resolveViewTemplate(request)
          val engine = RenderEngine.getEngine
          val writer = new StringWriter
          val pw = new PrintWriter(writer)
          val context = new BowlerRenderContext(view.uri, engine, pw)
          context.render(view.uri, model)
          writer.toString
        }
        case Some(component) => component.render.toString
      }
    }

    Layout.activeLayout(request) match{
      case None => response.getWriter.write(viewValue)
      case Some(layout) => renderLayout(layout, request, response, model, viewValue)
    }

    if (request.getMethod == GET) {
      request.getSession.setLastGetPath(HTTP.relativeUrl(request.getPath))
    }
  }

  private def renderLayout(layout: Layout, request: Request, response: Response, viewModel: Map[String, Any], view: String) {
    val engine = RenderEngine.getEngine
    var layoutModel = layout.layoutModel.model(request, viewModel, view)

    val parent = TemplateRegistry.templateResolver.resolveLayout(request, layout)
    val stringWriter = new StringWriter
    var writer: PrintWriter = null

    layout.parentLayout match{
      case None => writer = response.getWriter
      case Some(parent) => writer = new PrintWriter(stringWriter)
    }

    val responseContext = new BowlerRenderContext(TemplateRegistry.templateResolver.resolveLayout(request, layout).uri, engine, writer)
    responseContext.render(parent.uri, layoutModel.toMap)
    if (layout.parentLayout != None)
      renderLayout(layout.parentLayout.get, request, response, viewModel, stringWriter.toString)
  }

}
{'fraction_non_alphanumeric': 0.0889894419306184, 'fraction_numerical': 0.0, 'mean_word_length': 3.7954545454545454, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '832779', 'n_tokens_mistral': 1237, 'n_tokens_neox': 1221, 'n_words': 308}