Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
chbloemer committed Jan 2, 2025
1 parent 4986c6b commit 23541cb
Show file tree
Hide file tree
Showing 26 changed files with 41 additions and 115 deletions.
1 change: 0 additions & 1 deletion src/main/java/de/neuland/pug4j/compiler/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import de.neuland.pug4j.exceptions.ExpressionException;
import de.neuland.pug4j.model.PugModel;
import de.neuland.pug4j.parser.node.ExpressionString;

public class Utils {
public static Pattern interpolationPattern = Pattern.compile("(\\\\)?([#!])\\{");
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/de/neuland/pug4j/exceptions/PugException.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.neuland.pug4j.exceptions;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.net.URL;
import java.util.ArrayList;
Expand All @@ -12,7 +11,6 @@
import de.neuland.pug4j.Pug4J;
import de.neuland.pug4j.template.TemplateLoader;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

public abstract class PugException extends RuntimeException {

Expand Down Expand Up @@ -61,7 +59,7 @@ public int getLineNumber() {
public int getColNumber() {
return colNumber;
}
@NotNull

private String createErrorMessage(String message, int line, int column, String filename) {
String fullMessage;
String location = line + (column !=0 ? ":" + column : "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public Object evaluateExpression(String expression, PugModel model) throws Expre
try {
jsContextBindings.removeMember(memberKey);
}catch(UnsupportedOperationException e){
// e.printStackTrace();
jsContextBindings.putMember(memberKey, null);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public PugJexlArithmetic(boolean astrict) {
* @param left the left operand
* @param right the right operator
* @param operator the operator
* @return
* @return int
*/
@Override
protected int compare(final Object left, final Object right, final String operator) {
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/de/neuland/pug4j/jexl3/internal/MapBuilder.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package de.neuland.pug4j.jexl3.internal;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class MapBuilder implements org.apache.commons.jexl3.JexlArithmetic.MapBuilder {
protected final Map<Object, Object> map;

public MapBuilder(int size) {
this.map = new LinkedHashMap(size);
this.map = new LinkedHashMap<>(size);
}
public MapBuilder(int size, boolean extended) {
this.map = (Map<Object, Object>)(extended ? new LinkedHashMap(size) : new LinkedHashMap(size));
this.map = new LinkedHashMap<>(size);
}

public void put(Object key, Object value) {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/de/neuland/pug4j/lexer/Lexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1496,9 +1496,8 @@ private Token tok(Token token){
newToken.setFileName(this.filename);
return newToken;
} catch (CloneNotSupportedException e) {
e.printStackTrace();
throw new PugLexerException("Clone Not Supported",this.filename, this.lineno,this.colno,templateLoader);
}
return null;
}

private boolean pipelessText() {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/de/neuland/pug4j/model/PugModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class PugModel implements Map<String, Object> {
public static final String LOCAL_VARS = "pug4j__localVars";
public static final String PUG4J_MODEL_PREFIX = "pug4j__";
private Deque<Map<String, Object>> scopes = new LinkedList<Map<String, Object>>();
private Map<String, MixinNode> mixins = new HashMap<String, MixinNode>();
private Map<String, Filter> filter = new HashMap<String, Filter>();
private final Map<String, MixinNode> mixins = new HashMap<String, MixinNode>();
private final Map<String, Filter> filter = new HashMap<String, Filter>();

public PugModel(Map<String, Object> defaults) {
pushScope();
Expand Down Expand Up @@ -138,7 +138,7 @@ public Set<String> keySet() {
@Override
// adds the object to the correct scope
public Object put(String key, Object value) {
Set<String> localVars= getLocalVars();
Set<String> localVars = getLocalVars();
if(localVars.contains(key)) {
return putLocal(key, value);
}else{
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/de/neuland/pug4j/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -880,8 +880,6 @@ private Node parseConditional() {
conditional.setColumn(conditionalToken.getStartColumn());
conditional.setFileName(filename);

List<IfConditionNode> conditions = conditional.getConditions();

IfConditionNode main = new IfConditionNode(conditionalToken.getValue(), conditionalToken.getStartLineNumber());
main.setInverse(conditionalToken.isInverseCondition());
main.setFileName(filename);
Expand All @@ -890,7 +888,7 @@ private Node parseConditional() {
}else{
main.setBlock(emptyBlock());
}
conditions.add(main);
conditional.addCondition(main);

while (true) {
if(peek() instanceof Newline){
Expand All @@ -904,7 +902,7 @@ private Node parseConditional() {
}else{
elseIf.setBlock(emptyBlock());
}
conditions.add(elseIf);
conditional.addCondition(elseIf);
}else if(peek() instanceof Else){
Else token = (Else) expect(Else.class);
IfConditionNode elseNode = new IfConditionNode(null, token.getStartLineNumber());
Expand All @@ -915,7 +913,7 @@ private Node parseConditional() {
}else{
elseNode.setBlock(emptyBlock());
}
conditions.add(elseNode);
conditional.addCondition(elseNode);

break;
}else{
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/neuland/pug4j/parser/node/AttrsNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected String visitAttributes(PugModel model, PugTemplate template) {
try {
o = template.getExpressionHandler().evaluateExpression(attributeBlock, model);
} catch (ExpressionException e) {
e.printStackTrace();
throw new PugCompilerException(this, template.getTemplateLoader(), e);
}
if(o instanceof Map) {
Map<String, String> map = (Map<String, String>) o;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/de/neuland/pug4j/parser/node/BlockNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import de.neuland.pug4j.compiler.IndentWriter;
import de.neuland.pug4j.exceptions.ExpressionException;
import de.neuland.pug4j.exceptions.PugCompilerException;
import de.neuland.pug4j.expression.ExpressionHandler;
import de.neuland.pug4j.model.PugModel;
import de.neuland.pug4j.parser.Parser;
import de.neuland.pug4j.template.PugTemplate;
Expand Down
28 changes: 9 additions & 19 deletions src/main/java/de/neuland/pug4j/parser/node/CallNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ public class CallNode extends AttrsNode {
@Override
public void execute(IndentWriter writer, PugModel model, PugTemplate template) throws PugCompilerException {
boolean dynamic = getName().charAt(0)=='#';
if (dynamic)
this.dynamicMixins = true;
this.dynamicMixins = dynamic;
String newname = (dynamic ? getName().substring(2,getName().length()-1):'"'+getName()+'"');
try {
newname = (String) template.getExpressionHandler().evaluateExpression(newname,model);
} catch (ExpressionException e) {
e.printStackTrace();
throw new PugCompilerException(this, template.getTemplateLoader(), e);
}

MixinNode mixin;
Expand Down Expand Up @@ -102,7 +101,6 @@ private void writeVariables(PugModel model, MixinNode mixin, PugTemplate templat
}
}
if (key != null) {

model.putLocal(key, value);
}
}
Expand All @@ -128,31 +126,23 @@ private void writeVariables(PugModel model, MixinNode mixin, PugTemplate templat

private void writeAttributes(PugModel model, MixinNode mixin, PugTemplate template) {
LinkedList<Attr> newAttributes = new LinkedList<Attr>(attributes);
if (attributeBlocks.size()>0) {
//Todo: AttributesBlock needs to be evaluated

if (!attributeBlocks.isEmpty()) {
for (String attributeBlock : attributeBlocks) {
Object o = null;
try {
o = template.getExpressionHandler().evaluateExpression(attributeBlock, model);
} catch (ExpressionException e) {
e.printStackTrace();
throw new PugCompilerException(this, template.getTemplateLoader(), e);
}
if(o!=null) {
if(o instanceof Map) {
for (Map.Entry<String, String> entry : ((Map<String,String>) o).entrySet()) {
Attr attr = new Attr(entry.getKey(),entry.getValue(),false);
newAttributes.add(attr);
}
}else if(o instanceof String){
System.out.print(o);
}

if(o instanceof Map) {
((Map<String, String>) o).entrySet().stream()
.map(entry -> new Attr(entry.getKey(), entry.getValue(), false))
.forEachOrdered(newAttributes::add);
}
}
}

if (newAttributes.size()>0) {
if (!newAttributes.isEmpty()) {
Map<String,String> attrs = attrs(model, template, newAttributes);
model.putLocal("attributes", attrs);
}else{
Expand Down
24 changes: 0 additions & 24 deletions src/main/java/de/neuland/pug4j/parser/node/CaseConditionNode.java

This file was deleted.

4 changes: 0 additions & 4 deletions src/main/java/de/neuland/pug4j/parser/node/CaseNode.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package de.neuland.pug4j.parser.node;

import java.util.LinkedList;
import java.util.List;

import de.neuland.pug4j.compiler.IndentWriter;
import de.neuland.pug4j.exceptions.ExpressionException;
import de.neuland.pug4j.exceptions.PugCompilerException;
Expand All @@ -24,7 +21,6 @@ public void execute(IndentWriter writer, PugModel model, PugTemplate template) t
boolean skip = false;
for (Node when : block.getNodes()) {
if (skip || "default".equals(when.getValue()) || checkCondition(model, when,template.getExpressionHandler())) {
skip = false;
if(when.getBlock()!=null) {
when.execute(writer, model, template);
break;
Expand Down
16 changes: 0 additions & 16 deletions src/main/java/de/neuland/pug4j/parser/node/CodeNode.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.neuland.pug4j.parser.node;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

Expand Down Expand Up @@ -34,11 +35,11 @@ private boolean checkCondition(PugModel model, String condition, ExpressionHandl
}

public List<IfConditionNode> getConditions() {
return conditions;
return Collections.unmodifiableList(conditions);
}

public void setConditions(List<IfConditionNode> conditions) {
this.conditions = conditions;
public void addCondition(IfConditionNode condition) {
this.conditions.add(condition);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.neuland.pug4j.parser.node;

import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;

Expand Down
8 changes: 0 additions & 8 deletions src/main/java/de/neuland/pug4j/parser/node/FilterNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import java.util.*;

import com.vladsch.flexmark.ast.Link;
import de.neuland.pug4j.compiler.IndentWriter;
import de.neuland.pug4j.compiler.Utils;
import de.neuland.pug4j.exceptions.ExpressionException;
import de.neuland.pug4j.exceptions.PugCompilerException;
import de.neuland.pug4j.expression.ExpressionHandler;
Expand Down Expand Up @@ -60,12 +58,6 @@ public void execute(IndentWriter writer, PugModel model, PugTemplate template) t
result = filter.convert(result, convertToFilterAttributes(template, model, filterValue.getAttributes()), model);
}
}

// try {
// result = Utils.interpolate(result, model, false, expressionHandler);
// } catch (ExpressionException e) {
// throw new PugCompilerException(this, template.getTemplateLoader(), e);
// }
writer.append(result);
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/de/neuland/pug4j/parser/node/TagNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ private String bufferName(PugTemplate template, PugModel model) {
try {
return template.getExpressionHandler().evaluateStringExpression(name, model);
} catch (ExpressionException e) {
e.printStackTrace();
return null;
throw new PugCompilerException(this, template.getTemplateLoader(), e);
}
} else {
return name;
Expand Down
6 changes: 2 additions & 4 deletions src/test/java/de/neuland/pug4j/AttributeLexerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
import org.junit.Test;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;

public class AttributeLexerTest {
Expand All @@ -33,8 +32,7 @@ private List<Token> findAttributes(String attributeString) {
} catch (IOException e) {
e.printStackTrace();
}
LinkedList<Token> tokens = lexer.getTokens();
return tokens;
return lexer.getTokens();
}

private void assertExpressionStringWithValue(Object expression, String expectedValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public void testArrayList() throws ExpressionException {
List<Object> addedHostList = Arrays.asList(1,2,3,4);
pugModel.put("hostList",addedHostList);
graalJsExpressionHandler.evaluateExpression("var list = [1,2,3]", pugModel);
List list = (List) pugModel.get("list");
List hostList = (List) pugModel.get("hostList");
List<Object> list = (List<Object>) pugModel.get("list");
List<Object> hostList = (List) pugModel.get("hostList");
Integer expected = 1;
assertEquals(expected,list.get(0));
assertEquals(expected,hostList.get(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;

import java.io.FileNotFoundException;
import java.net.URISyntaxException;
Expand Down
Loading

0 comments on commit 23541cb

Please sign in to comment.