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/TodoAttr.java | 37 ++++++++++----------------- org/madore/damlengine/TodoDefaultElement.java | 11 +++++++- org/madore/damlengine/TodoElement.java | 32 +++++++++++------------ org/madore/damlengine/TodoXemptyAttr.java | 8 ++++++ 4 files changed, 47 insertions(+), 41 deletions(-) (limited to 'org/madore/damlengine') diff --git a/org/madore/damlengine/TodoAttr.java b/org/madore/damlengine/TodoAttr.java index 2b3df23..721b4f6 100644 --- a/org/madore/damlengine/TodoAttr.java +++ b/org/madore/damlengine/TodoAttr.java @@ -1,7 +1,5 @@ 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; @@ -9,16 +7,17 @@ import org.w3c.dom.Attr; public abstract class TodoAttr extends TodoItem { - protected static Map> damlAttrConstructors; + public static abstract class Factory { + public abstract TodoAttr newItem(Attr attr, Element owner, + Map context, + Map options); + } + + protected static Map damlAttrFactories; static { - damlAttrConstructors = new HashMap>(); - Class[] argTypes = new Class[]{ Attr.class, Element.class, Map.class, Map.class }; - try { - damlAttrConstructors.put("xempty", TodoXemptyAttr.class.getConstructor(argTypes)); - } catch (NoSuchMethodException e) { - // FIXME: Do something intelligent here! - } + damlAttrFactories = new HashMap(); + damlAttrFactories.put("xempty", new TodoXemptyAttr.Factory()); } Attr attr; @@ -37,21 +36,13 @@ public abstract class TodoAttr extends TodoItem { public static TodoAttr getTodoAttr(Attr attr, Element owner, Map context, Map options) { - Constructor constructor = null; + Factory factory = null; String nsuri = attr.getNamespaceURI(); if ( nsuri != null && nsuri.equals(DamlEngine.DAML_NS) ) - constructor = damlAttrConstructors.get(attr.getLocalName()); - if ( constructor != null ) - try { - return constructor.newInstance(new Object[]{attr, owner, 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 null; + factory = damlAttrFactories.get(attr.getLocalName()); + if ( factory == null ) + return null; + return factory.newItem(attr, owner, context, options); } } diff --git a/org/madore/damlengine/TodoDefaultElement.java b/org/madore/damlengine/TodoDefaultElement.java index 3329696..4041f82 100644 --- a/org/madore/damlengine/TodoDefaultElement.java +++ b/org/madore/damlengine/TodoDefaultElement.java @@ -7,8 +7,17 @@ import org.w3c.dom.*; public class TodoDefaultElement extends TodoElement { + public static class Factory extends TodoElement.Factory { + public TodoDefaultElement newItem(Element node, + Map context, + Map options) { + return new TodoDefaultElement(node, context, options); + } + } + public TodoDefaultElement(Element node, - Map context, Map options) { + Map context, + Map options) { super(node,context,options); } 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); } } diff --git a/org/madore/damlengine/TodoXemptyAttr.java b/org/madore/damlengine/TodoXemptyAttr.java index e2cbe61..7eaf636 100644 --- a/org/madore/damlengine/TodoXemptyAttr.java +++ b/org/madore/damlengine/TodoXemptyAttr.java @@ -5,6 +5,14 @@ import org.w3c.dom.*; public class TodoXemptyAttr extends TodoAttr { + public static class Factory extends TodoAttr.Factory { + public TodoXemptyAttr newItem(Attr attr, Element owner, + Map context, + Map options) { + return new TodoXemptyAttr(attr, owner, context, options); + } + } + public TodoXemptyAttr(Attr attr, Element owner, Map context, Map options) { super(attr, owner, context, options); -- cgit v1.2.3