Skip to content

Commit 966c452

Browse files
committed
Support arbitrary charsets
1 parent 141522f commit 966c452

24 files changed

Lines changed: 188 additions & 83 deletions

handlebars-guava-cache/src/main/java/com/github/jknack/handlebars/io/GuavaCachedTemplateLoader.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.github.jknack.handlebars.io;
1919

2020
import java.io.IOException;
21+
import java.nio.charset.Charset;
2122
import java.util.concurrent.Callable;
2223
import java.util.concurrent.ExecutionException;
2324
import java.util.concurrent.TimeUnit;
@@ -125,4 +126,11 @@ public void setSuffix(final String suffix) {
125126
delegate.setSuffix(suffix);
126127
}
127128

129+
@Override public void setCharset(Charset charset) {
130+
delegate.setCharset(charset);
131+
}
132+
133+
@Override public Charset getCharset() {
134+
return delegate.getCharset();
135+
}
128136
}

handlebars-springmvc/src/main/java/com/github/jknack/handlebars/springmvc/HandlebarsViewResolver.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.io.InputStream;
2626
import java.io.Reader;
2727
import java.net.URI;
28+
import java.nio.charset.Charset;
29+
import java.nio.charset.StandardCharsets;
2830
import java.util.ArrayList;
2931
import java.util.Enumeration;
3032
import java.util.List;
@@ -122,6 +124,9 @@ public class HandlebarsViewResolver extends AbstractTemplateViewResolver
122124
/** Template cache. */
123125
private TemplateCache templateCache = new HighConcurrencyTemplateCache();
124126

127+
/** Charset. */
128+
private Charset charset = StandardCharsets.UTF_8;
129+
125130
/**
126131
* Creates a new {@link HandlebarsViewResolver}.
127132
*
@@ -256,6 +261,8 @@ public void afterPropertiesSet() {
256261

257262
// set delete partial after merge
258263
handlebars.setDeletePartialAfterMerge(deletePartialAfterMerge);
264+
265+
handlebars.setCharset(charset);
259266
}
260267

261268
/**
@@ -598,4 +605,9 @@ public HandlebarsViewResolver registerDecorator(final String name, final Decorat
598605
registry.registerDecorator(name, decorator);
599606
return this;
600607
}
608+
609+
@Override public HandlebarsViewResolver setCharset(final Charset charset) {
610+
this.charset = notNull(charset, "Charset required.");
611+
return this;
612+
}
601613
}

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.lang.reflect.Array;
3030
import java.net.URI;
3131
import java.net.URL;
32+
import java.nio.charset.Charset;
33+
import java.nio.charset.StandardCharsets;
3234
import java.util.ArrayList;
3335
import java.util.Collection;
3436
import java.util.List;
@@ -342,6 +344,9 @@ public static CharSequence escapeExpression(final CharSequence input) {
342344
*/
343345
private boolean preEvaluatePartialBlocks = true;
344346

347+
/** Standard charset. */
348+
private Charset charset = StandardCharsets.UTF_8;
349+
345350
/**
346351
* Creates a new {@link Handlebars} with no cache.
347352
*
@@ -795,7 +800,6 @@ public boolean stringParams() {
795800
return stringParams;
796801
}
797802

798-
799803
/**
800804
* If true, unnecessary spaces and new lines will be removed from output. Default is: false.
801805
*
@@ -1208,7 +1212,9 @@ public Handlebars parentScopeResolution(final boolean parentScopeResolution) {
12081212
* If false, you will have to evaluate and render partial blocks explitly (this
12091213
* option is *much* faster).
12101214
*/
1211-
public boolean preEvaluatePartialBlocks() { return preEvaluatePartialBlocks; }
1215+
public boolean preEvaluatePartialBlocks() {
1216+
return preEvaluatePartialBlocks;
1217+
}
12121218

