use of org.thymeleaf.IEngineConfiguration in project thymeleaf by thymeleaf.
the class AbstractTextTemplateParser method parse.
private void parse(final IEngineConfiguration configuration, final String ownerTemplate, final String template, final Set<String> templateSelectors, final ITemplateResource resource, final int lineOffset, final int colOffset, final TemplateMode templateMode, final ITemplateHandler templateHandler) {
// For a String template, we will use the ownerTemplate as templateName for its parsed events
final String templateName = (resource != null ? template : ownerTemplate);
try {
// The final step of the handler chain will be the adapter that will convert the text parser's handler chain to thymeleaf's.
ITextHandler handler = new TemplateHandlerAdapterTextHandler(templateName, templateHandler, configuration.getElementDefinitions(), configuration.getAttributeDefinitions(), templateMode, lineOffset, colOffset);
// (which might see text blocks coming as several blocks instead of just one).
if (configuration instanceof EngineConfiguration && ((EngineConfiguration) configuration).isModelReshapeable(templateMode)) {
handler = new InlinedOutputExpressionTextHandler(configuration, templateMode, configuration.getStandardDialectPrefix(), handler);
}
// Obtain the resource reader
Reader templateReader = (resource != null ? resource.reader() : new StringReader(template));
// Add the required reader wrappers in order to process parser-level and prototype-only comment blocks
if (templateMode == TemplateMode.TEXT) {
// There are no /*[+...+]*/ blocks in TEXT mode (it makes no sense)
templateReader = new ParserLevelCommentTextReader(templateReader);
} else {
// TemplateMode.JAVASCRIPT || TemplateMode.CSS
templateReader = new ParserLevelCommentTextReader(new PrototypeOnlyCommentTextReader(templateReader));
}
this.parser.parse(templateReader, handler);
} catch (final IOException e) {
final String message = "An error happened during template parsing";
throw new TemplateInputException(message, (resource != null ? resource.getDescription() : template), e);
} catch (final TextParseException e) {
final String message = "An error happened during template parsing";
if (e.getLine() != null && e.getCol() != null) {
throw new TemplateInputException(message, (resource != null ? resource.getDescription() : template), e.getLine().intValue(), e.getCol().intValue(), e);
}
throw new TemplateInputException(message, (resource != null ? resource.getDescription() : template), e);
}
}
use of org.thymeleaf.IEngineConfiguration in project thymeleaf-tests by thymeleaf.
the class HtmlBlockSelectorMarkupHandlerTest method test.
public void test() throws Exception {
final HTMLTemplateParser parser = new HTMLTemplateParser(2, 4096);
final IEngineConfiguration templateEngineContext = TestTemplateEngineConfigurationBuilder.build();
final URL resourcesFolderURL = Thread.currentThread().getContextClassLoader().getResource(RESOURCES_FOLDER);
assertNotNull(resourcesFolderURL);
final File resourcesFolder = new File(resourcesFolderURL.toURI());
assertTrue(resourcesFolder.isDirectory());
final List<File> resourceFolderFiles = new ArrayList<File>(Arrays.asList(resourcesFolder.listFiles()));
Collections.sort(resourceFolderFiles, new Comparator<File>() {
public int compare(final File o1, final File o2) {
return o1.getName().compareTo(o2.getName());
}
});
final List<File> testFiles = new ArrayList<File>();
final List<File> resultFiles = new ArrayList<File>();
for (final File resourceFolderFile : resourceFolderFiles) {
if (resourceFolderFile.getName().startsWith("test") && resourceFolderFile.getName().endsWith(".html")) {
testFiles.add(resourceFolderFile);
} else if (resourceFolderFile.getName().startsWith("result") && resourceFolderFile.getName().endsWith(".html")) {
resultFiles.add(resourceFolderFile);
}
}
for (int i = 0; i < testFiles.size(); i++) {
final File testFile = testFiles.get(i);
final File resultFile = resultFiles.get(i);
final List<String> testFileLines = IOUtils.readLines(new InputStreamReader(new FileInputStream(testFile), "UTF-8"));
final String testFileContents = StringUtils.join(testFileLines, '\n');
final List<String> resultFileLines = IOUtils.readLines(new InputStreamReader(new FileInputStream(resultFile), "UTF-8"));
final String blockSelector = resultFileLines.get(0);
resultFileLines.remove(0);
final String resultFileContents = StringUtils.join(resultFileLines, '\n');
final String[] blockSelectors = StringUtils.split(blockSelector, ",");
check(parser, templateEngineContext, testFile.getName(), testFileContents, resultFileContents, blockSelectors);
}
}
use of org.thymeleaf.IEngineConfiguration in project thymeleaf-tests by thymeleaf.
the class TestTemplateEngineConfigurationBuilder method build.
public static IEngineConfiguration build() {
final StandardDialect standardDialect = new StandardDialect();
final DialectConfiguration standardDialectConfiguration = new DialectConfiguration(standardDialect);
return new EngineConfiguration(Collections.singleton(standardDialectConfiguration), TextRepositories.createLimitedSizeCacheRepository());
}
use of org.thymeleaf.IEngineConfiguration in project thymeleaf-tests by thymeleaf.
the class ElementProcessorIteratorTest method computeXmlTag.
private static TagObtentionTemplateHandler computeXmlTag(final String input, final Set<IDialect> dialects) {
final String templateName = "test";
final TagObtentionTemplateHandler handler = new TagObtentionTemplateHandler();
final IEngineConfiguration templateEngineContext = TestTemplateEngineConfigurationBuilder.build(dialects);
XML_PARSER.parse(templateEngineContext, TemplateMode.XML, new StringResource(templateName, input), handler);
return handler;
}
use of org.thymeleaf.IEngineConfiguration in project thymeleaf-tests by thymeleaf.
the class ElementProcessorIteratorTest method computeHtmlTag.
private static TagObtentionTemplateHandler computeHtmlTag(final String input, final Set<IDialect> dialects) {
final String templateName = "test";
final TagObtentionTemplateHandler handler = new TagObtentionTemplateHandler();
final IEngineConfiguration templateEngineContext = TestTemplateEngineConfigurationBuilder.build(dialects);
HTML_PARSER.parse(templateEngineContext, TemplateMode.HTML, new StringResource(templateName, input), handler);
return handler;
}
Aggregations