PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/src/play/src/main/scala/play/api/templates/Templates.scala

https://github.com/nining/Play20
Scala | 233 lines | 92 code | 51 blank | 90 comment | 0 complexity | de7ed88bd7221f7cc5eefe2f8373da5a MD5 | raw file
  1. package play.api.templates
  2. import play.api.mvc._
  3. import play.templates._
  4. /**
  5. * Content type used in default HTML templates.
  6. *
  7. * @param text the HTML text
  8. */
  9. case class Html(text: String) extends Appendable[Html] with Content with play.mvc.Content {
  10. val buffer = new StringBuilder(text)
  11. /**
  12. * Appends this HTML fragment to another.
  13. */
  14. def +(other: Html): Html = {
  15. buffer.append(other.buffer)
  16. this
  17. }
  18. override def toString = buffer.toString
  19. /**
  20. * Content type of HTML (`text/html`).
  21. */
  22. def contentType: String = "text/html"
  23. def body: String = toString
  24. }
  25. /**
  26. * Helper for HTML utility methods.
  27. */
  28. object Html {
  29. /**
  30. * Creates an empty HTML fragment.
  31. */
  32. def empty: Html = Html("")
  33. }
  34. /**
  35. * Formatter for HTML content.
  36. */
  37. object HtmlFormat extends Format[Html] {
  38. /**
  39. * Creates a raw (unescaped) HTML fragment.
  40. */
  41. def raw(text: String): Html = Html(text)
  42. /**
  43. * Creates a safe (escaped) HTML fragment.
  44. */
  45. def escape(text: String): Html = Html(org.apache.commons.lang.StringEscapeUtils.escapeHtml(text))
  46. }
  47. /**
  48. * Content type used in default text templates.
  49. *
  50. * @param text The plain text.
  51. */
  52. case class Txt(text: String) extends Appendable[Txt] with Content with play.mvc.Content {
  53. val buffer = new StringBuilder(text)
  54. /**
  55. * Appends this text fragment to another.
  56. */
  57. def +(other: Txt): this.type = {
  58. buffer.append(other.buffer)
  59. this
  60. }
  61. override def toString = buffer.toString
  62. /**
  63. * Content type of text (`text/plain`).
  64. */
  65. def contentType = "text/plain"
  66. def body = toString
  67. }
  68. /**
  69. * Helper for utilities Txt methods.
  70. */
  71. object Txt {
  72. /**
  73. * Creates an empty text fragment.
  74. */
  75. def empty = Txt("")
  76. }
  77. /**
  78. * Formatter for text content.
  79. */
  80. object TxtFormat extends Format[Txt] {
  81. /**
  82. * Create a text fragment.
  83. */
  84. def raw(text: String) = Txt(text)
  85. /**
  86. * No need for a safe (escaped) text fragment.
  87. */
  88. def escape(text: String) = Txt(text)
  89. }
  90. /**
  91. * Content type used in default XML templates.
  92. *
  93. * @param text the plain xml text
  94. */
  95. case class Xml(text: String) extends Appendable[Xml] with Content with play.mvc.Content {
  96. val buffer = new StringBuilder(text)
  97. /** Append this XML fragment to another. */
  98. def +(other: Xml) = {
  99. buffer.append(other.buffer)
  100. this
  101. }
  102. override def toString = buffer.toString
  103. /**
  104. * Content type of XML (`text/xml`).
  105. */
  106. def contentType = "text/xml"
  107. def body = toString
  108. }
  109. /**
  110. * Helper for XML utility methods.
  111. */
  112. object Xml {
  113. /**
  114. * Create an empty XML fragment.
  115. */
  116. def empty = Xml("")
  117. }
  118. /**
  119. * Formatter for XML content.
  120. */
  121. object XmlFormat extends Format[Xml] {
  122. /**
  123. * Creates an XML fragment.
  124. */
  125. def raw(text: String) = Xml(text)
  126. /**
  127. * Creates an escaped XML fragment.
  128. */
  129. def escape(text: String) = Xml(org.apache.commons.lang.StringEscapeUtils.escapeXml(text))
  130. }
  131. /** Defines a magic helper for Play templates. */
  132. object PlayMagic {
  133. /**
  134. * Generates a set of valid HTML attributes.
  135. *
  136. * For example:
  137. * {{{
  138. * toHtmlArgs(Seq('id -> "item", 'style -> "color:red"))
  139. * }}}
  140. */
  141. def toHtmlArgs(args: Map[Symbol, Any]) = Html(args.map(a => a._1.name + "=\"" + a._2 + "\"").mkString(" "))
  142. }
  143. /** Defines a magic helper for Play templates in a Java context. */
  144. object PlayMagicForJava {
  145. import scala.collection.JavaConverters._
  146. /** Transforms a Play Java `Option` to a proper Scala `Option`. */
  147. implicit def javaOptionToScala[T](x: play.libs.F.Option[T]): Option[T] = x match {
  148. case x: play.libs.F.Some[_] => Some(x.get)
  149. case x: play.libs.F.None[_] => None
  150. }
  151. implicit def implicitJavaLang: play.api.i18n.Lang = {
  152. try {
  153. play.mvc.Http.Context.Implicit.lang.asInstanceOf[play.api.i18n.Lang]
  154. } catch {
  155. case _ => play.api.i18n.Lang.defaultLang
  156. }
  157. }
  158. /**
  159. * Implicit conversion of a Play Java form `Field` to a proper Scala form `Field`.
  160. */
  161. implicit def javaFieldtoScalaField(jField: play.data.Form.Field): play.api.data.Field = {
  162. new play.api.data.Field(
  163. null,
  164. jField.name,
  165. jField.constraints.asScala.map { jT =>
  166. jT._1 -> jT._2.asScala
  167. },
  168. Option(jField.format).map(f => f._1 -> f._2.asScala),
  169. jField.errors.asScala.map { jE =>
  170. play.api.data.FormError(
  171. jE.key,
  172. jE.message,
  173. jE.arguments.asScala)
  174. },
  175. Option(jField.value)) {
  176. override def apply(key: String) = {
  177. javaFieldtoScalaField(jField.sub(key))
  178. }
  179. override lazy val indexes = jField.indexes.asScala.toSeq.map(_.toInt)
  180. }
  181. }
  182. }