12131219
/**
12141220
* If true, partial blocks will implicitly be evaluated before the partials will actually
@@ -1358,4 +1364,18 @@ public Handlebars registerDecorator(final String name, final Decorator decorator
13581364
return this;
13591365
}
13601366

1367+
@Override
1368+
public Handlebars setCharset(final Charset charset) {
1369+
this.charset = notNull(charset, "Charset required.");
1370+
registry.setCharset(charset);
1371+
loader.setCharset(charset);
1372+
return this;
1373+
}
1374+
1375+
/**
1376+
* @return Charset.
1377+
*/
1378+
public Charset getCharset() {
1379+
return charset;
1380+
}
13611381
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.io.InputStream;
3636
import java.io.Reader;
3737
import java.net.URI;
38+
import java.nio.charset.Charset;
3839
import java.util.Map.Entry;
3940
import java.util.Set;
4041

@@ -323,4 +324,13 @@ HelperRegistry registerHelpers(String filename, InputStream source)
323324
*/
324325
HelperRegistry registerDecorator(String name, Decorator decorator);
325326

327+
/**
328+
* Set the charset to use.
329+
*
330+
* @param charset Charset.
331+
* @return This registry.
332+
* @since 4.0.6
333+
*/
334+
HelperRegistry setCharset(Charset charset);
335+
326336
}

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

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,6 @@
1515
* See the License for the specific language governing permissions and
1616
* limitations under the License.
1717
*/
18-
/**
19-
* This copy of Woodstox XML processor is licensed under the
20-
* Apache (Software) License, version 2.0 ("the License").
21-
* See the License for details about distribution rights, and the
22-
* specific rights regarding derivate works.
23-
*
24-
* You may obtain a copy of the License at:
25-
*
26-
* http://www.apache.org/licenses/
27-
*
28-
* A copy is also included in the downloadable source code package
29-
* containing Woodstox, in file "ASL2.0", under the same directory
30-
* as this file.
31-
*/
3218
package com.github.jknack.handlebars.helper;
3319

