From 6cba05247ae4e6c202c4c126a535b17526f71127 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Sat, 17 Apr 2010 04:33:42 +0200 Subject: Use factories implemented as nested classes (rather than reflected constructors) to store classes to instantiate. Now every subclass of TodoElement or TodoAttr should also come with a nested subclass of TodoElement.Factory or TodoAttr.Factory which just contains a(n instance) method to call its constructor. In essecne, the factory instance is just used as a pointer to the class to create (or its constructor, as in the previous version). --- org/madore/damlengine/TodoElement.java | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) (limited to 'org/madore/damlengine/TodoElement.java') diff --git a/org/madore/damlengine/TodoElement.java b/org/madore/damlengine/TodoElement.java index a069b36..e2293ef 100644 --- a/org/madore/damlengine/TodoElement.java +++ b/org/madore/damlengine/TodoElement.java @@ -1,17 +1,23 @@ package org.madore.damlengine; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.util.Map; import java.util.HashMap; import org.w3c.dom.Element; public abstract class TodoElement extends TodoItem { - protected static Map> damlConstructors; + public static abstract class Factory { + public abstract TodoElement newItem(Element node, + Map context, + Map options); + } + + protected static Map damlFactories; + protected static Factory damlDefaultFactory; static { - damlConstructors = new HashMap>(); + damlFactories = new HashMap(); + damlDefaultFactory = new TodoDefaultElement.Factory(); } Element node; @@ -27,21 +33,13 @@ public abstract class TodoElement extends TodoItem { public static TodoElement getTodoElement(Element node, Map context, Map options) { - Constructor constructor = null; + Factory factory = null; String nsuri = node.getNamespaceURI(); if ( nsuri != null && nsuri.equals(DamlEngine.DAML_NS) ) - constructor = damlConstructors.get(node.getLocalName()); - if ( constructor != null ) - try { - return constructor.newInstance(new Object[]{node, context, options}); - } catch (InstantiationException e) { - // FIXME: Do something intelligent here! - } catch (IllegalAccessException e) { - // FIXME: Do something intelligent here! - } catch (InvocationTargetException e) { - // FIXME: Do something intelligent here! - } - return new TodoDefaultElement(node, context, options); + factory = damlFactories.get(node.getLocalName()); + if ( factory == null ) + factory = damlDefaultFactory; + return factory.newItem(node, context, options); } } -- cgit v1.2.3