Skip to Content.
Sympa Menu

notify-dpml - r1943 - in trunk/main/metro/part: etc/data src/main/net/dpml/runtime src/test/net/dpml/runtime/context src/test/org/acme

notify-dpml AT lists.ibiblio.org

Subject: DPML Notify

List archive

Chronological Thread  
  • From: mcconnell at BerliOS <mcconnell AT mail.berlios.de>
  • To: notify-dpml AT lists.ibiblio.org
  • Subject: r1943 - in trunk/main/metro/part: etc/data src/main/net/dpml/runtime src/test/net/dpml/runtime/context src/test/org/acme
  • Date: Mon, 2 Apr 2007 20:00:36 +0200

Author: mcconnell
Date: 2007-04-02 20:00:35 +0200 (Mon, 02 Apr 2007)
New Revision: 1943

Modified:
trunk/main/metro/part/etc/data/map.xml
trunk/main/metro/part/src/main/net/dpml/runtime/ContextDirective.java
trunk/main/metro/part/src/test/net/dpml/runtime/context/MapTestCase.java
trunk/main/metro/part/src/test/org/acme/MapWidget.java
Log:
Add support for alternative map implementation classes (e.g. EnumMap,
HashMap, Hashtable, IdentityHashMap, LinkedHashMap, TreeMap, and WeakHashMap)
declared under entry class attribute.

Modified: trunk/main/metro/part/etc/data/map.xml
===================================================================
--- trunk/main/metro/part/etc/data/map.xml 2007-04-02 17:02:18 UTC (rev
1942)
+++ trunk/main/metro/part/etc/data/map.xml 2007-04-02 18:00:35 UTC (rev
1943)
@@ -5,10 +5,14 @@
name="widget"
class="org.acme.MapWidget">
<context>
- <map key="demo">
+ <map key="primary">
<entry key="message" value="Hello from the map entry"/>
<entry key="foo" value="bar"/>
</map>
+ <map key="secondary" class="java.util.TreeMap">
+ <entry key="foo" value="bar"/>
+ <entry key="ping" value="pong"/>
+ </map>
</context>
</component>


Modified:
trunk/main/metro/part/src/main/net/dpml/runtime/ContextDirective.java
===================================================================
--- trunk/main/metro/part/src/main/net/dpml/runtime/ContextDirective.java
2007-04-02 17:02:18 UTC (rev 1942)
+++ trunk/main/metro/part/src/main/net/dpml/runtime/ContextDirective.java
2007-04-02 18:00:35 UTC (rev 1943)
@@ -22,6 +22,8 @@
import java.io.Writer;
import java.util.Map;
import java.util.Hashtable;
+import java.beans.Expression;
+import java.lang.reflect.Constructor;

import dpml.lang.Construct;
import dpml.lang.ValueDecoder;
@@ -100,7 +102,8 @@
else if( "map".equals( child.getLocalName() ) )
{
Map<String,Value> map = buildMapValue( child, resolver );
- MapWrapper wrapper = new MapWrapper( child, map );
+ String classname = ElementHelper.getAttribute( child,
"class", null, resolver );
+ MapWrapper wrapper = new MapWrapper( classloader, child,
map, classname );
m_entries.put( key, wrapper );
}
else
@@ -233,29 +236,47 @@
static class MapWrapper extends AbstractResolvable
{
private final Map<String,Value> m_values;
+ private final String m_classname;
+ private final ClassLoader m_classloader;

- MapWrapper( Element element, Map<String,Value> values )
+ MapWrapper( ClassLoader classloader, Element element,
Map<String,Value> values, String classname )
{
super( element );
m_values = values;
+ m_classname = classname;
+ m_classloader = classloader;
}

+ @SuppressWarnings( "unchecked" )
public <T>T resolve( ComponentStrategy parent, Class<T> type )
throws Exception
{
- Map<String,Object> map = new Hashtable<String,Object>();
-
+ Map map = new Hashtable();
for( String k : m_values.keySet() )
{
Value v = m_values.get( k );
Object o = v.resolve( parent.getContextMap() );
map.put( k, o );
}
- return type.cast( map );
+ if( null == m_classname )
+ {
+ return type.cast( map );
+ }
+ else
+ {
+ Class c = m_classloader.loadClass( m_classname );
+ Constructor<T> constructor = c.getConstructor( Map.class );
+ return constructor.newInstance( map );
+ }
}

public void encode( Buffer buffer, String key ) throws IOException
{
- buffer.nl( "<map key=\"" + key + "\">" );
+ buffer.nl( "<map key=\"" + key + "\"" );
+ if( null != m_classname )
+ {
+ buffer.write( " class=\"" + m_classname + "\"" );
+ }
+ buffer.nl( ">" );
for( String k : m_values.keySet() )
{
Value v = m_values.get( k );

Modified:
trunk/main/metro/part/src/test/net/dpml/runtime/context/MapTestCase.java
===================================================================
--- trunk/main/metro/part/src/test/net/dpml/runtime/context/MapTestCase.java
2007-04-02 17:02:18 UTC (rev 1942)
+++ trunk/main/metro/part/src/test/net/dpml/runtime/context/MapTestCase.java
2007-04-02 18:00:35 UTC (rev 1943)
@@ -19,6 +19,7 @@
package net.dpml.runtime.context;

import java.util.Map;
+import java.util.SortedMap;

import net.dpml.runtime.AbstractTestCase;

@@ -32,14 +33,25 @@
*/
public class MapTestCase extends AbstractTestCase
{
- public void testEquality() throws Exception
+ public void testSimpleMap() throws Exception
{
MapWidget widget =
load( MapWidget.class, "map.xml", "map" );
- Map<String,String> map = widget.getContext().getDemo();
+ Map<String,String> map = widget.getContext().getPrimary();
String message = map.get( "message" );
String value = map.get( "foo" );
assertEquals( "message", "Hello from the map entry", message );
assertEquals( "foo", "bar", value );
}
+
+ public void testQualifiedMap() throws Exception
+ {
+ MapWidget widget =
+ load( MapWidget.class, "map.xml", "map" );
+ SortedMap<String,String> map = widget.getContext().getSecondary();
+ String foo = map.get( "foo" );
+ String ping = map.get( "ping" );
+ assertEquals( "foo", "bar", foo );
+ assertEquals( "ping", "pong", ping );
+ }
}

Modified: trunk/main/metro/part/src/test/org/acme/MapWidget.java
===================================================================
--- trunk/main/metro/part/src/test/org/acme/MapWidget.java 2007-04-02
17:02:18 UTC (rev 1942)
+++ trunk/main/metro/part/src/test/org/acme/MapWidget.java 2007-04-02
18:00:35 UTC (rev 1943)
@@ -19,6 +19,7 @@
package org.acme;

import java.util.Map;
+import java.util.SortedMap;

import net.dpml.util.Logger;

@@ -30,7 +31,8 @@
{
public interface Context
{
- Map<String,String> getDemo();
+ Map<String,String> getPrimary();
+ SortedMap<String,String> getSecondary();
}

private Context m_context;
@@ -43,7 +45,7 @@

public String getMessage()
{
- return m_context.getDemo().get( "message" );
+ return m_context.getPrimary().get( "message" );
}

public Context getContext()




  • r1943 - in trunk/main/metro/part: etc/data src/main/net/dpml/runtime src/test/net/dpml/runtime/context src/test/org/acme, mcconnell at BerliOS, 04/02/2007

Archive powered by MHonArc 2.6.24.

Top of Page