/play2-providers/play2-provider-play22/src/main/java/com/google/code/play2/provider/play22/Play22TemplateCompiler.java

https://github.com/play2-maven-plugin/play2-maven-plugin · Java · 168 lines · 129 code · 23 blank · 16 comment · 4 complexity · 5953a72d0572b836bdf9c795b8a7462b MD5 · raw file

  1. /*
  2. * Copyright 2013-2020 Grzegorz Slowikowski (gslowikowski at gmail dot com)
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing,
  11. * software distributed under the License is distributed on an
  12. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. * KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations
  15. * under the License.
  16. */
  17. package com.google.code.play2.provider.play22;
  18. import java.io.File;
  19. import java.util.Arrays;
  20. import java.util.List;
  21. import scala.Option;
  22. import play.templates.ScalaTemplateCompiler;
  23. import play.templates.TemplateCompilationError;
  24. import com.google.code.play2.provider.api.Play2TemplateCompiler;
  25. import com.google.code.play2.provider.api.TemplateCompilationException;
  26. public class Play22TemplateCompiler
  27. implements Play2TemplateCompiler
  28. {
  29. private static final String[] templateExts = {
  30. "html",
  31. "txt",
  32. "xml",
  33. "js" };
  34. private static final String[] formatterTypes = {
  35. "play.api.templates.HtmlFormat",
  36. "play.api.templates.TxtFormat",
  37. "play.api.templates.XmlFormat",
  38. "play.api.templates.JavaScriptFormat" };
  39. private static final String[] javaAdditionalImports = new String[] {
  40. "play.api.templates._",
  41. "play.api.templates.PlayMagic._",
  42. "models._",
  43. "controllers._",
  44. "java.lang._",
  45. "java.util._",
  46. "scala.collection.JavaConversions._",
  47. "scala.collection.JavaConverters._",
  48. "play.api.i18n._",
  49. "play.core.j.PlayMagicForJava._",
  50. "play.mvc._",
  51. "play.data._",
  52. "play.api.data.Field",
  53. "play.mvc.Http.Context.Implicit._",
  54. "views.%format%._" };
  55. private static final String[] scalaAdditionalImports = new String[] {
  56. "play.api.templates._",
  57. "play.api.templates.PlayMagic._",
  58. "models._",
  59. "controllers._",
  60. "play.api.i18n._",
  61. "play.api.mvc._",
  62. "play.api.data._",
  63. "views.%format%._" };
  64. private File sourceDirectory;
  65. private File outputDirectory;
  66. private List<String> additionalImports;
  67. @Override
  68. public String getCustomOutputDirectoryName()
  69. {
  70. return null;
  71. }
  72. @Override
  73. public List<String> getDefaultJavaImports()
  74. {
  75. return Arrays.asList( javaAdditionalImports );
  76. }
  77. @Override
  78. public List<String> getDefaultScalaImports()
  79. {
  80. return Arrays.asList( scalaAdditionalImports );
  81. }
  82. @Override
  83. public void setSourceDirectory( File sourceDirectory )
  84. {
  85. this.sourceDirectory = sourceDirectory;
  86. }
  87. @Override
  88. public void setOutputDirectory( File outputDirectory )
  89. {
  90. this.outputDirectory = outputDirectory;
  91. }
  92. @Override
  93. public void setAdditionalImports( List<String> additionalImports )
  94. {
  95. this.additionalImports = additionalImports;
  96. }
  97. @Override
  98. public File compile( File templateFile )
  99. throws TemplateCompilationException
  100. {
  101. File result = null;
  102. String fileName = templateFile.getName();
  103. String ext = fileName.substring( fileName.lastIndexOf( '.' ) + 1 );
  104. int index = getTemplateExtIndex( ext );
  105. if ( index >= 0 )
  106. {
  107. String formatterType = formatterTypes[index];
  108. String importsAsString = getImportsAsString( ext );
  109. try
  110. {
  111. Option<File> resultOption =
  112. ScalaTemplateCompiler.compile( templateFile, sourceDirectory, outputDirectory, formatterType,
  113. importsAsString );
  114. result = resultOption.isDefined() ? resultOption.get() : null;
  115. }
  116. catch ( TemplateCompilationError e )
  117. {
  118. throw new TemplateCompilationException( e.source(), e.message(), e.line(), e.column() );
  119. }
  120. }
  121. return result;
  122. }
  123. private int getTemplateExtIndex( String ext )
  124. {
  125. int result = -1;
  126. for ( int i = 0; i < templateExts.length; i++ )
  127. {
  128. if ( templateExts[i].equals( ext ) )
  129. {
  130. result = i;
  131. break;
  132. }
  133. }
  134. return result;
  135. }
  136. private String getImportsAsString( String format )
  137. {
  138. StringBuilder sb = new StringBuilder();
  139. for ( String additionalImport : additionalImports )
  140. {
  141. sb.append( "import " ).append( additionalImport.replace( "%format%", format ) ).append( "\n" );
  142. }
  143. return sb.substring( 0, sb.length() - 1 );
  144. }
  145. }