Search in sources :

Example 41 with IEngineConfiguration

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);
    }
}
Also used : ParserLevelCommentTextReader(org.thymeleaf.templateparser.reader.ParserLevelCommentTextReader) PrototypeOnlyCommentTextReader(org.thymeleaf.templateparser.reader.PrototypeOnlyCommentTextReader) IEngineConfiguration(org.thymeleaf.IEngineConfiguration) EngineConfiguration(org.thymeleaf.EngineConfiguration) StringReader(java.io.StringReader) ParserLevelCommentTextReader(org.thymeleaf.templateparser.reader.ParserLevelCommentTextReader) Reader(java.io.Reader) StringReader(java.io.StringReader) PrototypeOnlyCommentTextReader(org.thymeleaf.templateparser.reader.PrototypeOnlyCommentTextReader) IOException(java.io.IOException) TemplateHandlerAdapterTextHandler(org.thymeleaf.engine.TemplateHandlerAdapterTextHandler) TemplateInputException(org.thymeleaf.exceptions.TemplateInputException)

Example 42 with IEngineConfiguration

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);
    }
}
Also used : IEngineConfiguration(org.thymeleaf.IEngineConfiguration) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) URL(java.net.URL) FileInputStream(java.io.FileInputStream) File(java.io.File)

Example 43 with IEngineConfiguration

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());
}
Also used : IEngineConfiguration(org.thymeleaf.IEngineConfiguration) EngineConfiguration(org.thymeleaf.EngineConfiguration) DialectConfiguration(org.thymeleaf.DialectConfiguration) StandardDialect(org.thymeleaf.standard.StandardDialect)

Example 44 with IEngineConfiguration

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;
}
Also used : StringResource(org.thymeleaf.resource.StringResource) IEngineConfiguration(org.thymeleaf.IEngineConfiguration)

Example 45 with IEngineConfiguration

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;
}
Also used : StringResource(org.thymeleaf.resource.StringResource) IEngineConfiguration(org.thymeleaf.IEngineConfiguration)

Aggregations

IEngineConfiguration (org.thymeleaf.IEngineConfiguration)45 Test (org.junit.Test)26 LinkedHashMap (java.util.LinkedHashMap)15 ServletContext (javax.servlet.ServletContext)14 HttpServletRequest (javax.servlet.http.HttpServletRequest)14 HttpServletResponse (javax.servlet.http.HttpServletResponse)14 WebEngineContext (org.thymeleaf.context.WebEngineContext)14 EngineContext (org.thymeleaf.context.EngineContext)10 TemplateProcessingException (org.thymeleaf.exceptions.TemplateProcessingException)8 TemplateInputException (org.thymeleaf.exceptions.TemplateInputException)6 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 EngineConfiguration (org.thymeleaf.EngineConfiguration)3 TemplateModel (org.thymeleaf.engine.TemplateModel)3 IPostProcessor (org.thymeleaf.postprocessor.IPostProcessor)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2