summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/TodoAttr.java
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 /org/madore/damlengine/TodoAttr.java
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).
Diffstat (limited to 'org/madore/damlengine/TodoAttr.java')
-rw-r--r--org/madore/damlengine/TodoAttr.java37
1 files changed, 14 insertions, 23 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);
}
}