View Javadoc

1   /*
2    * $Id: MapAdapter.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
22  package org.apache.struts2.views.xslt;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.w3c.dom.Node;
29  
30  /***
31   * MapAdapter adapters a java.util.Map type to an XML DOM with the following
32   * structure:
33   * <pre>
34   *  <myMap>
35   *      <entry>
36   *          <key>...</key>
37   *          <value>...</value>
38   *      </entry>
39   *      ...
40   *  </myMap>
41   * </pre>
42   */
43  public class MapAdapter extends AbstractAdapterElement {
44  
45      public MapAdapter() { }
46  
47      public MapAdapter(AdapterFactory adapterFactory, AdapterNode parent, String propertyName, Map value) {
48          setContext( adapterFactory, parent, propertyName, value );
49      }
50  
51      public Map map() {
52          return (Map)getPropertyValue();
53      }
54  
55      protected List<Node> buildChildAdapters() {
56          List<Node> children = new ArrayList<Node>(map().entrySet().size());
57  
58          for (Object o : map().entrySet()) {
59              Map.Entry entry = (Map.Entry) o;
60              Object key = entry.getKey();
61              Object value = entry.getValue();
62              EntryElement child = new EntryElement(
63                      getAdapterFactory(), this, "entry", key, value);
64              children.add(child);
65          }
66  
67          return children;
68      }
69  
70      static class EntryElement extends AbstractAdapterElement {
71          Object key, value;
72  
73          public EntryElement(  AdapterFactory adapterFactory,
74                                AdapterNode parent, String propertyName, Object key, Object value ) {
75              setContext( adapterFactory, parent, propertyName, null/*we have two values*/ );
76              this.key = key;
77              this.value = value;
78          }
79  
80          protected List<Node> buildChildAdapters() {
81              List<Node> children = new ArrayList<Node>();
82              children.add( getAdapterFactory().adaptNode( this, "key", key ) );
83              children.add( getAdapterFactory().adaptNode( this, "value", value ) );
84              return children;
85          }
86      }
87  }