1 package com.github.searls.jasmine.runner; 2 3 import com.github.searls.jasmine.format.FormatsScriptTags; 4 import org.codehaus.plexus.util.StringUtils; 5 import org.stringtemplate.v4.ST; 6 7 import java.io.IOException; 8 import java.util.Collection; 9 import java.util.List; 10 11 public abstract class AbstractSpecRunnerHtmlGenerator { 12 private static final String SOURCE_ENCODING = "sourceEncoding"; 13 private static final String CSS_DEPENDENCIES_TEMPLATE_ATTR_NAME = "cssDependencies"; 14 protected static final String JAVASCRIPT_DEPENDENCIES_TEMPLATE_ATTR_NAME = "javascriptDependencies"; 15 protected static final String SOURCES_TEMPLATE_ATTR_NAME = "sources"; 16 protected static final String REPORTER_ATTR_NAME = "reporter"; 17 private final HtmlGeneratorConfiguration configuration; 18 private final FormatsScriptTags formatsScriptTags = new FormatsScriptTags(); 19 20 protected AbstractSpecRunnerHtmlGenerator(HtmlGeneratorConfiguration configuration) { 21 this.configuration = configuration; 22 } 23 24 protected void setEncoding(HtmlGeneratorConfiguration htmlGeneratorConfiguration, ST template) { 25 template.add(SOURCE_ENCODING, StringUtils.isNotBlank(htmlGeneratorConfiguration.getSourceEncoding()) ? htmlGeneratorConfiguration.getSourceEncoding() : SpecRunnerHtmlGenerator.DEFAULT_SOURCE_ENCODING); 26 } 27 28 protected ST resolveHtmlTemplate() throws IOException { 29 String htmlTemplate = configuration.getRunnerTemplate(); 30 return new ST(htmlTemplate, '$', '$'); 31 } 32 33 protected void applyCssToTemplate(List<String> styles, ST template) throws IOException { 34 StringBuilder css = new StringBuilder(); 35 for (String cssFile : styles) { 36 css.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"").append(cssFile).append("\"/>"); 37 } 38 template.add(CSS_DEPENDENCIES_TEMPLATE_ATTR_NAME, css.toString()); 39 } 40 41 public HtmlGeneratorConfiguration getConfiguration() { 42 return configuration; 43 } 44 45 protected void applyScriptTagsToTemplate(String sourcesTemplateAttrName, Collection<String> scripts, ST template) throws IOException { 46 template.add(sourcesTemplateAttrName, formatsScriptTags.format(scripts)); 47 } 48 }