View Javadoc

1   /*
2    * $Id: MapAdapter.java 471756 2006-11-06 15:01:43Z husted $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "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      public MapAdapter(AdapterFactory adapterFactory, AdapterNode parent, String propertyName, Map value) {
47          setContext( adapterFactory, parent, propertyName, value );
48      }
49  
50      public Map map() {
51          return (Map)getPropertyValue();
52      }
53  
54      protected List<Node> buildChildAdapters() {
55          List<Node> children = new ArrayList<Node>(map().entrySet().size());
56  
57          for (Object o : map().entrySet()) {
58              Map.Entry entry = (Map.Entry) o;
59              Object key = entry.getKey();
60              Object value = entry.getValue();
61              EntryElement child = new EntryElement(
62                      getAdapterFactory(), this, "entry", key, value);
63              children.add(child);
64          }
65  
66          return children;
67      }
68  
69      class EntryElement extends AbstractAdapterElement {
70          Object key, value;
71  
72          public EntryElement(  AdapterFactory adapterFactory,
73                                AdapterNode parent, String propertyName, Object key, Object value ) {
74              setContext( adapterFactory, parent, propertyName, null/*we have two values*/ );
75              this.key = key;
76              this.value = value;
77          }
78  
79          protected List<Node> buildChildAdapters() {
80              List<Node> children = new ArrayList<Node>();
81              children.add( getAdapterFactory().adaptNode( this, "key", key ) );
82              children.add( getAdapterFactory().adaptNode( this, "value", value ) );
83              return children;
84          }
85      }
86  }