OSDN Git Service

Removed BeanException class: http://code.google.com/p/xerial/issues/detail?id=26
[xerial/xerial-core.git] / src / main / java / org / xerial / util / bean / MapWalker.java
1 /*--------------------------------------------------------------------------\r
2  *  Copyright 2008 Taro L. Saito\r
3  *\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
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
17 // XerialJ\r
18 //\r
19 // MapWalker.java\r
20 // Since: Jan 5, 2008 3:20:17 PM\r
21 //\r
22 // $URL$\r
23 // $Author$\r
24 //--------------------------------------\r
25 package org.xerial.util.bean;\r
26 \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
31 \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
37 \r
38 /**\r
39  * {@link TreeWalker} for map structured data\r
40  * \r
41  * @author leo\r
42  * \r
43  */\r
44 public class MapWalker implements TreeWalker\r
45 {\r
46     private final Map< ? , ? > map;\r
47 \r
48     public MapWalker(Map< ? , ? > map) {\r
49         if (map == null)\r
50             throw new NullPointerException("map cannot be null");\r
51         this.map = map;\r
52     }\r
53 \r
54     static class SingleTreeNode implements TreeNode\r
55     {\r
56         private final String nodeName;\r
57         private final String nodeValue;\r
58 \r
59         public SingleTreeNode(String nodeName, String nodeValue) {\r
60             this.nodeName = nodeName;\r
61             this.nodeValue = nodeValue;\r
62         }\r
63 \r
64         public List<TreeNode> getChildren() {\r
65             return new ArrayList<TreeNode>();\r
66         }\r
67 \r
68         public String getNodeName() {\r
69             return nodeName;\r
70         }\r
71 \r
72         public String getNodeValue() {\r
73             return nodeValue;\r
74         }\r
75 \r
76     }\r
77 \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
82         }\r
83         else\r
84             throw new XerialException(XerialErrorCode.NoMoreSubtree);\r
85     }\r
86 \r
87     public void skipDescendants() throws XerialException {\r
88     // there is nothing to do\r
89     }\r
90 \r
91     private Object currentKey;\r
92 \r
93     public void walk(TreeVisitor visitor) throws XerialException {\r
94         visitor.init(this);\r
95         walk(null, map, visitor);\r
96         visitor.finish(this);\r
97     }\r
98 \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
103             return;\r
104         }\r
105 \r
106         assert value != null;\r
107 \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
112             }\r
113         }\r
114         else if (TypeInfo.isCollection(valueType)) {\r
115             for (Object each : (Collection< ? >) value) {\r
116                 walk(nodeName, each, visitor);\r
117             }\r
118         }\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
123                 currentKey = key;\r
124                 String entryName = key.toString();\r
125                 Object entryValue = map.get(key);\r
126                 walk(entryName, entryValue, visitor);\r
127             }\r
128             visitor.leaveNode(nodeName, this);\r
129         }\r
130         else {\r
131             visitor.visitNode(nodeName, value.toString(), this);\r
132             visitor.leaveNode(nodeName, this);\r
133         }\r
134 \r
135     }\r
136 \r
137 }\r