All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.lkk.web.util.GeneralParaser Maven / Gradle / Ivy

The newest version!
package cn.lkk.web.util;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.core.resource.StringTemplateResourceLoader;
import org.dom4j.Attribute;
import org.dom4j.Element;

import cn.lkk.web.util.ext.ShiroExt;
import cn.lkk.web.util.ext.XSSDefenseFormat;

/**
 * @author wangfujun
 * @date 2018/12/18
 */
public class GeneralParaser {

    private static GroupTemplate classPathGt;
    private static GroupTemplate strGt;
    private static final int INDENT = 0;
    static {
        ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();
        StringTemplateResourceLoader stringResourceLoader = new StringTemplateResourceLoader();

        Configuration cfg = null;
        try {
            cfg = Configuration.defaultConfiguration();
        } catch (IOException e) {
            e.printStackTrace();
        }
        classPathGt = new GroupTemplate(resourceLoader, cfg);
        classPathGt.registerFunctionPackage("shiro", new ShiroExt());
        classPathGt.registerFormat("xss", new XSSDefenseFormat());
        strGt = new GroupTemplate(stringResourceLoader, cfg);
        strGt.registerFunctionPackage("shiro", new ShiroExt());
        strGt.registerFormat("xss", new XSSDefenseFormat());
    }

    public static StringBuffer appendLine(StringBuffer sb, StringBuffer str) {
        return sb.append(str).append(Consts.BREAKLINE);
    }

    public static StringBuffer appendLine(StringBuffer sb, String str) {
        return sb.append(str).append(Consts.BREAKLINE);
    }

    public static StringBuffer append(StringBuffer sb, String str) {
        return sb.append(str);
    }

    public static String parse(String fileName, Map context) {
        // final StringWriter writer = new StringWriter();
        Template template = classPathGt.getTemplate(fileName);
        template.binding(context);
        return template.render();
    }

    public static StringBuffer parseFile(String fileName, Map context) {
        final StringWriter writer = new StringWriter();
        Template template = classPathGt.getTemplate(fileName);
        template.binding(context);
        template.renderTo(writer);
        return writer.getBuffer();
    }

    public static StringBuffer parseString(String string, Map context) {
        final StringWriter writer = new StringWriter();
        Template template = strGt.getTemplate(string);
        template.binding(context);
        template.renderTo(writer);
        return writer.getBuffer();
    }

    public static String parseStr(String string, Map context) {
        // final StringWriter writer = new StringWriter();
        Template template = strGt.getTemplate(string);
        template.binding(context);
        return template.render();
    }

    public static StringBuffer printXML(Element elm) {
        return printXML(elm, INDENT);
    }

    /**
     * 

* Title:printXML *

*

* Description:打印xml *

* * @param elm * 需要打印的xml * @param indent * 首次缩进倍数 * @return 美化之后的StringBuffer */ @SuppressWarnings("rawtypes") public static StringBuffer printXML(Element elm, int indent) { StringBuffer sb = new StringBuffer(); // 缩进字符串 String indentStr = " "; // 打印缩进 for (int i = 0; i < indent; i++) { sb.append(indentStr); } // 得到该节点子节点的集合 List nodes = elm.elements(); // 获得其所有属性 List attrs = elm.attributes(); StringBuffer attrStr = new StringBuffer(""); if (attrs.size() > 0) { for (Iterator it = attrs.iterator(); it.hasNext();) { Attribute attr = (Attribute)it.next(); attrStr.append(" " + attr.getName() + "=\"" + attr.getText() + "\""); } } // 如果有子节点则换行,没有子节点不换行 String enterStr = (nodes.size() <= 0) ? "" : "\n"; // 得到标签的内容 String elmStr = elm.getText(); // 打印前标签 sb.append("<" + elm.getName() + attrStr + ">" + elmStr + enterStr); // 遍历子节点集合 并递归这个子节点 for (Iterator it = nodes.iterator(); it.hasNext();) { sb.append(printXML((Element)it.next(), indent + 1)); } // 如果有孩子节点 后标签不缩进 if (nodes.size() > 0) { for (int i = 0; i < indent; i++) { sb.append(indentStr); } } // 打印后标签 sb.append("").append(Consts.BREAKLINE); return sb; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy