-
-
Notifications
You must be signed in to change notification settings - Fork 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
806 additions
and
368 deletions.
There are no files selected for viewing
318 changes: 103 additions & 215 deletions
318
handlebars/src/main/java/com/github/jknack/handlebars/Context.java
Large diffs are not rendered by default.
Oops, something went wrong.
120 changes: 120 additions & 0 deletions
120
handlebars/src/main/java/com/github/jknack/handlebars/PathCompiler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/** | ||
* Copyright (c) 2012-2015 Edgar Espina | ||
* | ||
* This file is part of Handlebars.java. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.jknack.handlebars; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import com.github.jknack.handlebars.internal.path.DataPath; | ||
import com.github.jknack.handlebars.internal.path.IndexedPath; | ||
import com.github.jknack.handlebars.internal.path.ParentPath; | ||
import com.github.jknack.handlebars.internal.path.PropertyPath; | ||
import com.github.jknack.handlebars.internal.path.ResolveParentPath; | ||
import com.github.jknack.handlebars.internal.path.ResolveThisPath; | ||
import com.github.jknack.handlebars.internal.path.ThisPath; | ||
|
||
/** | ||
* Compile mustache/handlebars expressions. | ||
* | ||
* @author edgar | ||
* @since 4.0.1. | ||
*/ | ||
public final class PathCompiler { | ||
|
||
/** Cache with path expressions. */ | ||
private static Map<String, List<PathExpression>> cache = new ConcurrentHashMap<>(); | ||
|
||
/** Split pattern. */ | ||
private static Pattern pattern = Pattern | ||
.compile("((\\[[^\\[\\]]+])|([^" + Pattern.quote("./") + "]+))"); | ||
|
||
/** | ||
* Not allowed. | ||
*/ | ||
private PathCompiler() { | ||
} | ||
|
||
/** | ||
* Split the property name by separator (except within a [] escaped blocked) | ||
* and create an array of it. | ||
* | ||
* @param key The property's name. | ||
* @return A path representation of the property (array based). | ||
*/ | ||
public static List<PathExpression> compile(final String key) { | ||
List<PathExpression> path = cache.get(key); | ||
if (path == null) { | ||
path = parse(key); | ||
cache.put(key, path); | ||
} | ||
return path; | ||
} | ||
|
||
/** | ||
* Split the property name by separator (except within a [] escaped blocked) | ||
* and create an array of it. | ||
* | ||
* @param path The property's path. | ||
* @return A path representation of the property (array based). | ||
*/ | ||
private static List<PathExpression> parse(final String path) { | ||
LinkedList<PathExpression> resolvers = new LinkedList<>(); | ||
if ("this".equals(path) || "./".equals(path) || ".".equals(path)) { | ||
resolvers.add(new ResolveThisPath(path)); | ||
return resolvers; | ||
} | ||
if ("..".equals(path)) { | ||
resolvers.add(new ResolveParentPath()); | ||
return resolvers; | ||
} | ||
if (path.startsWith("../")) { | ||
resolvers.add(new ParentPath()); | ||
resolvers.addAll(parse(path.substring("../".length()))); | ||
return resolvers; | ||
} | ||
if (path.startsWith("./")) { | ||
resolvers.add(new ThisPath("./")); | ||
resolvers.addAll(parse(path.substring("./".length()))); | ||
return resolvers; | ||
} | ||
Matcher matcher = pattern.matcher(path); | ||
while (matcher.find()) { | ||
String key = matcher.group(1); | ||
if ("this".equals(key)) { | ||
resolvers.add(new ThisPath(key)); | ||
} else if (key.charAt(0) == '@') { | ||
resolvers.add(new DataPath(key)); | ||
} else { | ||
if (key.charAt(0) == '[' && key.charAt(key.length() - 1) == ']') { | ||
key = key.substring(1, key.length() - 1); | ||
} | ||
try { | ||
resolvers.add(new IndexedPath(Integer.parseInt(key), key)); | ||
} catch (NumberFormatException ex) { | ||
resolvers.add(new PropertyPath(key)); | ||
} | ||
} | ||
} | ||
return resolvers; | ||
} | ||
|
||
} |
66 changes: 66 additions & 0 deletions
66
handlebars/src/main/java/com/github/jknack/handlebars/PathExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* Copyright (c) 2012-2015 Edgar Espina | ||
* | ||
* This file is part of Handlebars.java. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.jknack.handlebars; | ||
|
||
/** | ||
* Compiled version of path expression, like: <code>this</code>, <code>foo</code>, | ||
* <code>foo.bar</code>. | ||
* | ||
* @author edgar | ||
* @since 4.0.1 | ||
* @see PathCompiler#compile(String) | ||
*/ | ||
public interface PathExpression { | ||
|
||
/** | ||
* Call the next expression in the chain and/or finalize the process if this was the tail. | ||
* | ||
* @author edgar | ||
* @since 4.0.1 | ||
*/ | ||
interface Chain { | ||
|
||
/** | ||
* Call the next resolver in the chain or finish the call. | ||
* | ||
* @param resolver Value resolver. | ||
* @param context Context object. | ||
* @param data Data object. | ||
* @return A resolved value or <code>null</code>. | ||
*/ | ||
Object next(ValueResolver resolver, Context context, Object data); | ||
} | ||
|
||
/** | ||
* Eval the expression and resolve it to a value. | ||
* | ||
* @param resolver Value resolver | ||
* @param context Context object. | ||
* @param data Data object. | ||
* @param chain Expression chain. | ||
* @return A resolved value or <code>null</code>. | ||
*/ | ||
Object eval(ValueResolver resolver, Context context, Object data, Chain chain); | ||
|
||
/** | ||
* @return True if this expression is local. That's lookup won't be propagate to parent (or any | ||
* other). Example of these expressions are: <code>this.name</code> <code>this</code>, | ||
* etc... | ||
*/ | ||
boolean local(); | ||
} |
93 changes: 0 additions & 93 deletions
93
handlebars/src/main/java/com/github/jknack/handlebars/PropertyPathParser.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.