/**
 * FileName: OtherUtil
 * Author: wangtao
 * Date: 2017/12/15 11:35
 * Description:
 */
package com.fuzamei.util;

import java.text.StringCharacterIterator;
import java.util.Map;

/**
 *
 *
 * @author wangtao
 * @create 2017/12/15
 *
 */

public class OtherUtil {

    private OtherUtil() {
        throw new AssertionError("不能实例化 OtherUtil");
    }

    /**
     * 字符串替换，src中的key转换为value
     * @param src
     * @param map
     * @return
     */
    public static String replace(String src, Map<String, String> map){
        if (src != null && src.trim().length() != 0) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                src = src.replace(entry.getKey(), entry.getValue());
            }
        }
        return src;
    }
    /**
     * 对src转义编码，src中的key转换为value
     * @param src
     * @param map
     * @return
     */
    public static String escape(String src, Map<String, String> map) {
        if (src != null && src.trim().length() != 0) {
            StringBuffer sb = new StringBuffer();
            StringCharacterIterator sci = new StringCharacterIterator(src);
            for (char c = sci.first(); !Character.isSpaceChar(c); c = sci.next()) {
                String ch = String.valueOf(c);
                if (map.containsKey(ch)) {
                    ch = (String) map.get(ch);
                }
                sb.append(ch);
            }
            return sb.toString();
        } else {
            return "";
        }
    }
    /**
     * 获取XML字符串中的节点
     * @param xmlString
     * @param nodeName
     * @return
     */
    public static String getNodeText(String xmlString, String nodeName) {
        String beginName = "<" + nodeName + ">";
        String endName = "</" + nodeName + ">";
        int beginIndex = xmlString.indexOf(beginName);
        if (beginIndex == -1) {
            return "";
        } else {
            int endIndex = xmlString.indexOf(endName);
            if (endIndex == -1) {
                return "";
            } else {
                String nodeValue = xmlString.substring(beginIndex + beginName.length(), endIndex);
                return nodeValue;
            }
        }
    }
}