3420
import static org.apache.commons.lang3.Validate.isTrue;
@@ -41,6 +27,8 @@
4127
import java.lang.reflect.Method;
4228
import java.lang.reflect.Modifier;
4329
import java.net.URI;
30+
import java.nio.charset.Charset;
31+
import java.nio.charset.StandardCharsets;
4432
import java.util.HashMap;
4533
import java.util.HashSet;
4634
import java.util.Map;
@@ -81,6 +69,9 @@ public class DefaultHelperRegistry implements HelperRegistry {
8169
*/
8270
private HandlebarsJs handlebarsJs;
8371

72+
/** Charset. */
73+
private Charset charset = StandardCharsets.UTF_8;
74+
8475
{
8576
Integer optimizationLevel = Integer.valueOf(Integer.parseInt(
8677
System.getProperty("handlebars.rhino.optmizationLevel", "-1")));
@@ -156,12 +147,12 @@ public HelperRegistry registerHelpers(final Class<?> helperSource) {
156147

157148
@Override
158149
public HelperRegistry registerHelpers(final URI location) throws Exception {
159-
return registerHelpers(location.getPath(), Files.read(location.toString()));
150+
return registerHelpers(location.getPath(), Files.read(location.toString(), charset));
160151
}
161152

162153
@Override
163154
public HelperRegistry registerHelpers(final File input) throws Exception {
164-
return registerHelpers(input.getAbsolutePath(), Files.read(input));
155+
return registerHelpers(input.getAbsolutePath(), Files.read(input, charset));
165156
}
166157

167158
@Override
@@ -173,7 +164,7 @@ public HelperRegistry registerHelpers(final String filename, final Reader source
173164
@Override
174165
public HelperRegistry registerHelpers(final String filename, final InputStream source)
175166
throws Exception {
176-
return registerHelpers(filename, Files.read(source));
167+
return registerHelpers(filename, Files.read(source, charset));
177168
}
178169

179170
@Override
@@ -271,4 +262,8 @@ public DefaultHelperRegistry with(final HandlebarsJs handlebarsJs) {
271262
return this;
272263
}
273264

265+
@Override public DefaultHelperRegistry setCharset(Charset charset) {
266+
this.charset = notNull(charset, "Charset required.");
267+
return this;
268+
}
274269
}

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,33 +46,36 @@ private Files() {
4646
* Read a file from a classpath location.
4747
*
4848
* @param location The classpath location.
49+
* @param charset Charset.
4950
* @return The file content.
5051
* @throws IOException If the file can't be read.
5152
*/
52-
public static String read(final String location) throws IOException {
53-
return read(Files.class.getResourceAsStream(location));
53+
public static String read(final String location, final Charset charset) throws IOException {
54+
return read(Files.class.getResourceAsStream(location), charset);
5455
}
5556

5657
/**
5758
* Read a file content.
5859
*
5960
* @param source The file.
61+
* @param charset Charset.
6062
* @return The file content.
6163
* @throws IOException If the file can't be read.
6264
*/
63-
public static String read(final File source) throws IOException {
64-
return read(new FileInputStream(source));
65+
public static String read(final File source, final Charset charset) throws IOException {
66+
return read(new FileInputStream(source), charset);
6567
}
6668

6769
/**
6870
* Read a file source.
6971
*
7072
* @param source The file source.
73+
* @param charset Charset.
7174
* @return The file content.
7275
* @throws IOException If the file can't be read.
7376
*/
74-
public static String read(final InputStream source) throws IOException {
75-
return read(new InputStreamReader(source, Charset.forName("UTF-8")));
77+
public static String read(final InputStream source, final Charset charset) throws IOException {
78+
return read(new InputStreamReader(source, charset));
7679
}
7780

7881
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public Template parse(final TemplateSource source) throws IOException {
7171
final ANTLRErrorListener errorReporter = new HbsErrorReporter(source.filename());
7272

7373
// 1. Lexer
74-
final HbsLexer lexer = newLexer(newStream(source.filename(), source.content()),
74+
final HbsLexer lexer = newLexer(newStream(source.filename(), source.content(handlebars.getCharset())),
7575
startDelimiter, endDelimiter);
7676
configure(lexer, errorReporter);
7777

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,10 @@
1515
* See the License for the specific language governing permissions and
1616
* limitations under the License.
1717
*/
18-
/**
19-
* This copy of Woodstox XML processor is licensed under the
20-
* Apache (Software) License, version 2.0 ("the License").
21-
* See the License for details about distribution rights, and the
22-
* specific rights regarding derivate works.
23-
*
24-
* You may obtain a copy of the License at:
25-
*
26-
* http://www.apache.org/licenses/
27-
*
28-
* A copy is also included in the downloadable source code package
29-
* containing Woodstox, in file "ASL2.0", under the same directory
30-
* as this file.
31-
*/
3218
package com.github.jknack.handlebars.internal;
3319

3420
import java.io.IOException;
21+
import java.nio.charset.StandardCharsets;
3522

3623
import org.mozilla.javascript.Context;
3724
import org.mozilla.javascript.Scriptable;
@@ -124,7 +111,7 @@ private Scriptable sharedScope(final String hbslocation, final Context ctx) {
124111
*/
125112
private String handlebarsScript(final String location) {
126113
try {
127-
return Files.read(location);
114+
return Files.read(location, StandardCharsets.UTF_8);
128115
} catch (IOException ex) {
129116
throw new IllegalArgumentException("Unable to read file: " + location, ex);
130117
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.io.FileNotFoundException;
2424
import java.io.IOException;
2525
import java.io.Writer;
26+
import java.nio.charset.Charset;
27+
import java.nio.charset.StandardCharsets;
2628
import java.util.Arrays;
2729
import java.util.Collections;
2830
import java.util.HashMap;
@@ -238,8 +240,8 @@ public String filename() {
238240
}
239241

240242
@Override
241-
public String content() throws IOException {
242-
return partialInput(source.content(), indent);
243+
public String content(Charset charset) throws IOException {
244+
return partialInput(source.content(charset), indent);
243245
}
244246

245247
@Override

handlebars/src/main/java/com/github/jknack/handlebars/internal/js/RhinoHandlebars.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.github.jknack.handlebars.internal.js;
1919

2020
import java.io.IOException;
21+
import java.nio.charset.StandardCharsets;
2122
import java.util.Collection;
2223
import java.util.HashMap;
2324
import java.util.List;
@@ -367,7 +368,7 @@ private Scriptable helpersEnvScope(final org.mozilla.javascript.Context ctx) {
367368
*/
368369
private static String envSource(final String location) {
369370
try {
370-
return Files.read(location);
371+
return Files.read(location, StandardCharsets.UTF_8);
371372
} catch (IOException ex) {
372373
throw new IllegalStateException("Unable to read " + location, ex);
373374
}

0 commit comments

Comments
 (0)