/play2-providers/play2-provider-play21/src/main/java/com/google/code/play2/provider/play21/Play21TemplateCompiler.java

https://github.com/play2-maven-plugin/play2-maven-plugin · Java · 172 lines · 132 code · 24 blank · 16 comment · 4 complexity · 34c3ebe790483f59d2eda86035ccc274 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.play21;
  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 Play21TemplateCompiler
  27. implements Play2TemplateCompiler
  28. {
  29. private static final String[] templateExts = {
  30. "html",
  31. "txt",
  32. "xml" };
  33. private static final String[] resultTypes = {
  34. "play.api.templates.Html",
  35. "play.api.templates.Txt",
  36. "play.api.templates.Xml" };
  37. private static final String[] formatterTypes = {
  38. "play.api.templates.HtmlFormat",
  39. "play.api.templates.TxtFormat",
  40. "play.api.templates.XmlFormat" };
  41. private static final String[] javaAdditionalImports = new String[] {
  42. "play.api.templates._",
  43. "play.api.templates.PlayMagic._",
  44. "models._",
  45. "controllers._",
  46. "java.lang._",
  47. "java.util._",
  48. "scala.collection.JavaConversions._",
  49. "scala.collection.JavaConverters._",
  50. "play.api.i18n._",
  51. "play.core.j.PlayMagicForJava._",
  52. "play.mvc._",
  53. "play.data._",
  54. "play.api.data.Field",
  55. "play.mvc.Http.Context.Implicit._",
  56. "views.%format%._" };
  57. private static final String[] scalaAdditionalImports = new String[] {
  58. "play.api.templates._",
  59. "play.api.templates.PlayMagic._",
  60. "models._",
  61. "controllers._",
  62. "play.api.i18n._",
  63. "play.api.mvc._",
  64. "play.api.data._",
  65. "views.%format%._" };
  66. private File sourceDirectory;
  67. private File outputDirectory;
  68. private List<String> additionalImports;
  69. @Override
  70. public String getCustomOutputDirectoryName()
  71. {
  72. return null;
  73. }
  74. @Override
  75. public List<String> getDefaultJavaImports()
  76. {
  77. return Arrays.asList( javaAdditionalImports );
  78. }
  79. @Override
  80. public List<String> getDefaultScalaImports()
  81. {
  82. return Arrays.asList( scalaAdditionalImports );
  83. }
  84. @Override
  85. public void setSourceDirectory( File sourceDirectory )
  86. {
  87. this.sourceDirectory = sourceDirectory;
  88. }
  89. @Override
  90. public void setOutputDirectory( File outputDirectory )
  91. {
  92. this.outputDirectory = outputDirectory;
  93. }
  94. @Override
  95. public void setAdditionalImports( List<String> additionalImports )
  96. {
  97. this.additionalImports = additionalImports;
  98. }
  99. @Override
  100. public File compile( File templateFile )
  101. throws TemplateCompilationException
  102. {
  103. File result = null;
  104. String fileName = templateFile.getName();
  105. String ext = fileName.substring( fileName.lastIndexOf( '.' ) + 1 );
  106. int index = getTemplateExtIndex( ext );
  107. if ( index >= 0 )
  108. {
  109. String resultType = resultTypes[index];
  110. String formatterType = formatterTypes[index];
  111. String importsAsString = getImportsAsString( ext );
  112. try
  113. {
  114. Option<File> resultOption =
  115. ScalaTemplateCompiler.compile( templateFile, sourceDirectory, outputDirectory, resultType,
  116. formatterType, importsAsString );
  117. result = resultOption.isDefined() ? resultOption.get() : null;
  118. }
  119. catch ( TemplateCompilationError e )
  120. {
  121. throw new TemplateCompilationException( e.source(), e.message(), e.line(), e.column() );
  122. }
  123. }
  124. return result;
  125. }
  126. private int getTemplateExtIndex( String ext )
  127. {
  128. int result = -1;
  129. for ( int i = 0; i < templateExts.length; i++ )
  130. {
  131. if ( templateExts[i].equals( ext ) )
  132. {
  133. result = i;
  134. break;
  135. }
  136. }
  137. return result;
  138. }
  139. private String getImportsAsString( String format )
  140. {
  141. StringBuilder sb = new StringBuilder();
  142. for ( String additionalImport : additionalImports )
  143. {
  144. sb.append( "import " ).append( additionalImport.replace( "%format%", format ) ).append( "\n" );
  145. }
  146. return sb.substring( 0, sb.length() - 1 );
  147. }
  148. }