Skip to content

Commit ced0423

Browse files
committed
code cleanup
- remove deprecated API calls - some minor java 8 cleanups
1 parent b987739 commit ced0423

File tree

8 files changed

+86
-42
lines changed

8 files changed

+86
-42
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ language: java
33
jdk:
44
- oraclejdk8
55
script:
6-
- mvn clean package coveralls:report -P coverage
6+
- mvn clean install coveralls:report -P coverage
77
after_success:
88
- bash <(curl -s https://codecov.io/bash)

handlebars/pom.xml

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
45

56
<parent>
67
<groupId>com.github.jknack</groupId>
@@ -32,7 +33,8 @@
3233
<minimizeJar>true</minimizeJar>
3334
<artifactSet>
3435
<includes>
35-
<include>org.apache.commons:commons-lang3:</include>
36+
<include>org.apache.commons:commons-lang3</include>
37+
<include>org.apache.commons:commons-text</include>
3638
<include>org.antlr:antlr4-runtime</include>
3739
</includes>
3840
</artifactSet>
@@ -41,6 +43,10 @@
4143
<pattern>org.apache.commons.lang3</pattern>
4244
<shadedPattern>com.github.jknack.handlebars.internal.lang3</shadedPattern>
4345
</relocation>
46+
<relocation>
47+
<pattern>org.apache.commons.text</pattern>
48+
<shadedPattern>com.github.jknack.handlebars.internal.text</shadedPattern>
49+
</relocation>
4450
<relocation>
4551
<pattern>org.antlr.v4.runtime</pattern>
4652
<shadedPattern>com.github.jknack.handlebars.internal.antlr</shadedPattern>
@@ -147,6 +153,12 @@
147153
<optional>true</optional>
148154
</dependency>
149155

156+
<dependency>
157+
<groupId>org.apache.commons</groupId>
158+
<artifactId>commons-text</artifactId>
159+
<optional>true</optional>
160+
</dependency>
161+
150162
<!-- ANTLRv4 -->
151163
<dependency>
152164
<groupId>org.antlr</groupId>
@@ -233,6 +245,21 @@
233245
</properties>
234246

235247
<profiles>
248+
<profile>
249+
<id>Xlint</id>
250+
<build>
251+
<plugins>
252+
<plugin>
253+
<artifactId>maven-compiler-plugin</artifactId>
254+
<configuration>
255+
<compilerArgs>
256+
<arg>-Xlint</arg>
257+
</compilerArgs>
258+
</configuration>
259+
</plugin>
260+
</plugins>
261+
</build>
262+
</profile>
236263
<profile>
237264
<id>coverage</id>
238265
<build>
@@ -244,8 +271,8 @@
244271
<configuration>
245272
<includes>
246273
<include>**/*Test.java</include>
247-
<include>**/*Hbs.java</include>
248-
<include>**/*Issue*.java</include>
274+
<include>**/Hbs*.java</include>
275+
<include>**/Issue*.java</include>
249276
</includes>
250277
<argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
251278
<systemPropertyVariables>

handlebars/src/main/java/com/github/jknack/handlebars/EscapingStrategy.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
*/
1818
package com.github.jknack.handlebars;
1919

20-
import org.apache.commons.lang3.StringEscapeUtils;
21-
import org.apache.commons.lang3.text.translate.LookupTranslator;
20+
import org.apache.commons.text.StringEscapeUtils;
21+
import org.apache.commons.text.translate.LookupTranslator;
22+
23+
import java.util.HashMap;
24+
import java.util.Map;
2225

2326
/**
2427
* <p>
@@ -63,6 +66,15 @@ class Hbs implements EscapingStrategy {
6366
* @param escapeMap A escape map.
6467
*/
6568
public Hbs(final String[][] escapeMap) {
69+
this(escapeMap(escapeMap));
70+
}
71+
72+
/**
73+
* Creates a new {@link Hbs} escaping strategy.
74+
*
75+
* @param escapeMap A escape map.
76+
*/
77+
public Hbs(final Map<CharSequence, CharSequence> escapeMap) {
6678
translator = new LookupTranslator(escapeMap);
6779
}
6880

@@ -74,6 +86,19 @@ public CharSequence escape(final CharSequence value) {
7486
return value == null || value.length() == 0 ? EMPTY : translator.translate(value);
7587
}
7688

89+
/**
90+
* Convert a table to a hash (internal usage).
91+
*
92+
* @param table Table.
93+
* @return A hash.
94+
*/
95+
private static Map<CharSequence, CharSequence> escapeMap(final String[][] table) {
96+
Map<CharSequence, CharSequence> result = new HashMap<>();
97+
for (String[] row : table) {
98+
result.put(row[0], row[1]);
99+
}
100+
return result;
101+
}
77102
}
78103

