Date: 2004-12-19T16:22:20
Editor: EricFixler <[EMAIL PROTECTED]>
Wiki: Jakarta-Velocity Wiki
Page: MultiUberspect
URL: http://wiki.apache.org/jakarta-velocity/MultiUberspect
Changed the init() code to use a Map for the configuration parameters
Change Log:
------------------------------------------------------------------------------
@@ -3,7 +3,7 @@
Allows Velocity to Introspect other types of Objects
November 2004
Eric Fixler <[EMAIL PROTECTED]>
- $Id: MultiUberspect.java,v 1.1.2.2 2004/11/05 05:51:00 fix Exp $
+ $Id: MultiUberspect.java,v 1.1.2.7 2004/12/20 00:20:34 fix Exp $
*/
/*
@@ -25,6 +25,9 @@
package org.apache.velocity.tools.generic.introspection;
import java.util.*;
+import java.lang.reflect.Constructor;
+
+import org.apache.log4j.Logger;
import org.apache.velocity.util.introspection.Info;
import org.apache.velocity.util.introspection.Uberspect;
@@ -46,7 +49,7 @@
* </p>
*
* @author <a href="mailto:[EMAIL PROTECTED]">Eric Fixler</a>
- * @version $Id: MultiUberspect.java,v 1.1.2.2 2004/11/05 05:51:00 fix Exp $
+ * @version $Id: MultiUberspect.java,v 1.1.2.7 2004/12/20 00:20:34 fix Exp $
*/
public class MultiUberspect extends UberspectImpl {
@@ -87,6 +90,7 @@
this.customIntrospectors.put(clz, u);
this.derivedIntrospectors.clear();
this.derivedIntrospectors.putAll(this.customIntrospectors);
+ Logger.getRootLogger().info("Added uberpect " +
u.getClass().getName() + " for " + clz.getName());
}
/** Unregister a custom uberspect.
@@ -125,6 +129,28 @@
}
/**
+ * Property iterarot - returns Iterator apropos for #foreach($foo in
$bar.).
+ * <br />
+ * Looks to see if a custom Uberspect has been registered for
obj.getClass().
+ * If an Iterator was not found, tries the regular routine.
+ *
+ * @param obj the object
+ * @param info a bunch of information.
+ * @return a valid <code>Iterator</code>, if it was found.
+ * @throws Exception failed to create a valid <code>Iterator</code>.
+ */
+ public Iterator getIterator(Object obj, Info info)
+ throws Exception {
+ Uberspect uberspect = (Uberspect)
this.getCustomIntrospector(obj.getClass());
+ return (uberspect != null)
+ ? uberspect.getIterator(obj, info)
+ : super.getIterator(obj, info);
+
+
+ }
+
+
+ /**
* Property getter - returns VelPropertyGet appropos for #set($foo =
$bar.woogie).
* <br />
* Looks to see if a custom Uberspect has been registered for
obj.getClass().
@@ -170,17 +196,97 @@
/**
Change this source if you use this, since you won't have the classes
referenced here.
- (Obviously, would be better to assign the mappings elsewhere)
+ (Obviously, would be better to assign the mappings elsewhere, and
it's set up to do its
+ configuration work based on a map, in the hopes that, at some point,
it'll be
+ configurable velocity.properties)
*/
public void init() throws Exception {
super.init();
- this.addIntrospector(Class.forName("com.canto.cumulus.Record"), new
CumulusRecordUberspect());
+ Map m = new HashMap();
+
m.put("org.apache.velocity.tools.generic.introspection.CumulusRecordUberspect",
+ new String[] { "com.canto.cumulus.Record" });
+
m.put("org.apache.velocity.tools.generic.introspection.CumulusCategoriesUberspect",
+ new String[] { "com.canto.cumulus.Categories"
});
+
m.put("org.apache.velocity.tools.generic.introspection.LuceneDocumentUberspect",
+ new String[] {
"org.apache.lucene.document.Document" });
+
m.put("org.apache.velocity.tools.generic.introspection.SearchResultItemUberspect",
+ new String[] {
"org.smete.search.SearchResultItem" });
+
+ Iterator it = m.entrySet().iterator();
+
+ while (it.hasNext()) {
+ Map.Entry entry = (Map.Entry) it.next();
+ String key = (String) entry.getKey();
+ Object val = entry.getValue();
+ String[] clzNames = (val instanceof Object[])
+ ? (val
instanceof String[])
+ ?
(String[]) val
+ : new
String[0] //need better handler here
+ : new String[]
{ val.toString() };
+
+
+ Class introspectorClass = null;
+
+ try {
+ introspectorClass = Class.forName(key);
+ } catch (Exception e) {
+ Logger.getRootLogger().warn("Uberspect class " + key +
" not found, ignoring", e);
+ continue;
+ }
+
+ Constructor ubc = null; Object[] args = null;
+
+ try {
+ ubc = introspectorClass.getConstructor(new Class[] {
MultiUberspect.class} );
+ args = new Object[] { this };
+ } catch (Exception e) {
+ try {
+ ubc = introspectorClass.getConstructor(new
Class[0]);
+ args = new Object[0];
+ } catch (Exception ee) {
+ Logger.getRootLogger().warn("Can't find
constructor for Uberspect " + key,e);
+ continue;
+ }
+ }
+
+ Uberspect introspector = null;
+ try {
+ introspector = (Uberspect)
ubc.newInstance(args);
+ } catch (Exception e) {
+ Logger.getRootLogger().warn("Can't instantiate
Uberspect " + key,e);
+ continue;
+ }
+
+ for (int i = 0; i < clzNames.length; i++) {
+ try {
+ Class clz = Class.forName(clzNames[i]);
+ this.addIntrospector(clz,introspector);
+ } catch (Exception e) {
+ Logger.getRootLogger().warn("Can't find class "
+ clzNames[i] + " for custom Uberspect, ignoring", e);
+ continue;
+ }
+ }
+
+ }
+
}
}
// $Log: MultiUberspect.java,v $
+ // Revision 1.1.2.7 2004/12/20 00:20:34 fix
+ // docs
+ //
+ // Revision 1.1.2.6 2004/12/16 03:45:01 fix
+ // -- Uberspect for LuceneDocuments
+ // -- Modified init() in MultiUberspect to make configuration make more sense
+ //
+ // Revision 1.1.2.5 2004/11/24 00:46:36 fix
+ // -- Fixed MultiUberspect so delegation works for Iterators
+ //
+ // Revision 1.1.2.3 2004/11/05 06:35:40 fix
+ // bug fix
+ //
// Revision 1.1.2.2 2004/11/05 05:51:00 fix
// Added useSuperclassIntrospector to turn superclass scaling on and off
- //
-}}}
+ //}}}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]