01: /*
02: * Copyright (c) 2002-2003 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.webwork.views.xslt;
06:
07: import org.apache.commons.logging.Log;
08: import org.apache.commons.logging.LogFactory;
09: import java.util.*;
10:
11: /**
12: * MapAdapter adapters a java.util.Map type to an XML DOM with the following
13: * structure:
14: * <pre>
15: * <myMap>
16: * <entry>
17: * <key>...</key>
18: * <value>...</value>
19: * </entry>
20: * ...
21: * </myMap>
22: * </pre>
23: *
24: * @author Pat Niemeyer (pat@pat.net)
25: */
26: public class MapAdapter extends AbstractAdapterElement {
27: private Log log = LogFactory.getLog(this .getClass());
28:
29: public MapAdapter() {
30: }
31:
32: public MapAdapter(AdapterFactory adapterFactory,
33: AdapterNode parent, String propertyName, Map value) {
34: setContext(adapterFactory, parent, propertyName, value);
35: }
36:
37: public Map map() {
38: return (Map) getPropertyValue();
39: }
40:
41: protected List buildChildAdapters() {
42: List children = new ArrayList(map().entrySet().size());
43:
44: for (Iterator i = map().entrySet().iterator(); i.hasNext();) {
45: Map.Entry entry = (Map.Entry) i.next();
46: Object key = entry.getKey();
47: Object value = entry.getValue();
48: EntryElement child = new EntryElement(getAdapterFactory(),
49: this , "entry", key, value);
50: children.add(child);
51: }
52:
53: return children;
54: }
55:
56: class EntryElement extends AbstractAdapterElement {
57: Object key, value;
58:
59: public EntryElement(AdapterFactory adapterFactory,
60: AdapterNode parent, String propertyName, Object key,
61: Object value) {
62: setContext(adapterFactory, parent, propertyName, null/*we have two values*/);
63: this .key = key;
64: this .value = value;
65: }
66:
67: protected List buildChildAdapters() {
68: List children = new ArrayList();
69: children.add(getAdapterFactory()
70: .adaptNode(this , "key", key));
71: children.add(getAdapterFactory().adaptNode(this , "value",
72: value));
73: return children;
74: }
75: }
76: }
|