Created
October 15, 2008 02:44
-
-
Save pjb3/16843 to your computer and use it in GitHub Desktop.
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
package clojure.velocity; | |
import org.apache.velocity.util.introspection.UberspectImpl; | |
import org.apache.velocity.util.introspection.VelPropertyGet; | |
import org.apache.velocity.util.introspection.Info; | |
import org.apache.velocity.runtime.parser.node.AbstractExecutor; | |
import clojure.lang.APersistentMap; | |
import clojure.lang.Keyword; | |
import java.lang.reflect.InvocationTargetException; | |
public class ClojureUberspectImpl extends UberspectImpl { | |
@Override | |
public VelPropertyGet getPropertyGet(Object obj, String identifier, Info i) throws Exception { | |
if (obj instanceof APersistentMap) { | |
final APersistentMap map = (APersistentMap)obj; | |
final Keyword key = Keyword.intern(null, identifier); | |
return new VelGetterImpl(new AbstractExecutor() { | |
public Object execute(Object o) throws IllegalAccessException, InvocationTargetException { | |
return map.get(key); | |
} | |
}); | |
} else { | |
return super.getPropertyGet(obj, identifier, i); | |
} | |
} | |
} |
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
(in-ns 'clojure.velocity) | |
(clojure/refer 'clojure) | |
(import '(org.apache.velocity.util.introspection UberspectImpl VelPropertyGet Info)) | |
(import '(org.apache.velocity.runtime.parser.node AbstractExecutor)) | |
(defn ClojureUberspectImpl-getPropertyGet-Object-String-Info [this obj id info] | |
(if (instance? clojure.lang.APersistentMap) | |
(new org.apache.velocity.util.introspection.UberspectImpl$VelGetterImpl | |
(proxy [AbstractExecutor] [] (execute [obj] (obj id)))) | |
(proxy-super getPropertyGet obj id info))) | |
(gen-and-load-class 'clojure.velocity.ClojureUberspectImpl :extends UberspectImpl) | |
(in-ns 'velocity) | |
(clojure/refer 'clojure) | |
(import '(org.apache.velocity.app VelocityEngine)) | |
(import '(org.apache.velocity.context Context)) | |
(import '(java.io StringWriter)) | |
(defn make-context [context] | |
(proxy [Context] [] | |
(containsKey [key] (contains? context (keyword key))) | |
(get [key] (context (keyword key))) | |
(getKeys [] (keys context)) | |
(put [key value] false) | |
(remove [key] false))) | |
(defn render [template context] | |
(let | |
[ve (new VelocityEngine) | |
ctx (make-context context) | |
out (new StringWriter) | |
props (new java.util.Properties)] | |
(.setProperty props "runtime.introspector.uberspect" "clojure.velocity.ClojureUberspectImpl") | |
(.init ve props) | |
(.evaluate ve ctx out "clojure" template) | |
(.toString out))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment