View Javadoc

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