79104
/**
@@ -110,7 +135,7 @@ public CharSequence escape(final CharSequence value) {
110135

111136
/** Escape variable for XML. */
112137
EscapingStrategy XML = value ->
113-
value == null ? null : StringEscapeUtils.escapeXml(value.toString());
138+
value == null ? null : StringEscapeUtils.escapeXml11(value.toString());
114139

115140
/** Escape variable for JavaScript. */
116141
EscapingStrategy JS = value ->

handlebars/src/main/java/com/github/jknack/handlebars/helper/StringHelpers.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@
3939

4040
import org.apache.commons.lang3.LocaleUtils;
4141
import org.apache.commons.lang3.StringUtils;
42-
import org.apache.commons.lang3.text.WordUtils;
4342

4443
import com.github.jknack.handlebars.Handlebars;
4544
import com.github.jknack.handlebars.Helper;
4645
import com.github.jknack.handlebars.Options;
46+
import org.apache.commons.text.WordUtils;
4747

4848
/**
4949
* Commons string function helpers.
@@ -459,8 +459,8 @@ protected CharSequence safeApply(final Object value, final Options options) {
459459
replace {
460460
@Override
461461
public CharSequence safeApply(final Object value, final Options options) {
462-
String target = (String) options.param(0, null);
463-
String replacement = (String) options.param(1, null);
462+
String target = options.param(0, null);
463+
String replacement = options.param(1, null);
464464
return value.toString().replace(target, replacement);
465465
}
466466
},

handlebars/src/main/java/com/github/jknack/handlebars/internal/FastStringWriter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
*/
1818
package com.github.jknack.handlebars.internal;
1919

20+
import org.apache.commons.text.TextStringBuilder;
21+
2022
import java.io.IOException;
2123
import java.io.Writer;
2224

23-
import org.apache.commons.lang3.text.StrBuilder;
24-
2525
/**
2626
* A string writer without locking.
2727
*
@@ -42,7 +42,7 @@ class FastStringWriter extends Writer {
4242
/**
4343
* The internal buffer.
4444
*/
45-
private StrBuilder buffer = new StrBuilder(BUFFER_SIZE);
45+
private TextStringBuilder buffer = new TextStringBuilder(BUFFER_SIZE);
4646

