package org.pinae.ndb.common;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

/**
 * ndb存储
 * 
 * @author Huiyugeng
 *
 *
 */
public class NodeWriter {
	
	/**
	 * 保存为ndb格式文件
	 * 
	 * @param filename 文件名
	 * @param rootNode 根节点名称
	 * @param ndb Map格式的ndb数据
	 * 
	 * @throws IOException IO异常
	 */
	public void write(String filename, String rootNode, Map<String, Object> ndb) throws IOException{
		PrintWriter writer = new PrintWriter(filename);
		
		writer.write(print(rootNode, ndb));
		
		writer.flush();
		writer.close();
	}
	
	/**
	 *  将Map输出ndb格式
	 * 
	 * @param rootNode 根节点名称
	 * @param ndb 需要输出ndb格式的Map
	 * 
	 * @return ndb内容
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String print(String rootNode, Map<String, Object> ndb){
		List<String> ndbInfo = new ArrayList<String>();

		ndbInfo.add(rootNode + "{\n");

		Set<String> nodeKeySet = ndb.keySet();
		for (String nodeKey : nodeKeySet){
			Object nodeValue = ndb.get(nodeKey);
			if (nodeValue instanceof List){
				List list = (List)nodeValue;
				for (Object item : list){
					if (item instanceof Map){
						ndbInfo.add(print(nodeKey, (Map<String, Object>)item));
					}else{
						ndbInfo.add(String.format("%s:%s\n", nodeKey, item.toString()));
					}
				}
			}else if(nodeValue instanceof Map){
				ndbInfo.add(print(nodeKey, (Map<String, Object>)nodeValue));
			}else{
				ndbInfo.add(String.format("%s:%s\n", nodeKey, nodeValue.toString()));
			}
		}
		ndbInfo.add("}\n");
		
        return StringUtils.join(ndbInfo, "");
	}
	
}

@@ -313,16 +313,12 @@
 * %E4%BF%9D%E5%AD%98
-%E4%B8%BAndb%E6%A0%BC%E5%BC%8F%E6%96%87%E4%BB%B6
+%E6%96%87%E6%9C%AC%E5%86%85%E5%AE%B9
 %0A%09 *
@@ -358,49 +358,20 @@
 ram 
-rootNode %E6%A0%B9%E8%8A%82%E7%82%B9%E5%90%8D%E7%A7%B0%0A%09 * @param ndb Map%E6%A0%BC%E5%BC%8F%E7%9A%84ndb%E6%95%B0%E6%8D%AE
+content %E6%96%87%E6%9C%AC%E5%86%85%E5%AE%B9
 %0A%09 *
@@ -453,41 +453,15 @@
 ing 
-rootNode, Map%3CString, Object%3E ndb
+content
 ) th
@@ -476,16 +476,17 @@
 xception
+ 
 %7B%0A%09%09Prin
@@ -551,28 +551,15 @@
 ite(
-print(rootNode, ndb)
+content
 );%0A%09
@@ -602,24 +602,309 @@
 ;%0A%09%7D%0A%09%0A%09/**%0A
+%09 * %E4%BF%9D%E5%AD%98%E4%B8%BAndb%E6%A0%BC%E5%BC%8F%E6%96%87%E4%BB%B6%0A%09 * %0A%09 * @param filename %E6%96%87%E4%BB%B6%E5%90%8D%0A%09 * @param rootNode %E6%A0%B9%E8%8A%82%E7%82%B9%E5%90%8D%E7%A7%B0%0A%09 * @param ndb Map%E6%A0%BC%E5%BC%8F%E7%9A%84ndb%E6%95%B0%E6%8D%AE%0A%09 * %0A%09 * @throws IOException IO%E5%BC%82%E5%B8%B8%0A%09 */%0A%09public void write(String filename, String rootNode, Map%3CString, Object%3E ndb) throws IOException%7B%0A%09%09write(filename, print(rootNode, ndb));%0A%09%7D%0A%09%0A%09/**%0A
 %09 *  %E5%B0%86Map%E8%BE%93%E5%87%BAn
@@ -1159,16 +1159,59 @@
 ng%3E();%0A%0A
+%09%09if (StringUtils.isNotEmpty(rootNode)) %7B%0A%09
 %09%09ndbInf
@@ -1235,16 +1235,22 @@
 %22%7B%5Cn%22);%0A
+%09%09%7D%0A%09%09
 %0A%09%09Set%3CS
@@ -1846,16 +1846,62 @@
 %7D%0A%09%09%7D%0A%09%09
+%0A%09%09if (StringUtils.isNotEmpty(rootNode)) %7B%0A%09%09%09
 ndbInfo.
@@ -1912,16 +1912,20 @@
 %22%7D%5Cn%22);%0A
+%09%09%7D%0A
 %09%09%0A     

Add write(content) to NodeWriter