com.love320.templateparser.label.impl.LabelBeanDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.love320.templateparser.label.impl.LabelBeanDaoImpl.java

Source

/**
* Copyright (c) 2010-2012 love320.com
* 
* Licensed under the Apache License, Version 2.0 (the "License");
* 
* Founder admin@love320.com
*/
package com.love320.templateparser.label.impl;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import com.love320.templateparser.factory.entity.LabelBean;
import com.love320.templateparser.label.LabelBeanDao;
import com.love320.templateparser.util.AppPath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @ClassName: LabelManagerImpl 
* @Description: TODO
* @author love320.com
* @date 2012-6-23 ?06:54:21 
*  
*/
public class LabelBeanDaoImpl implements LabelBeanDao {

    private final static Logger logger = LoggerFactory.getLogger(LabelBeanDaoImpl.class);

    private String configPath; //?
    private Element DOCROOT;//?

    public void setConfigPath(String confpath) {

        this.configPath = appPath(confpath);//

        //
        File labelFile = new File(this.configPath);
        if (!labelFile.exists()) {
            logger.warn("Label Bean Dao read config file null the not found :" + confpath);
            return;
        }

        SAXReader sax = new SAXReader();
        try {
            Document document = sax.read(labelFile);
            DOCROOT = document.getRootElement();
        } catch (DocumentException e) {
            logger.error("DocumentException", e);
        }
    }

    protected String appPath(String confpath) {
        return AppPath.path() + confpath;
    }

    //?Element
    private Element docExtractName(String labelName) {
        //???Element
        Element labelElement = (Element) DOCROOT
                .selectSingleNode("/labels/label[@name='" + labelName.trim() + "']");
        return labelElement;
    }

    private boolean upSeting(LabelBean labelBean) {
        Element docrootElement = (Element) DOCROOT.selectSingleNode("/labels");
        Element labelElement = docrootElement.addElement("label");
        return upSeting(labelBean, labelElement);
    }

    //labelbeanelement
    private boolean upSeting(LabelBean labelBean, Element labelElement) {
        //? labelBean?
        String name = labelBean.getName();
        String parameters = labelBean.getParameters();
        String template = labelBean.getTemplate();
        String bean = labelBean.getBean();
        String note = labelBean.getNote();

        //Element?
        labelElement.addAttribute("name", name);
        labelElement.addElement("parameters").addCDATA(parameters);
        labelElement.addElement("template").addCDATA(template);
        labelElement.addElement("bean").setText(bean);
        labelElement.addElement("note").setText(note);

        return true;
    }

    //elementlabelbean
    private boolean upSeting(Element labelElement, LabelBean labelBean) {
        //? Element?
        String name = labelElement.attributeValue("name");
        String parameters = labelElement.elementText("parameters");
        String template = labelElement.elementText("template");
        String bean = labelElement.elementText("bean");
        String note = labelElement.elementText("note");

        //LabelBean?
        labelBean.setName(name);
        labelBean.setParameters(parameters);
        labelBean.setTemplate(template);
        labelBean.setBean(bean);
        labelBean.setNote(note);
        return true;
    }

    //??
    private boolean XMLWriter() {
        try {
            OutputFormat format = OutputFormat.createPrettyPrint();//?
            format.setEncoding("UTF-8");//?
            XMLWriter output = new XMLWriter(new FileWriter(new File(configPath)), format);//?
            output.write(DOCROOT.getDocument());//
            output.close();//

            return true;
        } catch (IOException e) {
            logger.error("IOException", e);
        }
        return false;
    }

    /* (non-Javadoc)
     * @see com.love320.templateparser.factory.LabelBeanFactory#get(java.lang.String)
     */
    @Override
    public LabelBean get(String labelName) {
        LabelBean labelBean = new LabelBean();//?
        //??xml?
        Element labelElement = (Element) docExtractName(labelName);
        if (labelElement != null) {
            upSeting(labelElement, labelBean);//elementlabelbean
        } else {
            labelBean.setName(labelName);
        }

        return labelBean;
    }

    /* (non-Javadoc)
     * @see com.love320.templateparser.label.LabelDao#add(com.love320.templateparser.factory.entity.LabelBean)
     */
    @Override
    public boolean add(LabelBean labelBean) {
        upSeting(labelBean);//
        XMLWriter();//?
        return true;
    }

    /* (non-Javadoc)
     * @see com.love320.templateparser.label.LabelDao#delete(java.lang.String)
     */
    @Override
    public boolean delete(String labelName) {
        Element element = docExtractName(labelName);//?
        element.detach();//
        XMLWriter();//??
        return true;
    }

    /* (non-Javadoc)
     * @see com.love320.templateparser.label.LabelDao#delete(com.love320.templateparser.factory.entity.LabelBean)
     */
    @Override
    public boolean delete(LabelBean labelBean) {
        return delete(labelBean.getName());//??
    }

    /* (non-Javadoc)
     * @see com.love320.templateparser.label.LabelDao#getAll()
     */
    @Override
    public List<LabelBean> getAll() {
        List<LabelBean> labelBeanList = new ArrayList();

        List list = DOCROOT.selectNodes("/labels/label");
        Iterator iter = list.iterator();
        while (iter.hasNext()) {
            Element element = (Element) iter.next();
            LabelBean labelBean = new LabelBean();
            upSeting(element, labelBean);
            labelBeanList.add(labelBean);
        }
        return labelBeanList;
    }

    /* (non-Javadoc)
     * @see com.love320.templateparser.label.LabelDao#update(com.love320.templateparser.factory.entity.LabelBean)
     */
    @Override
    public boolean update(LabelBean labelBean) {
        Element element = docExtractName(labelBean.getName());//?
        element.detach();//
        upSeting(labelBean);//
        XMLWriter();//?
        return true;
    }

}