4747
@Override
4848
public Writer append(final char c) throws IOException {

handlebars/src/main/java/com/github/jknack/handlebars/internal/HbsParserFactory.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
import java.io.IOException;
2121

2222
import org.antlr.v4.runtime.ANTLRErrorListener;
23-
import org.antlr.v4.runtime.ANTLRInputStream;
23+
import org.antlr.v4.runtime.CharStream;
24+
import org.antlr.v4.runtime.CharStreams;
2425
import org.antlr.v4.runtime.CommonToken;
2526
import org.antlr.v4.runtime.CommonTokenStream;
2627
import org.antlr.v4.runtime.LexerNoViableAltException;
@@ -68,11 +69,12 @@ public Parser create(final Handlebars handlebars,
6869
@Override
6970
public Template parse(final TemplateSource source) throws IOException {
7071
logger.debug("About to parse: {}", source);
71-
final ANTLRErrorListener errorReporter = new HbsErrorReporter(source.filename());
72+
String sourceName = source.filename();
73+
final ANTLRErrorListener errorReporter = new HbsErrorReporter(sourceName);
7274

7375
// 1. Lexer
74-
final HbsLexer lexer = newLexer(newStream(source.filename(),
75-
source.content(handlebars.getCharset())), startDelimiter, endDelimiter);
76+
String content = source.content(handlebars.getCharset());
77+
final HbsLexer lexer = newLexer(CharStreams.fromString(content, sourceName), startDelimiter, endDelimiter);
7678
configure(lexer, errorReporter);
7779

7880
// 2. Parser
@@ -97,7 +99,7 @@ public Template parse(final TemplateSource source) throws IOException {
9799
/**
98100
* Build the AST.
99101
*/
100-
TemplateBuilder<Template> builder = new TemplateBuilder<Template>(handlebars, source) {
102+
TemplateBuilder builder = new TemplateBuilder(handlebars, source) {
101103
@Override
102104
protected void reportError(final CommonToken offendingToken, final int line,
103105
final int column,
@@ -113,21 +115,6 @@ protected void reportError(final CommonToken offendingToken, final int line,
113115
};
114116
}
115117

116-
/**
117-
* Creates a new {@link ANTLRInputStream}.
118-
*
119-
* @param filename The file's name.
120-
* @param content A content.
121-
* @return A new {@link ANTLRInputStream}.
122-
* @throws IOException If the reader can't be open.
123-
*/
124-
private ANTLRInputStream newStream(final String filename, final String content)
125-
throws IOException {
126-
ANTLRInputStream stream = new ANTLRInputStream(content);
127-
stream.name = filename;
128-
return stream;
129-
}
130-
131118
/**
132119
* Creates a new {@link HbsLexer}.
133120
*
@@ -136,7 +123,7 @@ private ANTLRInputStream newStream(final String filename, final String content)
136123
* @param endDelimiter The end delimiter.
137124
* @return A new {@link HbsLexer}.
138125
*/
139-
private HbsLexer newLexer(final ANTLRInputStream stream, final String startDelimiter,
126+
private HbsLexer newLexer(final CharStream stream, final String startDelimiter,
140127
final String endDelimiter) {
141128
return new HbsLexer(stream, startDelimiter, endDelimiter) {
142129

handlebars/src/main/java/com/github/jknack/handlebars/internal/TemplateBuilder.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,9 @@
8080
* Traverse the parse tree and build templates.
8181
*
8282
* @author edgar.espina
83-
* @param <it>
8483
* @since 0.10.0
8584
*/
86-
abstract class TemplateBuilder<it> extends HbsParserBaseVisitor<Object> {
85+
abstract class TemplateBuilder extends HbsParserBaseVisitor<Object> {
8786

8887
/**
8988
* Get partial info: static vs dynamic.
@@ -171,7 +170,7 @@ public Template visitRawBlock(final RawBlockContext ctx) {
171170

172171
hasTag(true);
173172
Block block = new Block(handlebars, name, false, "{{", params(sexpr.param()),
174-
hash(sexpr.hash()), Collections.<String> emptyList());
173+
hash(sexpr.hash()), Collections.emptyList());
175174

176175
if (block.paramSize > 0) {
177176
paramStack.addLast(block.params.get(0).toString());
@@ -304,8 +303,8 @@ public Template visitUnless(final UnlessContext ctx) {
304303
reportError(null, ctx.nameEnd.getLine(), ctx.nameEnd.getCharPositionInLine(),
305304
String.format("found: '%s', expected: '%s'", nameEnd, name));
306305
}
307-
Block block = new Block(handlebars, name, true, "^", Collections.<Param> emptyList(),
308-
Collections.<String, Param> emptyMap(), blockParams(ctx.blockParams()));
306+
Block block = new Block(handlebars, name, true, "^", Collections.emptyList(),
307+
Collections.emptyMap(), blockParams(ctx.blockParams()));
309308
block.filename(source.filename());
310309
block.position(nameStart.getLine(), nameStart.getCharPositionInLine());
311310
String startDelim = ctx.start.getText();
@@ -388,7 +387,7 @@ private Variable newVar(final Token name, final TagType varType, final List<Para
388387
String[] parts = varName.split("\\./");
389388
// TODO: try to catch this with ANTLR...
390389
// foo.0 isn't allowed, it must be foo.0.
391-
if (parts.length > 0 && NumberUtils.isNumber(parts[parts.length - 1])
390+
if (parts.length > 0 && NumberUtils.isCreatable(parts[parts.length - 1])
392391
&& !varName.endsWith(".")) {
393392
String evidence = varName;
394393
String reason = "found: " + varName + ", expecting: " + varName + ".";

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@
6868
<version>3.7</version>
6969
</dependency>
7070

71+
<dependency>
72+
<groupId>org.apache.commons</groupId>
73+
<artifactId>commons-text</artifactId>
74+
<version>1.3</version>
75+
</dependency>
76+
7177
<!-- Guava -->
7278
<dependency>
7379
<groupId>com.google.guava</groupId>

0 commit comments

Comments
 (0)