1 /*--------------------------------------------------------------------------
\r
2 * Copyright 2008 Taro L. Saito
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
15 *--------------------------------------------------------------------------*/
\r
16 //--------------------------------------
\r
20 // Since: Jan 5, 2008 3:20:17 PM
\r
24 //--------------------------------------
\r
25 package org.xerial.util.bean;
\r
27 import java.util.ArrayList;
\r
28 import java.util.Collection;
\r
29 import java.util.List;
\r
30 import java.util.Map;
\r
32 import org.xerial.core.XerialErrorCode;
\r
33 import org.xerial.core.XerialException;
\r
34 import org.xerial.util.tree.TreeNode;
\r
35 import org.xerial.util.tree.TreeVisitor;
\r
36 import org.xerial.util.tree.TreeWalker;
\r
39 * {@link TreeWalker} for map structured data
\r
44 public class MapWalker implements TreeWalker
\r
46 private final Map< ? , ? > map;
\r
48 public MapWalker(Map< ? , ? > map) {
\r
50 throw new NullPointerException("map cannot be null");
\r
54 static class SingleTreeNode implements TreeNode
\r
56 private final String nodeName;
\r
57 private final String nodeValue;
\r
59 public SingleTreeNode(String nodeName, String nodeValue) {
\r
60 this.nodeName = nodeName;
\r
61 this.nodeValue = nodeValue;
\r
64 public List<TreeNode> getChildren() {
\r
65 return new ArrayList<TreeNode>();
\r
68 public String getNodeName() {
\r
72 public String getNodeValue() {
\r
78 public TreeNode getSubTree() throws XerialException {
\r
79 if (currentKey != null) {
\r
80 Object value = map.get(currentKey);
\r
81 return new SingleTreeNode(currentKey.toString(), value != null ? value.toString() : null);
\r
84 throw new XerialException(XerialErrorCode.NoMoreSubtree);
\r
87 public void skipDescendants() throws XerialException {
\r
88 // there is nothing to do
\r
91 private Object currentKey;
\r
93 public void walk(TreeVisitor visitor) throws XerialException {
\r
95 walk(null, map, visitor);
\r
96 visitor.finish(this);
\r
99 private void walk(String nodeName, Object value, TreeVisitor visitor) throws XerialException {
\r
100 if (value == null) {
\r
101 visitor.visitNode(nodeName, null, this);
\r
102 visitor.leaveNode(nodeName, this);
\r
106 assert value != null;
\r
108 Class< ? > valueType = value.getClass();
\r
109 if (TypeInfo.isArray(valueType)) {
\r
110 for (Object each : (Object[]) value) {
\r
111 walk(nodeName, each, visitor);
\r
114 else if (TypeInfo.isCollection(valueType)) {
\r
115 for (Object each : (Collection< ? >) value) {
\r
116 walk(nodeName, each, visitor);
\r
119 else if (TypeInfo.isMap(valueType)) {
\r
120 visitor.visitNode(nodeName, null, this);
\r
121 Map< ? , ? > mapValue = (Map< ? , ? >) value;
\r
122 for (Object key : mapValue.keySet()) {
\r
124 String entryName = key.toString();
\r
125 Object entryValue = map.get(key);
\r
126 walk(entryName, entryValue, visitor);
\r
128 visitor.leaveNode(nodeName, this);
\r
131 visitor.visitNode(nodeName, value.toString(), this);
\r
132 visitor.leaveNode(nodeName, this);
\r