summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid A. Madore <david+git@madore.org>2010-04-17 04:33:42 +0200
committerDavid A. Madore <david+git@madore.org>2010-04-17 04:33:42 +0200
commit6cba05247ae4e6c202c4c126a535b17526f71127 (patch)
tree49501baff625f5d513b19d815808a4ade1c58513
parent0c124ba807b8a16f4119df64cb11734ed8e4ac1b (diff)
downloaddamlengine-6cba05247ae4e6c202c4c126a535b17526f71127.tar.gz
damlengine-6cba05247ae4e6c202c4c126a535b17526f71127.tar.bz2
damlengine-6cba05247ae4e6c202c4c126a535b17526f71127.zip
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).
-rw-r--r--org/madore/damlengine/TodoAttr.java37
-rw-r--r--org/madore/damlengine/TodoDefaultElement.java11
-rw-r--r--org/madore/damlengine/TodoElement.java32
-rw-r--r--org/madore/damlengine/TodoXemptyAttr.java8
4 files changed, 47 insertions, 41 deletions
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<String,Constructor<? extends TodoAttr>> damlAttrConstructors;
+ public static abstract class Factory {
+ public abstract TodoAttr newItem(Attr attr, Element owner,
+ Map<String,Object> context,
+ Map<String,Object> options);
+ }
+
+ protected static Map<String,Factory> damlAttrFactories;
static {
- damlAttrConstructors = new HashMap<String,Constructor<? extends TodoAttr>>();
- 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<String,Factory>();
+ 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<String,Object> context,
Map<String,Object> options) {
- Constructor<? extends TodoAttr> 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<String,Object> context,
+ Map<String,Object> options) {
+ return new TodoDefaultElement(node, context, options);
+ }
+ }
+
public TodoDefaultElement(Element node,
- Map<String,Object> context, Map<String,Object> options) {
+ Map<String,Object> context,
+ Map<String,Object> 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<String,Constructor<? extends TodoElement>> damlConstructors;
+ public static abstract class Factory {
+ public abstract TodoElement newItem(Element node,
+ Map<String,Object> context,
+ Map<String,Object> options);
+ }
+
+ protected static Map<String,Factory> damlFactories;
+ protected static Factory damlDefaultFactory;
static {
- damlConstructors = new HashMap<String,Constructor<? extends TodoElement>>();
+ damlFactories = new HashMap<String,Factory>();
+ damlDefaultFactory = new TodoDefaultElement.Factory();
}
Element node;
@@ -27,21 +33,13 @@ public abstract class TodoElement extends TodoItem {
public static TodoElement getTodoElement(Element node,
Map<String,Object> context,
Map<String,Object> options) {
- Constructor<? extends TodoElement> 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<String,Object> context,
+ Map<String,Object> options) {
+ return new TodoXemptyAttr(attr, owner, context, options);
+ }
+ }
+
public TodoXemptyAttr(Attr attr, Element owner,
Map<String,Object> context, Map<String,Object> options) {
super(attr, owner, context, options);