Search in sources :

Example 1 with ProcessingContext

use of org.thymeleaf.context.ProcessingContext in project thymeleaf-tests by thymeleaf.

the class HtmlBulkTester method main.

public static void main(final String[] args) throws Exception {
    if (args.length != 1) {
        System.err.println("Syntax: java " + HtmlBulkTester.class.getName() + " [test_folder]");
        System.exit(1);
    }
    final String testFolderName = args[0];
    final File testFolder = new File(testFolderName);
    if (!testFolder.exists() || !testFolder.isDirectory()) {
        System.err.println("Folder " + testFolderName + " does not exist or is not a folder");
        System.exit(1);
    }
    System.out.println("Using temporary folder for output: " + System.getProperty("java.io.tmpdir"));
    final File[] filesInTestFolder = testFolder.listFiles();
    for (final File fileInTestFolder : filesInTestFolder) {
        if (!fileInTestFolder.getName().endsWith(".html")) {
            continue;
        }
        final String fileInTestFolderName = fileInTestFolder.getName();
        final FileInputStream fileInTestFolderStream = new FileInputStream(fileInTestFolder);
        final InputStreamReader fileInTestFolderReader = new InputStreamReader(fileInTestFolderStream, "UTF-8");
        final File testOutput = File.createTempFile("thymeleaf-testing-" + fileInTestFolderName + "-", ".html");
        testOutput.deleteOnExit();
        final FileOutputStream testOutputStream = new FileOutputStream(testOutput);
        final OutputStreamWriter testOutputWriter = new OutputStreamWriter(testOutputStream, "UTF-8");
        final ITemplateHandler handler = new OutputTemplateHandler(testOutputWriter);
        handler.setProcessingContext(new ProcessingContext(TEMPLATE_ENGINE_CONFIGURATION, fileInTestFolderName, TemplateMode.HTML, Locale.US, null));
        System.out.print(fileInTestFolderName);
        System.out.print("[PARSING]");
        PARSER.parse(TEMPLATE_ENGINE_CONFIGURATION, TemplateMode.HTML, new ReaderResource(fileInTestFolderName, fileInTestFolderReader), handler);
        // Input stream will be closed by parser
        testOutputWriter.close();
        testOutputStream.close();
        System.out.print("[PARSED]");
        final FileInputStream testOutputCheckStream = new FileInputStream(testOutput);
        final List<String> outputLines = IOUtils.readLines(testOutputCheckStream, "UTF-8");
        final FileInputStream testInputCheckStream = new FileInputStream(fileInTestFolder);
        final List<String> inputLines = IOUtils.readLines(testInputCheckStream, "UTF-8");
        System.out.print("[CHECKING]");
        if (outputLines.equals(inputLines)) {
            System.out.print("[OK]");
        } else {
            System.out.print("[KO]");
        }
        System.out.println();
    }
}
Also used : ProcessingContext(org.thymeleaf.context.ProcessingContext) InputStreamReader(java.io.InputStreamReader) ReaderResource(org.thymeleaf.resource.ReaderResource) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File)

Example 2 with ProcessingContext

use of org.thymeleaf.context.ProcessingContext in project thymeleaf-tests by thymeleaf.

the class ExpressionBenchmark method main.

public static void main(String[] args) throws Exception {
    final Map<String, String> expressionsMap = ExpressionBenchmarkDefinitions.createExpressionsMap();
    final Configuration configuration = new Configuration();
    final IProcessingContext processingContext = new ProcessingContext(new Context());
    final IStandardExpressionParser parser = new StandardExpressionParser();
    for (final Map.Entry<String, String> expressionEntry : expressionsMap.entrySet()) {
        final String expression = expressionEntry.getKey();
        final String expectedParsingResult = expressionEntry.getValue();
        final IStandardExpression parsedExpression = parser.parseExpression(configuration, processingContext, expression);
        Assert.assertNotNull(parsedExpression);
        final String exp = parsedExpression.getStringRepresentation();
        Assert.assertEquals(expectedParsingResult, exp);
    }
    final StopWatch sw = new StopWatch();
    sw.start();
    for (int x = 0; x < 1000; x++) for (final String expression : expressionsMap.keySet()) parser.parseExpression(configuration, processingContext, expression);
    sw.stop();
    System.out.println("First pass: " + sw.toString());
    sw.reset();
    sw.start();
    for (int x = 0; x < 1000; x++) for (final String expression : expressionsMap.keySet()) parser.parseExpression(configuration, processingContext, expression);
    sw.stop();
    System.out.println("Second pass: " + sw.toString());
}
Also used : IProcessingContext(org.thymeleaf.context.IProcessingContext) ProcessingContext(org.thymeleaf.context.ProcessingContext) Context(org.thymeleaf.context.Context) IProcessingContext(org.thymeleaf.context.IProcessingContext) ProcessingContext(org.thymeleaf.context.ProcessingContext) Configuration(org.thymeleaf.Configuration) IProcessingContext(org.thymeleaf.context.IProcessingContext) StopWatch(org.apache.commons.lang3.time.StopWatch) Map(java.util.Map)

Example 3 with ProcessingContext

use of org.thymeleaf.context.ProcessingContext in project thymeleaf-tests by thymeleaf.

the class BareHtmlEngineTest method check.

private static void check(final String input, final String output, final String[] blockSelectors) throws Exception {
    final String templateName = "test";
    final StringWriter writer = new StringWriter();
    final ITemplateHandler handler = new OutputTemplateHandler(writer);
    handler.setProcessingContext(new ProcessingContext(TEMPLATE_ENGINE_CONFIGURATION, templateName, TemplateMode.HTML, Locale.US, null));
    if (blockSelectors != null) {
        PARSER.parse(TEMPLATE_ENGINE_CONFIGURATION, TemplateMode.HTML, new StringResource(templateName, input), blockSelectors, handler);
    } else {
        PARSER.parse(TEMPLATE_ENGINE_CONFIGURATION, TemplateMode.HTML, new StringResource(templateName, input), handler);
    }
    Assert.assertEquals("Test failed for file: " + templateName, output, writer.toString());
}
Also used : ProcessingContext(org.thymeleaf.context.ProcessingContext) StringResource(org.thymeleaf.resource.StringResource) StringWriter(java.io.StringWriter)

Aggregations

ProcessingContext (org.thymeleaf.context.ProcessingContext)3 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 StringWriter (java.io.StringWriter)1 Map (java.util.Map)1 StopWatch (org.apache.commons.lang3.time.StopWatch)1 Configuration (org.thymeleaf.Configuration)1 Context (org.thymeleaf.context.Context)1 IProcessingContext (org.thymeleaf.context.IProcessingContext)1 ReaderResource (org.thymeleaf.resource.ReaderResource)1 StringResource (org.thymeleaf.resource.StringResource)1