com.twitter.scrooge.mustache.MustacheLoader.scala Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2011 Twitter, Inc.
*
* 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 com.twitter.scrooge.mustache
import java.util.logging.{Level, Logger}
import java.util.Properties
import scala.collection.concurrent.TrieMap
import scala.io.Source
class HandlebarLoader(prefix: String, suffix: String = ".scala") {
private[this] val cache = new TrieMap[String, Handlebar]
def apply(name: String): Handlebar = {
val fullName = prefix + name + suffix
cache.getOrElseUpdate(name,
getClass.getResourceAsStream(fullName) match {
case null => {
throw new NoSuchElementException("template not found: " + fullName)
}
case inputStream => {
try {
new Handlebar(Source.fromInputStream(inputStream).getLines().mkString("\n"))
} catch {
case e: Exception => {
println("Exception parsing template at " + fullName)
throw e
}
}
}
}
)
}
val header = {
val p = new Properties
val resource = getClass.getResource("/com/twitter/scrooge-generator/build.properties")
if (resource == null)
Logger.getLogger("scrooge-generator").log(Level.WARNING, "Scrooge's build.properties not found")
else
p.load(resource.openStream())
Seq(
"/**",
" * Generated by Scrooge",
" * version: %s".format(p.getProperty("version", "?")),
" * rev: %s".format(p.getProperty("build_revision", "?")),
" * built at: %s".format(p.getProperty("build_name", "?")),
" */\n"
).mkString("\n")
}
}