+++ /dev/null
-/*--------------------------------------------------------------------------\r
- * Copyright 2008 Taro L. Saito\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- *--------------------------------------------------------------------------*/\r
-//--------------------------------------\r
-// XerialJ\r
-//\r
-// MapWalker.java\r
-// Since: Jan 5, 2008 3:20:17 PM\r
-//\r
-// $URL$\r
-// $Author$\r
-//--------------------------------------\r
-package org.xerial.util.bean;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.List;\r
-import java.util.Map;\r
-\r
-import org.xerial.core.XerialErrorCode;\r
-import org.xerial.core.XerialException;\r
-import org.xerial.util.tree.TreeNode;\r
-import org.xerial.util.tree.TreeVisitor;\r
-import org.xerial.util.tree.TreeWalker;\r
-\r
-/**\r
- * {@link TreeWalker} for map structured data\r
- * \r
- * @author leo\r
- * \r
- */\r
-public class MapWalker implements TreeWalker\r
-{\r
- private final Map< ? , ? > map;\r
-\r
- public MapWalker(Map< ? , ? > map) {\r
- if (map == null)\r
- throw new NullPointerException("map cannot be null");\r
- this.map = map;\r
- }\r
-\r
- static class SingleTreeNode implements TreeNode\r
- {\r
- private final String nodeName;\r
- private final String nodeValue;\r
-\r
- public SingleTreeNode(String nodeName, String nodeValue) {\r
- this.nodeName = nodeName;\r
- this.nodeValue = nodeValue;\r
- }\r
-\r
- public List<TreeNode> getChildren() {\r
- return new ArrayList<TreeNode>();\r
- }\r
-\r
- public String getNodeName() {\r
- return nodeName;\r
- }\r
-\r
- public String getNodeValue() {\r
- return nodeValue;\r
- }\r
-\r
- }\r
-\r
- public TreeNode getSubTree() throws XerialException {\r
- if (currentKey != null) {\r
- Object value = map.get(currentKey);\r
- return new SingleTreeNode(currentKey.toString(), value != null ? value.toString() : null);\r
- }\r
- else\r
- throw new XerialException(XerialErrorCode.NoMoreSubtree);\r
- }\r
-\r
- public void skipDescendants() throws XerialException {\r
- // there is nothing to do\r
- }\r
-\r
- private Object currentKey;\r
-\r
- public void walk(TreeVisitor visitor) throws XerialException {\r
- visitor.init(this);\r
- walk(null, map, visitor);\r
- visitor.finish(this);\r
- }\r
-\r
- private void walk(String nodeName, Object value, TreeVisitor visitor) throws XerialException {\r
- if (value == null) {\r
- visitor.visitNode(nodeName, null, this);\r
- visitor.leaveNode(nodeName, this);\r
- return;\r
- }\r
-\r
- assert value != null;\r
-\r
- Class< ? > valueType = value.getClass();\r
- if (TypeInfo.isArray(valueType)) {\r
- for (Object each : (Object[]) value) {\r
- walk(nodeName, each, visitor);\r
- }\r
- }\r
- else if (TypeInfo.isCollection(valueType)) {\r
- for (Object each : (Collection< ? >) value) {\r
- walk(nodeName, each, visitor);\r
- }\r
- }\r
- else if (TypeInfo.isMap(valueType)) {\r
- visitor.visitNode(nodeName, null, this);\r
- Map< ? , ? > mapValue = (Map< ? , ? >) value;\r
- for (Object key : mapValue.keySet()) {\r
- currentKey = key;\r
- String entryName = key.toString();\r
- Object entryValue = map.get(key);\r
- walk(entryName, entryValue, visitor);\r
- }\r
- visitor.leaveNode(nodeName, this);\r
- }\r
- else {\r
- visitor.visitNode(nodeName, value.toString(), this);\r
- visitor.leaveNode(nodeName, this);\r
- }\r
-\r
- }\r
-\r
-}\r