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

📄 xmlrwbeanimpl.java

📁 一个关于tlms的一个小程序 看看能否帮助到别人
💻 JAVA
字号:
/**
 * =============================================
 * Copyright 2005 TransFar
 *
 * Change Revision
 * --------------------------------
 *   Date        Author      Remarks
 *   2006-3-14     YHuang     Create class com.szmx.component.xml.rw.impl.XmlRwBeanImpl
 * =============================================
 */
package com.szmx.component.xml.rw.impl;

import com.szmx.component.xml.rw.XmlRwBean;
import com.szmx.component.xml.rw.XmlBeanService;
import com.szmx.component.xml.rw.AbstractXmlBean;
import com.szmx.component.xml.rw.XmlBeanException;
import com.szmx.framework.util.FileUtil;

import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.util.Date;

import org.springframework.core.io.ClassPathResource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <TODO: Write a short description on the purpose of the program>
 *
 * @author YHuang
 * @version 1.0
 * @class com.szmx.component.xml.rw.impl.XmlRwBeanImpl
 * @since 2006-3-14
 */
public class XmlRwBeanImpl implements XmlRwBean {
    public static final String BACK_FILE_SUFFIX = ".bak";
    public static final String CLASS_PATH = "classpath:";
    private static Log log = LogFactory.getLog(XmlRwBean.class);
    private XmlBeanService xmlBeanService;
    protected final Object synchronizedObject = new Object();
    private AbstractXmlBean xmlBean;

    public void setXmlBeanService(XmlBeanService xmlBeanService) {
        this.xmlBeanService = xmlBeanService;
    }

    private void saveOrUpdateLocal() throws XmlBeanException {
        if (this.xmlBean == null) {
            log.error("the xml bean is null ");
            throw new XmlBeanException("the xml bean is null , can't be saveOrUpdate");
        }
        File srcFile = getXmlFile();
        if (xmlBeanService.isBackup()) {
            String fileName = srcFile.getName() + "_" + new Date().getTime() + BACK_FILE_SUFFIX;
            FileUtil.renameFile(srcFile, fileName);
        }
        this.xmlBean.setVersion(new Integer(this.xmlBean.getVersion().intValue() + 1));
        xmlBeanService.getXmlBeanWriter().writerXmlFile(xmlBeanService.getBeanName(), srcFile.getPath()
                , getDtdFile().getPath(), this.xmlBean);
    }

    public void saveOrUpdate() throws XmlBeanException {
        synchronized (synchronizedObject) {
            saveOrUpdateLocal();
        }
    }

    public void destroy() {
        this.xmlBean = null;
    }

    public void init() throws XmlBeanException {
        if (this.xmlBean == null) {
            getBean();
        }
    }

    public AbstractXmlBean getXmlBean() throws XmlBeanException {
        synchronized (synchronizedObject) {
            if (this.xmlBean == null) {
                return getBean();
            }
            return this.xmlBean;
        }
    }

    protected AbstractXmlBean getBean() throws XmlBeanException {
        return xmlBeanService.getXmlBeanReader().parseXmlFile(
                xmlBeanService.getXmlBeanClass(), getXmlInputStream());
    }

    protected File getXmlFile() throws XmlBeanException {
        String xmlPath = xmlBeanService.getXmlPath();
        try {
            if (xmlPath.startsWith(CLASS_PATH)) {
                xmlPath = xmlPath.substring(CLASS_PATH.length());
                return new ClassPathResource(xmlPath).getFile();
            }
            return new File(xmlPath);
        } catch (Exception e) {
            log.error("Cant't read the file " + e.getMessage());
            throw new XmlBeanException("Cant't read the file " + e.getMessage());
        }
    }

    protected File getDtdFile() throws XmlBeanException {
        String dtdPath = xmlBeanService.getDtdPath();
        try {
            if (dtdPath.startsWith(CLASS_PATH)) {
                dtdPath = dtdPath.substring(CLASS_PATH.length());
                return new ClassPathResource(dtdPath).getFile();
            }
            return new File(dtdPath);
        } catch (Exception e) {
            log.error("Cant't read the file " + e.getMessage());
            throw new XmlBeanException("Cant't read the file " + e.getMessage());
        }
    }


    protected InputStream getXmlInputStream() throws XmlBeanException {
        try {
            String xmlPath = xmlBeanService.getXmlPath();
            if (xmlPath.startsWith(CLASS_PATH)) {
                String path = xmlPath.substring(CLASS_PATH.length());
                return Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
            } else {
                return new FileInputStream(xmlPath);
            }
        } catch (Exception e) {
            log.error("Cant't read the file " + e.getMessage());
            throw new XmlBeanException("Cant't read the file " + e.getMessage());
        }
    }

}

⌨️ 快捷键说明

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