Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code cleanup #284

Merged
merged 5 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 2 additions & 54 deletions src/main/java/liqp/Template.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package liqp;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -22,7 +19,6 @@
import org.antlr.v4.runtime.tree.ParseTree;

import liqp.exceptions.LiquidException;
import liqp.filters.Filters;
import liqp.nodes.LNode;
import liqp.parser.Inspectable;
import liqp.parser.LiquidSupport;
Expand Down Expand Up @@ -58,38 +54,6 @@ public class Template {

private final TemplateParser templateParser;

/**
* Creates a new Template instance from a given input.
*
* @param input the file holding the Liquid source.
*/
private Template(TemplateParser templateParser, String input) {
this(templateParser, CharStreams.fromString(input, input));
}

/**
* Creates a new Template instance from a given file.
*
* @param file
* the file holding the Liquid source.
*/
private Template(TemplateParser parser, File file)
throws IOException {
this(parser, fromFile(file));
}


/**
* Creates a new Template instance from a given stream.
*
* @param stream
* the file holding the Liquid source.
*/
private Template(TemplateParser parser, InputStream stream)
throws IOException {
this(parser, fromStream(stream));
}

Template(TemplateParser templateParser, CharStream stream) {
this.templateParser = templateParser;

Expand All @@ -108,23 +72,6 @@ private Template(TemplateParser parser, InputStream stream)
}
}


private static CharStream fromStream(InputStream in) {
try {
return CharStreams.fromStream(in);
} catch (IOException e) {
throw new RuntimeException("could not parse input: " + in, e);
}
}

private static CharStream fromFile(File path) {
try {
return CharStreams.fromFileName(path.getAbsolutePath());
} catch (IOException e) {
throw new RuntimeException("could not parse input: " + path, e);
}
}

