Differences between revisions 1 and 2
Revision 1 as of 2004-12-20 00:31:39
Size: 3778
Editor: m208-120
Comment: Concrete example of a custom Uberspect for use with MultiUberspect
Revision 2 as of 2009-09-20 22:06:28
Size: 3778
Editor: localhost
Comment: converted to 1.6 markup
No differences found!

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.

/** org.apache.velocity.tools.generic.introspection.LuceneDocumentUberspect
        Allows Velocity to Introspect Lucene Documents
        November 2004
        Eric Fixler <fix@smete.org>
        $Id: LuceneDocumentUberspect.java,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
 *     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,
 * See the License for the specific language governing permissions and
 * limitations under the License.
 package org.apache.velocity.tools.generic.introspection;
 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: LuceneDocumentUberspect.java,v $
 // Revision  2004/12/16 03:45:01  fix
 // -- Uberspect for LuceneDocuments

LuceneDocumentUberspect (last edited 2009-09-20 22:06:28 by localhost)