PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/atlassian-plugins-osgi-testrunner-bundle/src/main/java/com/atlassian/plugins/osgi/test/util/SoyTransformer.java

https://bitbucket.org/Adaptavist/atlassian-plugins-osgi-testrunner-parent
Java | 109 lines | 87 code | 19 blank | 3 comment | 2 complexity | 4b21f41003e20daccbab2ddead36f49d MD5 | raw file
  1. package com.atlassian.plugins.osgi.test.util;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.atlassian.plugin.elements.ResourceLocation;
  5. import com.atlassian.plugin.servlet.DownloadableResource;
  6. import com.atlassian.plugin.webresource.transformer.AbstractStringTransformedDownloadableResource;
  7. import com.atlassian.plugin.webresource.transformer.WebResourceTransformer;
  8. import com.atlassian.sal.api.ApplicationProperties;
  9. import com.atlassian.sal.api.message.I18nResolver;
  10. import com.google.inject.*;
  11. import com.google.inject.multibindings.Multibinder;
  12. import com.google.template.soy.SoyFileSet;
  13. import com.google.template.soy.SoyModule;
  14. import com.google.template.soy.jssrc.SoyJsSrcOptions;
  15. import com.google.template.soy.shared.restricted.SoyFunction;
  16. import com.google.template.soy.xliffmsgplugin.XliffMsgPluginModule;
  17. import org.dom4j.Element;
  18. /**
  19. * @since version
  20. */
  21. public class SoyTransformer implements WebResourceTransformer
  22. {
  23. private final I18nResolver i18n;
  24. private final ApplicationProperties applicationProperties;
  25. public SoyTransformer(I18nResolver i18n, ApplicationProperties applicationProperties)
  26. {
  27. this.i18n = i18n;
  28. this.applicationProperties = applicationProperties;
  29. }
  30. @Override
  31. public DownloadableResource transform(Element element, ResourceLocation location, String filePath, DownloadableResource nextResource)
  32. {
  33. return new SoyResource(nextResource, location.getLocation());
  34. }
  35. private class SoyResource extends AbstractStringTransformedDownloadableResource
  36. {
  37. private final String location;
  38. private SoyResource(DownloadableResource originalResource, String location)
  39. {
  40. super(originalResource);
  41. this.location = location;
  42. }
  43. @Override
  44. public String getContentType()
  45. {
  46. return "text/javascript";//NON-NLS
  47. }
  48. @Override
  49. protected String transform(String originalContent)
  50. {
  51. List<Module> guiceModules = new ArrayList<Module>();
  52. guiceModules.add(new SoyModule());
  53. guiceModules.add(new XliffMsgPluginModule());
  54. guiceModules.add(new SpringBridgeModule());
  55. guiceModules.add(new OurFunctionsModule());
  56. final Injector injector = Guice.createInjector(guiceModules);
  57. SoyFileSet.Builder sfsBuilder = injector.getInstance(SoyFileSet.Builder.class);
  58. SoyJsSrcOptions jsSrcOptions = new SoyJsSrcOptions();
  59. jsSrcOptions.setShouldGenerateJsdoc(false);
  60. sfsBuilder.add(originalContent, location);
  61. SoyFileSet sfs = sfsBuilder.build();
  62. final List<String> output = sfs.compileToJsSrc(jsSrcOptions, null);
  63. if (output.size() != 1)
  64. {
  65. throw new IllegalStateException(i18n.getText("it.test.console.exception.soy.did.not.manage.to.compile.soy.template", output.size()));
  66. }
  67. return output.get(0);
  68. }
  69. }
  70. private class SpringBridgeModule extends AbstractModule
  71. {
  72. @Override
  73. public void configure()
  74. {
  75. Binder binder = binder();
  76. binder.bind(ApplicationProperties.class).toInstance(applicationProperties);
  77. binder.bind(I18nResolver.class).toInstance(i18n);
  78. }
  79. }
  80. private static class OurFunctionsModule extends AbstractModule
  81. {
  82. @Override
  83. public void configure()
  84. {
  85. Multibinder<SoyFunction> binder = Multibinder.newSetBinder(binder(), SoyFunction.class);
  86. binder.addBinding().to(ContextFunction.class);
  87. binder.addBinding().to(GetTextFunction.class);
  88. }
  89. }
  90. }