private ParseTree parse(LiquidLexer lexer) {

lexer.removeErrorListeners();
Expand Down Expand Up @@ -249,7 +196,7 @@ public String renderObject(Object obj) {
}

private Object renderObjectToObject(Object obj) {
LiquidSupport evaluated = templateParser.evaluate(getTemplateParser().getMapper(), obj);
LiquidSupport evaluated = TemplateParser.evaluate(getTemplateParser().getMapper(), obj);
Map<String, Object> map = evaluated.toLiquid();
return renderToObject(map);
}
Expand Down Expand Up @@ -399,6 +346,7 @@ private TemplateContext newRootContext(Map<String, Object> variables) {
return context;
}

@SuppressWarnings("deprecation")
public Object renderToObjectUnguarded(Map<String, Object> variables, TemplateContext parent,
boolean doClearThreadLocal) {
if (doClearThreadLocal) {
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/liqp/TemplateContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;

import liqp.RenderTransformer.ObjectAppender;
import liqp.exceptions.ExceededMaxIterationsException;
import liqp.filters.Filters;
import liqp.parser.LiquidSupport;

public class TemplateContext {

Expand Down
20 changes: 18 additions & 2 deletions src/main/java/liqp/TemplateParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public enum EvaluateMode {
* This function don't need access to local context variables,
* as it operates with parameters.
*/
@SuppressWarnings("hiding")
public Map<String, Object> evaluate(final ObjectMapper mapper, Map<String, Object> variables) {
if (evaluateMode == TemplateParser.EvaluateMode.EAGER) {
return LiquidSupport.LiquidSupportFromInspectable.objectToMap(mapper, variables);
Expand Down Expand Up @@ -179,11 +180,13 @@ public Builder(TemplateParser parser) {
this.errorMode = parser.errorMode;
}

@SuppressWarnings("hiding")
public Builder withFlavor(Flavor flavor) {
this.flavor = flavor;
return this;
}

@SuppressWarnings("hiding")
public Builder withStripSpaceAroundTags(boolean stripSpacesAroundTags, boolean stripSingleLine) {

if (stripSingleLine && !stripSpacesAroundTags) {
Expand All @@ -196,15 +199,18 @@ public Builder withStripSpaceAroundTags(boolean stripSpacesAroundTags, boolean s
return this;
}

@SuppressWarnings("hiding")
public Builder withStripSpaceAroundTags(boolean stripSpacesAroundTags) {
return this.withStripSpaceAroundTags(stripSpacesAroundTags, false);
}

@SuppressWarnings("hiding")
public Builder withStripSingleLine(boolean stripSingleLine) {
this.stripSingleLine = stripSingleLine;
return this;
}

@SuppressWarnings("hiding")
public Builder withObjectMapper(ObjectMapper mapper) {
this.mapper = mapper;
return this;
Expand All @@ -220,26 +226,31 @@ public Builder withFilter(Filter filter) {
return this;
}

@SuppressWarnings("hiding")
public Builder withEvaluateInOutputTag(boolean evaluateInOutputTag) {
this.evaluateInOutputTag = evaluateInOutputTag;
return this;
}

@SuppressWarnings("hiding")
public Builder withLiquidStyleInclude(boolean liquidStyleInclude) {
this.liquidStyleInclude = liquidStyleInclude;
return this;
}

@SuppressWarnings("hiding")
public Builder withStrictVariables(boolean strictVariables) {
this.strictVariables = strictVariables;
return this;
}

@SuppressWarnings("hiding")
public Builder withShowExceptionsFromInclude(boolean showExceptionsFromInclude) {
this.showExceptionsFromInclude = showExceptionsFromInclude;
return this;
}

@SuppressWarnings("hiding")
public Builder withEvaluateMode(TemplateParser.EvaluateMode evaluateMode) {
this.evaluateMode = evaluateMode;
return this;
Expand All @@ -251,12 +262,14 @@ public Builder withEvaluateMode(TemplateParser.EvaluateMode evaluateMode) {
* @param renderTransformer The transformer, or {@code null} to use the default.
* @return This builder.
*/
@SuppressWarnings("hiding")
public Builder withRenderTransformer(RenderTransformer renderTransformer) {
this.renderTransformer = renderTransformer;
return this;
}

public Builder withLocale(Locale locale){
@SuppressWarnings("hiding")
public Builder withLocale(Locale locale) {
Objects.requireNonNull(locale);
this.locale = locale;
return this;
Expand All @@ -269,6 +282,7 @@ public Builder withLocale(Locale locale){
* @param defaultTimeZone - value or <code>null<code/>
* @return this builder
*/
@SuppressWarnings("hiding")
public Builder withDefaultTimeZone(ZoneId defaultTimeZone) {
this.defaultTimeZone = defaultTimeZone;
return this;
Expand All @@ -289,7 +303,8 @@ public Builder withEnvironmentMapConfigurator(Consumer<Map<String, Object>> conf
return this;
}

public Builder withSnippetsFolderName(String snippetsFolderName) {
@SuppressWarnings("hiding")
public Builder withSnippetsFolderName(String snippetsFolderName) {
this.snippetsFolderName = snippetsFolderName;
return this;
}
Expand Down Expand Up @@ -320,6 +335,7 @@ public Builder withErrorMode(ErrorMode errorMode) {
return this;
}

@SuppressWarnings("hiding")
public TemplateParser build() {
Flavor fl = this.flavor;
if (fl == null) {
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/liqp/nodes/ComparingExpressionNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import liqp.LValue;
import liqp.TemplateContext;

import java.util.Objects;

public abstract class ComparingExpressionNode extends LValue implements LNode {
protected final LNode lhs;
protected final LNode rhs;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/liqp/nodes/EqNode.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package liqp.nodes;

import liqp.LValue;
import liqp.TemplateContext;

import java.util.Objects;

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/liqp/nodes/GtEqNode.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package liqp.nodes;

import java.util.Objects;

public class GtEqNode extends ComparingExpressionNode {

public GtEqNode(LNode lhs, LNode rhs) {
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/liqp/nodes/GtNode.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package liqp.nodes;

import java.util.Objects;

public class GtNode extends ComparingExpressionNode {

public GtNode(LNode lhs, LNode rhs) {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/liqp/nodes/NEqNode.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package liqp.nodes;

import liqp.LValue;
import liqp.TemplateContext;

import java.util.Objects;

Expand Down
9 changes: 0 additions & 9 deletions src/main/java/liqp/parser/v4/NodeVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,7 @@
import liquid.parser.v4.LiquidParser.Term_lookupContext;
import liquid.parser.v4.LiquidParser.Unless_tagContext;
import liquid.parser.v4.LiquidParser.When_tagContext;
import liqp.nodes.*;
import liquid.parser.v4.LiquidParserBaseVisitor;
import org.antlr.v4.runtime.misc.Interval;
import org.antlr.v4.runtime.tree.TerminalNode;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import static liquid.parser.v4.LiquidParser.*;

public class NodeVisitor extends LiquidParserBaseVisitor<LNode> {

Expand Down
26 changes: 15 additions & 11 deletions src/test/java/liqp/tags/IncludeTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package liqp.tags;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.isA;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand All @@ -15,7 +14,6 @@
import java.util.Map;

import org.antlr.v4.runtime.RecognitionException;
import org.junit.Rule;
import org.junit.Test;

import liqp.Template;
Expand All @@ -24,14 +22,8 @@
import liqp.exceptions.VariableNotExistException;
import liqp.filters.Filter;
import liqp.parser.Flavor;
import org.junit.rules.ExpectedException;

public class IncludeTest {

@Rule
public ExpectedException thrown = ExpectedException.none();


@Test
public void renderTest() throws RecognitionException {

Expand Down Expand Up @@ -194,8 +186,6 @@ public void renderTestWithIncludeSubdirectorySpecifiedInLiquidFlavorWithStrictVa
@Test
public void renderTestWithIncludeSubdirectorySpecifiedInLiquidFlavorWithStrictVariablesException() throws Exception {

thrown.expectCause(isA(VariableNotExistException.class));

File index = new File("src/test/jekyll/index_with_variables.html");
TemplateParser parser = new TemplateParser.Builder() //
.withFlavor(Flavor.LIQUID) //
Expand All @@ -204,7 +194,17 @@ public void renderTestWithIncludeSubdirectorySpecifiedInLiquidFlavorWithStrictVa
.withErrorMode(TemplateParser.ErrorMode.STRICT) //
.build();
Template template = parser.parse(index);
template.render();

try {
template.render();
fail("Should have thrown a RuntimeException with a VariableNotExistException cause");
} catch (RuntimeException e) {
if (e.getCause() instanceof VariableNotExistException) {
// expected
} else {
throw e;
}
}
}

// https://github.com/bkiers/Liqp/issues/95
Expand Down Expand Up @@ -243,6 +243,7 @@ public void expressionInIncludeTagDefaultFlavorThrowsException() {
TemplateParser.DEFAULT.parse(source).render();
}

@SuppressWarnings("deprecation")
@Test
public void includeDirectoryKeyInInputShouldChangeIncludeDirectory() throws IOException {
// given
Expand All @@ -259,6 +260,7 @@ public void includeDirectoryKeyInInputShouldChangeIncludeDirectory() throws IOEx
assertTrue(result.contains("ALTERNATIVE"));
}

@SuppressWarnings("deprecation")
@Test
public void includeDirectoryKeyStringInInputShouldChangeIncludeDirectory() throws IOException {
// given
Expand All @@ -275,6 +277,8 @@ public void includeDirectoryKeyStringInInputShouldChangeIncludeDirectory() throw
// then
assertTrue(result.contains("ALTERNATIVE"));
}

@SuppressWarnings("deprecation")
@Test
public void includeDirectoryKeyPathInInputShouldChangeIncludeDirectory() throws IOException {
// given
Expand Down
Loading