Skip to content

Commit ddf047f

Browse files
committed
better and clean context lookup
1 parent c935fb2 commit ddf047f

23 files changed

+806
-368
lines changed

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

Lines changed: 103 additions & 215 deletions
Large diffs are not rendered by default.
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
* Copyright (c) 2012-2015 Edgar Espina
3+
*
4+
* This file is part of Handlebars.java.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package com.github.jknack.handlebars;
19+
20+
import java.util.LinkedList;
21+
import java.util.List;
22+
import java.util.Map;
23+
import java.util.concurrent.ConcurrentHashMap;
24+
import java.util.regex.Matcher;
25+
import java.util.regex.Pattern;
26+
27+
import com.github.jknack.handlebars.internal.path.DataPath;
28+
import com.github.jknack.handlebars.internal.path.IndexedPath;
29+
import com.github.jknack.handlebars.internal.path.ParentPath;
30+
import com.github.jknack.handlebars.internal.path.PropertyPath;
31+
import com.github.jknack.handlebars.internal.path.ResolveParentPath;
32+
import com.github.jknack.handlebars.internal.path.ResolveThisPath;
33+
import com.github.jknack.handlebars.internal.path.ThisPath;
34+
35+
/**
36+
* Compile mustache/handlebars expressions.
37+
*
38+
* @author edgar
39+
* @since 4.0.1.
40+
*/
41+
public final class PathCompiler {
42+
43+
/** Cache with path expressions. */
44+
private static Map<String, List<PathExpression>> cache = new ConcurrentHashMap<>();
45+
46+
/** Split pattern. */
47+
private static Pattern pattern = Pattern
48+
.compile("((\\[[^\\[\\]]+])|([^" + Pattern.quote("./") + "]+))");
49+
50+
/**
51+
* Not allowed.
52+
*/
53+
private PathCompiler() {
54+
}
55+
56+
/**
57+
* Split the property name by separator (except within a [] escaped blocked)
58+
* and create an array of it.
59+
*
60+
* @param key The property's name.
61+
* @return A path representation of the property (array based).
62+
*/
63+
public static List<PathExpression> compile(final String key) {
64+
List<PathExpression> path = cache.get(key);
65+
if (path == null) {
66+
path = parse(key);
67+
cache.put(key, path);
68+
}
69+
return path;
70+
}
71+
72+
/**
73+
* Split the property name by separator (except within a [] escaped blocked)
74+
* and create an array of it.
75+
*
76+
* @param path The property's path.
77+
* @return A path representation of the property (array based).
78+
*/
79+
private static List<PathExpression> parse(final String path) {
80+
LinkedList<PathExpression> resolvers = new LinkedList<>();
81+
if ("this".equals(path) || "./".equals(path) || ".".equals(path)) {
82+
resolvers.add(new ResolveThisPath(path));
83+
return resolvers;
84+
}
85+
if ("..".equals(path)) {
86+
resolvers.add(new ResolveParentPath());
87+
return resolvers;
88+
}
89+
if (path.startsWith("../")) {
90+
resolvers.add(new ParentPath());
91+
resolvers.addAll(parse(path.substring("../".length())));
92+
return resolvers;
93+
}
94+
if (path.startsWith("./")) {
95+
resolvers.add(new ThisPath("./"));
96+
resolvers.addAll(parse(path.substring("./".length())));
97+
return resolvers;
98+
}
99+
Matcher matcher = pattern.matcher(path);
100+
while (matcher.find()) {
101+
String key = matcher.group(1);
102+
if ("this".equals(key)) {
103+
resolvers.add(new ThisPath(key));
104+
} else if (key.charAt(0) == '@') {
105+
resolvers.add(new DataPath(key));
106+
} else {
107+
if (key.charAt(0) == '[' && key.charAt(key.length() - 1) == ']') {
108+
key = key.substring(1, key.length() - 1);
109+
}
110+
try {
111+
resolvers.add(new IndexedPath(Integer.parseInt(key), key));
112+
} catch (NumberFormatException ex) {
113+
resolvers.add(new PropertyPath(key));
114+
}
115+
}
116+
}
117+
return resolvers;
118+
}
119+
120+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright (c) 2012-2015 Edgar Espina
3+
*
4+
* This file is part of Handlebars.java.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package com.github.jknack.handlebars;
19+
20+
/**
21+
* Compiled version of path expression, like: <code>this</code>, <code>foo</code>,
22+
* <code>foo.bar</code>.
23+
*
24+
* @author edgar
25+
* @since 4.0.1
26+
* @see PathCompiler#compile(String)
27+
*/
28+
public interface PathExpression {
29+
30+
/**
31+
* Call the next expression in the chain and/or finalize the process if this was the tail.
32+
*
33+
* @author edgar
34+
* @since 4.0.1
35+
*/
36+
interface Chain {
37+
38+
/**
39+
* Call the next resolver in the chain or finish the call.
40+
*
41+
* @param resolver Value resolver.
42+
* @param context Context object.
43+
* @param data Data object.
44+
* @return A resolved value or <code>null</code>.
45+
*/
46+
Object next(ValueResolver resolver, Context context, Object data);
47+
}
48+
49+
/**
50+
* Eval the expression and resolve it to a value.
51+
*
52+
* @param resolver Value resolver
53+
* @param context Context object.
54+
* @param data Data object.
55+
* @param chain Expression chain.
56+
* @return A resolved value or <code>null</code>.
57+
*/
58+
Object eval(ValueResolver resolver, Context context, Object data, Chain chain);
59+
60+
/**
61+
* @return True if this expression is local. That's lookup won't be propagate to parent (or any
62+
* other). Example of these expressions are: <code>this.name</code> <code>this</code>,
63+
* etc...
64+
*/
65+
boolean local();
66+
}

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

Lines changed: 0 additions & 93 deletions
This file was deleted.

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import java.util.Iterator;
2323
import java.util.Map.Entry;
2424

25-
import org.apache.commons.lang3.StringUtils;
26-
2725
import com.github.jknack.handlebars.Context;
2826
import com.github.jknack.handlebars.Helper;
2927
import com.github.jknack.handlebars.Options;
@@ -53,13 +51,12 @@ public class EachHelper implements Helper<Object> {
5351
@Override
5452
public CharSequence apply(final Object context, final Options options)
5553
throws IOException {
56-
if (context == null) {
57-
return StringUtils.EMPTY;
58-
}
5954
if (context instanceof Iterable) {
6055
return iterableContext((Iterable) context, options);
56+
} else if (context != null) {
57+
return hashContext(context, options);
6158
}
62-
return hashContext(context, options);
59+
return options.buffer();
6360
}
6461

6562
/**

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import com.github.jknack.handlebars.Helper;
3737
import com.github.jknack.handlebars.Lambda;
3838
import com.github.jknack.handlebars.Options;
39+
import com.github.jknack.handlebars.PathCompiler;
40+
import com.github.jknack.handlebars.PathExpression;
3941
import com.github.jknack.handlebars.TagType;
4042
import com.github.jknack.handlebars.Template;
4143
import com.github.jknack.handlebars.helper.EachHelper;
@@ -105,6 +107,9 @@ class Block extends HelperResolver {
105107
/** Tag type, default: is {@link TagType#SECTION}. */
106108
protected TagType tagType;
107109

110+
/** Compiled path for {@link #name()}. */
111+
private List<PathExpression> path;
112+
108113
/**
109114
* Creates a new {@link Block}.
110115
*
@@ -121,6 +126,7 @@ public Block(final Handlebars handlebars, final String name,
121126
final Map<String, Object> hash, final List<String> blockParams) {
122127
super(handlebars);
123128
this.name = notNull(name, "The name is required.");
129+
this.path = PathCompiler.compile(name);
124130
this.inverted = inverted;
125131
this.type = type;
126132
params(params);
@@ -167,7 +173,7 @@ protected void merge(final Context context, final Writer writer) throws IOExcept
167173
final Object it;
168174
Context itCtx = context;
169175
if (helper == null) {
170-
it = Transformer.transform(context.get(name));
176+
it = Transformer.transform(itCtx.get(this.path));
171177
if (inverted) {
172178
helperName = UnlessHelper.NAME;
173179
} else if (it instanceof Iterable) {
@@ -198,14 +204,13 @@ protected void merge(final Context context, final Writer writer) throws IOExcept
198204
it = Transformer.transform(determineContext(context));
199205
}
200206

201-
Options options = new Options.Builder(handlebars, helperName, TagType.SECTION, itCtx,
202-
template)
203-
.setInverse(inverse)
204-
.setParams(params(itCtx))
205-
.setHash(hash(itCtx))
206-
.setBlockParams(blockParams)
207-
.setWriter(writer)
208-
.build();
207+
Options options = new Options.Builder(handlebars, helperName, tagType, itCtx, template)
208+
.setInverse(inverse)
209+
.setParams(params(itCtx))
210+
.setHash(hash(itCtx))
211+
.setBlockParams(blockParams)
212+
.setWriter(writer)
213+
.build();
209214
options.data(Context.PARAM_SIZE, this.params.size());
210215

211216
CharSequence result = helper.apply(it, options);

0 commit comments

Comments
 (0)