This class is an Uberspect implementation that gets values from Lucene Documents for display in Velocity templates.

It's not finished – it does not yet handle situations where the documents has more than one field withe the same name (or more than one value per field, depending on how you look at it), but still provides a useful (I think) example of a concrete Uberspect implementation that works with MultiUberspect (and how simple it can be).

Using this class, one can echo the value of fields in Lucene documents inside templates just as one would echo values from regular JavaBeans.

	Allows Velocity to Introspect Lucene Documents
	November 2004
	Eric Fixler <>
	$Id:,v 2004/12/16 03:45:01 fix Exp $

 * Copyright 2003-2004 The Apache Software Foundation.
 * 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
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.
 import org.apache.velocity.util.introspection.Info;
 import org.apache.velocity.util.introspection.Uberspect;
 import org.apache.velocity.util.introspection.UberspectImpl;
 import org.apache.velocity.util.introspection.VelPropertyGet;
 import org.apache.velocity.util.introspection.VelPropertySet;
 import org.apache.log4j.Logger;
 import org.apache.lucene.document.*;
 public class LuceneDocumentUberspect extends UberspectImpl {

  	public LuceneDocumentUberspect() {
     * Property getter - returns VelPropertyGet appropos for #set($foo = $bar.woogie).
     * <br />
     * @param obj the object
     * @param identifier the name of the property
     * @param i a bunch of information.
     * @return a valid <code>VelPropertyGet</code>, if it was found.
     * @throws Exception failed to create a valid <code>VelPropertyGet</code>.
    public VelPropertyGet getPropertyGet(Object obj, String identifier, Info info)
            throws Exception
        if (obj == null) throw new IllegalArgumentException("Target must not be null");
    	if (! (obj instanceof Document)) throw new IllegalArgumentException("Target must be a lucene document, not a(n) " + obj.getClass().getName());
       	return new LuceneDocumentGet(identifier);
    protected static class LuceneDocumentGet implements VelPropertyGet {
    	protected String fieldName = null;
    	protected LuceneDocumentGet(String fieldName) { 
    		this.fieldName = fieldName; 
    	public final String getMethodName() { 
    		return "Document.getField(" + fieldName + ")"; 
    	public final boolean isCacheable() { return true; }
    	public Object invoke(Object o) throws Exception { 
    		if (o == null) return null;
    		if (! (o instanceof Document)) throw new IllegalArgumentException("Target must be a lucene, not a(n) " + o.getClass().getName());
    		Document doc = (Document) o;
    		return doc.get(this.fieldName);
 // $Log:,v $
 // Revision  2004/12/16 03:45:01  fix
 // -- Uberspect for LuceneDocuments

  • No labels