⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 printtag.java

📁 一个真实项目的源代码。有一个比较优秀的时间类
💻 JAVA
字号:
package com.work.tag;

import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.Iterator;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * @author wangmj
 * @date 2007-7-12 读取打印配置的标签
 */
public class PrintTag extends BodyTagSupport {
    private static Log log = LogFactory.getLog(PrintTag.class);

    //打印配置文件路径
    public static final String PRINT_CONFIG_FILE_PATH = "/WEB-INF/conf/printConfig.xml";

    //标签属性解析起始位置
    private int BEGIN_INDEX = 0;

    //标签属性解析分隔符
    private String PHASE_SINGLE = ".";

    //打印配置属性名
    private String PRINT_SINGLE = "printName";

    //打印属性配置属性名
    private String PROPERTY_SINGLE = "propertyName";

    //打印属性配置属性值
    private String VALUE_SINGLE = "value";

    //标签属性
    private String name;

    /**
     * 标签开始运行方法,输出打印配置属性值
     */
    public int doStartTag() throws JspException {
        InputStream is = pageContext.getServletContext().getResourceAsStream(
                PRINT_CONFIG_FILE_PATH);
        //pageContext.getServletContext().getRealPath("/WEB-INF/conf/printConfig.xml");
        if (is == null) {
            log.error("打印配置文件不存在:" + PRINT_CONFIG_FILE_PATH);
            throw new JspException("打印配置文件不存在:" + PRINT_CONFIG_FILE_PATH);
        }
        if (log.isDebugEnabled()) {
            log.debug("打印配置文件路径 = " + PRINT_CONFIG_FILE_PATH);
        }
        try {
            pageContext.getOut().print(getConfigValue(is, getName()));//在这里获得。
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug(e);
            }
            e.printStackTrace();
        }
        return SKIP_BODY;
    }

    /**
     * 标签释放方法,清空所有域
     */
    public void release() {
        //PRINT_CONFIG_FILE_PATH = null;
        PHASE_SINGLE = null;
        PRINT_SINGLE = null;
        PROPERTY_SINGLE = null;
        name = null;
    }

    /**
     * 通过标签属性值获取打印配置项值
     * 
     * @param configName
     * @return 打印配置项的值
     * @throws MalformedURLException
     * @throws DocumentException
     * @throws JspTagException
     */
    String getConfigValue(InputStream is, String configName)
            throws MalformedURLException, DocumentException, JspTagException {
        if (log.isDebugEnabled()) {
            log.debug("打印配置项名称 = " + configName);
        }
        if (null == configName || "".equals(configName)) {
            throw new JspTagException("Could not find the property '"
                    + configName + "' or the property was not defined !");
        }
        String result = null;
        String printName = configName.substring(BEGIN_INDEX, configName
                .indexOf(PHASE_SINGLE));
        String propertyName = configName.substring(configName
                .indexOf(PHASE_SINGLE)
                + PHASE_SINGLE.length(), configName.length());
        //File file = new File(PRINT_CONFIG_FILE_PATH);
        SAXReader reader = new SAXReader();
        Document doc;
        try {
            doc = reader.read(is);
            Iterator i = doc.getRootElement().elements().iterator();
            while (i.hasNext()) {
                Element printElement = (Element) i.next();
                if (printName.equals(printElement.attribute(PRINT_SINGLE)
                        .getText())) {
                    Iterator ii = printElement.elements().iterator();
                    while (ii.hasNext()) {
                        Element propertyElement = (Element) ii.next();
                        if (propertyName.equals(propertyElement.attribute(
                                PROPERTY_SINGLE).getText())) {
                            result = propertyElement.attribute(VALUE_SINGLE)
                                    .getText();
                            if (log.isDebugEnabled()) {
                                log.debug("获得的配置项值 = " + result);
                            }
                            return result;
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return result;
    }

    /**
     * @return 返回 name。
     */
    public String getName() {
        return name;
    }

    /**
     * @param name
     *            要设置的 name。
     */
    public void setName(String name) {
        this.name = name;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -