01: /*
02: * $Id: MapAdapter.java 471756 2006-11-06 15:01:43Z husted $
03: *
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "License"); you may not use this file except in compliance
10: * with the License. You may obtain a copy of the License at
11: *
12: * http://www.apache.org/licenses/LICENSE-2.0
13: *
14: * Unless required by applicable law or agreed to in writing,
15: * software distributed under the License is distributed on an
16: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17: * KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations
19: * under the License.
20: */
21: package org.apache.struts2.views.xslt;
22:
23: import java.util.ArrayList;
24: import java.util.List;
25: import java.util.Map;
26:
27: import org.w3c.dom.Node;
28:
29: /**
30: * MapAdapter adapters a java.util.Map type to an XML DOM with the following
31: * structure:
32: * <pre>
33: * <myMap>
34: * <entry>
35: * <key>...</key>
36: * <value>...</value>
37: * </entry>
38: * ...
39: * </myMap>
40: * </pre>
41: */
42: public class MapAdapter extends AbstractAdapterElement {
43:
44: public MapAdapter() {
45: }
46:
47: public MapAdapter(AdapterFactory adapterFactory,
48: AdapterNode parent, String propertyName, Map value) {
49: setContext(adapterFactory, parent, propertyName, value);
50: }
51:
52: public Map map() {
53: return (Map) getPropertyValue();
54: }
55:
56: protected List<Node> buildChildAdapters() {
57: List<Node> children = new ArrayList<Node>(map().entrySet()
58: .size());
59:
60: for (Object o : map().entrySet()) {
61: Map.Entry entry = (Map.Entry) o;
62: Object key = entry.getKey();
63: Object value = entry.getValue();
64: EntryElement child = new EntryElement(getAdapterFactory(),
65: this , "entry", key, value);
66: children.add(child);
67: }
68:
69: return children;
70: }
71:
72: class EntryElement extends AbstractAdapterElement {
73: Object key, value;
74:
75: public EntryElement(AdapterFactory adapterFactory,
76: AdapterNode parent, String propertyName, Object key,
77: Object value) {
78: setContext(adapterFactory, parent, propertyName, null/*we have two values*/);
79: this .key = key;
80: this .value = value;
81: }
82:
83: protected List<Node> buildChildAdapters() {
84: List<Node> children = new ArrayList<Node>();
85: children.add(getAdapterFactory()
86: .adaptNode(this , "key", key));
87: children.add(getAdapterFactory().adaptNode(this , "value",
88: value));
89: return children;
90: }
91: }
92: }
|